~mapperr/trk

ref: d6a0779919a35ffdf380bf91c38906095f24a07e trk/trk -rwxr-xr-x 10.4 KiB
d6a07799mapperr Improve the git test 11 months ago
                                                                                
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#! /bin/sh

trkfile_date_format="%Y-%m-%d"
trkdir_default="$HOME/.trk"
trkdir="$trkdir_default"
if [ ! -z "$TRK_DIR" ]; then
    trkdir="$TRK_DIR"
fi
trkfile="$trkdir/`date +$trkfile_date_format`.log"
trk_active_file="$trkdir/active_timer"

trk_workday_hours_default=8
trk_workday_hours=$trk_workday_hours_default
if [ ! -z "$TRK_WORKDAY_HOURS" ]; then
    trk_workday_hours="$TRK_WORKDAY_HOURS"
fi

tmpdir_default='/tmp'
tmpdir="$tmpdir_default"
if [ ! -z "$TMPDIR" ]; then
    # works in termux
    tmpdir="$TMPDIR"
fi

tag_valid_regex='[0-9A-Za-z_-]*'

print_env() {
echo "
export trkdir=$trkdir
export trk_active_file=$trk_active_file
export trkfile=$trkfile
export tmpdir=$tmpdir
export trkfile_date_format=$trkfile_date_format
"
}

help="
Usage:
    trk
        shows a 'today' report
    trk <entry_name>
        starts a timer tracking a named entry
    trk <time_spent> <entry_name>
        adds a new entry manually, with the specified time spent
        e.g.: '40m', '2h', '1h20m', etc., with 'h' and 'm' being
        [h]ours and [m]inutes
        (you can even use [d]ays and [s]econds, but why should you? :)
    trk t
        terminates the current timer
    trk r
    trk r today | month
        shows the monthly report
        or a report of the time range specified
    trk l
        lists the trk files
    trk e [trk_file]
        edit the trk file directly, defaulting to today trkfile
    trk y
        sync: commits eventual changes and do a pull/push of the trk dir
    trk g git_args
        runs git with git_args in the trk dir
    trk env
        prints exports of trk vars,
        eval-uating this output is useful for testing
    trk help | h
        shows help

Options:
    --help -h   Print this help

Notes:
    - trk default dir is [$trkdir_default]
    - to change default trk dir set TRK_DIR env var
    - trk default workday hours is 8
    - to change workday hours set TRK_WORKDAY_HOURS env var
    - to show debug infos set TRK_DEBUG env var to something
"

log() {
    local msg="$1"
    test -z "$TRK_DEBUG" || echo "$msg" >&2
}

ensure_trkdir_is_present() {
    test -d $trkdir || mkdir -p $trkdir
}

ensure_trkfile_is_present() {
    ensure_trkdir_is_present
    test -f "$trkfile" || touch "$trkfile"
}

edit_trkfile() {
    local filename="$1"
    local filepath="$trkdir/$file_name.log"

    if test -z "$filename"; then
        $EDITOR "$trkfile"
    elif test -r ; then
        $EDITOR "$filepath"
    else
        echo "no trk file named [$filename]" >&2
    fi
}

get_timestamp() {
    date '+%s'
}

is_there_a_pending_timer() {
    test -f $trk_active_file || return 2
    test ! -z "`cat $trk_active_file`"
}

stop_timer() {
    ensure_trkfile_is_present
    if is_there_a_pending_timer; then
        tag=`cat "$trk_active_file" | cut -d ' ' -f 1`
        start_ts=`cat "$trk_active_file" | cut -d ' ' -f 2`
        log "start timestamp: [$start_ts]"
        now=`get_timestamp`
        seconds_spent=`expr $now - $start_ts`
        humanized_time_spent=`get_friendly_time_from_seconds "$seconds_spent"`
        echo "$tag $humanized_time_spent" >> "$trkfile"
        echo -n '' > "$trk_active_file"
        compact_tags "$trkfile"
    else
        echo "no active timer to stop" 
    fi
}

start_timer() {
    local tag="$1"
    local now=`get_timestamp`

    is_there_a_pending_timer && stop_timer
    echo "$tag $now" >> "$trk_active_file"
}

get_seconds_from_friendly_time() {
    local time_spent="$1"

    days=`echo "$time_spent" | grep -o '[0-9]*d' | tr -d 'd'`
    hours=`echo "$time_spent" | grep -o '[0-9]*h' | tr -d 'h'`
    minutes=`echo "$time_spent" | grep -o '[0-9]*m' | tr -d 'm'`
    seconds=`echo "$time_spent" | grep -o '[0-9]*s' | tr -d 's'`
    
    workdays_in_hours=0
    test -z "$days" || \
       workdays_in_hours=`expr $trk_workday_hours \* $days`
    test -z "$hours" && hours=0
    dateline="`expr $workdays_in_hours + $hours` hours"
    test -z "$minutes" || dateline="$dateline $minutes minutes"
    test -z "$seconds" || dateline="$dateline $seconds seconds"

    log "dateline [$dateline]"

    now=`get_timestamp`
    future=`date -d "$dateline" '+%s'`
    secsspent=`expr $future - $now`
    log "time spent [$time_spent] is [$secsspent] seconds"
    echo -n "$secsspent"
}

get_friendly_time_from_seconds() {
    local seconds_spent="$1"
    
    log "converting [$seconds_spent] seconds to time spent"

    timeline="${seconds_spent}s"
    minutes_spent=`expr $seconds_spent / 60`
    hours_spent=`expr $minutes_spent / 60`
    days_spent=`expr $hours_spent / $trk_workday_hours`

    if [ $hours_spent -ge $trk_workday_hours ]; then
        days_in_hours=`expr $days_spent \* $trk_workday_hours`
        hours_remainder=`expr $hours_spent - $days_in_hours`
        hours_in_minutes=`expr $hours_spent \* 60`
        minutes_remainder=`expr $minutes_spent - $hours_in_minutes`
        timeline="${days_spent}d"
        test $hours_remainder -gt 0 && \
            timeline="${days_spent}d${hours_remainder}h"
        test $minutes_remainder -gt 0 && \
            timeline="${days_spent}d${minutes_remainder}m"
        test $hours_remainder -gt 0 && \
            test $minutes_remainder -gt 0 && \
        timeline="${days_spent}d${hours_remainder}h${minutes_remainder}m"

    elif [ $minutes_spent -ge 60 ]; then
        hours_in_minutes=`expr $hours_spent \* 60`
        minutes_remainder=`expr $minutes_spent - $hours_in_minutes`
        timeline="${hours_spent}h"
        test $minutes_remainder -gt 0 && \
            timeline="${hours_spent}h${minutes_remainder}m"

    elif [ $seconds_spent -ge 60 ]; then
        timeline="${minutes_spent}m"
    fi

    log "[$seconds_spent] seconds are [$timeline] time spent"
    echo "$timeline"
}

add_entry() {
    local tag="$1"
    local time_spent="$2"

    if [ -z "$tag" ]; then
        echo "missing tag"
        exit 1
    fi
    if [ -z "$time_spent" ]; then
        echo "missing time spent"
        exit 1
    fi

    entry="$tag $time_spent"
    log "adding entry [$entry]"
    echo "$entry" >> "$trkfile"
    compact_tags "$trkfile"
}

get_unique_tag_list() {
    local trkfile_to_compact="$1"
    test -z "$trkfile_to_compact" && trkfile_to_compact="$trkfile"
    cat $trkfile_to_compact | grep -o "^$tag_valid_regex" | sort | uniq
}

compact_tags() {
    local trkfile_to_compact="$1"
    test -z "$trkfile_to_compact" && trkfile_to_compact="$trkfile"
    local temp_trk_file="$tmpdir/`basename $trkfile_to_compact`-compacting"

    test -f $temp_trk_file && rm -f $temp_trk_file
    unique_tag_list=`get_unique_tag_list "$trkfile_to_compact"`
    for tag in $unique_tag_list; do
        log "processing tag [$tag]"
        tag_total_seconds_spent=0
        while read entry; do
            echo -n "$entry" | \
                grep "^$tag " >/dev/null || \
                continue
            log "found entry [$entry]"
            time_spent=`echo "$entry" | cut -d ' ' -f 2`
            log "time_spent [$time_spent]"
            seconds_spent=`get_seconds_from_friendly_time $time_spent`
            tag_total_seconds_spent=`expr $tag_total_seconds_spent + $seconds_spent`
        done < "$trkfile_to_compact"
        humanized_time_spent=`get_friendly_time_from_seconds "$tag_total_seconds_spent"`
        echo "$tag $humanized_time_spent" >> "$temp_trk_file"
    done
    mv -f "$temp_trk_file" "$trkfile_to_compact"
    test -f "$temp_trk_file" && rm -f "$temp_trk_file"
}

get_active_entry() {
    if ! is_there_a_pending_timer; then
        return
    fi
    local now=`get_timestamp`
    local tag=`cat "$trk_active_file" | cut -d ' ' -f 1`
    local entry_start_time=`cat "$trk_active_file" | cut -d ' ' -f 2`

    log "entry_start_time: [$entry_start_time]"
    log "tag: [$tag]"

    local seconds_spent=`expr $now - $entry_start_time`
    local friendly_time=`get_friendly_time_from_seconds "$seconds_spent"`

    echo -n "$tag $friendly_time"
}

show_report() {
    local from="$1"
    local to="$2"
    local reportfile="$tmpdir/trkreportfile"

    echo
    if [ "$from" = "today" ]; then
        if test -f "$trkfile"; then
        echo 'today report:'
        _show_report "$trkfile"
        else
            echo "No records for today"
        fi
    elif [ "$from" = "month" ]; then
        echo 'monthly report:'
        pattern="`date '+%Y-%m-*'`.log"
        cat $trkdir/$pattern >$reportfile
        compact_tags "$reportfile"
        _show_report "$reportfile"
        test -f "$reportfile" && rm -f "$reportfile"
    fi

    active_entry=`get_active_entry`
    if test ! -z "$active_entry"; then
        echo
        print_active_entry_info $active_entry
    fi
}

_show_report() {
    local trkfile_to_report="$1"

    total_seconds_spent=0
    echo
    while read entry; do
        log "found entry [$entry]"
        tag=`echo "$entry" | cut -d ' ' -f 1`
        time_spent=`echo "$entry" | cut -d ' ' -f 2`
        log "time_spent [$time_spent]"
        seconds_spent=`get_seconds_from_friendly_time $time_spent`
        total_seconds_spent=`expr $total_seconds_spent + $seconds_spent`
        print_entry_info "$tag" "$time_spent"
    done < "$trkfile_to_report"
    echo
    tot_friendly_time="`get_friendly_time_from_seconds "$total_seconds_spent"`"
    echo "[$tot_friendly_time] spent in total"
}

print_entry_info() {
    local tag="$1"
    local time="$2"
    echo "$time spent on $tag"
}

print_active_entry_info() {
    local tag="$1"
    local time="$2"
    echo "$time spent working on $tag (active entry)"
}

ensure_trkdir_is_present

command="$1"

if [ -z "$command" ]; then
    show_report 'today'
    echo
    echo "run [`basename $0` h] for help"

elif [ "$command" = "t" ]; then
    stop_timer
    show_report 'today'

elif [ "$command" = "l" ]; then
    cd $trkdir
    ls -1 *.log | sed 's/\.log//'
    cd - >/dev/null

elif [ "$command" = "e" ]; then
    file_name="$2"
    edit_trkfile $file_name

elif [ "$command" = "r" ]; then
    from="$2"
    to="$3"
    test -z "$from" && from="month"
    show_report $from $to

elif [ "$command" = "y" ]; then
    cd $trkdir
    git status | grep 'nothing to commit' &>/dev/null
    test $? = 1 && git add -A && git commit -am 'trk autosync'
    git pull && git push

elif [ "$command" = "g" ]; then
    shift 1
    cd $trkdir
    git $@

elif [ "$command" = "env" ]; then
    print_env

elif [ "$command" = "--help" ] \
    || [ "$command" = "-h" ] \
    || [ "$command" = "help" ] \
    || [ "$command" = "h" ] ; then
    echo "$help"
else
    tag="$command"
    if test -z "$2"; then
        start_timer "$tag"
    else
        time_spent="$command"
        tag="$2"
        add_entry "$tag" "$time_spent"
    fi
fi