-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.sh
executable file
·630 lines (481 loc) · 13.2 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
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
#! /bin/sh
_DEBUG=1
# == PROCESS OPTIONS ==
while getopts ":fhpcnNaAtTvVx+@Pd:" Option
do
case $Option in
h )
# Short-circuit option parsing and forward to the action.
# Cannot just invoke shorthelp() because we need the configuration
# processed to locate the add-on actions directory.
set -- '-h' 'shorthelp'
OPTIND=2
;;
v )
: $(( TODO_VERBOSE++ ))
;;
V )
version
;;
esac
done
# Shift the params to delete options
shift $(($OPTIND - 1))
# Defaults if not defined
TODO_LIST=${TODO_LIST:-./todo}
TODO_LIST_DEPTH=${TODO_LIST_DEPTH:-3}
TODO_VERBOSE=${TODO_VERBOSE:-1}
TODO_CFG_FILE=${TODO_CFG_FILE:-$HOME/.todo/config}
TODO_CLEAR_TERM=${TODO_CLEAR_TERM:-1}
TODO_FILTER_BYPASS=1
TODO_FILTER_STATUS='.'
TODO_FILTER_PRIORITY='.'
TODO_HIDE_DONE=${TODO_HIDE_DONE:-1}
TODO_HL_ID=0
TODO_HL_COL=0
# Colors
BLACK=30
RED=31
GREEN=32
YELLOW=33
BLUE=34
MAGENTA=35
CYAN=36
WHITE=37
# Style
BOLD=1
NORMAL=0
TODO_COL_NO=`echo -e "\e[0m"`
TODO_COL_CAT=`echo -e "\e[$BOLD;${BLUE}m"`
TODO_COL_NEW=`echo -e "\e[$NORMAL;$(( $GREEN+10 ))m"`
TODO_COL_DEL=`echo -e "\e[$NORMAL;$(( $RED+10 ))m"`
TODO_COL_UPD=`echo -e "\e[$NORMAL;$(( $YELLOW+10 ))m"`
TODO_COL_DONE=`echo -e "\e[$BOLD;${RED}m"`
TODO_COL_PEND=`echo -e "\e[$BOLD;${YELLOW}m"`
TODO_COL_BUG=`echo -e "\e[$BOLD;${RED}m"`
TODO_COL_H=`echo -e "\e[$BOLD;${YELLOW}m"`
TODO_COL_L=`echo -e "\e[$BOLD;${GREEN}m"`
######## Options parsing end
# REGEX
TODO_RE_TODO_LINE='^[0-9][0-9]*'
# REGEX END
DEBUG() {
[ $_DEBUG -ne 0 ] && $@
}
_countTodo() {
TODO_COUNT=`sed -n -e "/$TODO_RE_TODO_LINE \[/p" $TODO_LIST | sed -n '$='`
}
_lastTodoID() {
TODO_LAST_ID=`sed -n -e "/$TODO_RE_TODO_LINE/p" $TODO_LIST |
sort -n |
sed -n -e '$p' |
sed "s/\($TODO_RE_TODO_LINE\).*/\1/"`
# When there are not TODOs
[ -z "$TODO_LAST_ID" ] && TODO_LAST_ID=0
}
add() {
# Create a TODO on a given category
# Get category
cat=$1; shift
if [ $# -eq 0 ]; then
echo "Can't create an empty TODO in category $cat"
exit 1;
fi
found_cat=`lsCategories | sed -n -e "/^$cat$/p"`
if [ "$found_cat" != "$cat" ]; then
createCategory $cat
echo -e "Created category $TODO_COL_CAT$cat"
fi
# Default values
mark=' '
priority=' '
# Search mark and prio at the beginning of the sentence
for i in "$@"; do
case $i in
'?' | "x" | "o") mark=$i; shift ;;
'!' | "L" | "H" | "0") priority=$i; shift ;;
# Out when you find the first letter, otherwise treat chars in
# the TODO as priority and mark
* ) break ;;
esac
done
# Check for non empty body
body="$@"
[ -z "$body" ] && exit 1
# Find the last TODO ID (which may be not the count!)
_lastTodoID
id=$(( $TODO_LAST_ID+1 ))
# Line number of the '=' below category name
line=$(( `sed -n -e "/^$cat$/=" $TODO_LIST`+1 ))
if [ -z "$line" ]; then exit 1; fi
# Write the TODO
todo="$id [$mark] ($priority) $body"
sed -i "${line}a $todo" $TODO_LIST
_setHighlighter $id $TODO_COL_NEW
lsTodoCategories $cat | draw
echo -e "\nCreated ${TODO_COL_NEW}TODO #${id}${TODO_COL_NO} in '$cat'"
}
delete() {
# Delete a todo
#
# $*: ids
for id in $*; do
sed -i "/^$id /d" $TODO_LIST
done
_setHighlighter $id $TODO_COL_DEL
lsTodoAll | draw
echo -e "\nDeleted ${TODO_COL_DEL}TODO #${id}${TODO_COL_NO} from '...'"
}
edit() {
# Edit a TODO
#
# $1: ID
id=$1
body=`sed -n "/^$id /s/^$id \[.\] (.) \(.*\)/\1/p" $TODO_LIST`
read -e -p "Edit> " -i "$body" body_new
_setHighlighter $id $TODO_COL_UPD
sed "s/\(^$id \[.\] (.) \).*/\1$body_new/" $TODO_LIST | draw
echo -e "\nUpdated body of ${TODO_COL_UPD}TODO #$id${TODO_COL_NO}. Old: ${TODO_COL_UPD}${body}${TODO_COL_NO}"
}
setStatus() {
# Mark as done, pending or todo
#
# $1: ? pending, x done, 'o' todo
# $2: id
# Set the mark
if [ $1 = "o" ]; then
mark=' '
else
mark=$1
fi
# TODO: put ids in or so dont iterate
shift;
for id in $*; do
sed -i "s/\(^$id \)\[.\]/\1[$mark]/" $TODO_LIST
done
# Bypass filter otherwise may hide the todo with the new status
TODO_FILTER_BYPASS=1
_setHighlighter $id $TODO_COL_UPD
lsTodoAll
echo -e "\nMarked ${TODO_COL_UPD}TODO #${id}${TODO_COL_NO} as ${TODO_COL_UPD}$mark${TODO_COL_NO}"
}
setPriority() {
# Set priority
#
# $1: ! bug/critical, H high, L low, 0 unset
# $2: id
# Set the priority
if [ $1 = "0" ]; then
priority=' '
else
priority=$1
fi
# TODO: put ids in or so dont iterate
shift;
for id in $*; do
sed -i "s/\(^$id \[.\] \)(.)/\1($priority)/" $TODO_LIST
done
# Bypass filter otherwise may hide the todo with the new priority
TODO_FILTER_BYPASS=1
_setHighlighter $id $TODO_COL_UPD
lsTodoAll
echo -e "\nSet priority for ${TODO_COL_UPD}TODO #${id}${TODO_COL_NO} to ${TODO_COL_UPD}$priority${TODO_COL_NO}"
}
createCategory() {
# Create a new category
#
# $1: cat name
cat=$1
# Find string length and print N '='
underline=`printf "%${#cat}s" | tr " " "="`
if [ `stat -c %s $TODO_LIST` -eq 0 ]; then
echo -e "$cat\n$underline" > $TODO_LIST
else
sed -i '$a'"\\\n$cat\n$underline" $TODO_LIST
fi
}
## List actions ##
showTodo() {
sed -n -e "/^$1 /p" $TODO_LIST | draw
}
lsCategories() {
# List categories
sed -n -e '/^[A-Za-z]/p' $TODO_LIST
}
lsTodoCategories() {
# List all todo in category
#
# $1: category
for category in "$@"; do
# Delete the last line only if it is blank
sed -n -e "/^$category/,/^$/p" $TODO_LIST | sed '${/^$/d;}' | draw
done
}
lsTodoAll() {
cat $TODO_LIST | draw
}
### Core functions ###
_setFilter() {
# Filter by params
TODO_FILTER_STATUS=''
TODO_FILTER_PRIORITY=''
# Search status and priority
for i in "$@"; do
# Use the == "$i" for enhanced reliability due to strange chars
if [[ `echo $i | sed -n '/^^*[x?]/p'` == "$i" ]]; then
#if [ "$i" == "o" ]; then i=' '; fi
TODO_FILTER_STATUS="${TODO_FILTER_STATUS}[$i]\|";
TODO_FILTER_BYPASS=0
shift
elif [[ `echo $i | sed -n '/^^*[!HL]/p'` == "$i" ]]; then
#if [ "$i" == "0" ]; then i=' '; fi
TODO_FILTER_PRIORITY="${TODO_FILTER_PRIORITY}[$i]\|";
TODO_FILTER_BYPASS=0
shift
elif [[ $i == '*' || $i == '.' ]]; then
TODO_FILTER_STATUS="."
TODO_FILTER_PRIORITY="."
TODO_FILTER_BYPASS=0
else
# Out when you find the first letter, may be the category or
# free text
break
fi
done
# The filter has been emptied at the beginning of the function
if [ -n "$TODO_FILTER_STATUS" ]; then
TODO_FILTER_STATUS="\($TODO_FILTER_STATUS\)"
else
TODO_FILTER_STATUS='.'
fi
if [ -n "$TODO_FILTER_PRIORITY" ]; then
TODO_FILTER_PRIORITY="\($TODO_FILTER_PRIORITY\)"
else
TODO_FILTER_PRIORITY='.'
fi
}
_setHighlighter() {
# Setup highlighter
#
# $1: ID
# $2: Highlight color
TODO_HL_ID=$1
TODO_HL_COL=$2
}
highlightify() {
# Highlight an entire TODO
#
# $TODO_HL_ID: ID
# $TODO_HL_COL: Highlight color
id=$TODO_HL_ID
color=$TODO_HL_COL
sed "s/^[ ]*$id .*/$color&$TODO_COL_NO/"
}
columnify() {
_lastTodoID
if [ $TODO_LAST_ID -le 9 ]; then
#TODO_ID_DIGITS=1
colorify
elif [ $TODO_LAST_ID -ge 10 ] && [ $TODO_LAST_ID -le 99 ]; then
#TODO_ID_DIGITS=2
sed "s/^\(^[0-9]\) \[/ \1 [/"
elif [ $TODO_LAST_ID -ge 100 ] && [ $TODO_LAST_ID -le 999 ]; then
#TODO_ID_DIGITS=3
sed "s/^\([0-9]\) \[/ \1 [/" |
sed "s/^\([0-9]\{2\}\) \[/ \1 [/"
else
#TODO_ID_DIGITS=4
sed "s/^\([0-9]\) \[/ \1 [/" |
sed "s/^\([0-9]\{2\}\) \[/ \1 [/" |
sed "s/^\([0-9]\{3\}\) \[/ \1 [/"
fi
}
colorify() {
sed "s/^[a-zA-Z].*/$TODO_COL_CAT&$TODO_COL_NO/" |
if [ $TODO_HL_ID -ne 0 ]; then
highlightify
else
sed "s/\([0-9][0-9]* \[\)\(x\)\]\|\(?\)\]/\1$TODO_COL_DONE\2$TODO_COL_PEND\3$TODO_COL_NO]/"|
sed "s/\((\)\(!\))\|\(H\))\|\(L\))/\1$TODO_COL_BUG\2$TODO_COL_H\3$TODO_COL_L\4$TODO_COL_NO)/"
fi
}
filter() {
status=${TODO_FILTER_STATUS:-'.'}
priority=${TODO_FILTER_PRIORITY:-'.'}
sed -n -e "/\(^[0-9][0-9]* \[$status\] ($priority)\)\|\(^[^0-9]\)\|\(^$\)/p"
}
comments() {
# To remove blank lines before content, use:
# sed '/./,$!d'
# Remove comments
sed "/^[#]/d"
}
draw() {
# Always pipe to this function to print!
if [ $TODO_FILTER_BYPASS -eq 0 ]; then
comments | filter | columnify | colorify
else
comments | columnify | colorify
fi
}
_createListFile() {
new_list=`cat <<HERE
# To do list managed with: TODO http://github.com/lucafaggianelli/todo
#
# You can edit it by hand following the format:
#
# 28 = Unique id in entire list!
# [ ] = Status. [x] done, [?] pending, [ ] to do
# ( ) = Priority. (!) critical, (H) high, (L) low
# text= TODO body on 1 line!
# # = 1 line comment
#
# Note: No spaces at the beginning of the line! 1 space to separate elements!
#
# Category
# ========
# 28 [x] (!) TODO body on one line
HERE`
# Check again that file doesn't exist, you don't want to mess up the user list
# also use append (>>) as a triple check!
if [ ! -e "$TODO_LIST" ]; then
touch "$TODO_LIST"
echo "$new_list" >> "$TODO_LIST"
fi
}
_findTodoListFile() {
for (( i=0; i<=TODO_LIST_DEPTH; i++ )); do
depth=''
for (( j=0; j<i; j++ )); do
depth="$depth../"
done
if [[ -e "${depth}${TODO_LIST}" && ! -d "${depth}${TODO_LIST}" ]]; then
if [ ! -r "${depth}${TODO_LIST}" ];then
echo "File "${depth}${TODO_LIST}" not readable"
exit 1
fi
if [ ! -w "${depth}${TODO_LIST}" ];then
echo "File "${depth}${TODO_LIST}" not writable"
exit 1
fi
TODO_LIST="${depth}${TODO_LIST}"
return
fi
done
echo "File $TODO_LIST doesn't exist here, till $TODO_LIST_DEPTH levels up."
read -e -p "Create the file $TODO_LIST? (y/n)> " decision
[ "$decision" == 'y' ] && _createListFile
exit 1
}
clean() {
rm $TODO_LIST
touch $TODO_LIST
}
##########################################################################
# MAIN
# $0 is still the script name
# $1 is the command
_findTodoListFile
# Clear terminal
[ $TODO_CLEAR_TERM -eq 1 ] && clear
# Set filter for user preferences
[ $TODO_HIDE_DONE -eq 1 ] && _setFilter '^x'
# Default action
if [ $# -eq 0 ]; then
lsTodoAll
exit 1
fi
# Fetch the command
action=$1;shift
# Smart CLI
# Workaround for / command
TODO_ID=`echo $action | sed -n '/^[0-9][0-9]*$/p'`
[ `echo $action | sed -n '/^[a-zA-Z][a-zA-Z]*$/p'` ] &&
TODO_CAT=`lsCategories | sed -n "/^$action$/p"` || TODO_CAT=''
# You selected a TODO what next?
if [ -n "$TODO_ID" ]; then
# If no command, show the todo
# e.g.: t 14
if [ "$#" == "0" ]; then
showTodo $TODO_ID
exit 1
# Set status
# e.g.: t 14 x
elif [[ $1 == 'x' || $1 == '?' || $1 == 'o' ]]; then
setStatus $1 $TODO_ID
exit 1
# Set priority
# e.g.: t 14 !
elif [[ $1 == '!' || $1 == 'H' || $1 == 'L' || $1 == '0' ]]; then
setPriority $1 $TODO_ID
exit 1
# Edit
# e.g.: t 14 edit
elif [[ "$1" == 'edit' || "$1" == 'e' ]]; then
edit $TODO_ID
exit 1
# Remove
# e.g.: t 14 rm
elif [[ "$1" == 'remove' || "$1" == 'rm' ]]; then
delete $TODO_ID
fi
# You selected a Category what next?
elif [ -n "$TODO_CAT" ]; then
# Category as first arg and nothing => lscat
# e.g.: t Core
if [ "$#" == "0" ];then
lsTodoCategories "$TODO_CAT"
exit 1
# If there is something more than the cat, you want to add a TODO into it
# t Core ! Great todo
else
add "$TODO_CAT" "$@"
fi
fi
### RETROCOMPATIBILITY ###
# Execute a command
case $action in
"a" | "add" ) add "$@" ;; # Done
"r" | "rm" ) delete $* ;; # Done
# List TODOs by category
"ls" )
# If you are typing ls, maybe you want to see everything...
_setFilter '.'
if [ "$#" == "0" ]; then
# No category, list all
lsTodoAll
else
lsTodoCategories "$@"
fi
;;
# List categories
"lscat" ) lsCategories ;;
# Edit TODO
"e" | "edit" ) edit $1 ;; # Done
# Filter, find, order...
"/" | "f" )
_setFilter "$@"
lsTodoAll
;;
# Mark as done, pending or todo
"x" | "?" | "o" ) setStatus $action $* ;; #Done
"m" | "mark" )
marker=$1
shift;
setStatus $marker $*
;;
# Set priority
"!" | "H" | "L" | "0" ) setPriority $action $* ;; # Done
"p" | "priority" )
priority=$1
shift;
setPriority $priority $*
;;
# Test
"t" )
echo 'test'
;;
* )
echo 'Any help?'
;;
esac