-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.sh
executable file
·84 lines (79 loc) · 1.38 KB
/
todo.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
sedPolyfill(){
os=$("uname")
if [[ "$os" == "Darwin" ]]
then
sed -i '' "${1}d" $2
else
sed -i -e "${1}d" $2
fi
}
todoAdd(){
if [ ! -f ${TODO_PATH}/$1 ]
then
printf '%s ' "File $1 is to be created? [y/n]"
read ans
if [[ "$ans" == "y" ]]
then
echo "$2" >> ${TODO_PATH}/$1
else
echo "Todo cancelled"
fi
else
echo "$2" >> ${TODO_PATH}/$1
fi
}
todoSave(){
cd ${TODO_PATH}
git add .
git commit -m "todo updated"
git push origin master
cd -
}
todoRm(){
if [[ $2 == "all" ]]
then
rm ${TODO_PATH}/$1
else
if [[ $2 =~ '^[0-9]+$' ]]
then
sedPolyfill $2 ${TODO_PATH}/$1
else
match=$(grep "$2" ${TODO_PATH}/$1)
num_matches=$(echo ${match} | wc -l)
if [ -z "$match" ]
then
echo "!! no matches !!"
return 1
elif [[ $num_matches -gt 1 ]]
then
echo "!! multiple matches !!"
return 1
else
line_number=$(grep -n "$2" ${TODO_PATH}/$1 | cut -d: -f1)
sedPolyfill $line_number ${TODO_PATH}/$1
fi
fi
if [ ! -s ${TODO_PATH}/$1 ]
then
rm ${TODO_PATH}/$1
fi
fi
}
todoFunc(){
if [[ $1 == "see" ]]
then
cat -n "${TODO_PATH}/$2"
elif [[ $1 == "add" ]]
then
todoAdd $2 $3
elif [[ $1 == "rm" ]]
then
todoRm $2 $3
elif [[ $1 == "save" ]]
then
todoSave
else
echo "usage: [see|add|rm|save]"
fi
}
alias todo=todoFunc