-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathcli-clock
executable file
·261 lines (217 loc) · 6.33 KB
/
cli-clock
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
#!/bin/bash
DIGIT_COLOR=RED
LETTER_COLOR=YELLOW
PUNCT_COLOR=YELLOW
BLACK=0 RED=1 GREEN=2 YELLOW=3 BLUE=4 MAGENTA=5 CYAN=6 WHITE=7
DC="\e[3${!DIGIT_COLOR};4${!DIGIT_COLOR}m"
LC="\e[3${!LETTER_COLOR};4${!LETTER_COLOR}m"
PC="\e[3${!PUNCT_COLOR};4${!PUNCT_COLOR}m"
NC="\e[m"
NUM[0]="######|##__##|##__##|##__##|######"
NUM[1]="____##|____##|____##|____##|____##"
NUM[2]="######|____##|######|##____|######"
NUM[3]="######|____##|######|____##|######"
NUM[4]="##__##|##__##|######|____##|____##"
NUM[5]="######|##____|######|____##|######"
NUM[6]="######|##____|######|##__##|######"
NUM[7]="######|____##|____##|____##|____##"
NUM[8]="######|##__##|######|##__##|######"
NUM[9]="######|##__##|######|____##|____##"
A="_AAAA_|AA__AA|AAAAAA|AA__AA|AA__AA"
B="AAAAA_|AA__AA|AAAAA_|AA__AA|AAAAAA"
C="_AAAA_|AA__AA|AA____|AA__AA|_AAAA_"
D="AAAAA_|AA__AA|AA__AA|AA__AA|AAAAA_"
E="AAAAAA|AA____|AAAAAA|AA____|AAAAAA"
F="AAAAAA|AA____|AAAA__|AA____|AA____"
G="_AAAA_|AA____|AA_AAA|AA__AA|_AAAA_"
H="AA__AA|AA__AA|AAAAAA|AA__AA|AA__AA"
I="AAAA|_AA_|_AA_|_AA_|AAAA"
J="___AAA|____AA|____AA|AA__AA|_AAAA_"
K="AA__AA|AA_AA_|AAAA__|AA_AA_|AA__AA"
L="AA____|AA____|AA____|AA____|AAAAAA"
M="AA___AA|AAA_AAA|AAAAAAA|AA_A_AA|AA___AA"
N="AA__AA|AAA_AA|AAAAAA|AA_AAA|AA__AA"
O="_AAAA_|AA__AA|AA__AA|AA__AA|_AAAA_"
P="AAAAAA|AA__AA|AAAAAA|AA____|AA____"
Q="_AAAA_|AA__AA|AA__AA|AA_AAA|_AAAA_"
R="AAAAA_|AA__AA|AAAAAA|AA_AA_|AA__AA"
S="_AAAAA|AA____|_AAAA_|____AA|AAAAA_"
T="AAAAAA|__AA__|__AA__|__AA__|__AA__"
U="AA__AA|AA__AA|AA__AA|AA__AA|_AAAA_"
V="AA__AA|AA__AA|AA__AA|_AAAA_|__AA__"
W="AA___AA|AA_A_AA|AAAAAAA|AAA_AAA|AA___AA"
X="AA__AA|_AAAA_|__AA__|_AAAA_|AA__AA"
Y="AA__AA|AA__AA|_AAAA_|__AA__|__AA__"
Z="AAAAAA|___AA_|__AA__|_AA___|AAAAAA"
INDENT="________|________|________|________|________"
COLON="__|::|__|::|__"
COMMA="__|__|__|::|_:"
EXCLEMATION="::|::|::|__|::"
PERIOD="__|__|__|__|::"
SEMICOLON="__|::|__|::|_:"
SPACE="__|__|__|__|__"
NEWLINE="NEWLINE"
SPACE_WIDTH=2
LINE_HEIGHT=$(awk -F\| '{ print NF }' <<<$COLON)
LOCAL_TIMEZONE=$([[ -f /etc/sysconfig/clock ]] && awk -F'"' '/ZONE=/ { print $2 }' /etc/sysconfig/clock)
[[ -n $LOCAL_TIMEZONE ]] || LOCAL_TIMEZONE=America/Los_Angeles
HOUR=H
while [[ $1 ]]; do
case $1 in
--world)
TIMEZONES="America/New_York Europe/London Europe/Paris Asia/Baghdad Asia/Singapore Asia/Tokyo"
;;
--24hour)
HOUR=H
;;
--12hour)
HOUR=I
;;
*)
ARGS="${ARGS+: $ARGS}$1"
;;
esac
shift
done
initStrings() {
local string_name
for string_name in {0..9} {A..Z} ':' ',' '!' '.' ';' ' ' '>' $'\n'; do
case $string_name in
[a-z]) string_name=${string_name^^};;
:) string_name=COLON;;
,) string_name=COMMA;;
!) string_name=EXCLEMATION;;
.) string_name=PERIOD;;
\;) string_name=SEMICOLON;;
' ') string_name=SPACE;;
'>') string_name=INDENT;;
$'\n') string_name=NEWLINE;;
esac
string_names+=("$string_name")
local string
if [[ $string_name =~ [0-9] ]]; then
string=${NUM[$string_name]}
else
string=${!string_name}
fi
local char_width=$(cut -f 1 -d '|' <<<$string)
char_width=${#char_width}
char_widths+=("$char_width")
local movement='\\e['$char_width'D\\e[1B'
# bash has memory leaks when using parameter substitution, so sed is employed instead
string=$(sed "s/\(#\+\)/\\$DC\1\\$NC/g;\
s/\(A\+\)/\\$LC\1\\$NC/g;\
s/\(:\+\)/\\$PC\1\\$NC/g;\
s/_/ /g;\
s/|/\\$movement/g" <<<$string
)
strings+=("$string")
done
}
screenSetup() {
# determine terminal size
dimensions=$(
tput -S <<-!
lines
cols
!
)
lines=${dimensions%%$'\n'*}
cols=${dimensions##*$'\n'}
# redraw screen if terminal dimensions change
if [[ $dimensions != $dimensions_log ]]; then
dimensions_log=$dimensions
printf "\Ec" # clear terminal
tput civis
fi
# put the cursor in the correct position to center the clock in the terminal
start_row=$(( ( lines - HEIGHT ) / 2 ))
start_col=$(( ( cols - WIDTH ) / 2 ))
start_row=2
start_col=$(( SPACE_WIDTH + 1 ))
vert_offset=0
printf "\E[$(( start_row + vert_offset ));${start_col}f"
space_on_line=$(( cols - start_col ))
}
drawChar() {
local char="$1"
# translate string
case $char in
[a-z]) char=${char^^};;
:) char=COLON;;
,) char=COMMA;;
!) char=EXCLEMATION;;
.) char=PERIOD;;
\;) char=SEMICOLON;;
' ') char=SPACE;;
'>') char=INDENT;;
$'\n') char=NEWLINE;;
esac
local i
for (( i=0; i < ${#string_names[@]}; i++ )); do
if [[ "${string_names[$i]}" == "$char" ]]; then
local string="${strings[$i]}"
local char_width=${char_widths[$i]}
break
fi
done
# newline check
if [[ $char_width -gt $space_on_line || $string == "NEWLINE" ]]; then
(( vert_offset+=$(( LINE_HEIGHT + 1 )) ))
printf "\E[$(( start_row + vert_offset ));${start_col}f"
space_on_line=$(( cols - start_col ))
[[ $string == "NEWLINE" ]] && return
fi
# after the formatted character, and move cursor right and up to prepare for next character
printf "$string\e[${SPACE_WIDTH}C\e[4A"
# keep track of remaining space on the current line
(( space_on_line-=$(( $char_width + $SPACE_WIDTH )) ))
}
printString() {
local string="$@"
while [[ $string ]]; do
# print the first character in $string
local char="${string:0:1}"
drawChar "$char"
# remove the character we printed from ${string}
string="${string:1}"
done
}
getDate() {
local TZ
if [[ ! $@ ]]; then
local date_string="%$HOUR:%M:%S"
TZ=$LOCAL_TIMEZONE date +"$date_string"
else
local date_string="%$HOUR:%M:%S %Z"
TZ=$LOCAL_TIMEZONE date +"$date_string"
date_string="%$HOUR:%M %Z"
local i
for i in $@; do
printf ">"
TZ=$i date +"$date_string"
done
fi
}
end() {
# put cursor below any output, make it visibile, and exit
printf "\n%.s" $(eval echo {0..$LINE_HEIGHT})
tput cvvis
exit
}
trap end SIGHUP SIGINT SIGTERM
initStrings
# Main Loop
while sleep ${delay:-0}; do
screenSetup
date="$(getDate ${TIMEZONES})"
printString "${ARGS:-$date}"
[[ $ARGS ]] && end
# determine how long to sleep before the next update
#nanoseconds=10#$(date +%N) # removed this super-elegant solution because OSX's bash can't handle it :(
nanoseconds=$(date +%N)
while [[ $nanoseconds =~ ^0 ]]; do
nanoseconds=${nanoseconds:1}
done
delay=0.$(( 1000000000 - nanoseconds ))
done