Commit 7ab0abbc9ecc719cc9911799de84f3717703e9f2
1 parent
fceb2ecb
Split timesheet in reusable helper
Showing
5 changed files
with
148 additions
and
139 deletions
timesheet/datetimehelper.sh
0 → 100755
1 | +function tstodate() { | ||
2 | + [[ $# -eq 0 ]] && return 1 | ||
3 | + date -d @${1} +%Y-%m-%d | ||
4 | +} | ||
5 | + | ||
6 | +function extracttime() { | ||
7 | + local OIFS=${IFS} | ||
8 | + | ||
9 | + if [[ $# -eq 0 || -z $1 ]] | ||
10 | + then | ||
11 | + TIMEKEY="" | ||
12 | + TIME="00:00:00" | ||
13 | + else | ||
14 | + IFS=: | ||
15 | + set -- $=1 | ||
16 | + IFS=${OIFS} | ||
17 | + TIMEKEY=$1 | ||
18 | + TIME=$(printf "%02d:%02d:%02d" ${2:-0} ${3:-0} $4) | ||
19 | + fi | ||
20 | +} | ||
21 | + | ||
22 | +# vim: set et ts=4 sw=4: |
timesheet/stringhelper.sh
0 → 100755
timesheet/taskhelper.sh
0 → 100755
1 | +#!/bin/zsh | ||
2 | + | ||
3 | +function tasktimes() { | ||
4 | + [[ $# -eq 0 ]] && return 1 | ||
5 | + | ||
6 | + local KEY=${2:-\\1} | ||
7 | + local T | ||
8 | + local D='([0-9]{4}-[0-9]{2}-[0-9]{2})' | ||
9 | + local N='([0-9]+)' | ||
10 | + local EXPR='s/'$D'.*PT('$N'H)?('$N'M)?('$N').*/'${KEY}':\3:\5:\7/;te;d;:e' | ||
11 | + | ||
12 | + task $1 info | sed -r ${EXPR} | awk -F: ' | ||
13 | + { | ||
14 | + s = (d[$1][2] + $4) % 60 | ||
15 | + _m = int((d[$1][2] + $4) / 60) | ||
16 | + m = (d[$1][1] + $3 + _m) % 60 | ||
17 | + _h = int((d[$1][1] + $3 + _m) / 60) | ||
18 | + d[$1][0] += $2 + _h | ||
19 | + d[$1][1] = m | ||
20 | + d[$1][2] = s | ||
21 | + } | ||
22 | + END { | ||
23 | + for(i in d) | ||
24 | + printf("%s:%d:%d:%d\n", i, d[i][0], d[i][1], d[i][2]) | ||
25 | + }' | sort -t: -k1 | ||
26 | +} | ||
27 | + | ||
28 | +function tasktimestotal() { | ||
29 | + tasktimes $1 TOTAL | ||
30 | +} | ||
31 | + | ||
32 | +function taskdescription() { | ||
33 | + [[ $# -eq 0 ]] && return 1 | ||
34 | + task $1 _unique description | ||
35 | +} | ||
36 | + | ||
37 | +function taskproject() { | ||
38 | + [[ $# -eq 0 ]] && return 1 | ||
39 | + task $1 _unique project | ||
40 | +} | ||
41 | + | ||
42 | +function taskurgency() { | ||
43 | + [[ $# -eq 0 ]] && return 1 | ||
44 | + task $1 _urgency | cut -d\ -f 4 | ||
45 | +} | ||
46 | + | ||
47 | +function taskentry() { | ||
48 | + [[ $# -eq 0 ]] && return 1 | ||
49 | + task $1 _unique entry | ||
50 | +} | ||
51 | + | ||
52 | +function taskend() { | ||
53 | + [[ $# -eq 0 ]] && return 1 | ||
54 | + task $1 _unique end | ||
55 | +} | ||
56 | + | ||
57 | +function taskuuids() { | ||
58 | + local UUID | ||
59 | + | ||
60 | + [[ $# -eq 0 ]] && set -- \( +PENDING or +DONE \) | ||
61 | + for UUID in $(task $@ _uuid) | ||
62 | + do | ||
63 | + printf "%05.2f\037%s\037%s\n" \ | ||
64 | + "$(taskurgency $UUID)" \ | ||
65 | + "$(taskdescription $UUID)" \ | ||
66 | + $UUID | ||
67 | + done | sort -t$'\037' -k1nr,2 | cut -d$'\037' -f3 | ||
68 | +} | ||
69 | + | ||
70 | +function pendinguuids() { | ||
71 | + taskuuids +PENDING $@ | ||
72 | +} | ||
73 | + | ||
74 | +function doneuuids() { | ||
75 | + taskuuids +COMPLETED $@ | ||
76 | +} | ||
77 | + | ||
78 | +function blockeduuids() { | ||
79 | + taskuuids +BLOCKED $@ | ||
80 | +} | ||
81 | + | ||
82 | +function blockinguuids() { | ||
83 | + taskuuids +BLOCKING $@ | ||
84 | +} | ||
85 | + | ||
86 | +function deleteduuids() { | ||
87 | + taskuuids +DELETED $@ | ||
88 | +} | ||
89 | + | ||
90 | +# vim: set et ts=4 sw=4: |
1 | #!/bin/zsh | 1 | #!/bin/zsh |
2 | 2 | ||
3 | -function tasktimes() { | ||
4 | - [[ $# -eq 0 ]] && return 1 | ||
5 | - | ||
6 | - local KEY=${2:-\\1} | ||
7 | - local T | ||
8 | - local D='([0-9]{4}-[0-9]{2}-[0-9]{2})' | ||
9 | - local N='([0-9]+)' | ||
10 | - local EXPR='s/'$D'.*PT('$N'H)?('$N'M)?('$N').*/'${KEY}':\3:\5:\7/;te;d;:e' | ||
11 | - | ||
12 | - task $1 info | sed -r ${EXPR} | awk -F: ' | ||
13 | - { | ||
14 | - s = (d[$1][2] + $4) % 60 | ||
15 | - _m = int((d[$1][2] + $4) / 60) | ||
16 | - m = (d[$1][1] + $3 + _m) % 60 | ||
17 | - _h = int((d[$1][1] + $3 + _m) / 60) | ||
18 | - d[$1][0] += $2 + _h | ||
19 | - d[$1][1] = m | ||
20 | - d[$1][2] = s | ||
21 | - } | ||
22 | - END { | ||
23 | - for(i in d) | ||
24 | - printf("%s:%d:%d:%d\n", i, d[i][0], d[i][1], d[i][2]) | ||
25 | - }' | sort -t: -k1 | ||
26 | -} | ||
27 | - | ||
28 | -function tasktimestotal() { | ||
29 | - tasktimes $1 TOTAL | ||
30 | -} | ||
31 | - | ||
32 | -function taskdescription() { | ||
33 | - [[ $# -eq 0 ]] && return 1 | ||
34 | - task $1 _unique description | ||
35 | -} | ||
36 | - | ||
37 | -function taskproject() { | ||
38 | - [[ $# -eq 0 ]] && return 1 | ||
39 | - task $1 _unique project | ||
40 | -} | ||
41 | - | ||
42 | -function taskurgency() { | ||
43 | - [[ $# -eq 0 ]] && return 1 | ||
44 | - task $1 _urgency | cut -d\ -f 4 | ||
45 | -} | ||
46 | - | ||
47 | -function taskentry() { | ||
48 | - [[ $# -eq 0 ]] && return 1 | ||
49 | - task $1 _unique entry | ||
50 | -} | ||
51 | - | ||
52 | -function taskend() { | ||
53 | - [[ $# -eq 0 ]] && return 1 | ||
54 | - task $1 _unique end | ||
55 | -} | ||
56 | - | ||
57 | -function taskuuids() { | ||
58 | - local UUID | ||
59 | - | ||
60 | - [[ $# -eq 0 ]] && set -- \( +PENDING or +DONE \) | ||
61 | - for UUID in $(task $@ _uuid) | ||
62 | - do | ||
63 | - printf "%05.2f\037%s\037%s\n" \ | ||
64 | - "$(taskurgency $UUID)" \ | ||
65 | - "$(taskdescription $UUID)" \ | ||
66 | - $UUID | ||
67 | - done | sort -t$'\037' -k1nr,2 | cut -d$'\037' -f3 | ||
68 | -} | ||
69 | - | ||
70 | -function pendinguuids() { | ||
71 | - taskuuids +PENDING $@ | ||
72 | -} | ||
73 | - | ||
74 | -function doneuuids() { | ||
75 | - taskuuids +COMPLETED $@ | ||
76 | -} | ||
77 | - | ||
78 | -function blockeduuids() { | ||
79 | - taskuuids +BLOCKED $@ | ||
80 | -} | ||
81 | - | ||
82 | -function blockinguuids() { | ||
83 | - taskuuids +BLOCKING $@ | ||
84 | -} | ||
85 | - | ||
86 | -function deleteduuids() { | ||
87 | - taskuuids +DELETED $@ | ||
88 | -} | ||
89 | - | ||
90 | -function underline() { | ||
91 | - [[ $# -lt 2 ]] && return 1 | ||
92 | - printf "\033[4m%${1}s\033[24m" "$2" | ||
93 | -} | ||
94 | - | ||
95 | -function bold() { | ||
96 | - [[ $# -lt 2 ]] && return 1 | ||
97 | - printf "\033[1m%${1}s\033[22m" "$2" | ||
98 | -} | ||
99 | - | ||
100 | -function tstodate() { | ||
101 | - [[ $# -eq 0 ]] && return 1 | ||
102 | - date -d @${1} +%Y-%m-%d | ||
103 | -} | ||
104 | - | ||
105 | -function extracttime() { | ||
106 | - [[ $# -eq 0 ]] && return 1 | ||
107 | - | ||
108 | - local OIFS=${IFS} | ||
109 | - | ||
110 | - if [[ -z $1 ]] | ||
111 | - then | ||
112 | - TIMEKEY="" | ||
113 | - TIME="00:00:00" | ||
114 | - else | ||
115 | - IFS=: | ||
116 | - set -- $(echo $1) | ||
117 | - IFS=${OIFS} | ||
118 | - TIMEKEY="$1" | ||
119 | - TIME=$(printf "%02d:%02d:%02d" ${2:-0} ${3:-0} $4) | ||
120 | - fi | ||
121 | -} | 3 | +source $0:A:h/datetimehelper.sh |
4 | +source $0:A:h/stringhelper.sh | ||
5 | +source $0:A:h/taskhelper.sh | ||
6 | +source $0:A:h/ttyhelper.sh | ||
122 | 7 | ||
123 | function printtimes() { | 8 | function printtimes() { |
124 | local T | 9 | local T |
125 | - for T in "$@" | 10 | + for T in $@ |
126 | do | 11 | do |
127 | - extracttime "$T" | 12 | + extracttime $T |
128 | printf "%37s%-4s%10s: %s\n" "spent - " "$(date -d "$TIMEKEY" +%a)" \ | 13 | printf "%37s%-4s%10s: %s\n" "spent - " "$(date -d "$TIMEKEY" +%a)" \ |
129 | - "$TIMEKEY" "$TIME" | 14 | + $TIMEKEY $TIME |
130 | done | 15 | done |
131 | } | 16 | } |
132 | 17 | ||
133 | -function truncate() { | ||
134 | - [[ $# -lt 2 ]] && return 1 | ||
135 | - | ||
136 | - local STRING=$1 | ||
137 | - | ||
138 | - [[ ${#STRING} -gt ${2} ]] && STRING="${STRING:0:$(($2-3))}..." | ||
139 | - echo -n "${STRING}" | ||
140 | -} | ||
141 | - | ||
142 | function showall() { | 18 | function showall() { |
143 | [[ $# -eq 0 ]] && return 1 | 19 | [[ $# -eq 0 ]] && return 1 |
144 | 20 | ||
@@ -151,9 +27,9 @@ function showall() { | @@ -151,9 +27,9 @@ function showall() { | ||
151 | "$(underline -10 completed)" \ | 27 | "$(underline -10 completed)" \ |
152 | "$(underline -10 entered)" | 28 | "$(underline -10 entered)" |
153 | 29 | ||
154 | - for UUID in "$@" | 30 | + for UUID in $@ |
155 | do | 31 | do |
156 | - extracttime "$(tasktimes $UUID TOTAL)" | 32 | + extracttime $(tasktimes $UUID TOTAL) |
157 | printf "%-16s %-35s %-10s %-10s %-8s\n" \ | 33 | printf "%-16s %-35s %-10s %-10s %-8s\n" \ |
158 | "$(truncate "$(taskproject $UUID)" 16)" \ | 34 | "$(truncate "$(taskproject $UUID)" 16)" \ |
159 | "$(truncate "$(taskdescription $UUID)" 35)" \ | 35 | "$(truncate "$(taskdescription $UUID)" 35)" \ |
@@ -225,21 +101,21 @@ NL=$'\n' | @@ -225,21 +101,21 @@ NL=$'\n' | ||
225 | # | 101 | # |
226 | SHORTOPTS=?hf: | 102 | SHORTOPTS=?hf: |
227 | LONGOPTS=help,html,file | 103 | LONGOPTS=help,html,file |
228 | -ARGS=$(getopt -o "${SHORTOPTS}" --long "${LONGOPTS}" -n "timesheet" -- "$@") | 104 | +ARGS=$(getopt -o ${SHORTOPTS} --long ${LONGOPTS} -n timesheet -- $@) |
229 | if [ $? -ne 0 ] | 105 | if [ $? -ne 0 ] |
230 | then | 106 | then |
231 | - usage "$0" | 107 | + usage $0 |
232 | exit 1 | 108 | exit 1 |
233 | fi | 109 | fi |
234 | -eval set -- "${ARGS}" | 110 | +eval set -- ${=ARGS} |
235 | unset ARGS | 111 | unset ARGS |
236 | 112 | ||
237 | while true | 113 | while true |
238 | do | 114 | do |
239 | - case "$1" in | 115 | + case $1 in |
240 | '-?'|'--help') | 116 | '-?'|'--help') |
241 | shift | 117 | shift |
242 | - usage "$0" | 118 | + usage $0 |
243 | exit 0 | 119 | exit 0 |
244 | ;; | 120 | ;; |
245 | '-h'|'--html') | 121 | '-h'|'--html') |
@@ -263,7 +139,7 @@ do | @@ -263,7 +139,7 @@ do | ||
263 | esac | 139 | esac |
264 | done | 140 | done |
265 | 141 | ||
266 | -[[ $OUTFILE ]] && { | 142 | +[[ -n $OUTFILE ]] && { |
267 | if [[ ${DOHTML} -eq 1 ]] | 143 | if [[ ${DOHTML} -eq 1 ]] |
268 | then | 144 | then |
269 | exec 1> >(ansi2html >${OUTFILE}.html) | 145 | exec 1> >(ansi2html >${OUTFILE}.html) |
Please
register
or
login
to post a comment