-
Notifications
You must be signed in to change notification settings - Fork 1
/
tt.sh
executable file
·204 lines (194 loc) · 6.83 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
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
#!/usr/bin/env sh
# First 2 chars in the file are called Shebang https://en.wikipedia.org/wiki/Shebang_%28Unix%29
# In this case:
# Execute this file with a "sh" interpreter, using the "env" program search path to find it.
# Now you know!
tt() {
TT_VERSION="v1.1.4"
# :- means that if TT_LOGS doesn't exit, it will assign $HOME/.tt_log (~/.tt_log)
TT_LOGS="${TT_LOGS:-$HOME/.tt_logs}"
TT_SESSION="${TT_SESSION:-$HOME/.tt_session}"
if ! [ -f "$TT_SESSION" ]; then
touch "$TT_SESSION"
fi
if ! [ -f "$TT_LOGS" ]; then
echo "UTC,activity name,human time spent,total seconds" >"$TT_LOGS"
fi
# See end of function for starting of _options
# Internal functions
_options() {
echo ""
case "$1" in
--activity-name)
# Shows the current activity name if there is one for this session
activity_name=$(grep 'activity_name=' "$TT_SESSION" | sed -E "s/.*activity_name=(.+)$.*/\\1/")
if [ -z "${activity_name}" ]; then
echo "No activity started"
else
echo "Activity '$activity_name'"
fi
;;
-h | --help)
# Help
echo "tt - time tracker"
echo ""
echo "Tracks activity time with a simple start/stop syntax. Logs to CSV."
echo "Allows one activity active at a time, per session."
echo ""
echo "usage: tt # show this help"
echo "usage: tt (--help or -h) # show this help"
echo "usage: tt --version # show tt current version"
echo "usage: tt (--start or -s) [activity name] # start a new activity"
echo "usage: tt (--pause or -p) # pauses current activity"
echo "usage: tt (--done or -d or --finish or -f) # stop and log activity"
echo "usage: tt (--abort or -a) # stop activity, no log"
echo "usage: tt --clear-logs # delete log of previous activities"
echo "usage: tt --activity-name # show activity for current session"
echo "usage: tt (--logs or -l) # show logs of previous activities"
;;
--version)
echo "$TT_VERSION"
;;
-p | --pause)
_pause
;;
-d | --done | -f | --finish)
_finish
;;
-a | --abort)
echo "Abort activity"
echo "" >"$TT_SESSION"
crontab -l | grep -v "tt" | crontab -
;;
-l | --logs)
cat "$TT_LOGS"
;;
--clear-logs)
echo "Logs cleared"
echo "UTC,activity name,human time spent,total seconds" >"$TT_LOGS"
;;
-s | --start)
_start "$2"
;;
*)
echo "!!!! Invalid option !!!!"
_options -h
;;
esac
}
_start() {
start_timestamp=$(date +%s)
# No activity name passed
if [ -z "$1" ]; then
activity_name=$(grep 'activity_name=' "$TT_SESSION" | sed -E "s/.*activity_name=(.+)$.*/\\1/")
if [ -z "$activity_name" ]; then
echo "No activity started"
return
else
echo "Restarting '$activity_name'"
elapsed_sec=$(grep 'elapsed_sec=' "$TT_SESSION" | sed -E "s/.*elapsed_sec=([0-9]+).*/\\1/")
echo "start_time=${start_timestamp}" >"$TT_SESSION"
echo "elapsed_sec=${elapsed_sec}" >>"$TT_SESSION"
echo "activity_name=${activity_name}" >>"$TT_SESSION"
# runs on MacOS
if [ "$(uname)" = "Darwin" ]; then
(crontab -l | grep -v "tt" ; printf '*/15 * * * * say "Activity from tt is active!"\n') | crontab -
fi
fi
return
fi
# finish old activity if exists
old_activity_name=$(grep 'activity_name=' "$TT_SESSION" | sed -E "s/.*activity_name=(.+)$.*/\\1/")
if [ -n "$old_activity_name" ]; then
_finish
fi
echo "Starting '$1'"
echo "start_time=${start_timestamp}" >"$TT_SESSION"
echo "elapsed_sec=0" >>"$TT_SESSION"
echo "activity_name=$1" >>"$TT_SESSION"
# runs on MacOS
if [ "$(uname)" = "Darwin" ]; then
(crontab -l | grep -v "tt" ; printf '*/15 * * * * say "Activity from tt is active!"\n') | crontab -
fi
}
_pause() {
# Do we have an activity active for this session?
start_time=$(grep 'start_time=' "$TT_SESSION" | sed -E "s/.*start_time=([0-9]+).*/\\1/")
elapsed_sec=$(grep 'elapsed_sec=' "$TT_SESSION" | sed -E "s/.*elapsed_sec=([0-9]+).*/\\1/")
activity_name=$(grep 'activity_name=' "$TT_SESSION" | sed -E "s/.*activity_name=(.+)$.*/\\1/")
if [ -z "$start_time" ]; then
echo "No activity started"
return
fi
if [ "$start_time" = "0" ]; then
echo "Activity '$activity_name' is already paused"
return
fi
pause_timestamp=$(date +%s)
sec_diff=$((pause_timestamp - start_time + elapsed_sec))
hours=$((sec_diff / 3600))
mins=$(((sec_diff - (hours * 3600)) / 60))
echo "start_time=0" >"$TT_SESSION"
echo "elapsed_sec=${sec_diff}" >>"$TT_SESSION"
echo "activity_name=${activity_name}" >>"$TT_SESSION"
echo "Activity '$activity_name' paused at ${hours}h ${mins}m"
# runs on MacOS
if [ "$(uname)" = "Darwin" ]; then
(crontab -l | grep -v "tt" ; printf '* * * * * say "Activity from tt is paused!"\n') | crontab -
fi
}
_finish() {
# Do we have an activity active for this session?
start_time=$(grep 'start_time=' "$TT_SESSION" | sed -E "s/.*start_time=([0-9]+).*/\\1/")
elapsed_sec=$(grep 'elapsed_sec=' "$TT_SESSION" | sed -E "s/.*elapsed_sec=([0-9]+).*/\\1/")
activity_name=$(grep 'activity_name=' "$TT_SESSION" | sed -E "s/.*activity_name=(.+)$.*/\\1/")
if [ -z "$activity_name" ]; then
echo "No activity started"
return
fi
# Activity was paused
if [ "$start_time" = "0" ]; then
_save "$activity_name" "$elapsed_sec"
echo "" >"$TT_SESSION"
crontab -r
return
fi
finish_timestamp=$(date +%s)
sec_diff=$((finish_timestamp - start_time + elapsed_sec))
_save "$activity_name" $sec_diff
echo "" >"$TT_SESSION"
crontab -l | grep -v "tt" | crontab -
}
_save() {
activity_name=$1
sec_diff=$2
hours=$((sec_diff / 3600))
mins=$(((sec_diff - (hours * 3600)) / 60))
utc_date=$(date -u)
echo "$utc_date | $activity_name | ${hours}h ${mins}m"
log="$utc_date,\"$activity_name\",${hours}h ${mins}m,$sec_diff"
echo "$log" >>"$TT_LOGS"
}
# Parse params
if [ $# -eq 0 ]; then
# No parameters = show help
_options -h
else
_options "$1" "$2"
fi
}
# Autocomplete
# taken https://askubuntu.com/questions/68175/how-to-create-script-with-auto-complete
_tt()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="--help --start --pause --done --finish --abort --clear-logs --activity-name --logs --version"
if [[ ${cur} == -* ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi
}
complete -F _tt tt