-
Notifications
You must be signed in to change notification settings - Fork 0
/
taskcont
executable file
·94 lines (77 loc) · 2.22 KB
/
taskcont
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
#!/usr/bin/env bash
#
# taskcont, timecont, tasklast, timelast
# resume the last or second to last recorded task or time interval
#
# desc:
# - starts the last worked task in timewarrior database
# - if 'task' then given timew interval must also be in taskw db
# - if 'time' then time/ prefix will match (ie, no taskw task)
# - if 'cont' then start the last worked task
# - if 'last' then start the task before that
#
# https://github.com/smemsh/taskwtools/
# https://spdx.org/licenses/GPL-2.0
#
taskcont () { resume_task; }
timecont () { resume_task; }
tasklast () { resume_task; }
timelast () { resume_task; }
resume_task ()
{
local candidate lastcand interval
fql_of_interval ()
{
printf $(
timew export \
| jq -r ".[] | select(.id == ${1:?}) | .tags[]" \
| grep -v -e ^+ -e /$ \
| paste -sd,
)
}
interval=1
while true
do
candidate=$(fql_of_interval $interval)
let interval++
if [[ $cmdprefix == "task" ]]
then if [[ ${candidate:0:5} == "time/" ]]; then
continue; fi; fi
if [[ $cmdsuffix == "cont" ]]; then
break; fi
if [[ $lastcand ]]
then [[ $candidate == $lastcand ]] && continue || break; fi
if ! [[ $candidate ]]
then echo "cannot find last fql" >&2; false; return; fi
lastcand=$candidate
done
# after the loop, we are one past the target
let interval--
# when used non-interactively, it's probably rptaskline, which
# already compares before/after tasklines and shows delta. we
# don't want failure on no-change because rptaskline reports
# failure, and timecont on already open labels is idempotent, so
# no failure per se. shown delta will make clear that state did
# not change. interactively, this should fail however, so it's
# not confusing when a new interval seems to have been made
#
if ((interval == 1)) && tty -s
then timew continue # aborts if @1 is an open interval
else timew continue @$interval # succeeds whether @1 open or closed
fi
}
main ()
{
cd "$startdir" || exit
cmdprefix=${invname:0:4}
cmdsuffix=${invname:(-4)}
if [[ $(declare -F $invname) ]]
then $invname "$@"
else echo "unimplemented command '$invname'" >&2; fi
}
startdir=$HOME
invname=${0##*/}
invdir=${0%/*}
[[ $invname == cont ]] && invname=timecont
main "$@"