-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtodofi.sh
executable file
·493 lines (419 loc) · 13.5 KB
/
todofi.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
#! /usr/bin/env bash
VERSION="1.0.0"
TODOTXT_CFG_FILE="${HOME}/.config/todo/config"
CONFIG_FILE="${HOME}/.config/todofish.conf"
FILTER_FILE="`dirname ${CONFIG_FILE}`/todofish_filter.sh"
FILTER=""
KIND=""
# if LINES_MODE == auto, window height is set from the lines count
# and by following the constraints LINES_MIN and LINES_MAX
LINES_MODE="auto"
LINES_MIN=1
LINES_MAX=15
COLOR_TITLE="#00CC00"
COLOR_SHORTCUT="#0000CC"
COLOR_INFO="#FF0000"
COLOR_EXAMPLE="#0000CC"
COLOR_ITEM="#0000FF"
# Don't forget to quote regex char
MARKUP_PRIORITY='<b>\1<\/b> \2'
MARKUP_PROJECT='<span fgcolor="darkblue"><b>\1<\/b><\/span>'
MARKUP_CONTEXT='<span fgcolor="darkgreen"><b>\1<\/b><\/span>'
MARKUP_TAG='<span fgcolor="gray"><b>\1<\/b><\/span>'
MARKUP_DUE='<span fgcolor="red"><b>\1<\/b><\/span>'
SHORTCUT_NEW="Alt+a"
SHORTCUT_DONE="Alt+d"
SHORTCUT_EDIT="Alt+e"
SHORTCUT_SWITCH="Alt+Tab"
SHORTCUT_TERM="Alt+t"
SHORTCUT_FILTERS="Alt+p"
SHORTCUT_CLEAR="Alt+c"
SHORTCUT_HELP="Alt+h"
EDITOR='gedit'
ROFI_BIN="$(command -v rofi)"
TODO_BIN=$(command -v todo-txt || command -v todo.sh)
readonly PROGNAME=$(basename $0)
if [[ -z "$ROFI_BIN" ]]; then
echo "Missing rofi, please install it !"
exit 1
fi
if [[ -z "$TODO_BIN" ]]; then
echo "Missing todo-txt, please install it !"
exit 1
fi
runrofi () {
$ROFI_BIN -width 80% -matching glob -tokenize -i -no-levenshtein-sort "$@"
}
runtodo_verbose() {
$TODO_BIN -p -d "$TODOTXT_CFG_FILE" "$@"
}
runtodo() {
TODOTXT_VERBOSE=0 runtodo_verbose "$@"
}
list() {
case $KIND in
"all")
runtodo listall;;
*)
runtodo list "$@";;
esac
}
filter_by_priority() {
IFS=$'\n'
n=0
while read LINE; do
priority=`echo $LINE | grep -oP "^\d+ \K(\([A-Z]\))"`
priority="${priority:1:1}"
if [[ $priority == $1 ]]; then
echo -n "${n},"
fi
n=$((n+1))
done
}
confirm() {
action=$1
context=$2
message="Confirm $action ?"
# Todo: remove duplicate code...
if [[ $context ]]; then
mesg="Item: <span foreground=\"${COLOR_ITEM}\">${context}</span>"
response=$(echo -e "Yes\nNo" | $ROFI_BIN -lines 2 -u 0 -a 1 -dmenu -mesg "$mesg" -i -p "$message")
else
response=$(echo -e "Yes\nNo" | $ROFI_BIN -lines 2 -u 0 -a 1 -dmenu -i -p "$message")
fi
if [[ "$response" == "Yes" ]]; then
return 0;
else
return 1;
fi
}
add() {
projcon=`getprojconheader`
new_todo=$(echo -e "< Cancel" | runrofi -lines 1 -dmenu -mesg "New todo
${projcon}" -p "> ")
if [[ "$new_todo" != "" ]]; then
runtodo add $new_todo
fi
}
ere_quote() {
# Picked from https://stackoverflow.com/a/16951928
sed 's/[]\.|$(){}?+*^[]/\\&/g' <<< "$*"
}
highlight() {
# Escape &, <, >
line=`echo "$1" | sed 's/\&/\&/g' | sed 's/</\</g; s/>/\>/g;'`
# Highlight
WORD_REGEX="[[:alnum:]]+"
echo "${line}" | sed -r "
s/^\(([a-zA-Z]+)\) (.*)/${MARKUP_PRIORITY}/g;
s/(\+${WORD_REGEX})/${MARKUP_PROJECT}/g;
s/(\@${WORD_REGEX})/${MARKUP_CONTEXT}/g;
s/(\#${WORD_REGEX})/${MARKUP_TAG}/g;
s/(due\:[0-9\-]+)/${MARKUP_DUE}/g"
}
unhighlight() {
# Unescape <, >, &
line=`echo "$1" | sed 's/\</</g; s/\>/>/g;' | sed 's/\&/\&/g'`
UNMARKUP_PRIORITY="${MARKUP_PRIORITY/\\1/([a-zA-Z]+)}"
UNMARKUP_PRIORITY="${UNMARKUP_PRIORITY/\\2/(.*)}"
REGEX="([^<]*)"
UNMARKUP_PROJECT="${MARKUP_PROJECT/\\1/${REGEX}}"
UNMARKUP_CONTEXT="${MARKUP_CONTEXT/\\1/${REGEX}}"
UNMARKUP_TAG="${MARKUP_TAG/\\1/${REGEX}}"
UNMARKUP_DUE="${MARKUP_DUE/\\1/${REGEX}}"
echo "${line}" | sed -r "
s/^${UNMARKUP_PRIORITY}/(\1) \2/g;
s/${UNMARKUP_PROJECT}/\1/g;
s/${UNMARKUP_CONTEXT}/\1/g;
s/${UNMARKUP_TAG}/\1/g;
s/${UNMARKUP_DUE}/\1/g"
}
getprojconheader() {
listproj=`runtodo listproj | tr '\n' ' '`
listcon=`runtodo listcon | tr '\n' ' '`
listproj=`highlight "${listproj}"`
listcon=`highlight "${listcon}"`
echo "Projects: ${listproj}
Context: ${listcon}"
}
getlinenumber() {
line=`unhighlight "$1"`
line=`ere_quote "${line}"`
echo `runtodo ls | grep -P "\d+ ${line}$" | awk '{print $1}'`
}
extractcontent() {
line="$1"
if [[ "${line:0:1}" == '(' ]]; then
echo "$line" | sed 's/\([^ ]*\) \(.*\)/\2/'
else
echo "$line"
fi
}
edit() {
lineno=$1
current_line=`unhighlight "$2"`
current_line=`extractcontent "${current_line}"`
projcon=`getprojconheader`
todo=$(runrofi -lines 0 -dmenu -mesg "Edit todo
${projcon}" -p "> " -filter "$current_line")
if [[ -n "$todo" ]]; then
runtodo replace "$lineno" "$todo"
fi
}
editpriority() {
lineno=$1
current_line="$2"
priority=$(for letter in {A..Z}; do echo "$letter"; done| runrofi -dmenu -mesg "Item: <span foreground=\"${COLOR_ITEM}\">${current_line}</span>" -p "> ")
runtodo pri "$lineno" "$priority"
}
option() {
current_line="$1"
while true
do
if [[ ${current_line:0:1} == 'x' ]]; then
selection=$(echo -e "Not implemented" | runrofi -sep "|" -kb-accept-entry "Return" -mesg "Item: ${current_line}" -dmenu -p "Action")
break
else
selection=$(echo -e "1. Mark Done|2. Edit|3. Edit priority|4. Remove priority|5. Delete" | runrofi -lines 5 -sep "|" -u 4 -a 0 -kb-accept-entry "Return" -mesg "Item: ${current_line}" -dmenu -p "Action")
lineno=`getlinenumber "$current_line"`
case "${selection:0:1}" in
"1")
confirm "mark as done" "$current_line" && runtodo do $lineno && break;;
"2")
edit $lineno "$current_line" && break;;
"3")
editpriority $lineno "$current_line" && break;;
"4")
confirm "remove priority" "$current_line" && runtodo depri $lineno && break;;
"5")
confirm "deletion" "$current_line" && runtodo -f del $lineno && break;;
*)
break;;
esac
fi
done
}
savefilter() {
echo "# File used by todofi.sh" > $FILTER_FILE
echo "FILTER=\"$FILTER\"" >> $FILTER_FILE
}
loadfilter() {
[ -f $FILTER_FILE ] && source $FILTER_FILE
}
termfilter() {
HEADER="Filter tasks by using term
Ex:
* Display only tasks that contains FOO: <span color='${COLOR_EXAMPLE}'>FOO</span>
* Display only tasks that contains FOO or BAR: <span color='${COLOR_EXAMPLE}'>FOO|BAR</span>
* Hide tasks that do not contains FOO: <span color='${COLOR_EXAMPLE}'>-FOO</span>"
FILTER=$(runrofi -format f -lines 0 -kb-accept-entry "Return" -mesg "${HEADER}" -dmenu -p "Term" -filter "$FILTER")
savefilter
}
listprojectandcontext() {
HEADER="Select a project (+) or context (@) that will serve as a persistent filter"
listproj=`runtodo listproj`
listcon=`runtodo listcon`
selection=$(echo -e "All\n""${listproj}\n${listcon}\n${listprojno}\n${listconno}" | runrofi -kb-accept-entry "Return" -mesg "${HEADER}" -dmenu -p "Action")
if [[ $selection == 'All' ]]; then
FILTER=""
elif [[ -n $selection ]]; then
FILTER="$selection"
fi
savefilter
}
formatline() {
while read LINE; do
LINE=`echo "${LINE}" | sed -r 's/[0-9]*\ (.*)/\1/g'`
highlight "${LINE}"
done
}
linescount() {
count="$1"
if [[ $LINES_MODE == 'auto' ]]; then
count=$(($count < $LINES_MIN ? $LINES_MIN : $count))
count=$(($count > $LINES_MAX ? $LINES_MAX : $count))
echo "-lines $count"
fi
}
TODOFISH_HEADER="<span color=\"${COLOR_TITLE}\">Todofi.sh</span>"
config() {
HELP="${TODOFISH_HEADER} - Configuration files"
source $TODOTXT_CFG_FILE
selection=$(
echo -e "1. Open todo.txt config file (${TODOTXT_CFG_FILE})|2. Open todofish config file (${CONFIG_FILE})|3. Open current filter file (${FILTER_FILE})" | \
runrofi -sep "|" -lines 3 -u 0 -a 1 -kb-accept-entry "Return" -mesg "${HELP}" -dmenu -p "Action"
)
val=$?
if [[ $val -eq 0 ]]; then
case "${selection:0:1}" in
"1")
$EDITOR $TODOTXT_CFG_FILE;;
"2")
$EDITOR $CONFIG_FILE;;
"3")
$EDITOR $FILTER_FILE;;
*)
exit;;
esac
fi
}
help() {
HELP="${TODOFISH_HEADER} - Version ${VERSION} - Charles Rincheval, April 2021
--
* Add todo <span color='${COLOR_SHORTCUT}'>${SHORTCUT_NEW}</span>
* Mark as done <span color='${COLOR_SHORTCUT}'>${SHORTCUT_DONE}</span>
* Edit <span color='${COLOR_SHORTCUT}'>${SHORTCUT_EDIT}</span>
* Switch Active / Done <span color='${COLOR_SHORTCUT}'>${SHORTCUT_SWITCH}</span>
* Filter: Create a filter term <span color='${COLOR_SHORTCUT}'>${SHORTCUT_TERM}</span> / Choose from list <span color='${COLOR_SHORTCUT}'>${SHORTCUT_FILTERS}</span> / Clear <span color='${COLOR_SHORTCUT}'>${SHORTCUT_CLEAR}</span>
--
Todo.txt format: https://github.com/todotxt/todo.txt"
source $TODOTXT_CFG_FILE
selection=$(
echo -e "1. Archive|2. Deduplicate|3. Report|4. Open todo.txt (${TODO_FILE})|5. Open done.txt (${DONE_FILE})|6. See configuration files" | \
runrofi -sep "|" -lines 6 -u 0 -a 1 -kb-accept-entry "Return" -mesg "${HELP}" -dmenu -p "Action"
)
val=$?
if [[ $val -eq 0 ]]; then
result=""
case "${selection:0:1}" in
"1")
confirm "archive" && result=`runtodo_verbose archive`;;
"2")
confirm "deduplicate" && result=`runtodo deduplicate`;;
"3")
confirm "report" && result=`runtodo report`;;
"4")
$EDITOR $TODO_FILE;;
"5")
$EDITOR $DONE_FILE;;
"6"):
config;;
*)
exit;;
esac
if [[ "$result" ]]; then
$ROFI_BIN -e "$result"
fi
fi
}
main() {
if [[ $FILTER_ARG ]]; then
FILTER=$FILTER_ARG
else
loadfilter
fi
while true
do
escaped_filter=`echo "$FILTER" | sed -e 's=|=\\\|=g'`
list=`list $escaped_filter`
list=${list//\\/\\\\}
high=`echo "$list" | filter_by_priority A`
medium=`echo "$list" | filter_by_priority B`
count=0
if [[ "$list" ]]; then
count=`echo "$list" | wc -l`
fi
count_string="<span color=\"${COLOR_INFO}\">${count}</span>"
if [[ "$escaped_filter" ]]; then
countall=`runtodo list | wc -l`
count_string="${count_string} displayed / <span color=\"${COLOR_INFO}\">${countall}</span> item(s)"
else
count_string="${count_string} item(s)"
fi
current_filter=""
if [[ $FILTER ]]; then
current_filter="- Current filter is <span color=\"${COLOR_INFO}\">${FILTER}</span>"
fi
HEADER="${TODOFISH_HEADER} - <span color='${COLOR_SHORTCUT}'>${SHORTCUT_HELP}</span> for help - $count_string $current_filter"
selection=$( \
echo -E "${list}" | \
formatline | \
runrofi `linescount $count` -kb-custom-1 "${SHORTCUT_NEW}" \
-kb-custom-2 "${SHORTCUT_DONE}" \
-kb-custom-3 "${SHORTCUT_EDIT}" \
-kb-custom-4 "${SHORTCUT_FILTERS}" \
-kb-custom-5 "${SHORTCUT_CLEAR}" \
-kb-custom-6 "${SHORTCUT_SWITCH}" \
-kb-custom-7 "${SHORTCUT_HELP}" \
-kb-custom-8 "${SHORTCUT_TERM}" \
-kb-accept-entry "Return" \
-markup-rows \
-u "${high}" -a "${medium}" -mesg "${HEADER}" -dmenu -p "Filter")
val=$?
lineno=`getlinenumber "$selection"`
if [[ $val -eq 0 ]]; then
if [[ $lineno ]]; then
option "$selection"
else
echo "No line number for '${selection}' !"
exit
fi
elif [[ $val -eq 10 ]]; then
add
elif [[ $val -eq 12 ]]; then
edit $lineno "$selection"
elif [[ $val -eq 11 ]]; then
confirm "mark as done" "$selection" && runtodo do "$lineno"
elif [[ $val -eq 17 ]]; then
termfilter
elif [[ $val -eq 13 ]]; then
listprojectandcontext
elif [[ $val -eq 14 ]]; then
FILTER=""
savefilter
elif [[ $val -eq 15 ]]; then
if [[ $KIND == 'all' ]]; then
KIND=""
else
KIND="all"
fi
echo "kind is $KIND"
elif [[ $val -eq 16 ]]; then
help
elif [[ $val -ne 1 ]]; then
echo "Value '$val' not handled !"
exit
else
exit
fi
done
}
usage() {
cat <<- EOF
usage: $PROGNAME options
Todo-txt + Rofi = Todofi.sh
Handle your todo-txt tasks directly from Rofi
OPTIONS:
-a Open in add mode
-f filter Filter applied on tasks
-F filename Filter file (read and write filter on this file)
-c filename Config file
-d filename Todo.txt config file
EOF
}
if [[ -e $CONFIG_FILE ]]; then
source $CONFIG_FILE
fi
while getopts "h?af:F:c:d:" opt; do
case "$opt" in
h|\?)
usage
exit 0;;
a)
ADD_MODE=true;;
f)
FILTER_ARG=$OPTARG;;
F)
FILTER_FILE=$OPTARG;;
c)
CONFIG_FILE=$OPTARG
source $CONFIG_FILE;;
d)
TODOTXT_CFG_FILE=$OPTARG;;
esac
done
if [[ $ADD_MODE ]]; then
add
else
main
fi