-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo_completer.sh
executable file
·104 lines (88 loc) · 3.03 KB
/
todo_completer.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
# todo.sh completion by Pedro Melo <melo@simplicidade.org>
#
# for updates see: http://todotxt.com/library/todo_completer.sh/
# Modified by Gina Trapani 7/20/2006
# - added new options (fqv) and command listall in todo.sh 1.7
# - removed @ symbols (threw an error)
# Modified by Gina Trapani 7/29/2006
# - added new command options, listall/ls/lsa/a/p/app/prep/lsp
seq_replacement()
{
local lower upper output;
lower=$1 upper=$2;
while [ $lower -le $upper ];
do
output="$output $lower";
lower=$[ $lower + 1 ];
done;
echo $output
}
_todo_sh()
{
local cur prev commands options command expand_options
long_commands='add adda addx append archive del do edit go list listpri listall \
prepend pri replace remdup report unwait wait waiting'
short_commands='a app b ls lsp lsa prep p pv uw w'
options="-d -f -p -h -v -V"
if [ -n "$TODOSH_DONT_COMPLETE_SHORT_COMMANDS" ] ; then
commands=$long_commands
else
commands="$long_commands $short_commands"
fi
if [ -n "$TODOSH_OTHER_COMMANDS" ] ; then
commands="$commands $TODOSH_OTHER_COMMANDS"
fi
TODOSHRC=${TODOTXT_CFG_FILE:-$HOME/.todo/todo.cfg}
if [[ -r $TODOSHRC ]] ; then
. $TODOSHRC
fi
if [[ ! -r $TODO_FILE ]] ; then
echo "ERROR: cannot read todo.txt file."
echo "Make sure TODOSHRC is set with the correct .todo config file";
return 0
fi
expand_options=1
command=""
up_to=$(($COMP_CWORD-1))
for i in `seq_replacement 1 $up_to` ; do
cur=${COMP_WORDS[i]}
if [[ "${cur}" != -* ]] ; then
expand_options=0
if [[ -z "${command}" ]] ; then
command=${cur}
fi
fi
done
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
if [[ ${expand_options} == 1 && ${cur} == -* ]] ; then
COMPREPLY=( $( compgen -W "$options" -- $cur ) )
elif [[ -z "${command}" ]] ; then
COMPREPLY=( $( compgen -W "$commands" -- $cur ) )
else
case "${command}" in
add|a|list|ls|listall|lsa)
local contexts=$(perl -ne 'END { print "$_\n" foreach sort { $p{$a} <=> $p{$b} } keys %p; } $p{$_}++ foreach /\B(\@\w+)/g' $TODO_FILE)
local projects=$(perl -ne 'END { print "$_\n" foreach sort { $p{$a} <=> $p{$b} } keys %p; } $p{$_}++ foreach /\b(p-\w+)/g' $TODO_FILE)
local projects_plus=$(perl -ne 'END { print "$_\n" foreach sort { $p{$a} <=> $p{$b} } keys %p; } $p{$_}++ foreach /\B(\+\w+)/g' $TODO_FILE)
COMPREPLY=( $(compgen -W "${projects} ${projects_plus} ${contexts}" -- ${cur}) )
return 0
;;
append|app|del|rm|do|prepend|prep|pri|p|replace)
local n_todos=$(wc -l $TODO_FILE | awk '{print $1}')
local tasks=$(seq_replacement 1 $n_todos)
COMPREPLY=( $(compgen -W "${tasks}" -- ${cur}) )
return 0
;;
listpri | lsp)
local pris=$(egrep -o '\(\w+\)' $TODO_FILE | cut -c2 | sort | uniq -c | sort -rn | awk '{ print $2 }')
COMPREPLY=( $(compgen -W "${pris}" -- ${cur}) )
return 0
;;
*)
;;
esac
fi
return 0
}
complete -F _todo_sh -o default todo.sh