-
Notifications
You must be signed in to change notification settings - Fork 2
/
tt.sh
102 lines (90 loc) · 1.95 KB
/
tt.sh
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
#!/bin/bash
set -e
TIME_FILE=~/.hours.timeclock
function usage {
scriptName="$(basename ${0})"
cat <<EOF
Usage: ${scriptName} [COMMAND] [ARGS]
Track time
i|in "Time in" to begin a task. Args: [optional date] [task name]
o|out "Time out" to end a task. Args: [optional date]
s|stat|t|tail Show the end of the timeclock file. Args: [optional n lines]
b|bal Show total balances.
d|daily Show daily balances for the week.
w|weekly Show weekly balances for the month.
q|quarterly Show quarterly balances for the year.
n|sw|next|switch End (time out of) the current task, and begin the next.
task. New task name is the next arg.
e|edit Edit the timeclock file with \${EDITOR}.
EOF
}
if [[ $# == 0 ]]; then
usage
exit 0
fi
function time_in {
local time=$(date '+%Y-%m-%d %H:%M:%S')
local activity="${@}"
if [[ "${1}" =~ ^[0-9]{2}:[0-9]{2}$ ]]; then
time="$(date '+%Y-%m-%d') ${1}:00"
activity="${@:2}"
fi
if [[ -z "${activity}" ]]; then
activity="work"
fi
echo "Begin ${activity} at ${time}"
echo "i ${time} ${activity}" >> $TIME_FILE
}
function time_out {
local time=$(date '+%Y-%m-%d %H:%M:%S')
if [[ "${1}" =~ ^[0-9]{2}:[0-9]{2}$ ]]; then
time="$(date '+%Y-%m-%d') ${1}:00"
fi
echo "End at $time"
echo "o $time" >> $TIME_FILE
}
function tail_file {
local n=10
if [[ $# -gt 0 ]]; then
n=$1
fi
tail -n $n $TIME_FILE
}
case $1 in
i|in)
shift
time_in $*
;;
o|out)
shift
time_out $*
;;
s|stat|t|tail)
shift
tail_file $*
;;
b|bal)
hledger -f $TIME_FILE bal
;;
d|daily)
hledger -f $TIME_FILE -p "daily this week" bal
;;
w|weekly)
hledger -f $TIME_FILE -p "weekly this month" bal
;;
q|quarterly)
hledger -f $TIME_FILE -p "quarterly this year" bal
;;
n|sw|next|switch)
shift
time_out $* && time_in $*
;;
e|edit)
$EDITOR $TIME_FILE
;;
*)
echo "Unknown command ${1}"
usage
exit 1
;;
esac