This repository has been archived by the owner on Mar 29, 2022. It is now read-only.
/
habit
executable file
·377 lines (292 loc) · 8.74 KB
/
habit
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
#!/usr/bin/env bash
set -eo pipefail
# set -x
# colors
declare red="\e[1;31m"
declare yellow="\e[1;33m"
declare cyan="\e[1;36m"
declare reset="\e[0m"
# constants
declare -r HABIT_CONFIG_DIR="$HOME/.habit"
declare -r HABIT_CONFIG_LOG="$HOME/.habit/log"
declare -r HABIT_CONFIG_GOALS="$HOME/.habit/goals"
declare -r HABIT_PLANNING="$HOME/.habit/plan.md"
x.help() {
printf '
habit is a program that functions as a journal of your
habits/doings. You can visualize the weekly, monthly or
total progress of those habits/doings.
They are written to a file under $HOME/.habit/log which you
can access by doing `habit edit`.
Commands:
help Show help commands
add <habit> <hours> Adds an habit entry
edit.habits Opens the log file in the default $EDITOR
edit.goals Opens the goals file in the default $EDITOR
edit.plan Opens the `plan.md` file in the default $EDITOR
goal <habit> <hours> Adds an goal entry of an habit/doing
list Display the progress of the current week
list.month Display the progress of the current month
'
}
x.add() {
local date slug habit="${1}" time="${2:-25}"
if [[ -z $habit ]] || [[ -n "${time##[0-9]*}" ]]; then
echo "Invalid arguments."
exit 1
fi
date=$(date +%Y-%m-%d)
slug=$(echo "$habit" | slugify)
mkdir -p "$HABIT_CONFIG_DIR"
echo "${habit}|${time}|${date}|${slug}" >>"$HABIT_CONFIG_LOG"
echo -e " ✨ Nice job! Added ${cyan}${habit}${reset} to your tracking log..."
}
# Writes an weekly goal into the $HABIT_CONFIG_GOALS file
#
# habit - the habit being tracked
# type - how the hours will be tracked ("weekly", "monthly")
# hours - the number of hours to spent on a habit
#
# Note: I wont track monthly because I only need weekly; a feature to be
# added.
x.goal() {
local habit="${1}" hours="${2}" type="weekly"
if [[ -z $habit ]] || [[ -z $hours ]] || [[ -z $type ]]; then
echo "Invalid arguments."
exit 1
fi
slug=$(echo "$habit" | slugify)
mkdir -p "$HABIT_CONFIG_DIR"
echo "${habit}|${hours}|${type}|${slug}" >>"$HABIT_CONFIG_GOALS"
echo -e " ✨ Tracking ${cyan}${habit}${reset}! Added ${cyan}${habit}${reset} to your goals log..."
}
x.edit.habits() { popup nvim "$HABIT_CONFIG_LOG"; }
x.edit.goals() { popup nvim "$HABIT_CONFIG_GOALS"; }
x.edit.plan() { popup nvim "$HABIT_PLANNING"; }
x.list() {
local -A logs
local -A habits
local -A goals
# process weekly goals
#
while IFS="|" read -ra log; do
[[ -z "${log[0]}" ]] && continue
habit="${log[0]}"
hours="${log[1]}"
type="${log[2]}"
slug="${log[3]}"
if [[ $type == "weekly" ]]; then
goals["${slug}.title"]=$habit
goals["${slug}.hours"]=$hours
fi
done <"$HABIT_CONFIG_GOALS"
# process weekly habits
#
while IFS="|" read -ra log; do
[[ -z "${log[0]}" ]] && continue
habit="${log[0]}"
time="${log[1]}"
date="${log[2]}"
slug="${log[3]}"
currentWeekNumber=$(gdate -d "$date" +"%V")
weekDay=$(gdate -d "$date" +"%u")
index="${slug}.${currentWeekNumber}.${weekDay}"
count="${logs["${index}.count"]}"
if [[ -z $count ]]; then
# get update all the slugs
habits["${slug}"]="$slug"
logs["${index}.count"]=1
logs["${index}.title"]="$habit"
logs["${index}.time"]="$time"
logs["${index}.date"]="$date"
else
totalCounter=:
totalTimeInvested=:
currentCounter="${logs["${index}.count"]}"
currentTime="${logs["${index}.time"]}"
((totalCounter = currentCounter + 1))
((totalTimeInvested = currentTime + time))
logs["${index}.count"]=$totalCounter
logs["${index}.time"]=$totalTimeInvested
fi
done <"$HABIT_CONFIG_LOG"
currentWeekNumber=$(gdate +"%V")
currentWeekDay=$(gdate +"%u")
# get the total weekly invested
#
local -A totalInvested
for ((i = 1; i <= currentWeekDay; i++)); do
for slug in ${!habits[*]}; do
index="${slug}.${currentWeekNumber}.${i}"
count="${logs["${index}.count"]}"
[[ -z $count ]] && continue
time="${logs["${index}.time"]}"
currentTotalInvested="${totalInvested["${slug}"]}"
((currentTotalInvested += time))
totalInvested["${slug}"]=$currentTotalInvested
done
done
# display the results
#
padlimit=60
pad=$(printf '%*s' "$padlimit")
pad=${pad// /.}
padlength=40
for weekDay in {1..7}; do
case $weekDay in
1)
printf "\n"
echo -e "${yellow}Monday:${reset}"
;;
2)
printf "\n"
echo -e "${yellow}Tuesday:${reset}"
;;
3)
printf "\n"
echo -e "${yellow}Wednesday:${reset}"
;;
4)
printf "\n"
echo -e "${yellow}Thursday:${reset}"
;;
5)
printf "\n"
echo -e "${yellow}Friday:${reset}"
;;
6)
printf "\n"
echo -e "${yellow}Saturday:${reset}"
;;
7)
printf "\n"
echo -e "${yellow}Sunday:${reset}"
;;
esac
# Print all your tracked habits
for slug in ${!habits[*]}; do
index="${slug}.${currentWeekNumber}.${weekDay}"
weekIndex="${logs["${index}.count"]}"
if [[ -n $weekIndex ]]; then
title="${logs["${index}.title"]}"
count="${logs["${index}.count"]}"
time="${logs["${index}.time"]}"
date="${logs["${index}.date"]}"
time=$(bc <<<"scale=2; ${time}/60")
printf ' %s (%s) ' "$title" "$count"
printf '%*.*s' 0 $((padlength - ${#title})) "$pad"
printf " H: ${cyan}%s${reset}\n" "$time"
fi
done
# Print all your missing habits based on goals
if [[ "$weekDay" == "$currentWeekDay" ]]; then
for slug in ${!habits[*]}; do
index="${slug}.${currentWeekNumber}.${weekDay}"
title="${goals["${slug}.title"]}"
goalTime="${goals["${slug}.hours"]}"
timeInvested="${totalInvested["${slug}"]}"
if [[ -n $goalTime ]]; then
timeLeft=$(((goalTime - timeInvested)))
timeLeftInHours=$(bc <<<"scale=2; ${timeLeft}/60")
if [[ $timeLeft -gt 0 ]]; then
printf " ${red}%s (n)${reset} " "$title"
printf "${red}%*.*s${reset}" 0 $((padlength - ${#title})) "$pad"
printf " ${red}H: %s${reset}\n" "$timeLeftInHours"
fi
fi
done
fi
done
printf "\n"
}
x.list.month() {
local -A logs
local -A habits
while IFS="|" read -ra log; do
[[ -z "${log[0]}" ]] && continue
habit="${log[0]}"
time="${log[1]}"
date="${log[2]}"
slug="${log[3]}"
monthNumber=$(gdate -d "$date" +"%m")
index="${slug}.${monthNumber}"
count="${logs["${index}.count"]}"
if [[ -z $count ]]; then
habits["${slug}"]="$slug"
logs["${index}.count"]=1
logs["${index}.title"]="$habit"
logs["${index}.time"]="$time"
else
totalCounter=:
totalTimeInvested=:
currentCounter="${logs["${index}.count"]}"
currentTime="${logs["${index}.time"]}"
((totalCounter = currentCounter + 1))
((totalTimeInvested = currentTime + time))
logs["${index}.count"]=$totalCounter
logs["${index}.time"]="$totalTimeInvested"
fi
done <"$HABIT_CONFIG_LOG"
padlimit=60
pad=$(printf '%*s' "$padlimit")
pad=${pad// /.}
padlength=40
printf "\n"
echo -e "${yellow}This Month:${reset}"
currentMonth=$(gdate +"%m")
for slug in ${!habits[*]}; do
index="${slug}.${currentMonth}"
title="${logs["${index}.title"]}"
count="${logs["${index}.count"]}"
time="${logs["${index}.time"]}"
time=$(bc <<<"scale=2; ${time}/60")
printf ' %s (%s) ' "$title" "$count"
printf '%*.*s' 0 $((padlength - ${#title})) "$pad"
printf " H: ${cyan}%s${reset}\n" "$time"
done
printf "\n"
}
# ----------------- completion & cmd automation -------------------
#
# Adapted from: https://github.com/rwxrob/cmd-zet
while IFS= read -r line; do
[[ $line =~ ^declare\ -f\ x\. ]] || continue
COMMANDS+=("${line##declare -f x.}")
done < <(declare -F)
mapfile -t COMMANDS < \
<(LC_COLLATE=C sort < <(printf "%s\n" "${COMMANDS[@]}"))
declare -A HABITS
while IFS="|" read -ra log; do
habit="${log[0]}"
HABITS["${habit}"]="$habit"
done <"$HABIT_CONFIG_LOG"
if [[ -n $COMP_LINE ]]; then
line=${COMP_LINE#* }
if [[ ${line,,} =~ add ]]; then
line=${line//add /}
for c in "${HABITS[@]}"; do
[[ ${c:0:${#line}} == "${line}" ]] && echo "$c"
done
elif [[ ${line,,} =~ goal ]]; then
line=${line//goal /}
for c in "${HABITS[@]}"; do
[[ ${c:0:${#line}} == "${line}" ]] && echo "$c"
done
else
for c in "${COMMANDS[@]}"; do
[[ ${c:0:${#line}} == "${line,,}" ]] && echo "$c"
done
fi
exit
fi
if [[ -n "$1" ]]; then
declare CMD="$1"
shift
for c in "${COMMANDS[@]}"; do
if [[ $c == "$CMD" ]]; then
"x.$CMD" "$@"
exit $?
fi
done
fi
x.help "$@"