-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoc
executable file
·179 lines (121 loc) · 3.29 KB
/
woc
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
#!/bin/bash
### Tracker
# A command-line utility that tracks how you spend your time.
# @Author: David Duan
# @Date: 2018 Dec.
### Sample Usage:
# - tracker start <task-name>: start a new tracker.
# - tracker stop -m <comment>: stop the tracker and enter a descriptive message.
# - tracker add <task-name> <task-duration> -m <comment>
### More commands and flags will be supported in the future.
###################################################################################################
### Constants (you can modify these to suit your needs)
DIRECTORY=".tracker/"
INPUT_FILE="${DIRECTORY}input.txt"
OUTPUT_FILE="${DIRECTORY}output.txt"
### Helper Functions
error () {
echo "[ERROR] $0: $1. Aborted" 1>&2
exit 1
}
info () {
echo "[INFO] $0: $1"
}
prompt() {
echo "[PROMPT] $0: $1"
}
warning() {
echo "[WARNING] $0: $1"
}
### Main Logic
start_handler () {
task_name=$1
start_time=$(date '+%H:%M:%S')
info "Starting $task_name at ${start_time}."
if [ -f $INPUT_FILE ]; then
old_task_name=$(sed '1q;d' $INPUT_FILE)
old_start_time=$(sed '2q;d' $INPUT_FILE)
warning "You haven't stopped your last tracker ${old_task_name} (started at ${old_start_time}).
To discard this record, enter N; to save the record, enter S."
while true; do
read response
if [ ${response} == "N" ] || [ ${response} == "n" ]; then
rm $INPUT_FILE
break
elif [ ${response} == "S" ] || [ ${response} == "s" ]; then
echo "Enter a descriptive message:"
read message
stop_handler "-m" message
break
else
echo "Unknown Input. Try again."
fi
done
fi
touch $INPUT_FILE
echo $task_name >> $INPUT_FILE
echo $start_time >> $INPUT_FILE
info "Data written to $INPUT_FILE. GLHF!"
}
stop_handler () {
flag=$1
case $flag in
-m)
message=$2
;;
*)
error "unknown flag" 1>&2
esac
if [ -f ${INPUT_FILE} ]; then
task_name=$(sed '1q;d' ${INPUT_FILE})
start_time=$(sed '2q;d' ${INPUT_FILE})
end_time=$(date '+%H:%M:%S')
duration=$(dateutils.ddiff ${start_time} ${end_time} -f '%M')
info "You have invested ${duration} minutes on ${task_name}."
today=$(date '+%D')
echo ${today} ${task_name} ${duration} >> ${OUTPUT_FILE}
echo ${message} >> ${OUTPUT_FILE}
echo -en '\n' >> ${OUTPUT_FILE}
rm ${INPUT_FILE}
info "Data written to ${OUTPUT_FILE}. Take a break!"
else
error "INPUT_FILE missing."
fi
}
# Implementing later
add_handler() {
:
}
### Main
mkdir -p $DIRECTORY
if [ ! -f $OUTPUT_FILE ]; then
touch $OUTPUT_FILE
fi
if [ $# -lt 2 ]; then
error "not enough arguments"
else
if [ "$1" == "start" ];
then
if [ $# -ne 2 ]; then
error "USAGE: tracker start <task_name>"
fi
start_handler $2
elif [ "$1" == "stop" ];
then
if [ $# -lt 3 ]; then
error "USAGE: tracker stop -m <message>"
fi
stop_handler $2 "${@:3}"
elif [ "$1" == "add" ];
then
add_handler $2 $3 $4 $5
elif [ "$1" == "clean" ] && [ "$2" == "-f" ];
then
rm $INPUT_FILE > /dev/null
info "Removed INPUT_FILE."
elif [ "$1" == "hard-clean" ] && [ "$2" == "-f" ];
then
rm $INPUT_FILE $OUTPUT_FILE > /dev/null
info "Removed both INPUT_FILE and OUTPUT_FILE."
fi
fi