timesheet.sh
4.91 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/zsh
source $0:A:h/datetimehelper.sh
source $0:A:h/stringhelper.sh
source $0:A:h/taskhelper.sh
source $0:A:h/ttyhelper.sh
function showall() {
[[ $# -eq 0 ]] && return 1
local T UUID OIFS WORK NO
printf "%s %s %s %s %s\n" \
"$(underline -10 project)" \
"$(underline -33 description)" \
"$(underline -13 spent)" \
"$(underline -10 completed)" \
"$(underline -10 entered)"
for UUID in $@
do
NO=1
WORK="$(flattened_work ${UUID}|jq -M -r 'map([.[]]|@sh)|join("\n")')"
UNITS=$(echo ${WORK}|wc -l)
echo "${WORK}"|while read W; do
eval set -- ${W}
if [[ ${NO} -eq 1 ]]; then
local TIME="$(formattime ${4:-0:00:00:00})"
local PROJECT="$(task _get $UUID.project)"
printf "%-10.10s %-33.33s %s %-10.10s %-10.10s\n" \
"$(truncate "${PROJECT:-(none)}" 12)" \
"$(truncate "$(task _get $UUID.description)" 35)" \
"$(bold "13" "${TIME}")" \
"$(todate $(task _get $UUID.end))" \
"$(todate $(task _get $UUID.entry))"
fi
if [[ $# -ne 0 ]]; then
if [[ ${NO} -eq ${UNITS} ]]; then
printf "└─ %s %13.13s\n" \
"$(timestring "$1" "$2")" \
"$(formattime $2)"
else
printf "├─ %s %13.13s\n" \
"$(timestring "$1" "$2")" \
"$(formattime $2)"
fi
fi
NO=$((NO+1))
done
done
}
function timestring() {
local END="$1"
local DUR="$2"
local FORMAT="%d.%m.%Y %H:%M:%S"
eval set -- ${DUR//:/ }
local START=$(date -u -d \
"${END}-${1}days-${2}hours-${3}minutes-${4}seconds" \
+%Y-%m-%dT%H:%M:%SZ)
printf "%s - %s" \
"$(date -d "${START}" +${FORMAT})" \
"$(date -d "${END}" +${FORMAT})"
}
function timesheet() {
local PHRASE="1-week-ago"
local START="$(date +%Y-%m-%d -d ${PHRASE})"
local DONE="$(showall $(uuids -d -s end +COMPLETED end.after:${START} $@))"
local UPCOMING="$(showall $(uuids +PENDING $@))"
local BLOCKED="$(showall $(uuids +BLOCKED $@))"
local BLOCKING="$(showall $(uuids +BLOCKING $@))"
DONE="${DONE:-No tasks}"
UPCOMING="${UPCOMING:-No tasks}"
BLOCKED="${BLOCKED:-No tasks}"
BLOCKING="${BLOCKING:-No tasks}"
DONE="${DONE}${NL}"
UPCOMING="${UPCOMING}${NL}"
BLOCKED="${BLOCKED}${NL}"
BLOCKING="${BLOCKING}${NL}"
printf " (generated at %s)\n\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n" \
"${TODAY}" \
"$(bold "" "Tasks completed from $START to $TODAY (back $PHRASE)")" \
"${DONE}" \
"$(bold "" "Current tasks")" \
"${UPCOMING}" \
"$(bold "" "Blocked tasks")" \
"${BLOCKED}" \
"$(bold "" "Blocking tasks")" \
"${BLOCKING}"
halfyear="entry.after:$(date -d '6 month ago' +%F)"
twoyears="entry.after:$(date -d '2 years ago' +%F)"
printf "%s\n%s\n\n%s%s\n%s\n\n%s\n\n%s\n" \
"$(bold "" "Summary")" \
"$(task rc._forcecolor=on $@ summary 2>/dev/null|sed '/./,$!d')" \
"$(bold "" "History")" \
"$(task rc._forcecolor=on $@ ${halfyear} history 2>/dev/null)" \
"$(task rc._forcecolor=on $@ ${halfyear} ghistory 2>/dev/null)" \
"$(task rc._forcecolor=on $@ ${halfyear} burndown.daily 2>/dev/null)" \
"$(task rc._forcecolor=on $@ ${twoyears} burndown 2>/dev/null)"
}
function usage() {
local USAGE=$(cat <<-USAGE
Usage: %s [OPTION]... [FILTER]
OPTIONS:
-?, --help Show this help
-h, --html Create HTML form ANSI data.
-f, --file The file to write data to. Actually this specifies the
basename which will be postfixed by the creation date.
FILTER can be additional taskwarriors filters to limit the result
any further.
USAGE
)
/usr/bin/printf "${USAGE}\n" $0
}
TODAY="$(date +%Y-%m-%d)"
NL=$'\n'
#
# parse command line arguments
#
SHORTOPTS=?hf:
LONGOPTS=help,html,file
ARGS=$(getopt -o ${SHORTOPTS} --long ${LONGOPTS} -n timesheet -- $@)
if [ $? -ne 0 ]
then
usage $0
exit 1
fi
eval set -- ${=ARGS}
unset ARGS
while true
do
case $1 in
'-?'|'--help')
shift
usage $0
exit 0
;;
'-h'|'--html')
DOHTML=1
shift
continue
;;
'-f'|'--file')
OUTFILE="${2}_$TODAY"
shift 2
continue
;;
'--')
shift
break
;;
*)
echo 'Internal error!' >&2
exit 1
;;
esac
done
[[ -n $OUTFILE ]] && {
if [[ ${DOHTML} -eq 1 ]]
then
exec 1> >(ansi2html >${OUTFILE}.html)
else
exec 1> ${OUTFILE}.ansi
fi
}
timesheet $@
# vim: set et ts=4 sw=4: