Skip to content

Commit e368d78

Browse files
committed
code reverted to v1.2
1 parent 45d8e46 commit e368d78

File tree

13 files changed

+355
-399
lines changed

13 files changed

+355
-399
lines changed

src/ask.sh

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,62 +14,47 @@ source "${Dir}"/string.sh
1414
# source string.sh
1515

1616
# Stores object that you selected.
17-
askObject='';
17+
askChoice='';
1818
# Stores position of object that you selected.
19-
askPosition='';
19+
askReply='';
2020

21-
# ask.case(title) ~ bool
22-
# This takes case ( yes or no ) for any work.
23-
#
21+
# ask.case(title) -> bool
22+
# This takes case ( yes or no ) for any work.
2423
# Args:
25-
# - title (str): takes title (eg: You're Agree).
24+
# title (str) > takes title (eg: You're Agree).
2625
ask.case(){
2726
echo -ne "\n ${1}";
2827
read -p " ? [Y/n] " ARGS;
2928
echo;
3029
case "${ARGS}" in
31-
y|Y|'')
32-
return 0;
33-
;;
34-
n|N)
35-
say.error "Process Aborted.\n";
36-
exit 1;
37-
;;
38-
*) say.error "You have to enter only \n
39-
\t\t 'Y' for Yes & \n
40-
\t\t 'n' for No.\n";
41-
exit 1;
42-
;;
30+
y|Y|'') return 0;;
31+
n|N) { say.error "Process Aborted.\n" && exit 1; };;
32+
*) { say.error "You have to enter only \n\t\t'Y' for Yes & \n\t\t'n' for No.\n" && exit 1; };;
4333
esac
4434
}
4535

46-
# ask.choice(title,list) ~ var
47-
# This creates a simple menu.
48-
#
36+
# ask.choice(title,list) -> var
37+
# This creates a simple menu.
4938
# Args:
50-
# - title (str): takes title (eg: Choose One).
51-
# - list (array): takes array as arg.
52-
#
39+
# title (str) > takes title (eg: Choose One).
40+
# list (array) > takes array as arg.
5341
# Returns:
54-
# - var (str): result in 'askObject' & 'askPosition' variables.
42+
# object (str) > result in 'askChoice' & 'askReply' variables.
5543
ask.choice(){
5644
PS3="
57-
${1}: ";
45+
${1} > ";
5846
shift;
5947
local ARGs=("${@}");
6048
echo;
6149
select ARG in "${ARGs[@]}"
6250
do
63-
if text.isdigit "${REPLY}"; then
64-
say.error "You can only input 'digits'.\n";
65-
exit 1;
66-
elif [[ "${REPLY}" -gt "${#ARGs[@]}" ]]; then
67-
say.error "You should input correct digits.\n";
68-
exit 1;
69-
else
70-
askObject="${ARG}";
71-
askPosition="${REPLY}";
72-
break;
73-
fi
51+
text.isdigit "${REPLY}" || {
52+
say.error "You can only input 'digits'.\n" && exit 1;
53+
};
54+
[[ "${REPLY}" -gt "${#ARGs[@]}" ]] &&
55+
say.error "You should input correct digits.\n" && exit 1;
56+
askChoice="${ARG}";
57+
askReply="${REPLY}";
58+
break;
7459
done
75-
}
60+
}

src/cursor.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

33
# setCursor(on~off)
4-
# Switch terminal cursor easily.
4+
# Switch terminal cursor easily.
55
setCursor(){
66
setterm -cursor "${1}";
77
}

src/db.sh

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,95 @@
11
#!/bin/bash
22

3-
# Supports only yaml syntax database.
3+
# Support only Yaml syntax database.
44

5-
# _db.isKeyExist(key,file) ~ bool
6-
# Checks that key is exist or not.
7-
#
5+
# _db.isfile(file) -> bool
6+
# Checks that db file exist or not.
87
# Args:
9-
# - key (str): takes key of db.
10-
# - file (str): takes file path.
11-
_db.isKeyExist(){
8+
# file (str) > takes file path.
9+
_db.isfile(){
10+
# checking file exist or not.
11+
test -f "${1}";
12+
}
13+
14+
# _db.iskey(key,file) -> bool
15+
# Checks that key is exist or not.
16+
# Args:
17+
# key (str) > takes key of db.
18+
# file (str) > takes file path.
19+
_db.iskey(){
1220
grep "${1}: " "${2}" &>/dev/null;
1321
}
1422

15-
# db.read(key,file) ~ str
16-
# Reads value of given key in db file.
17-
#
23+
# db.read(key,file) -> str
24+
# Give you data of key of yaml db.
1825
# Args:
19-
# - key (str): takes key of db.
20-
# - file (str): takes file path.
26+
# key (str) > takes key of db.
27+
# file (str) > takes file path.
2128
db.read(){
2229
# checking args given or not.
2330
[[ ${#} -eq 2 ]] ||
2431
{ echo "error: 'missing args'" && return 1; };
25-
_db.isKeyExist "${1}" "${2}" ||
32+
_db.iskey "${1}" "${2}" ||
2633
{ echo "error: ${1}: 'key not exists'" && return 1; };
2734
local dbValue="$(grep "${1}: " "${2}")";
2835
echo "${dbValue}" | awk -F': ' '{sub(/^[^:]*: /, ""); print}';
2936
}
3037

3138
# db.create(key,value,file)
32-
# Creates data key and value to db.
33-
#
39+
# Adds data key and value to db.
3440
# Args:
35-
# - key (str): takes key of db.
36-
# - value (str): takes value of key.
37-
# - file (str): takes file path.
41+
# key (str) > takes key of db.
42+
# value (str) > takes value of key.
43+
# file (str) > takes file path.
3844
db.create(){
3945
# checking args given or not.
4046
[[ ${#} -eq 3 ]] ||
4147
{ echo "error: 'missing args'" && return 1; };
42-
_db.isKeyExist "${1}" "${3}" &&
48+
_db.iskey "${1}" "${3}" &&
4349
{ echo "error: ${1}: 'key already exists'" && return 1; };
4450
echo -ne "\n${1}: ${2}" >> "${3}";
4551
}
4652

4753
# db.update(key,value,file)
48-
# Update data of key in db file.
49-
#
54+
# Update data of key in db file.
5055
# Args:
51-
# - key (str): takes key of db.
52-
# - value (str): takes update value of key.
53-
# - file (str): takes file path.
56+
# key (str) > takes key of db.
57+
# value (str) > takes update value of key.
58+
# file (str) > takes file path.
5459
db.update(){
5560
# checking args given or not.
5661
[[ ${#} -eq 3 ]] ||
5762
{ echo "error: 'missing args'" && return 1; };
58-
_db.isKeyExist "${1}" "${3}" ||
63+
_db.iskey "${1}" "${3}" ||
5964
{ echo "error: ${1}: 'key not exists'" && return 1; };
65+
# taking key arg.
6066
local dbKey="${1}";
61-
local dbUpdateValue="${2}";
67+
# taking update value of given key.
68+
local dbUpdatedValue="${2}";
69+
# taking db file path.
6270
local dbFile="${3}";
63-
local dbCurrentValue="$(db.read "${dbKey}" "${dbFile}")";
64-
local dbCurrentPair="${dbKey}: ${dbCurrentValue}"; # concated to create pair.
65-
local dbCurrentPairPos="$(grep -n "${dbCurrentPair}" "${dbFile}" | cut -d: -f1 | head -n 1;)";
66-
local dbUpdatedPair="${dbCurrentPair/${dbCurrentValue}/"${dbUpdateValue}"}";
67-
sed -i "${dbCurrentPairPos}c\\${dbUpdatedPair}" "${dbFile}"; # replaced full pair.
71+
# getting old value of given key.
72+
local dbValue="$(db.read "${dbKey}" "${dbFile}")";
73+
# concating key and old value.
74+
local dbKeyValuePair="${dbKey}: ${dbValue}";
75+
# getting position of concated line in file.
76+
local dbKeyValuePairPos="$(grep -n "${dbKeyValuePair}" "${dbFile}" | cut -d: -f1 | head -n 1;)";
77+
# replacing value from old to new.
78+
local dbUpdatedKeyValuePair="${dbKeyValuePair/${dbValue}/"${dbUpdatedValue}"}";
79+
# placing updated value to position.
80+
sed -i "${dbKeyValuePairPos}c\\${dbUpdatedKeyValuePair}" "${dbFile}";
6881
}
6982

7083
# db.delete(key,file)
71-
# Delete key and value of db file.
72-
#
84+
# Delete key and value of db file.
7385
# Args:
74-
# - key (str): takes key of db.
75-
# - file (str): takes file path.
86+
# key (str) > takes key of db.
87+
# file (str) > takes file path.
7688
db.delete(){
7789
# checking args given or not.
7890
[[ ${#} -eq 2 ]] ||
7991
{ echo "error: 'missing args'" && return 1; };
80-
_db.isKeyExist "${1}" "${2}" ||
92+
_db.iskey "${1}" "${2}" ||
8193
{ echo "error: ${1}: 'key not exists'" && return 1; };
8294
local dbKey="${1}: ";
8395
local dbFile="${2}";

0 commit comments

Comments
 (0)