Commit 0246eab53716f897ac0f0fb670e0f1adde9e6678

Authored by Georg Hopp
1 parent 12d3d757

use task export and jq to get sorted uuids

  1 +function todate() {
  2 + [[ $# -eq 0 ]] && return 1
  3 + date -d ${1} +%Y-%m-%d
  4 +}
  5 +
1 function tstodate() { 6 function tstodate() {
2 [[ $# -eq 0 ]] && return 1 7 [[ $# -eq 0 ]] && return 1
3 date -d @${1} +%Y-%m-%d 8 date -d @${1} +%Y-%m-%d
@@ -20,8 +25,8 @@ function extracttime() { @@ -20,8 +25,8 @@ function extracttime() {
20 } 25 }
21 26
22 function formattime() { 27 function formattime() {
23 - days=${1%%:*}  
24 - printf "%03dd %s" ${days:-0} ${1#*:} 28 + local DAYS=${1%%:*}
  29 + printf "%03dd %s" ${DAYS:-0} ${1#*:}
25 } 30 }
26 31
27 # vim: set et ts=4 sw=4: 32 # vim: set et ts=4 sw=4:
1 -#!/bin/zsh  
2 -  
3 -function duration() {  
4 - [[ $# -eq 0 ]] && return 1  
5 - task $1 info|\  
6 - sed 's/\([^ ]*\) \([^ ]*\).*duration: \(\([0-9]*\)d \)\?\([^)]*\).*/\1_\2|\4:\5/;te;d;:e'  
7 -}  
8 -  
9 -function tasktimes() {  
10 - [[ $# -eq 0 ]] && return 1  
11 - duration $1  
12 -}  
13 -  
14 -function tasktimestotal() {  
15 - set -- $(duration $1)  
16 -  
17 - # == Total calculation ==  
18 - DAYS=0  
19 - TI=0  
20 - for dur; do  
21 - # first skip key  
22 - key=${dur%%|*}  
23 - dur=${dur#*|}  
24 -  
25 - days=${dur%%:*}  
26 - DAYS=$((DAYS+${days:-0}))  
27 -  
28 - ti=$(printf "1970-01-01 %s" ${dur#*:}|xargs -d '\n' date +%s -u -d)  
29 - TI=$((TI+$ti))  
30 -  
31 - if [[ $TI -ge 86400 ]]; then  
32 - # bigger than a day?  
33 - DAYS=$((DAYS+1))  
34 - TI=$((TI-86400))  
35 - fi 1 +#!/bin/env sh
  2 +
  3 +function uuids() {
  4 + # This returns a space separated string of UUIDs fitting
  5 + # a given sort key and direction and using the provided
  6 + # filter. Currently only one key can be specified also theoretically
  7 + # jq could sort on multiple keys.
  8 + eval set -- $(getopt -o ads: -l asc,desc,sort-by: -- "$@")
  9 +
  10 + local DIR="."
  11 + local KEY="entry"
  12 +
  13 + while true; do
  14 + case "$1" in
  15 + -a|--asc) DIR="."; shift;;
  16 + -d|--desc) DIR="-."; shift;;
  17 + -s|--sort-by) KEY="$2"; shift 2;;
  18 + --) shift; break;;
  19 + *) echo "Internal error!"; exit 1;;
  20 + esac
36 done 21 done
37 - TI="$(date -u -d@${TI} +%X)"  
38 - echo "${DAYS}:${TI}"  
39 - # == End Total Calculation ==  
40 -}  
41 22
42 -function taskdescription() {  
43 - [[ $# -eq 0 ]] && return 1  
44 - task $1 _unique description 23 + task "$@" export|\
  24 + jq -r '.|=sort_by(.'${KEY}'
  25 + |explode
  26 + |map('${DIR}'))
  27 + |map(.uuid)
  28 + |join(" ")'
45 } 29 }
46 30
47 -function taskproject() { 31 +function worktimes() {
48 [[ $# -eq 0 ]] && return 1 32 [[ $# -eq 0 ]] && return 1
49 - task $1 _unique project  
50 -}  
51 33
52 -function taskurgency() {  
53 - [[ $# -eq 0 ]] && return 1  
54 - task $1 _urgency | cut -d\ -f 4  
55 -} 34 + local WHEN='\([^ ]*\) \([^ ]*\)'
  35 + local DUR='.*duration: \(\([0-9]*\)d \)\?\([^)]*\)'
  36 + local OUT='{"uuid":"'"$1"'","stop":"\1T\2Z","duration":"\4:\5"}'
  37 + local TIME DAYS
56 38
57 -function taskentry() {  
58 - [[ $# -eq 0 ]] && return 1  
59 - task $1 _unique entry 39 + echo '['
  40 + task $1 info|\
  41 + sed 's/'"${WHEN}${DUR}"'.*/'"${OUT}"'/;te;d;:e'|\
  42 + while read TIME; do
  43 + DUR="$(echo "${TIME}"|jq -r '.duration')"
  44 + DAYS="${DUR%%:*}"
  45 + DUR="${DAYS:-0}:$(date +%H:%M:%S -d "1970-01-01 ${DUR#*:}")"
  46 + echo "${TIME}"|jq -M -c '.duration="'"${DUR}"'"'
  47 + done|\
  48 + sed '$!s/$/,/'
  49 + echo ']'
60 } 50 }
61 51
62 -function taskend() { 52 +function work() {
63 [[ $# -eq 0 ]] && return 1 53 [[ $# -eq 0 ]] && return 1
64 - task $1 _unique end  
65 -}  
66 54
67 -function taskuuids() {  
68 - local UUID  
69 -  
70 - [[ $# -eq 0 ]] && set -- \( +PENDING or +DONE \)  
71 - for UUID in $(task $@ _uuid)  
72 - do  
73 - printf "%05.2f\037%s\037%s\n" \  
74 - "$(taskurgency $UUID)" \  
75 - "$(taskdescription $UUID)" \  
76 - $UUID  
77 - done | sort -t$'\037' -k1nr,2 | cut -d$'\037' -f3 55 + local UUID="$1"
  56 + local TIMES="$(worktimes ${UUID}|jq -M -c '.|map(del(.uuid))')"
  57 +
  58 + set -- $(echo "${TIMES}"|jq -r '.|map(.duration)|join(" ")')
  59 +
  60 + local DUR
  61 + for DUR; do
  62 + local DAYS_ADD=${DUR%%:*}
  63 + local SECS=$((SECS+$(date +%s -u -d "1970-01-01 ${DUR#*:}")))
  64 + DAYS=$((DAYS+${DAYS_ADD:-0}))
  65 + while [[ ${SECS} -ge 86400 ]]; do
  66 + # Bigger or equal one day.
  67 + DAYS=$((DAYS+1))
  68 + SECS=$((SECS-86400))
  69 + done
  70 + done
  71 +
  72 + printf '{"uuid":"%s","times":%s,"total":"%s:%s"}'\
  73 + ${UUID} "${TIMES}" ${DAYS} $(date +%H:%M:%S -u -d@${SECS})
78 } 74 }
79 75
80 -function pendinguuids() {  
81 - taskuuids +PENDING $@  
82 -}  
83 -  
84 -function doneuuids() {  
85 - taskuuids +COMPLETED $@  
86 -}  
87 -  
88 -function blockeduuids() {  
89 - taskuuids +BLOCKED $@  
90 -} 76 +function flattened_work() {
  77 + [[ $# -eq 0 ]] && return 1
91 78
92 -function blockinguuids() {  
93 - taskuuids +BLOCKING $@  
94 -} 79 + WORK="$(work $1)"
  80 + ITEM="$(echo "${WORK}"|jq -M -c '.|del(.times)')"
95 81
96 -function deleteduuids() {  
97 - taskuuids +DELETED $@ 82 + echo "${WORK}"|jq -M -c '.times|map(.+'"${ITEM}"')'
98 } 83 }
99 84
100 -# vim: set et ts=4 sw=4: 85 +# set -- $(task end.after:$(date +%Y-%m-%d -d "1 week ago") export|\
  86 +# jq -r '.|=sort_by(.end|explode|map(-.))|map(.uuid)|join("\n")')
  87 +
  88 +# set -- $(uuids "$@")
  89 +#
  90 +# for UUID; do
  91 +# # task _get ${UUID}.entry
  92 +# # task _get ${UUID}.end
  93 +# # task _get ${UUID}.modified
  94 +# # task _get ${UUID}.description
  95 +# # task _get ${UUID}.urgency
  96 +# # task _get ${UUID}.project
  97 +# echo $(work ${UUID})
  98 +# echo "------"
  99 +# flattened_work ${UUID}
  100 +# echo "======"
  101 +# done
@@ -5,23 +5,10 @@ source $0:A:h/stringhelper.sh @@ -5,23 +5,10 @@ source $0:A:h/stringhelper.sh
5 source $0:A:h/taskhelper.sh 5 source $0:A:h/taskhelper.sh
6 source $0:A:h/ttyhelper.sh 6 source $0:A:h/ttyhelper.sh
7 7
8 -function printtimes() {  
9 - local T  
10 - for T  
11 - do  
12 - key=${T%%|*}  
13 - dur=${T#*|}  
14 - date="$(date -u -d "${key//_/ }" +%c)"  
15 - ti=$(printf "1970-01-01 %s" ${dur#*:}|xargs -d '\n' date +%X -u -d)  
16 - TIME="$(formattime ${dur})"  
17 - printf "%50s: %14s\n" "${date}" $ti  
18 - done  
19 -}  
20 -  
21 function showall() { 8 function showall() {
22 [[ $# -eq 0 ]] && return 1 9 [[ $# -eq 0 ]] && return 1
23 10
24 - local T UUID OIFS 11 + local T UUID OIFS WORK FIRST
25 12
26 printf "%s %s %s %s %s\n" \ 13 printf "%s %s %s %s %s\n" \
27 "$(underline -16 project)" \ 14 "$(underline -16 project)" \
@@ -32,15 +19,30 @@ function showall() { @@ -32,15 +19,30 @@ function showall() {
32 19
33 for UUID in $@ 20 for UUID in $@
34 do 21 do
35 - TIME="$(formattime $(tasktimestotal $UUID))"  
36 - printf "%-16s %-35s %13s %-10s %-10s\n" \  
37 - "$(truncate "$(taskproject $UUID)" 16)" \  
38 - "$(truncate "$(taskdescription $UUID)" 35)" \  
39 - "$(bold "" "$TIME")" \  
40 - "$(tstodate $(taskend $UUID))" \  
41 - "$(tstodate $(taskentry $UUID))" \  
42 -  
43 - printtimes $(tasktimes $UUID) 22 + FIRST=true
  23 +
  24 + flattened_work ${UUID}|\
  25 + jq -M -r 'map([.[]]|@sh)|join("\n")'|\
  26 + while read WORK; do
  27 + eval set -- ${WORK}
  28 +
  29 + if ${FIRST}; then
  30 + local TIME="$(formattime $4)"
  31 + local PROJECT="$(task _get $UUID.project)"
  32 +
  33 + printf "%-16s %-35s %13s %-10s %-10s\n" \
  34 + "$(truncate "${PROJECT:-(none)}" 16)" \
  35 + "$(truncate "$(task _get $UUID.description)" 35)" \
  36 + "$(bold "" "${TIME}")" \
  37 + "$(todate $(task _get $UUID.end))" \
  38 + "$(todate $(task _get $UUID.entry))"
  39 + fi
  40 +
  41 + printf "%50s: %14s\n" \
  42 + "$(date -u -d "$1" +%c)" \
  43 + "$(formattime $2)"
  44 + FIRST=false
  45 + done
44 done 46 done
45 } 47 }
46 48
@@ -48,10 +50,10 @@ function timesheet() { @@ -48,10 +50,10 @@ function timesheet() {
48 local PHRASE="1-week-ago" 50 local PHRASE="1-week-ago"
49 local START="$(date +%Y-%m-%d -d ${PHRASE})" 51 local START="$(date +%Y-%m-%d -d ${PHRASE})"
50 52
51 - local DONE="$(showall $(doneuuids end.after:${START} $@))"  
52 - local UPCOMING="$(showall $(pendinguuids $@))"  
53 - local BLOCKED="$(showall $(blockeduuids $@))"  
54 - local BLOCKING="$(showall $(blockinguuids $@))" 53 + local DONE="$(showall $(uuids -d -s end +COMPLETED end.after:${START} $@))"
  54 + local UPCOMING="$(showall $(uuids +PENDING $@))"
  55 + local BLOCKED="$(showall $(uuids +BLOCKED $@))"
  56 + local BLOCKING="$(showall $(uuids +BLOCKING $@))"
55 57
56 DONE="${DONE:+${DONE}${NL}}" 58 DONE="${DONE:+${DONE}${NL}}"
57 UPCOMING="${UPCOMING:+${UPCOMING}${NL}}" 59 UPCOMING="${UPCOMING:+${UPCOMING}${NL}}"
Please register or login to post a comment