forked from jazzband/Watson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatson.zsh-completion
205 lines (181 loc) · 5.89 KB
/
watson.zsh-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
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
#compdef watson
# ~~~ LIST OF COMMANDS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_watson_commands=(
'add:Add a frame for a project that was not tracked live.'
'cancel:Cancel the last call to the start command.'
'config:Get and set configuration options.'
'edit:Edit a frame.'
'frames:Display the list of all frame IDs.'
'help:Display help information'
'log:Display each recorded session during the...'
'merge:Perform a merge of the existing frames with a...'
'rename:Rename a project or tag.'
'projects:Display the list of all the existing...'
'remove:Remove a frame.'
'report:Display a report of the time spent on each...'
'restart:Restart monitoring time for a previously...'
'start:Start monitoring time for the given project.'
'status:Display when the current project was started...'
'stop:Stop monitoring time for the current project.'
'sync:Get the frames from the server and push the...'
'tags:Display the list of all the tags.'
)
# ~~~ LOCATING THE FRAMES FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if [[ -n "$WATSON_DIR" ]]; then
_watson_frame_file=${WATSON_DIR}/frames
elif [[ "$OSTYPE" =~ "^darwin" ]]; then
_watson_frame_file="${HOME}/Library/Application Support/watson/frames"
else
_watson_frame_file=${HOME}/.config/watson/frames
fi
# ~~~ CACHING ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Using the cache mechanism of zsh to store projects, tags and frame lists
(( $+functions[_watson_get_projects] )) ||
_watson_get_projects() {
local cacheid='WATSON_PROJECTS'
if ( [[ ${+_watson_project_list} -eq 0 ]] || _cache_invalid $cacheid ) \
&& ! _retrieve_cache $cacheid;
then
_watson_project_list=("${(f)$(_call_program watson-projects watson projects)}")
_store_cache $cacheid _watson_project_list
fi;
}
(( $+functions[_watson_get_tags] )) ||
_watson_get_tags() {
local cacheid='WATSON_TAGS'
if ( [[ ${+_watson_tag_list} -eq 0 ]] || _cache_invalid $cacheid ) \
&& ! _retrieve_cache $cacheid;
then
_watson_tag_list=("${(f)$(_call_program watson-tags watson tags)}")
_store_cache $cacheid _watson_tag_list
fi;
}
(( $+functions[_watson_get_frames] )) ||
_watson_get_frames() {
local cacheid='WATSON_FRAMES'
if ( [[ ${+_watson_frame_list} -eq 0 ]] || _cache_invalid $cacheid ) \
&& ! _retrieve_cache $cacheid;
then
_watson_frame_list=("${(f)$(_call_program watson-log watson log)}")
_store_cache $cacheid _watson_frame_list
fi;
}
# Define a default caching policy for watson: we rebuild the cache
# if the cache file is older than watson's frame file.
local cache_policy
zstyle -s ":completion::complete:watson:*" cache-policy cache_policy
if [[ -z "$cache_policy" ]]; then
zstyle ":completion::complete:watson:*" cache-policy _watson_caching_policy
fi
_watson_caching_policy() {
[[ "$_watson_frame_file" -nt "$1" ]] && return 0
return 1
}
# ~~~ COMPLETION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_watson() {
typeset -A opt_args
local context state line curcontext="$curcontext"
_arguments : \
'--version[Show the version and exit.]' \
'--help[Show help and exit.]' \
'1: :_watson_cmds' \
'*::arg:->args' \
&& return 0
case "$state" in
(args)
case $words[1] in
(cancel|help|projects|restart|status|stop|sync|tags)
_arguments : '--help'
;;
(add)
_arguments -A '-*' : \
': :_watson_projects' \
'*: :_watson_plus_tags' \
'(--from -f)'{-f,--from}'[start date and time (YYYY-MM-DD H:M:S)]' \
':date (YYYY-MM-DD H:M:S):' \
'(--to -t)'{-t,--to}'[end date and time (YYYY-MM-DD H:M:S)]' \
':date (YYYY-MM-DD H:M:S):' \
'--help'
;;
(start)
_arguments -A '-*' : \
': :_watson_projects' \
'*: :_watson_plus_tags' \
'--help'
;;
(edit)
_arguments -A '-*' : \
': :_watson_frames' \
'--help'
;;
(remove)
_arguments : \
'-f[Dont ask for confirmation]' \
': :_watson_frames' \
'--help'
;;
(log|report)
_arguments : \
'*'{-p,--project}'[only for the given project]: :_watson_projects' \
'*'{-T,--tag}'[only for the given tag]: :_watson_tags' \
'(--from -f)'{-f,--from}'[start date]:date (YYYY-MM-DD):' \
'(--to -t)'{-t,--to}'[end date]:date (YYYY-MM-DD):' \
'(--all -a)'{-a,--all}'[output all frames]:' \
'--help'
;;
(merge)
_arguments : \
'--help' \
{-f,--force} \
': :_files'
;;
(rename)
_arguments : \
'*'{project}'[rename given project]: :_watson_projects' \
'*'{tag}'[rename given tag]: :_watson_tags' \
'--help'
;;
esac
;;
esac;
}
(( $+functions[_watson_projects] )) ||
_watson_projects() {
_watson_get_projects
local expl
_description projects expl 'projects'
compadd "$expl[@]" -a -- _watson_project_list
}
(( $+functions[_watson_frames] )) ||
_watson_frames() {
_watson_get_frames
local expl i
for l in $_watson_frame_list; do
if [[ "$l" =~ '^[A-Z]' ]];
then
i=$(($i+1))
_description -V frames$i expl "${l%%\(*\)}"
else
desc=(${l##[[:space:]]})
IFS=' ' read -r val _ <<< $desc
compadd "$expl[@]" -d desc -l - "$val"
fi
done;
}
(( $+functions[_watson_tags] )) ||
_watson_tags() {
_watson_get_tags
compadd "$@" -a -- _watson_tag_list
}
(( $+functions[_watson_plus_tags] )) ||
_watson_plus_tags() {
_watson_get_tags
local expl
_description tags expl 'tags'
compadd "$expl[@]" -a -p '+' -- _watson_tag_list
}
(( $+functions[_watson_cmds] )) ||
_watson_cmds() {
_describe -t commands 'commands' _watson_commands "$@"
}
_watson "$@"