forked from jazzband/Watson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watson.zsh-completion
255 lines (231 loc) · 8.39 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
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
#compdef watson
# ~~~ LIST OF COMMANDS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_watson_commands=(
'add:Add a frame for a project that was not tracked live.'
'aggregate: Display a report of the time spent on each project aggregated...'
'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...'
'projects:Display the list of all the existing...'
'remove:Remove a frame.'
'rename:Rename a project or tag.'
'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|frames|help|projects|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' \
'(--gap -g)'{-g,--gap}'[leave gap after previous project]:' \
'(--no-gap -G)'{-G,--no-gap}'[set start time to end time of previous project]:' \
'--help'
;;
(stop)
_arguments : \
'--at [stop time (YYYY-MM-DDT)?HH:MM(:SS)]:' \
'--help'
;;
(edit)
_arguments -A '-*' : \
': :_watson_frames' \
'--help'
;;
(restart)
_arguments -A '-*' : \
': :_watson_frames' \
'(--stop -s)'{-s,--stop}'[stop already running project]:' \
'(--no-stop -S)'{-S,--no-stop}'[do not stop already running project]:' \
'--help'
;;
(remove)
_arguments : \
'(--force -f)'{-f,--force}'[Dont ask for confirmation]' \
': :_watson_frames' \
'--help'
;;
(status)
_arguments : \
'(--project -p)'{-p,--project}'[only output projects]' \
'(--tag -t)'{-t,--tag}'[only output tags]' \
'(--elapsed -e)'{-e,--elapsed}'[only output time elapsed]' \
'--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]:' \
'(--current -c)'{-c,--current}'[include currently running frame]:' \
'(--no-current -C)'{-C,--no-current}'[exclude currently running frame]:' \
'(--year -y)'{-y,--year}'[activity of current year]:' \
'(--month -m)'{-m,--month}'[activity of current month]:' \
'(--luna -l)'{-l,--luna}'[activity of current luna]:' \
'(--week -w)'{-w,--week}'[activity of current week]:' \
'(--day -d)'{-d,--day}'[activity of current day]:' \
'(--json -j)'{-j,--json}'[output in JSON format]:' \
'(--pager -g)'{-g,--pager}'[view through pager]:' \
'(--no-pager -G)'{-G,--no-pager}'[avoid using pager]:' \
'--help'
;;
(aggregate)
_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):' \
'(--current -c)'{-c,--current}'[include currently running frame]:' \
'(--no-current -C)'{-C,--no-current}'[exclude currently running frame]:' \
'(--json -j)'{-j,--json}'[output in JSON format]:' \
'(--pager -g)'{-g,--pager}'[view through pager]:' \
'(--no-pager -G)'{-G,--no-pager}'[avoid using pager]:' \
'--help'
;;
(config)
_arguments : \
'(--edit -e)'{-e,--edit}'[edit config file]:' \
'--help'
;;
(merge)
_arguments : \
'--help' \
'(--force -f)'{-f,--force}'[perform automatic merge]:' \
': :_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 "$@"