-
Notifications
You must be signed in to change notification settings - Fork 1
/
gitodo
executable file
·539 lines (466 loc) · 9.7 KB
/
gitodo
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#!/bin/sh
TMPFILE=$(mktemp /tmp/gitodo.XXXXXX)
TMPSEARCH=$(mktemp /tmp/gitodo_search.XXXXXX)
trap 'rm -f $TMPFILE' EXIT
trap 'rm -f $TMPSEARCH' EXIT
function error()
{
echo "Error: $1"
exit
}
function success()
{
echo $1
echo "${commit_message}$1" >> ${TMPFILE}
}
function create_file()
{
if flag_exists "--list-name"; then
find_flag_index "--list-name"
list_name=$flag_value
else
list_name=${a[1]}
fi
echo "--gitodo--
# $list_name" >> "$filename"
success "File $filename created"
}
function create_sublist()
{
echo "
### $1" >> "$filename"
success "Added sublist $1 in list $filename"
}
function remove()
{
confirm_file_exists
if todo_exists "- [ ] ${a[2]}"; then
echo "${content/"- [ ] ${a[2]}"/}" > "$filename"
success "${a[2]} successfully removed"
else
error "Todo '${a[2]}' does not exist"
fi
}
function find_flag_index()
{
for i in "${!a[@]}" ; do
if [ "${a[$i]}" = $1 ]; then
idx=$(($i+1))
flag_value="${a[$idx]}"
fi
done
}
function flag_exists()
{
for i in "${!a[@]}" ; do
if [ "${a[$i]}" = $1 ]; then
find_flag_index $1
return 0
fi
done
return 1
}
function create_sublist_from_flag()
{
if flag_exists "-s"; then
find_flag_index "-s"
if [ -z "$flag_value" ]; then
if ! sublist_exists "General"; then
create_sublist "General"
fi
else
if ! sublist_exists "${a[$idx]}"; then
create_sublist "${a[$idx]}"
fi
fi
elif ! sublist_exists "General"; then
create_sublist "General"
fi
}
function create()
{
if [ -z $filename ]; then
error "File name must be specified"
elif file_exists; then
error "Requested File already exists."
fi
create_file
}
function todo_exists()
{
if [[ "$content" == *"$1"* ]]; then
return 0
else
return 1
fi
}
function add_todo()
{
if todo_exists "$todo"; then
if flag_exists "-f" || flag_exists "--force"; then
write_todo "$todo" "$filename"
elif todo_is_complete "$todo"; then
error "Todo already exists and is already complete. To add it anyway, add the -f Flag"
else
error "Todo already exists. To add it anyway, add the -f Flag"
fi
else
write_todo "$todo" "$filename"
fi
}
function write_todo()
{
if flag_exists "-l"; then
find_flag_index "-l"
if [ "$flag_value" != "" ]; then
todo_content="
- [ ] $todo "
else
todo_content="
- [ ] [$todo]($flag_value) "
fi
else
todo_content="
- [ ] $todo "
fi
content=$(< $filename )
if flag_exists "-s"; then
echo "${content/"### $flag_value"/### $flag_value $todo_content}" > "$filename"
else
echo "${content/"### General"/### General $todo_content}" > "$filename"
fi
success "Todo $1 successfully added"
}
function todo()
{
todo="${a[2]}"
content=$(< $filename )
if [[ -z "${a[1]}" ]] || [[ -z "${a[2]}" ]]; then
error "Filename and todo must be specified"
elif [ file_exists ]; then
create_sublist_from_flag
add_todo
else
error "File does not exist. To force file creation, add the -f flag"
fi
}
function todo_is_complete()
{
if [[ $content == *"[x] $1"* ]]; then
return 0
else
return 1
fi
}
function complete()
{
confirm_file_exists
if todo_exists "${a[2]}"; then
if todo_is_complete "${a[2]}"; then
error "Todo is already complete"
else
echo "${content/"- [ ] ${a[2]}"/- [x] ${a[2]} }" > "$filename"
success "${a[2]} marked as complete"
fi
else
error "Todo ${a[2]} does not exist"
fi
}
function sublist_exists()
{
if [[ $content == *"### $1"* ]]; then
return 0
else
return 1
fi
}
function sublist()
{
if [ -z ${a[1]} ] || [ -z "${a[2]}" ]; then
error "Filename and sublist must be specified"
elif [ -a "$filename" ]; then
if sublist_exists ${a[2]}; then
if flag_exists "-f" || flag_exists "--force"; then
create_sublist "${a[2]}" ${a[1]}
else
error "Sublist ${a[2]} already exists. If you would like to add it anyway, add the -f flag"
fi
else
create_sublist "${a[2]}" ${a[1]}
fi
else
if flag_exists "-f" || flag_exists "--force"; then
create
create_sublist "${a[2]}" ${a[1]}
else
error "File does not exist. To force file creation, add the -f Flag"
fi
fi
}
function should_be_shown()
{
if flag_exists "--done"; then
if [[ $1 == *"- [x]"* ]] || line_is_sublist "$1"; then
return 0
else
return 1
fi
elif flag_exists "--open"; then
if [[ $1 == *"- [ ]"* ]] || line_is_sublist "$1"; then
return 0
else
return 1
fi
elif line_is_todo "$1" || line_is_sublist "$1"; then
return 0
else
return 1
fi
}
function show()
{
if [ -z "${a[1]}" ]; then
error "Filename must be specified"
fi
confirm_file_exists
while read line; do
if flag_exists "-s"; then
if line_is_matching_sublist "$line"; then
start=1
fi
if [[ $start -eq 1 ]] && ! line_is_nonmatching_sublist "$line" && should_be_shown "$line"; then
echo $line
fi
if [[ $start -eq 1 ]] && line_is_nonmatching_sublist "$line"; then
break
fi
else
if should_be_shown "$line"; then
echo "$line"
fi
fi
done < "$filename"
}
function line_is_nonmatching_sublist()
{
if line_is_sublist "$1" && ! line_is_matching_sublist "$1"; then
return 0
fi
return 1
}
function line_is_matching_sublist()
{
if [[ "$1" == *"### $flag_value"* ]]; then
return 0
fi
return 1
}
function line_is_todo()
{
if line_is_unfinished_todo "$1" || line_is_finished_todo "$1"; then
return 0
fi
return 1
}
function line_is_unfinished_todo()
{
if [[ "$1" == *"- [ ]"* ]]; then
return 0
fi
return 1
}
function line_is_finished_todo()
{
if [[ "$1" == *"- [x]"* ]]; then
return 0
fi
return 1
}
function line_is_sublist()
{
if [[ "$1" == *"### "* ]]; then
return 0
fi
return 1
}
function search()
{
if ! flag_exists "-a" && ! flag_exists "--all" ; then
confirm_file_exists
fi
if [ -z "${a[1]}" ]; then
error "Search Term must be specified"
fi
output_file=""
search_output=""
if flag_exists "-a" || flag_exists "--all"; then
searchterm=${a[1]}
for file in `ls .`; do
if is_file $file && is_markdown $file; then
searchfile $file "$searchterm"
fi
done
else
searchterm=${a[2]}
searchfile "$filename" "$searchterm"
fi
if [[ $search_output != "" ]]; then
echo "$search_output"
else
error "Search term '$searchterm' not found"
fi
}
function searchfile()
{
while read line; do
if ! line_is_sublist $line; then
if line_is_todo "$line" && [[ "$line" == *"$2"* ]]; then
if [[ $output_file != $file ]] ; then
search_output+="$1
"
fi
output_file=$1
search_output+="$line
"
fi
fi
done < "$1"
}
function write()
{
set_commit_message
for file in `ls -ld $(find . -name '*.md')`; do
if is_markdown $file && is_gitodo_file $file; then
response=$(git add "${file}")
fi
done
response=$(git commit -m "${commit_message}" && git push origin master)
exit
}
function set_commit_message
{
if flag_exists "-m"; then
find_flag_index "-m"
commit_message=$flag_value
else
commit_message=$(<"${TMPFILE}")
fi
rm ${TMPFILE}
}
function is_gitodo_file
{
first_line=$(head -n 1 $file)
if [[ "$first_line" == "--gitodo--" ]]; then
return 0
fi
return 1
}
# Check to see if the file containing your todo list exists and exits the script
# with an error if it does not. As such, it is not suitable for use any time the
# file existing is not a fatal error.
function confirm_file_exists()
{
if [[ ! file_exists ]]; then
error "Requested list ${a[1]} doesn't exist"
fi
}
function read_content()
{
if ! flag_exists "-a" && ! flag_exists "--all"; then
set_file_name
if ! file_exists && flag_exists "-f" || flag_exists "--force"; then
create_file
fi
content=$(< $filename )
fi
}
function set_file_name()
{
if [[ ${a[1]} != "" ]] || force_flag_is_set; then
if [[ ${a[1]} != *".md" ]]; then
filename=${a[1]}.md
else
filename=${a[1]}
fi
else
error "File $filename does not exist. To force creation, use the -f flag"
fi
}
function file_exists()
{
if [[ -a "$filename" ]]; then
return 0
fi
return 1
}
function force_flag_is_set()
{
if flag_exists "-f" || flag_exists "--force"; then
return 0
fi
return 1
}
function is_file()
{
if [[ -f "$1" ]]; then
return 0
fi
return 1
}
function is_markdown()
{
if [[ ${file: -3} == ".md" ]]; then
return 0
fi
return 1
}
function help()
{
echo $"Usage: $0 <create|todo|search|remove|complete|show|sublist> [--open | --done]"
echo ""
echo "Examples:"
echo " gitodo create MyFile"
echo " gitodo todo MyFile 'Take Over The World'"
echo " gitodo show MyFile --open"
echo " gitodo complete MyFile 'Take Over The World'"
echo " gitodo sublist MyFile 'Sublist Name'"
echo " gitodo remove MyFile 'Todo Name Here'"
echo " gitodo search MyFile 'Search Term'"
exit 0
}
a=("$@")
read_content
case $1 in
create)
create
create_sublist_from_flag
write
;;
todo)
todo
write
;;
remove)
remove
write
;;
complete)
complete
write
;;
sublist)
sublist
write
;;
show)
show
;;
search)
search
;;
help)
help
;;
*)
help
esac