blob: 31f08d0f573f3d5adf7e774cf2535e134b414a25 (
plain)
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
|
#!/bin/sh
clocks="$HOME"/sync/share/clocks.csv
# Create csv file with headers if not exist
[ -f "$clocks" ] ||
printf 'start,end,message\n' >"$clocks"
if [ "$1" = "-h" ]; then
>&2 cat <<EOF
usage: clock [OPTION]
-h shows this help
-p print clockings prettily
-e edit clocks file
-a add time manually
With no option it will start clocking and prompt for a task. Use
Ctrl-D to signify the end of the task. Ctrl-c to quit.
EOF
exit 1
fi
# Note(Luca):
# You can add a time manually with this command
if [ "$1" = "-a" ]; then
>&2 printf 'start>'
start_time="$(head -n1)"
[ "$start_time" ] || exit 1
>&2 printf 'end>'
end_time="$(head -n1)"
[ "$end_time" ] || exit 1
>&2 printf 'message>'
message="$(head -n1)"
[ "$message" ] || exit 1
printf -- '%s,%s,%s\n' \
"$(date -d "$start_time" +%s)" \
"$(date -d "$end_time" +%s)" \
"$message" >>"$clocks"
exit
fi
# print clocks file prettily
if [ "$1" = "-p" ]; then
# empty
[ "$(wc -l <"$clocks")" -eq 1 ] && exit
IFS=","
# skip csv header
tail -n +2 "$clocks" |
while read -r start end message; do
printf "[%s] %s-%s | %s\n" \
"$(date -d "@$start" +'%m/%d')" \
"$(date -d "@$start" +'%R')" \
"$(date -d "@$end" +'%R')" \
"$message"
done
exit
fi
# edit clocks file in $EDITOR
if [ "$1" = "-e" ]; then
$EDITOR "$clocks"
exit
fi
trap 'exit 0' INT # The proper way to exit
# When no arguments are provided start clocking
while true; do
>&2 printf ' > '
message="$(head -n 1)"
[ "$message" ] || exit 1
printf '\033[1A' # move cursor up once: https://en.wikipedia.org/wiki/ANSI_escape_code
start_time="$(date +%s)"
start_time_pretty="$(date -d "@$start_time" +%R)"
>&2 printf -- '\r%s- > %s' "$start_time_pretty" "$message"
# Wait for EOF
cat >/dev/null 2>&1
end_time="$(date +%s)"
end_time_pretty="$(date -d "@$end_time" +%R)"
>&2 printf -- '\r%s-%s > %s\n' "$start_time_pretty" "$end_time_pretty" "$message"
if printf '%s' "$message" | grep ',' >/dev/null; then
# escape potential double quotes
message="$(printf '%s' "$message" | sed -e 's/"/""/g')"
message="\"$message\""
fi
# save clocked time and message
printf '%s,%s,%s\n' "$start_time" "$end_time" "$message" >>"$clocks"
done
|