/
arttime.zsh
2776 lines (2662 loc) · 97.9 KB
/
arttime.zsh
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyrights 2022 Aman Mehra.
# Check ../LICENSE_CODE, ../LICENSE_ART, and ../LICENSE_ADDENDUM_CFLA
# files to know the terms of license
# License files are also on github: https://github.com/poetaman/arttime
# This is main source file of arttime that can be invoked like this:
# zsh -fi arttime.zsh [args]
# Though you don't need to invoke it like that if arttime is installed
# on your system. In that case it is preferable arttime like this:
# arttime [args]
autoload -Uz is-at-least
if ! is-at-least "5.7"; then
echo "Error: your zsh version $ZSH_VERSION is less than the required version: 5.7"
exit 1
fi
arttime_version="2.3.2"
zmodload zsh/zselect
zmodload zsh/zutil
zmodload zsh/datetime
zmodload zsh/terminfo
zmodload zsh/system
zmodload zsh/complist
autoload -Uz compinit; compinit
setopt nomonitor
setopt extendedglob
setopt globcomplete
#zstyle ':completion:*' completer _expand _complete _approximate _match
#zstyle ':completion::expand*' expand glob substitute sort all-expansions add-space
zstyle ':completion:*' matcher-list '' 'm:{a-zA-Z}={A-Za-z}'
setopt nobeep
setopt menucomplete
#setopt badpattern
setopt alwayslastprompt
setopt listrowsfirst
#zstyle ':completion:*:default' list-prompt '%S%m%s'
zstyle ':completion:*:default' select-prompt '%S%m%s'
setopt histignorespace
setopt histignorealldups
setopt sharehistory
setopt noincappendhistory
setopt appendhistory
statedir=$HOME/.local/state/arttime
if [[ ! -d $statedir/hist ]]; then
if ! mkdir -p $statedir/hist; then
print "E: Could not create $statedir/hist"
exit 1
fi
fi
if [[ ! -w $statedir/hist ]]; then
print "E: Directory $statedir/hist is not writable, please change it's write permission before proceeding."
exit 1
fi
histcmd="fc -pa"
fc -p
bindkey -e
bindkey "^R" history-incremental-pattern-search-backward
bindkey "^F" history-incremental-pattern-search-forward
autoload -Uz history-beginning-search-menu
zle -N history-beginning-search-menu
bindkey '^L' history-beginning-search-menu
autoload -Uz history-pattern-search
zle -N history-pattern-search-backward history-pattern-search
zle -N history-pattern-search-forward history-pattern-search
bindkey '^K' history-pattern-search-backward
bindkey '^J' history-pattern-search-forward
zstyle ':completion:*' menu select
bindkey -M menuselect '^M' .accept-line
#setopt AUTO_LIST
#zstyle ':completion:*:*:*:*:default' list-colors ${(s.:.)LSCOLORS}
#zstyle ':completion:*:*:*:*:descriptions' format '%F{blue}-- %D %d --%f'
# NOTE: enable minute/nonblocking mode if/when its functional,
# or remove it completely as sample rate is a waste now that
# we account for execution delay in loop to set timer
# m=minute_arg \
# n=nonblocking_arg \
# s:=samples_arg \
# This means all functions ending in _nonblocking, etc are unreachable for now
# NOTE: To allow user to specify fully custom time format in future add
# -tf-local:=tflocal_arg \
# -tf-other:=tfother_arg \
if is-at-least "5.8"; then
zparseoptscmd='zparseopts -D -E -F - '
else
zparseoptscmd='zparseopts -D -E - '
fi
$(printf "$zparseoptscmd") \
a:=artname_arg \
b:=flipartname_arg \
-random=random_arg \
t:=title_arg \
g:=goal_arg \
k:=keyfile_arg \
-keypoem:=keyfile_arg \
h=help_arg \
m=man_arg \
-theme:=theme_arg \
-tc:=titlecolor_arg \
-ac:=artcolor_arg \
-style:=style_arg \
-pa:=pbpendingchar_arg \
-pb:=pbcompletechar_arg \
-pl:=pblength_arg \
-hours:=hours_arg \
-debug=debug_arg \
-nolearn=nolearn_arg \
-line-editor:=lineeditor_arg \
-version=version_arg \
-help=help_arg \
-man=man_arg \
|| return
# NOTE: --line-editor and --debug are "undocumented" options that are not printed by --help
# --debug
# As the name suggests, it is for debug. It's meaning might change over time.
# --line-editor [zle|readline]
# There is a bug in zsh's line editor interface that makes it more desirable to
# read a line using bash on slower machines/connections. It avoids a flicker on last line
# of screen; on fast machines this problem won't occur. This option might get removed
# at some point, and it is not recommended for use. The line editor passed with this
# option only applies to keybindings 'g' (set goal) and 'm' (set title message).
# Another reason to not use this is it that arttime will add more zle-specific completion
# features in future, and they might not be available with readline.
if [[ "${#@}" != "0" ]]; then
printf "Error: arttime does not understand the following part of passed options,\n "
printf ' %s ' "[1m[4m$@[0m"
printf '\n%s\n' "check the valid options by invoking arttime with --help"
exit 1
fi
function printhelp {
read -r -d '' VAR <<-'EOF'
Usage:
arttime [OPTION]...
Option summary:
-a <name>
name of a-art file from artdir
-b <name>
name of b-art file from artdir
-t <str>
text placed under art
--random
select random art from arttime's collection
-g <time[;time;...;[loop[N]]]>
set timers
--hours <12|24>
show times in 12 or 24 hour format
-k, --keypoem <name|file|fifo>
feed keystrokes from a keypoem file/fifo.
--ac <num>
art color, value between 0-15
--tc <num>
title color, value between 0-15
--theme <light|dark>
theme
--style <0|1>
style number
--pa <str>
char/str to denote pending part of progress bar
--pb <str>
char/str to denote complete part of progress bar
--pl <num>
progress bar length
--nolearn
don't show keybindings upon launch
--version
Print version number of arttime, and exit
-m, --man
Open arttime's manual
-h, --help
Print help string, and exit
For more information on arttime, try:
$ arttime -m
$ man arttime
EOF
echo $VAR
}
# directories
bindir="${0:A:h}/../../../bin"
artdir="$bindir/../share/arttime/textart"
unamestr="$(uname -a)"
case "${unamestr}" in
Linux*)
if [[ "$unamestr" =~ ^.*[Mm]icrosoft.*$ ]]; then
machine="WSL"
else
machine="Linux"
fi
;;
Darwin*) machine="Darwin";;
*BSD*) machine="BSD";;
*) machine="UNKNOWN:${unamestr}";;
esac
if [[ ! -z $help_arg[-1] ]]; then
printhelp
exit 0
elif [[ ! -z $man_arg[-1] ]]; then
setopt monitor
docdir="$bindir/../share/arttime/doc"
mandir="$bindir/../share/man/man1"
if [[ -f $docdir/arttime.1.ans ]]; then
if command -v less &>/dev/null; then
LESS="" exec less -SRKP 'Press q to quit\. Use arrow keys, j/k, PageDown, PageUp to scroll\.' $docdir/arttime.1.ans
elif command -v most &>/dev/null; then
COLORTERM=16 exec most $docdir/arttime.1.ans
else
exec cat $docdir/arttime.1.ans
fi
fi
if command -v less &>/dev/null; then
export LESS=""
export LESS_TERMCAP_md=$(echoti setaf 4; echoti bold)
export LESS_TERMCAP_us=$(echoti setaf 5; echoti sitm 2>/dev/null)
export LESS_TERMCAP_ue=$(echoti sgr0)
fi
export MANWIDTH=80
export MANROFFOPT=""
export GROFF_SGR=1
if [[ -f $mandir/arttime.1.gz ]]; then
if [[ $machine == "Linux" ]]; then
export MANPAGER="less -SRK"
export MANLESS="Press q to quit\. Use arrow keys, j/k, PageDown, PageUp to scroll\."
exec command man --nh --nj $mandir/arttime.1.gz
elif [[ $machine == "Darwin" ]] && command -v gman &>/dev/null; then
export MANPAGER="less -SRK"
export MANLESS="Press q to quit\. Use arrow keys, j/k, PageDown, PageUp to scroll\."
exec command gman --nh --nj $mandir/arttime.1.gz
elif command -v man &>/dev/null; then
export MANPAGER="less -SRKP \"Press q to quit\. Use arrow keys, j/k, PageDown, PageUp to scroll\.\""
exec command man $mandir/arttime.1.gz
else
echo "E: command 'man' not found"
exit 1
fi
else
echo "E: arttime manual not found"
exit 1
fi
elif [[ ! -z $version_arg[-1] ]]; then
echo "$arttime_version"
exit 0
fi
function trimwhitespace {
local var="$*"
# remove leading whitespace characters
var="${var#"${var%%[![:space:]]*}"}"
# remove trailing whitespace characters
var="${var%"${var##*[![:space:]]}"}"
printf '%s' "$var"
}
if [[ $machine = "Linux" ]]; then
datecmd="date"
dateisgnu="1"
else
if command -v gdate &> /dev/null; then
datecmd="gdate"
dateisgnu="1"
else
datecmd="date"
if date -d '0' '+%s' &>/dev/null; then
dateisgnu="1"
else
dateisgnu="0"
fi
fi
fi
datehelpstr=""
function setdatehelpstr {
if [[ $dateisgnu = "1" ]]; then
read -r -d '' datehelpstr <<-'EOF'
You reached this help page because you entered an [31mincorrect[0m
value for goal time or used an [31munrecognized[0m format or entered 'help'.
arttime supports two types of time formats: 1) Native, 2) External.
External formats are enabled by GNU date, which is [32mINSTALLED[0m on your
system, and should be preferred (as they can be very expressive).
One can set multiple goals, and even sprint (loop) over a pattern of goals
multiple times. Multiple goals or a pattern of goals can be specified by
separating them with a semi-colon (;). Goals can be repeated by specifying
the last goal as 'loop'/'sprint' (to sprint forever) or 'loopN'/'sprintN'
(to sprint N number of times). See section 3) below to learn how to set
multiple goals, and/or a pattern of goals to repeat.
1) Native (simple, relative) formats:
1m (1 minute from now)
30s (30 seconds from now)
48h (48 hours from now)
2d 10s (2 days 10 seconds from now)
1h 1m 1s (1 hour 1 minute 1 seconds from now)
500m (500 minutes from now)
400m 100s (400 minutes and 100 seconds from now)
etc...
Native format can have any of: Ad Bh Cm Ds, where A/B/C/D are numbers.
2) External (simple/complex, absolute/relative, very expressive) formats:
10AM (upcoming 10AM, local timezone unless specified)
10:30AM IST (upcoming 10:30AM Indian Standard Time)
May 26 10:30AM IST (10:30AM on May 26 in Indian Standard Time)
Note: there is no comma after date 'May 26'
next friday (upcoming friday)
1min, +1min, 1 min, +1 minute, +1 minutes (1 minute from now)
1min 30sec, 1min 30sec (1 minute 30 sec from now)
next friday +4 hours (4AM on next friday in local time zone)
next monday 1PM (1PM on next monday in local time zone)
next monday 1PM IST (1PM on next monday in Indian Standard Time)
and much more...
For detailed documentation on External date/time formats please refer:
Web: https://www.gnu.org/software/coreutils/date
Locally: $ info '(coreutils) date invocation'
and jump directly to the section on "Date input formats".
3) Multiple and/or pattern of repeating goals:
10s;20s (10 seconds and 20 seconds from now)
10s;20s;1PM (10 seconds and 20 seconds from now, at 1PM in local time zone)
10s;loop, 10s;sprint (every 10 seconds, repeat forever)
10s;loop4, 10s;sprint4 (after 10 seconds, sprint 4 times)
10s;1m;loop2 (after 10 seconds and 1m from now, sprint 2 times)
5m;1PM;2PM;3PM;4PM;loop (after 5m from now and at 1-4 PM, repeat forever)
25m;30m;55m;1h;1h25m;1h30m;1h55m;2h25m;loop (pattern of Pomodoro Technique)
EOF
else
read -r -d '' datehelpstr <<-'EOF'
You reached this help page because you entered an [31mincorrect[0m
value for goal time or used an [31munrecognized[0m format or entered 'help'.
arttime supports two types of time formats: 1) Native, 2) External.
External formats are enabled by GNU date, which is [31mNOT INSTALLED[0m on your
please search the web to find if/how to install GNU date on your system.
If installed, it will open up the possibility of being very expressive in
specifying goal date/time. After installing GNU date, restart arttime.
One can set multiple goals, and even sprint (loop) over a pattern of goals
multiple times. Multiple goals or a pattern of goals can be specified by
separating them with a semi-colon (;). Goals can be repeated by specifying
the last goal as 'loop'/'sprint' (to sprint forever) or 'loopN'/'sprintN'
(to sprint N number of times). See section 3) below to learn how to set
multiple goals, and/or a pattern of goals to repeat.
1) Native (simple, relative, should work on your system) formats:
1m (1 minute from now)
30s (30 seconds from now)
48h (48 hours from now)
2d 10s (2 days 10 seconds from now)
1h 1m 1s (1 hour 1 minute 1 seconds from now)
500m (500 minutes from now)
400m 100s (400 minutes and 100 seconds from now)
etc...
Native format can have any of: Ad Bh Cm Ds, where A/B/C/D are numbers.
2) External (very expressive, though won't work on your system) formats:
10AM (upcoming 10AM, local timezone unless specified)
10:30AM IST (upcoming 10:30AM Indian Standard Time)
May 26 10:30AM IST (10:30AM on May 26 in Indian Standard Time)
Note: there is no comma after date 'May 26'
next friday (upcoming friday)
1min, +1min, 1 min, +1 minute, +1 minutes (1 minute from now)
1min 30sec, 1min 30sec (1 minute 30 sec from now)
next friday +4 hours (4AM on next friday in local time zone)
next monday 1PM (1PM on next monday in local time zone)
next monday 1PM IST (1PM on next monday in Indian Standard Time)
and much more...
For detailed documentation on External date/time formats please refer:
Web: https://www.gnu.org/software/coreutils/date
Locally: $ info '(coreutils) date invocation'
and jump directly to the section on "Date input formats".
3) Multiple and/or pattern of repeating goals:
10s;20s (10 seconds and 20 seconds from now)
10s;20s;1PM (10 seconds and 20 seconds from now, at 1PM in local time zone)
10s;loop, 10s;sprint (every 10 seconds, repeat forever)
10s;loop4, 10s;sprint4 (after 10 seconds, sprint 4 times)
10s;1m;loop2 (after 10 seconds and 1m from now, sprint 2 times)
5m;1PM;2PM;3PM;4PM;loop (after 5m from now and at 1-4 PM, repeat forever)
25m;30m;55m;1h;1h25m;1h30m;1h55m;2h25m;loop (pattern of Pomodoro Technique)
EOF
fi
}
function getaart {
if [[ $helpactive = "1" ]]; then
echo $restoreartname
else
echo $artname
fi
}
function getbart {
if [[ $helpactive = "1" ]]; then
if [[ -z $restoreflipartname ]]; then
return 1
else
echo $restoreflipartname
fi
else
if [[ -z $flipartname ]]; then
return 1
else
echo $flipartname
fi
fi
}
function notetimezone {
if [[ -z $TZ ]]; then
local etclocaltime="/etc/localtime"
if [[ -L $etclocaltime ]]; then
tzlong=$(sed -n 's/^.*zoneinfo[^/]*\/\(.*\)$/\1/p' <<<"${etclocaltime:A}")
else
echo "W: make $etclocaltime a symlink for faster load time" >/dev/stderr
tzlong=$(find /usr/share/zoneinfo/* -type f | while read fname; do cmp -s "$etclocaltime" "$fname" && sed -n 's/^.*zoneinfo[^/]*\/\(.*\)$/\1/p'<<<"$fname" && break; done)
fi
else
if [[ -f $TZ && $TZ =~ ^.*zoneinfo[^/]*/.*$ ]]; then
tzlong=$(sed -n 's/^.*zoneinfo[^/]*\/\(.*\)$/\1/p' <<<${TZ:A})
else
tzlong=$TZ
fi
#if [[ -z $tzlong ]]; then
# tzlong="$TZ"
#fi
fi
tzshort=$(strftime '%Z')
}
notetimezone
tzlonginit="$tzlong"
tzshortinit="$tzshort"
tzlongcurrent="$tzlong"
tzshortcurrent="$tzshort"
# Function to push desktop notifications
function notifydesktop {
local notifytitle="ARTTIME"
local notifysubtitle=$(strftime "%a %b %d, %Y $hms %Z")
local CR=$'\r'
if [[ $printsprints = "1" ]]; then
if [[ $goalmaxsprint != "-" ]]; then
local notifymessage="sprint-$goalsprint/$goalmaxsprint "
else
local notifymessage="sprint-$goalsprint "
fi
else
local notifymessage=""
fi
if [[ $goalmaxptreff -gt 1 ]]; then
local goalptrofmaxstr="-$goalptr/$goalmaxptreff"
else
local goalptrofmaxstr=""
fi
notifymessage+="goal$goalptrofmaxstr: $goalstr"
if bart=$(getbart); then
notifymessage+="${CR}arts: $(getaart), $(getbart)"
else
notifymessage+="${CR}art: $(getaart)"
fi
notifymessage+="${CR}ID: $$"
if [[ $machine = "Darwin" ]]; then
local notifystr="display notification \"$notifymessage\" with title \"$notifytitle\" subtitle \"$notifysubtitle\" sound name \"Blow\""
osascript -e $notifystr 2>/dev/null
elif [[ $machine = "Linux" || $machine = "BSD" ]]; then
[[ -z $XDG_DATA_DIRS ]] && export XDG_DATA_DIRS="$HOME/.local/share:/usr/share/xfce4:/usr/local/share:/usr/share"
read -r freedesk_message_sound <<(print -rC1 -- ${(s[:])^XDG_DATA_DIRS}/sounds/freedesktop/stereo/message-new-instant.oga(ND))
if command -v paplay &>/dev/null && pulseaudio --check &>/dev/null; then
soundcommand="paplay"
elif command -v pw-play &>/dev/null && {pactl info 2>/dev/null | grep "Server Name" 2>/dev/null | grep "PipeWire" &>/dev/null}; then
soundcommand="pw-play"
else
soundcommand="ogg123"
fi
[[ -n $freedesk_message_sound && -e $freedesk_message_sound ]] && $soundcommand $freedesk_message_sound &>/dev/null &
notify-send -u critical $notifytitle "$notifysubtitle${CR}$notifymessage" &>/dev/null
elif [[ $machine == "WSL" ]]; then
wsl-notify-send.exe --category "$notifytitle" "$notifysubtitle${CR}$notifymessage" &>/dev/null
fi
}
# Look at TODO note on lines 10-16, minute/nonblocking mode, sample rate is disabled
#updateonminute="${minute_arg[-1]}"
#nonblocking="${nonblocking_arg[-1]}"
#samples="${samples_arg[-1]}"
updateonminute=""
nonblocking=""
# The following options are functional, and complete
artname="${artname_arg[-1]}"
title="${title_arg[-1]}"
flipartname="${flipartname_arg[-1]}"
randomarg="${random_arg[-1]}"
theme="${theme_arg[-1]}"
goal="${goal_arg[-1]}"
termwidth="$COLUMNS"
termheight="$LINES"
artcolorarg="${artcolor_arg[-1]}"
titlecolorarg="${titlecolor_arg[-1]}"
style="${style_arg[-1]}"
pbcompletechar="${pbcompletechar_arg[-1]}"
pbpendingchar="${pbpendingchar_arg[-1]}"
pblength="${pblength_arg[-1]}"
debug="${debug_arg[-1]}"
nolearn="${nolearn_arg[-1]}"
#tflocal="${tflocal_arg[-1]}"
#tfother="${tfother_arg[-1]}"
hoursfmt="$hours_arg[-1]"
lineeditor="${lineeditor_arg[-1]}"
keyfilearg="${keyfile_arg[-1]}"
hm12="%I:%M%p"
hms12="%I:%M:%S%p"
hm24="%H:%M"
hms24="%H:%M:%S"
# This function sets hm, hms, tflocal, tfother used in rest of the program
function settimeformats {
if [[ $hoursfmt = "12" || $hoursfmt = "" ]]; then
hoursfmt="12"
hm="$hm12"
hms="$hms12"
elif [[ $hoursfmt = "24" ]]; then
hm="$hm24"
hms="$hms24"
else
printf "E: --hours can only be passed a value of 24 or 12 (default)\n"
exit 1
fi
tflocal="%s|%b %d, $hm"
tfother="%s|%b %d, $hm %Z"
}
settimeformats
timeformat=$tflocal
if [[ ! -z $debug ]]; then
tchar="|"
debugopt="--debug"
else
tchar=" "
debugopt=""
fi
if [[ -z $artname && ! -z $flipartname ]]; then
printf "E: if b-art is passed, a-art should also be passed\n"
exit 1
fi
if [[ ! -z $artname || ! -z $flipartname ]] && [[ ! -z $randomarg ]]; then
printf "E: if --random is passed, a-art/b-art should not be\n"
exit 1
fi
if [[ $lineeditor == "" || $lineeditor == "zle" ]]; then
usereadline="0"
elif [[ $lineeditor == "readline" ]]; then
if command -v bash &>/dev/null; then
usereadline="1"
else
printf "E: --line-editor readline requires bash installed\n"
exit 1
fi
else
printf "E: --line-editor can only accept values: readline or zle (default)\n"
exit 1
fi
function checkartfile {
local artfile=""
if [[ ! -z "$1" ]]; then
if [[ -f "${1/#\~/$HOME}" ]]; then
artfile="${1/#\~/$HOME}"
else
artfile="$artdir/${1}"
fi
if [[ ! -f "$artfile" ]]; then
return 1
fi
fi
return 0
}
checkartfile $artname
if [[ "$?" != "0" ]]; then
printf "E: artfile not found for a-art: $artname\n"
exit 1
fi
checkartfile $flipartname
if [[ "$?" != "0" ]]; then
printf "E: artfile not found for b-art: $flipartname\n"
exit 1
fi
if [[ ! -z $randomarg ]]; then
prevdir=$PWD
cd $artdir
RANDOM="${EPOCHREALTIME#*.}"
defaultshuffle=(^tarot-*(.Nnoe['REPLY=$RANDOM']))
cd $prevdir
defaultart="$defaultshuffle[1]"
else
defaultart="butterfly"
fi
if [[ ! -z "$nolearn" ]]; then
helpactive="0"
if [[ -z "$artname" ]]; then
artname="$defaultart"
fi
restoreartname="help"
if [[ ! -z "$flipartname" ]]; then
restoreflipartname="$flipartname"
fi
else
helpactive="1"
if [[ -z "$artname" ]]; then
restoreartname="$defaultart"
else
restoreartname="$artname"
fi
if [[ ! -z "$flipartname" ]]; then
restoreflipartname="$flipartname"
fi
artname="help"
flipartname=""
fi
if [[ $artcolorarg = "" ]]; then
artcolorarg="1"
elif ! [[ $artcolorarg =~ ^([0-9]|1[0-5])$ ]]; then
echo "W: artcolor (--ac) accepts takes range [0:15], defaulting to 1" >&2
artcolorarg="1"
fi
if [[ $titlecolorarg = "" ]]; then
titlecolorarg="2"
elif ! [[ $titlecolorarg =~ ^([0-9]|1[0-5])$ ]]; then
echo "W: titlecolor (--tc) accepts takes range [0:15] defaulting to 2" >&2
titlecolorarg="2"
fi
function keyfilefeed {
local keyfilepath=$(trimwhitespace "$1")
if [[ -e $bindir/../share/arttime/keypoems/$keyfilepath ]]; then
keyfilepath=$bindir/../share/arttime/keypoems/$keyfilepath
elif [[ -e "${keyfilepath/#\~/$HOME}" ]]; then
keyfilepath="${keyfilepath/#\~/$HOME}"
elif [[ -e "${keyfilepath/\$HOME/$HOME}" ]]; then
keyfilepath="${keyfilepath/\$HOME/$HOME}"
else
return 1
fi
if [[ -p $keyfilepath ]]; then
local fileispipe="1"
elif [[ -f $keyfilepath ]]; then
local fileispipe="0"
else
return 2
fi
if [[ $keyfilepath =~ ^.*_([1-9][0-9]*)(b|bps)$ ]]; then
local centidelay=$((100/match[1]))
elif [[ $keyfilepath =~ ^.*_([1-9][0-9]+)(ms|mspb)$ ]]; then
local centidelay=$((match[1]/10))
elif [[ $keyfilepath =~ ^.*_([1-9][0-9]*)(s|spb)$ ]]; then
local centidelay=$((match[1]*100))
else
local centidelay="0"
fi
[[ ! -z $streamfd ]] && { exec {streamfd}>&-; unset streamfd; }
if [[ $centidelay == "0" ]]; then
if [[ $fileispipe == "1" ]]; then
exec {streamfd}<>$keyfilepath
else
exec {streamfd}<$keyfilepath
fi
else
if [[ $fileispipe == "1" ]]; then
coproc { trap -; while sysread -s1 -i0; do zselect -t $centidelay; printf "$REPLY"; done <>$keyfilepath }
exec {streamfd}<&p
else
coproc { trap -; while sysread -s1 -i0; do zselect -t $centidelay; printf "$REPLY"; done <$keyfilepath }
exec {streamfd}<&p
fi
fi
return 0
}
handoff="0"
modestrnorm=""
modestrscript="$(echoti cub 6)SCRIPT"
modestrpos=$(echoti cup $termheight $((termwidth-1)))
function setmodestr {
if [[ $streamclosed == "1" ]]; then
modestr=$modestrnorm
else
modestr=$modestrpos$modestrscript
fi
}
if [[ -t 0 ]]; then
streamclosed="1"
streamfd=""
if [[ $keyfilearg != "" ]]; then
if keyfilefeed "$keyfilearg"; then
streamclosed="0"
else
printf "E: -k/--keypoem can either be passed a file/fifo name under\n $bindir/../share/arttime/keypoems/\n or full path to a file/fifo that has keystrokes for arttime.\n"
exit 1
fi
fi
else
unset streamfd
exec {streamfd}>&0
exec 0>&-
exec 0>&1
streamclosed="0"
fi
setmodestr
inputstr=""
promptstrpost=""
function readstr {
inputstr="$1"
if [[ $streamclosed == "1" ]] && [[ -z $2 ]]; then
vared -h -t $TTY -p "$promptstr%{$tput_cnorm%} " -c inputstr; retstatus="$?"
else
while read -r -s -k1 -t0 tempttychar && [[ $tempttychar != "p" ]] ; do : ; done
[[ ! -z $2 ]] && printf "${tput_el}${promptstr}${tput_cnorm} ${inputstr}"
local inputchard=""
local inputcharint=0
local inputchardint=0
retstatus="1"
while true; do
readchar "" "$2" || { inputstr=""; break; }
inputcharint=$(printf '%d' "'$inputchar")
if ((inputcharint == 10 || inputcharint == 13 )); then
retstatus="0"
[[ ! -z $2 ]] && printf "$promptstrpost"
break
elif ((inputchardint == 92 && inputcharint == 110)); then
inputstr="${inputstr:0:-1}"
retstatus="0"
[[ ! -z $2 ]] && printf "$tput_cub1 $tput_cub1$promptstrpost"
break
else
if [[ ! -z $inputchar ]]; then
inputchard=$inputchar
inputchardint=$(printf '%d' "'$inputchar")
inputstr+=$inputchar
[[ ! -z $2 ]] && printf $inputchar
else
retstatus="1"
inputstr=""
break
fi
fi
done
fi
return $retstatus
}
inputchar=""
function readchar {
inputchar=""
if [[ ! -z $1 ]]; then
local timeout="-t$1"
else
local timeout=""
fi
if [[ $streamclosed == "1" ]] && [[ -z $2 ]]; then
read -r -s $timeout -k1 inputchar; retstatus="$?"
else
ERRNO=0
sysread -s1 $timeout -i$streamfd inputchar; retstatus=$?; reterrno=$ERRNO
case "$retstatus" in
"0"|"4")
retstatus=0
[[ $tempttychar != "p" ]] && read -r -s -k1 -t0 tempttychar
if [[ $tempttychar == "p" ]]; then
tempttychar=""
printf "$tput_sc$modestrpos$(echoti cub 6)PAUSED$tput_rc"
trap "tstpdone=1" TSTP
trap "contdone=1" CONT
tstpdone="0"
kill -TSTP 0 &>/dev/null
while [[ $tstpdone == "0" ]]; do zselect -t 10; done
unset tstpdone
while [[ $tempttychar != "p" ]]; do read -r -s -k1 tempttychar; done
printf "$tput_sc$modestr$tput_rc"
contdone="0"
kill -CONT 0 &>/dev/null
while [[ $contdone == "0" ]]; do zselect -t 10; done
unset contdone
trap "trapstop_blocking" TSTP
trap "trapcont_blocking" CONT
tempttychar=""
fi
;;
*)
trap "intdone=1" INT
intdone="0"
kill -s INT 0 &>/dev/null
while [[ $intdone == "0" ]]; do zselect -t 10; done
unset intdone
while read -r -s -k1 -t0 inputchar ; do : ; done
inputchar=""
streamclosed="1"
exec {streamfd}>&-
unset streamfd
exec {streamfd}>&1
sttyset
setmodestr
handoff="1"
trap "trapint_blocking" INT
;;
esac
fi
return $retstatus
}
function slurp {
local slurpchar
if [[ $streamclosed == "1" ]]; then
while read -r -s -k1 -t0 slurpchar ; do : ; done
else
while sysread -s1 -t0 -i$streamfd slurpchar ; do : ; done
fi
}
tputset_str=$(echoti rmam 2>/dev/null; echoti civis; echoti smcup)
function tputset { printf "$tputset_str"; }
tputreset_str=$(echoti rmcup; echoti smam 2>/dev/null; echoti cnorm)
function tputreset { printf "$tputreset_str"; }
regexpart='[[:space:]]*([0-9]+)([dhms])[[:space:]]*'
regex1='^'$regexpart'$'
regex2='^'$regexpart$regexpart'$'
regex3='^'$regexpart$regexpart$regexpart'$'
regex4='^'$regexpart$regexpart$regexpart$regexpart'$'
function getseconds {
amount="$1"
suffix="$2"
if [[ $suffix = "d" ]]; then
echo $(( amount*86400 ))
elif [[ $suffix = "h" ]]; then
echo $(( amount*3600 ))
elif [[ $suffix = "m" ]]; then
echo $(( amount*60 ))
elif [[ $suffix = "s" ]]; then
echo $amount
fi
}
function setgoaltime {
local currenttime="$2"
local goal="$1"
local goaltime=""
local goaltimestr=""
local goalstatus="0"
if [[ $goal =~ $regex1 ]]; then
goaltime=$(getseconds $match[1] $match[2])
goaltime=$(( currenttime+goaltime ))
elif [[ $goal =~ $regex2 ]]; then
local goaltime1=$(getseconds $match[1] $match[2])
local goaltime2=$(getseconds $match[3] $match[4])
goaltime=$(( currenttime+goaltime1+goaltime2 ))
elif [[ $goal =~ $regex3 ]]; then
local goaltime1=$(getseconds $match[1] $match[2])
local goaltime2=$(getseconds $match[3] $match[4])
local goaltime3=$(getseconds $match[5] $match[6])
goaltime=$(( currenttime+goaltime1+goaltime2+goaltime3 ))
elif [[ $goal =~ $regex4 ]]; then
local goaltime1=$(getseconds $match[1] $match[2])
local goaltime2=$(getseconds $match[3] $match[4])
local goaltime3=$(getseconds $match[5] $match[6])
local goaltime4=$(getseconds $match[7] $match[8])
goaltime=$(( currenttime+goaltime1+goaltime2+goaltime3+goaltime4 ))
elif goaltime=$($datecmd -d $goal '+%s' 2>/dev/null ); then
if [[ $goaltime -gt $currenttime ]]; then
elif goaltime=$($datecmd -d "$goal +1days" '+%s' 2>/dev/null); then
if [[ $goaltime -gt $currenttime ]]; then
goalstatus="1"
#echoti cup 0 0
#echoti el
#printf "Today's $goal has passed, setting goal time to tomorrow's $goal"
#sleep 2
else
goaltime='-'
goalstatus="2"
fi
fi
else
goaltime='-'
goalstatus="2"
fi
#echo "$ goal:$goal, goaltime:$goaltime"
if [[ $goaltime != '-' ]]; then
local tmpgoaltime=$(strftime $timeformat $goaltime)
local tmpgoaltimearray=(${(s/|/)tmpgoaltime})
goaltimestr=$tmpgoaltimearray[2]
#if goaltimestr=$($datecmd -d "@$goaltime" "+%b %d, %I:%M%p" 2>/dev/null); then
#elif goaltimestr=$($datecmd -jf "%s" $goaltime "+%b %d, %I:%M%p" 2>/dev/null); then
#else
# goaltimestr=""
#fi
else
goaltimestr=""
fi
returngoaltime="$goaltime"
returngoaltimestr="$goaltimestr"
returngoalstatus="$goalstatus"
#return $returnval
#echo "$ goal:$goal, goaltime:$goaltime, goaltimestr:$goaltimestr"
}
#goaltimes=()
#goaltimesstrs=()
#goalvalids=()
goalarray=()
goalarraysorted=()
goalentry=""
goalsprint="1"
function setnextgoal {
if [[ $goalptr != $goalmaxptr ]] then;
local lastgoaltime="$goaltime"
local lastgoaltimestr="$goaltimestr"
goalptr=$((goalptr+1))
goalentry="$goalarraysorted[$goalptr]"
#$returngoaltime;$numgoals;$returngoalstatus;$returngoaltimestr;$goal
local goalentryarray=(${(s/;/)goalentry})
goaltime="$goalentryarray[1]"
goalnum="$goalentryarray[2]"
local localgoalnum="$goalnum"
goalstatus="$goalentryarray[3]"
goaltimestr="$goalentryarray[4]"
local localgoaltimestr="$goaltimestr"
goalstr="$goalentryarray[5]"
if [[ $goalstr = "loop" ]]; then
if [[ $localgoaltimestr = "-" ]]; then
local lastgoalptr="$goalptr"
goalsprint=$((goalsprint+1))
accumulatedsprinttime="0"
sprintstarttime=$(strftime '%s')
setgoals $sprintstarttime
goaldone="0"
#goalarraysorted[-1]=("loop;$localgoalnum;0;-;loop")
elif [[ $localgoaltimestr -gt 1 ]]; then
local lastgoalptr="$goalptr"
goalsprint=$((goalsprint+1))
accumulatedsprinttime="0"
sprintstarttime=$(strftime '%s')
setgoals $sprintstarttime
goaldone="0"
goalarraysorted[-1]=("loop;$localgoalnum;0;$((localgoaltimestr-1));loop")
else
goaltime="$lastgoaltime"
goaltimestr="$lastgoaltimestr"
goalptr="$((goalmaxptr-1))"
goaldone="1"
fi
else
goaldone="0"
fi
fi
}
function setgoals {
goalmaxptr="0"
goalptr="0"
local tmpgoals=(${(s/;/)goal})
local goal=""
local numgoals="0"
local errdetected="0"
local tmpgoalarray=()
for goal in "$tmpgoals[@]"; do
numgoals=$((numgoals+1))
if [[ $goal =~ ^[[:space:]]*(loop|sprint|x)(.*)$ ]]; then
goal="loop"
local match2="$match[2]"
if [[ $match2 =~ ^([0-9]+)[[:space:]]*$ ]]; then
returngoaltimestr="$match[1]"
elif [[ $match2 =~ ^[[:space:]]*$ ]]; then
returngoaltimestr="-"