forked from karibou/tl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimelog_completion
69 lines (62 loc) · 1.74 KB
/
timelog_completion
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
# This should handle the following cases:
# * tl.py <options> - completes available options
# * tl.py <TAB> - completes category name
# * tl.py category <TAB> - completes colon (:)
# * tl.py category : <TAB> - completes with tasks list in a given category
_TIMELOG=tl.py
if which tl 2>&1 > /dev/null; then
_TIMELOG=tl
else
if which tl.py 2>&1 > /dev/null; then
_TIMELOG=tl.py
fi
fi
_print_categories()
{
$_TIMELOG -c -r
}
_print_tasks()
{
$_TIMELOG -r -t $1
}
_timelog()
{
local cur prev opts categories
COMPREPLY=()
curr="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="-r -c --list-categories -t --list-tasks new"
categories=$(_print_categories)
case "${prev}" in
-t)
COMPREPLY=( $(compgen -W "${categories}" -- ${curr}) )
;;
--list-tasks)
COMPREPLY=( $(compgen -W "${categories}" -- ${curr}) )
;;
-c)
COMPREPLY=( $(compgen -W "-r" -- ${curr}) )
;;
--list-categories)
COMPREPLY=( $(compgen -W "-r" -- ${curr}) )
;;
:)
local category="${COMP_WORDS[COMP_CWORD-2]}"
local tasks=$(_print_tasks $category)
tasks="${tasks//\\ /___}"
for iter in ${tasks}; do
if [[ $iter =~ ^$curr ]]; then
COMPREPLY+=( "${iter//___/ }" )
fi
done
;;
*)
if [[ "${categories}" = *"${prev}"* ]]; then
COMPREPLY=( $(compgen -W ":" -- ${curr}) )
return 0
fi
COMPREPLY=( $(compgen -W "${categories} ${opts}" -- ${curr}) )
;;
esac
}
complete -F _timelog $_TIMELOG