-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo
More file actions
executable file
·264 lines (246 loc) · 6.16 KB
/
todo
File metadata and controls
executable file
·264 lines (246 loc) · 6.16 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/bin/bash -e
todo_flag=' '
done_flag='+'
todo_color='91'
done_color='92'
today_highlight_color='33'
todo_file_fmt='%Y-%m-%d'
todo_item_time_fmt='%H:%M'
list_file_delimiter="\\n"
todo_folder="${HOME}/.todo"
todo_file="${todo_folder}/$(date +${todo_file_fmt}).txt"
command_name="$1"
date_params=""
if [ -n "$command_name" ]; then
shift 1
fi
while getopts ":d:" opt; do
case $opt in
d )
date_params=$(date -d "$OPTARG" +${todo_file_fmt})
if [ -n "$date_params" ]; then
todo_file=""${todo_folder}/${date_params}.txt""
fi
;;
esac
done
shift $(($OPTIND - 1))
command_params="${@#$0}"
usage () {
cat <<EOF | xargs -I{} echo -e {}
Usage: $0 [ COMMAND ] [ OPTIONS ] [ PARAMETERS ]
COMMANDS:
\\\\t \\\e[2mcommand parameters\\\e[0m
\\\\t \\\e[1madd | a\\\e[0m \\\e[4mCONTENT\\\e[0m add CONTENT to todo file.
\\\\t \\\e[1mlist | ls\\\e[0m [ \\\e[4mFILE\\\e[0m ] show FILE\'s todo items, defaul is today\'s todo file.
\\\\t \\\e[1mremove | rm\\\e[0m [ \\\e[4mN\\\e[0m ] ... remove todo item of line N, default is all lines.
\\\\t \\\e[1mmark | m\\\e[0m [ \\\e[4mN\\\e[0m ] ... mark todo item of line N as completed, default is all lines.
\\\\t \\\e[1munmark | um\\\e[0m [ \\\e[4mN\\\e[0m ] ... mark todo itemof line N as uncompleted. default is all lines.
\\\\t \\\e[1mls-file| sf\\\e[0m [ \\\e[4mN\\\e[0m ] show latest N todo files.
OPTIONS:
\\\\t -d \\\e[4m DATE \\\e[0m which day to be operated, default is today.
DESCRIPTION:
\\\\t \\\e[4mDATE\\\e[0m DATE is like '"'today'"' '"'tomorrow'"' '"'last 2 days'"' '"'2017-11-11'"' etc,
\\\\t """""" """""""" which can be recognized be GNU date program.
\\\\t \\\e[4mN\\\e[0m N is a positive number.
\\\\t \\\e[4mFILE\\\e[0m FILE is a filename of todo list files, just name with suffix without path name.
EXAMPLES:
\\\\t todo add 'go shopping'
\\\\t todo add -d 'tomorrow' 'fix bugs'
\\\\t todo ls
\\\\t todo ls -d 'last 2 days'
\\\\t todo rm 2
\\\\t todo m -d 'last day' 2, 3
\\\\t todo um -d '2017-11-11' 1
EOF
}
echo_today_highlight () {
if [ -n "$today_highlight_color" ]; then
echo -en "\e[${today_highlight_color}m${@}\e[0m"
else
echo -en "$@"
fi
}
get_todo_folder () {
if ! [ -d "$todo_folder" ]; then
mkdir -p "$todo_folder"
fi
}
get_todo_file () {
get_todo_folder
if ! [ -f "$todo_file" ]; then
touch "$todo_file"
fi
}
get_line_number () {
echo "$command_params" |sed -rn '/^( *[1-9]+)((,? *| +)[1-9]+)* *$/p' |sed 's/,/ /g'
}
trunc_array () {
local array=($1)
local newArr=()
for (( i=0; i<$2; i++)); do
newArr[${i}]=${array[${i}]}
done
echo -n "${newArr[*]}"
}
ls_file () {
local show_amount=$(echo $command_params |sed -rn '/^ *[0-9]+ *$/p')||0
if [ -z "$show_amount" ]; then show_amount=0; fi
if [ -d "$todo_folder" ]; then
local file_list=($(ls -Cr $todo_folder))
if [ $show_amount -gt 0 ]; then
file_list=($(trunc_array "${file_list[*]}" $show_amount))
fi
local filename
for filename in "${file_list[@]}"; do
if [ "${filename%.txt}" = "$(date +${todo_file_fmt})" ]; then
echo_today_highlight "$filename${list_file_delimiter}"
else
echo -en "$filename${list_file_delimiter}"
fi
done
if [ $(echo -e "$list_file_delimiter" |awk "END { print NR }") -eq 1 ]; then
echo
fi
fi
}
print_item () {
cat <<<"$@" |
awk -f <(cat <<EOF
BEGAIN { done_count = 0; todo_count = 0 }
{
is_item_done = (\$0 ~ /^${done_flag}/)
line_color = is_item_done ? "$done_color" : "$todo_color"
if (is_item_done) done_count += 1
else todo_count += 1
color_str = line_color ? "\\033[" line_color "m" : ""
printf("%s%s %s %s \n", color_str, NR, \$0, "\\033[0m")
}
END {
format = ":todo %d :done %d"
printf(format, todo_count, done_count)
}
EOF
) |
xargs -I{} echo -e {}
}
ls_item () {
local is_other_ls=$(( $1 + 0))
local target_file="$todo_file"
if [ -n "$command_params" ]; then
local specific_file="${todo_folder}/$command_params"
if [ -f "$specific_file" ]; then
target_file="$specific_file"
elif [ -f "${specific_file}.txt" ]; then
echo -ne "Did you mean '${command_params}.txt'? [Y, N]: ";
local choice
read choice
case "$choice" in
y | Y | yes | YES | Yes )
target_file="${specific_file}.txt"
;;
* )
exit 0
;;
esac
elif [ $is_other_ls -eq 0 ]; then
echo "File \"$command_params\" doesn't exist."
exit 0
fi
fi
if [ -f "$target_file" ]; then
if [ -z "$(cat $target_file)" ]; then
exit 0
fi
print_item "$(cat $target_file)"
fi
}
add_item () {
get_todo_file
local content="$todo_flag $command_params"
if [ -n "$command_params" ]; then
if [ -z "$(cat $todo_file)" ]; then
echo -e "$content" > "$todo_file"
else
echo -e "$content" >> "$todo_file"
fi
else
usage
fi
}
op_item () {
get_todo_file
local line_number_array=$(get_line_number)
if [ -z "$command_params" ]; then
line_number_array=()
else
if [ -z "${line_number_array[*]}" ]; then
usage
fi
fi
local content
local sed_line_numbers=$(echo ${line_number_array[*]} |sed -r 's/ +/,/g')
case "$1" in
mark )
content=$(sed -r "${sed_line_numbers}s/^${todo_flag}/${done_flag}/" $todo_file)
;;
remove )
content=$(sed "${sed_line_numbers}d" $todo_file)
;;
unmark )
content=$(sed -r "${sed_line_numbers}s/^\\${done_flag}/${todo_flag}/" $todo_file)
;;
* )
usage
esac
if [ -z "${line_number_array[*]}" ]; then
ls_item
echo -ne "Are you really want to $1 all todo items ? [Y, N]: "
local choice_action
read choice_action
case "$choice_action" in
Y | y | Yes | YES | yes )
echo -e "$content" > "$todo_file"
;;
N | n | No | NO | * )
exit 0
;;
esac
else
echo -e "$content" > "$todo_file"
fi
ls_item 1
}
if [ -z "$command_name" ]; then
ls_item
exit 0
fi
case "$command_name" in
a | add )
add_item
;;
ls | list)
ls_item
;;
rm | remove)
op_item remove
;;
m | mark )
op_item mark
;;
um | unmark )
op_item unmark
;;
lf | ls-file )
ls_file
;;
sf | show-folder )
echo "$todo_folder"
;;
lt | ls-todo )
ls_todo
;;
* )
usage
;;
esac