Repository navigation
Expand file tree
/
Copy pathtimer
More file actions
executable file
·631 lines (488 loc) · 14 KB
/
Copy pathtimer
File metadata and controls
executable file
·631 lines (488 loc) · 14 KB
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
#!/usr/bin/env bash
# timer
# A lightweight, feature-rich timer program written entirely in bash
# See README.md for details
# Nate Faber, 2016-2018
# Main function body
function main () {
# Define where all the files are
timerFile=~/.timer
undoFile=~/.timerundo
alarmFile=$(dirname "$(readlink -f "$0")")/timer_expire.wav
scriptFile=~/.timerscript
# If the timer file doesn't exist create it
if [ ! -e $timerFile ]; then
touch $timerFile
fi
# If the first argument is a number, use that as the active timer
if [ "$1" -eq "$1" ] 2>/dev/null; then
nTimer="$1"
shift
showNumber=true
else
nTimer=1
showNumber=
fi
# TODO: Allow option to make timer 0 based by adding 1 here
nLine=$(( nTimer + 0 ))
# If the timer file does not currently have enough lines, add timers until it does
while [ $(wc -l < $timerFile) -lt "$nLine" ]; do
echo "+0" >> $timerFile
done
# Get the command
if [ $# -gt 0 ]; then
command=$1
shift
else
# Default command
command=show
fi
case $command in
set|s)
remember
set $*
;;
from|until|til|to)
remember
relative $*
;;
recur|r)
remember
symbol="R"
set $*
;;
add|plus|+)
remember
add $*
;;
sub|minus|-)
remember
doSubtraction=true
add $*
;;
reverse|rev)
remember
reverse
;;
pause|stop)
remember
pause
;;
go|start|play)
remember
unpause
;;
p|pp|toggle)
remember
toggle_pause
;;
zero|z|0)
remember
zero
;;
undo|u)
undo
;;
view|v)
while [ $# -gt "0" ]; do
case "$1" in
alarm)
useAlarm=true
;;
script)
alarmScript=true
;;
*)
echo "Unrecognized alarm function" >&2
;;
esac
shift
done
view
;;
print|show|now)
show
;;
*)
echo "$1" is an invalid command
;;
esac
}
# Clean up when done
function finish () {
# Turn cursor visible again
tput cnorm
# Echo keyboard strokes to terminal
stty echo
}
trap finish EXIT
function get () {
# Parses the timer FILE. Only needs to be run each time the FILE is changed
# Read the value in the file
inLine=$(awk "NR==$nLine"'{print $1}' < $timerFile)
# Get the starting symbol
case ${inLine:0:1} in
[0-9]) # Just a normal timer
symbol=
currentVal=$inLine
;;
\+) # A countup timer
symbol=+
currentVal=${inLine##\+}
;;
\-) # An expired timer
symbol=-
currentVal=${inLine##\-}
;;
R) # A recurring timer (uses comma separated current,recurvalue)
symbol=R
inLine=${inLine##\R}
IFS=',' read currentVal RECUR_VAL <<< $inLine
# Make it easier for functions to write recurring timers
recurStr=",$RECUR_VAL"
;;
*)
echo "Invalid timer file. Use 'timer zero' or 'timer set' to fix"
exit 26
;;
esac
# Divide the current epoch time by 2 to get the arbitrary divider between paused and active timers
epochDivider=$((( $(date +%s) / 2 )))
# Set the state of the timer and also calculate the complementary second amount
if [ "$currentVal" -gt "$epochDivider" ]; then
timerState=running
runningTime="$currentVal"
case $symbol in
\-|\+)
pauseTime=$(( $(date '+%s') - $currentVal ))
;;
*)
pauseTime=$(( $currentVal - $(date '+%s') ))
;;
esac
else
timerState=paused
pauseTime="$currentVal"
case $symbol in
\-|\+)
runningTime=$(date -d "now - $currentVal seconds" '+%s')
;;
*)
runningTime=$(date -d "now + $currentVal seconds" '+%s')
;;
esac
fi
# This variable tells timer view whether or not it has refreshed yet since the last get
viewRefreshed=
}
function parse-time () {
# Parses durations specified on the command line
# 2 formats are available:
# 1. [HH:[[MM]:SS]] style phrases (colon separated numbers)
# 2. '5 minutes 3 seconds', time phrases, which are parsed by date utility
# Determine which format the value is in
if [ $# -eq "0" ]; then
echo "duration must be specified with that command" >&2
elif [ $# -eq "1" ]; then
# Format 1: M, MM:SS, HH:MM:SS, DDD:HH:MM:SS (use this format if only one argument is given)
# Use janky bash method to get the hours, minutes and seconds into an array (splitting at :)
IFS_OLD=$IFS
IFS=':'
read -r -a timeArray <<< "$1"
IFS=$IFS_OLD
# Now look at the length of the array to determine if minutes, minutes and seconds, or hours minutes and seconds
arrayLen=${#timeArray[@]}
case $arrayLen in
1)
days=0
hours=0
minutes=${timeArray[0]}
seconds=0
;;
2)
days=0
hours=0
minutes=${timeArray[0]}
seconds=${timeArray[1]}
;;
3)
days=0
hours=${timeArray[0]}
minutes=${timeArray[1]}
seconds=${timeArray[2]}
;;
4)
days=${timeArray[0]}
hours=${timeArray[1]}
minutes=${timeArray[2]}
seconds=${timeArray[3]}
;;
esac
totalSeconds=$(( (($days * 24 + $hours) * 60 + $minutes) * 60 + $seconds ))
else
# Format 2: 5 days 4 hours 3 minutes, smart parsing using the built in date parser
timePoint=$(date -d "now + $*" '+%s')
totalSeconds=$(( $timePoint - $(date '+%s') ))
fi
}
function set () {
# Parse the inputs
parse-time $*
# After setting the timer, start immediately
setTime=$(date -d "now + $totalSeconds seconds" '+%s')
# If this is a recurring timer, set the recur string to be the seconds until the set time
if [[ $symbol == "R" ]]; then
recurStr=",$totalSeconds"
fi
timerVal="$symbol$setTime$recurStr"
# Change the correct line in the timer file
sed -i --follow-symlinks "$nLine"'s/.*/'"$timerVal"'/' "$timerFile"
}
function relative () {
# Set the timer relative to a certain time
timerVal=$(date -d "$*" '+%s')
# If date occured in the past, this is a countup timer
if [ "$timerVal" -lt "$(date '+%s')" ]; then
timerVal="+$timerVal"
fi
# Change the correct line in the timer file
sed -i --follow-symlinks "$nLine"'s/.*/'"$timerVal"'/' "$timerFile"
}
function reverse () {
# Reverse the direction of the timer
# Countup becomes countdown
# Unexpired countdown becomes countup
# Expired countdown becomes unexpired countdown
get
case $symbol in
+|-)
symbol=
;;
R)
echo "You can't reverse a recurring timer" >&2
exit 1
;;
*)
symbol='+'
;;
esac
# Write to file
timerVal="$symbol$pauseTime$recurStr"
sed -i --follow-symlinks "$nLine"'s/.*/'"$timerVal"'/' "$timerFile"
# If timer was running before, keep it running now
if [ "$timerState" == "running" ]; then
unpause
fi
}
function add () {
# Adds minutes to the timer
parse-time $*
get
# If subtracting, flip the sign
if [[ $doSubtraction ]]; then
totalSeconds=$(( $totalSeconds * -1 ))
fi
# Use what the pause time would be to calculate the new pause time
if [[ $symbol == "-" ]]; then
# If timer is expired then direction is inverted
newPauseTime=$(( $pauseTime - $totalSeconds ))
echo $newPauseTime
# if now above 0, get rid of the minus sign (it's less than 0 in the equation because pauseTime ignores the symbol)
if [ $newPauseTime -lt 0 ]; then
newPauseTime=$(( $newPauseTime * -1 ))
symbol=
fi
else
newPauseTime=$(( $pauseTime + $totalSeconds ))
fi
# Write to file
timerVal="$symbol$newPauseTime$recurStr"
sed -i --follow-symlinks "$nLine"'s/.*/'"$timerVal"'/' "$timerFile"
# If timer was running before, keep it running now
if [ "$timerState" == "running" ]; then
unpause
fi
}
function recur () {
parse-time $1
totalSeconds=$(( (($days * 24 + $hours) * 60 + $minutes) * 60 + $seconds ))
# Add the recur value after the timer value (separated by a comma)
timerVal="R$(date -d "now + $totalSeconds seconds" '+%s'),$totalSeconds"
# Change the correct line in the timer file
sed -i --follow-symlinks "$nLine"'s/.*/'"$timerVal"'/' "$timerFile"
}
####### Pause and Unpause the Timer ########
function pause () {
get
case $timerState in
running)
timerVal="$symbol$pauseTime$recurStr"
# Change the correct line in the timer file
sed -i --follow-symlinks "$nLine"'s/.*/'"$timerVal"'/' "$timerFile"
timerState=paused
;;
paused)
;;
esac
}
function unpause () {
get
case $timerState in
running)
;;
paused)
timerVal="$symbol$runningTime$recurStr"
# Change the correct line in the timer file
sed -i --follow-symlinks "$nLine"'s/.*/'"$timerVal"'/' "$timerFile"
timerState=running
;;
esac
}
function toggle_pause () {
get
case $timerState in
running)
pause
;;
paused)
unpause
;;
esac
}
############################################
function alarm () {
if [[ "$useAlarm" ]]; then
paplay $alarmFile
fi
if [[ "$alarmScript" ]]; then
$scriptFile $nTimer
fi
}
function output () {
# Find the days worth of seconds left as an integer, than get the remainder. Repeat for hours, minutes, and seconds
displayDays=$(( $secondsLeft / $(( 60 * 60 * 24 )) ))
secondsLeft=$(( $secondsLeft - $displayDays * 60 * 60 * 24 ))
displayHours=$(( $secondsLeft / $(( 60 * 60)) ))
secondsLeft=$(( $secondsLeft - $displayHours * 60 * 60 ))
displayMinutes=$(( $secondsLeft / 60 ))
secondsLeft=$(( $secondsLeft - $displayMinutes * 60 ))
displaySeconds=$secondsLeft
# Format string for outputting the timer
formatStr='%1.1s%d:%.2d:%.2d:%.2d'
# If days or hours are zero, make them blank
if [ "$displayDays" -eq "0" ]; then
formatStr='%1.1s%.0d%.2d:%.2d:%.2d'
if [ "$displayHours" -eq "0" ]; then
formatStr='%1.1s%.0d%.0d%.2d:%.2d'
fi
fi
# if timer view is called with a timer number, show the number
if [ "$showNumber" ]; then
tput dim
echo -n "$nTimer|"
tput sgr0
fi
# Print the time
printf "$formatStr" "$symbol" "$displayDays" "$displayHours" "$displayMinutes" "$displaySeconds"
}
function view () {
tput civis
tput clear
stty -echo
get
# Whenever the timer file is updated, rerun get to get the new state
trap get SIGUSR1
echo $timerFile | entr -p kill -SIGUSR1 $$ &
while true; do
case $timerState in
running)
secondsLeft=$(( $currentVal - $(date '+%s') ))
if [ -z $viewRefreshed ]; then
# Refresh the screen if a get has been run
tput clear
fi
# If seconds is less than 0, then change seconds to positive. Symbols are added later.
if [ "$secondsLeft" -le "0" ]; then
(( secondsLeft *= -1 ))
# If symbol is empty, this means the timer has counted down to zero. Sound alarm and signal timer as expired
if [[ ! $symbol ]]; then
alarm &
# Write a '-' to the beginning of the line in the timer file
symbol="-"
sed -i --follow-symlinks "$nLine"'s/^\(.*\)/'"$symbol"'\1/' "$timerFile"
fi
# If symbol is R, this is a recurring timer. Sound the alarm and reset the timer
if [ "$symbol" = "R" ]; then
alarm &
# Add the recur value to the current timer
# If no timer view has been active might have to catch up multiple cycles (hence the while loop)
while
let "currentVal += RECUR_VAL"
[ "$currentVal" -lt $(date '+%s') ]
do true; done
# Change the correct line in the timer file
timerVal="$symbol$currentVal$recurStr"
sed -i --follow-symlinks "$nLine"'s/.*/'"$timerVal"'/' "$timerFile"
fi
fi
# Move cursor to begining of the linene, outoutput the timer, and then clear to the end of thline
tput cup 0 0
output
tput el
;;
paused)
if [ -z $viewRefreshed ]; then
secondsLeft=$currentVal
# Refresh the screen if a get has been run
tput clear
output
viewRefreshed=true
fi
# Refresh if enter is pressed on the timer
if $(read -t0); then
tput clear
output
fi
;;
esac
sleep 1
done
}
function show () {
get
case $timerState in
running)
secondsLeft=$(( $currentVal - $(date '+%s') ))
if [ "$secondsLeft" -le "0" ]; then
(( secondsLeft *= -1 ))
fi
;;
paused)
secondsLeft=$currentVal
;;
esac
output
}
function remember () {
# Save the current timer state to the UNDO file
cat $timerFile > $undoFile
}
function undo () {
# If undo has already been used once report that to the user
if [ ! -s $undoFile ]; then
echo "Undo currently has a max depth of 1"
else
# Replace the timer file with the undo file
cat $undoFile > $timerFile
# Empty the undoFile to indicate that it has been used
:>$undoFile
fi
}
function zero () {
timerVal="+0"
# Change the correct line in the timer file
sed -i --follow-symlinks "$nLine"'s/.*/'"$timerVal"'/' "$timerFile"
get
}
# Run the main function
main $*