-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwatson-sh
More file actions
executable file
·176 lines (158 loc) · 3.79 KB
/
watson-sh
File metadata and controls
executable file
·176 lines (158 loc) · 3.79 KB
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
#!/bin/bash
#
# Copyright © 2016 Yannick Loiseau <me@yloiseau.net>
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
#
# TODO: add tags to the context
CONFIG_DIR=$HOME/.config/watson
if [ ! -z $XDG_CONFIG_HOME ] ; then
CONFIG_DIR=$XDG_CONFIG_HOME/watson
fi
if [ ! -z $WATSON_DIR ] ; then
CONFIG_DIR=$WATSON_DIR
fi
AUTOCOMPLETE=${CONFIG_DIR}/complete
WATSON="python3 -m watson"
function usage {
cat <<EOC
Usage: $0 [OPTION]
An iteractive shell for Watson.
For improved experience, use it with rlwrap. For instance:
alias watson-sh='rlwrap -pcyan -f $AUTOCOMPLETE $0'
Options:
--help Show this message and exit
--update Update the completion cache
Commands:
update Update the completion cache
quit Exit the shell
exit Exit the shell
ls Alias for projets or report according to the context
cd Set the current project
from Set the current starting date. Accepts any format understood by
'date', e.g. "1 week ago"
to Set the current ending date
any valid watson commands
commands prefixed with '!' are dispatched to the shell
EOC
$WATSON help
}
function updatecomp {
cat <<EOC >| $AUTOCOMPLETE
cancel
config
edit
frames
help
log
merge
projects
remove
report
restart
start
status
stop
sync
tags
update
quit
exit
ls
cd
from
to
EOC
watson projects >> $AUTOCOMPLETE
echo "" >> $AUTOCOMPLETE
watson tags | sed 's/^/+/g' >> $AUTOCOMPLETE
}
function _filter_options {
local options=""
if [ -n "$CURRENT_PROJECT" ] ; then
options="$options -p $CURRENT_PROJECT"
fi
if [ -n "$CURRENT_FROM" ] ; then
options="$options -f $(date -d "$CURRENT_FROM" -Iminutes)"
fi
if [ -n "$CURRENT_TO" ] ; then
options="$options -t $(date -d "$CURRENT_TO" -Iminutes)"
fi
echo $options
}
function _prompt {
local prompt="watson"
if [ -n "$CURRENT_PROJECT" ] ; then
prompt="${prompt}:${CURRENT_PROJECT}"
fi
if [ -n "$CURRENT_FROM" ] ; then
prompt="${prompt}:${CURRENT_FROM}"
fi
if [ -n "$CURRENT_TO" ] ; then
prompt="${prompt} → ${CURRENT_TO}"
fi
prompt="${prompt}> "
echo "$prompt"
}
function dispatch {
local cmd=$1; shift
case $cmd in
"") return ;;
update) updatecomp;;
quit|exit) exit 0;;
ls)
if [ -n "$CURRENT_PROJECT" ] ; then
dispatch report $@
else
dispatch projects $@
fi
;;
cd)
if [ "$1" == ".." -o -z "$1" ] ; then
CURRENT_PROJECT='';
else
CURRENT_PROJECT=$1
fi
;;
from)
if [ -z "$1" ] ; then
CURRENT_FROM='';
else
CURRENT_FROM="$@"
fi
;;
to)
if [ -z "$1" ] ; then
CURRENT_TO='';
else
CURRENT_TO="$@"
fi
;;
log) $WATSON log $(_filter_options) $@ ;;
report) $WATSON report $(_filter_options) $@ ;;
start)
if [ -n "$CURRENT_PROJECT" ] ; then
$WATSON start "$CURRENT_PROJECT" $@
else
$WATSON start "$@"
fi
;;
help) usage;;
!*)
${cmd#\!} $@ ;;
*) $WATSON $cmd $@;;
esac
}
case $1 in
-u|--update) updatecomp; exit 0 ;;
-h|--help) usage; exit 0 ;;
esac
CURRENT_PROJECT=''
CURRENT_FROM=''
CURRENT_TO=''
echo "Interactive Watson shell. Type 'Ctrl+D', 'exit' or 'quit' to exit."
echo "Type 'help' for more information"
while read -p "$(_prompt)" ; do
dispatch $REPLY
done