-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.py
executable file
·464 lines (430 loc) · 11.8 KB
/
clock.py
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
#!/usr/bin/env python3
"""A command line program to display the time in multiple formats."""
import os
import sys
from datetime import datetime
from typing import cast
from md_to_py import md_table_to_lines
def analog_clock(hours: int, minutes: int) -> list[str]:
"""
Return a list of strings representing an analog
clock with the passed in time
"""
time = _get_analog_time(hours, minutes)
return [
r" _____ ",
r" _.'_____'._ ",
r" .'.-' 12 '-.'. ",
r" /.' 11 1 '.\ ",
rf" // 10{time[0]} 2 \\ ",
rf":: {time[1]} ::",
rf"|| 9 {time[2]} 3 ||",
rf":: {time[3]} ::",
rf" \\ 8 {time[4]} 4 // ",
r" '. 7 5 .'/ ",
r" '.'-.__6__.-'.' ",
r" ((-._____.-)) ",
r" _)) ((_ ",
r" '--' '--' ",
]
def circular_analog(hours: int, minutes: int) -> list[str]:
"""
Return a list of strings representing a circular
analog clock with the passed in time
"""
time = _get_analog_time(hours, minutes)
return [
r" , - ~ - , ",
r" , ' 12 ' , ",
r" , 11 1 , ",
f" , 10 {time[0]} 2 , ",
f" , {time[1]} , ",
f" , 9 {time[2]} 3 , ",
f" , {time[3]} , ",
f" , 8 {time[4]} 4 , ",
r" , 7 5 , ",
r" , 6 , ' ",
r" ' - ,___ , ' ",
]
def _get_analog_time(hours: int, minutes: int) -> list[str]:
"""
Test inputs and combine hours and minutes into
ASCII representation of analog time
"""
guard_inputs(hours, minutes)
hours = to_twelve_hours(hours)
minutes = int(round_to_nearest_minutes(minutes)) // 5
return [
combine_strings(h, m)
for h, m in zip(time_to_analog(hours, True), time_to_analog(minutes, False))
]
def time_to_analog(time: int, short: bool = False) -> list[str]:
"""
Return the time (0-11) as an analog ASCII grid with
short or long hands
"""
return [
[
[
r" | ",
r" | ",
r" O ",
r" ",
r" ",
],
[
r" ",
r" | ",
r" O ",
r" ",
r" ",
],
],
[
[
r" / ",
r" / ",
r" O ",
r" ",
r" ",
],
[
r" ",
r" / ",
r" O ",
r" ",
r" ",
],
],
[
[
r" ",
r" /",
r" O-- ",
r" ",
r" ",
],
[
r" ",
r" /",
r" O ",
r" ",
r" ",
],
],
[
[
r" ",
r" ",
r" O----",
r" ",
r" ",
],
[
r" ",
r" ",
r" O-- ",
r" ",
r" ",
],
],
[
[
r" ",
r" ",
r" O-- ",
" \\",
r" ",
],
[
r" ",
r" ",
r" O ",
" \\",
r" ",
],
],
[
[
r" ",
r" ",
r" O ",
r" \ ",
r" \ ",
],
[
r" ",
r" ",
r" O ",
r" \ ",
r" ",
],
],
[
[
r" ",
r" ",
r" O ",
r" | ",
r" | ",
],
[
r" ",
r" ",
r" O ",
r" | ",
r" ",
],
],
[
[
r" ",
r" ",
r" O ",
r" / ",
r" / ",
],
[
r" ",
r" ",
r" O ",
r" / ",
r" ",
],
],
[
[
r" ",
r" ",
r" --O ",
r"/ ",
r" ",
],
[
r" ",
r" ",
r" O ",
r"/ ",
r" ",
],
],
[
[
r" ",
r" ",
r"----O ",
r" ",
r" ",
],
[
r" ",
r" ",
r" --O ",
r" ",
r" ",
],
],
[
[
r" ",
r"\ ",
r" --O ",
r" ",
r" ",
],
[
r" ",
r"\ ",
r" O ",
r" ",
r" ",
],
],
[
[
r" \ ",
r" \ ",
r" O ",
r" ",
r" ",
],
[
r" ",
r" \ ",
r" O ",
r" ",
r" ",
],
],
][0 if time == 12 else time][int(short)]
def combine_strings(hours: str, minutes: str) -> str:
"""Combine a hours string with a minutes string"""
output = ""
for h_char, m_char in zip(hours, minutes):
if h_char == m_char:
output += h_char
else:
output += chr(max(ord(h_char), ord(m_char)))
return output
def digital_watch(hours: int, minutes: int) -> list[str]:
"""
Return a list of strings representing a digital
clock with the passed in time
"""
guard_inputs(hours, minutes)
str_minutes = str(minutes).zfill(2)
hours = to_twelve_hours(hours)
a = time_to_digital(1)[1:] if len(str(hours)) == 2 else [" ", " "]
b = time_to_digital(int(str(hours)[-1]))
c = time_to_digital(int(str_minutes[0]))
d = time_to_digital(int(str_minutes[1]))
return [
r" .--.-----.--. ",
r" |--|-----|--| ",
r" |--| |--| ",
r" | |-----| | ",
r" __|--| |--|__ ",
r" / | |-----| | \ ",
r" / \__|-----|__/ \ ",
r" / ______---______ \/\ ",
r" / / \ \/ ",
rf"{{ / {b[0]} {c[0]} {d[0]} \ }} ",
rf"| {{{a[0]} {b[1]} . {c[1]} {d[1]} }} |-.",
rf"| |{a[1]} {b[2]} . {c[2]} {d[2]} | | |",
r"| { } |-'",
r"{ \ / } ",
r" \ `------___------' /\ ",
r" \ __|-----|__ /\/ ",
r" \ / |-----| \ / ",
r" \ |--| |--| / ",
r" --| |-----| |-- ",
r" |--| |--| ",
r" |--|-----|--| ",
r" '--'-----'--' ",
]
def time_to_digital(time: int) -> list[str]:
"""
Return a list of strings representing a digital number.
`time` must be an int (0-9)
"""
return [
[
" _ ",
"| |",
"|_|",
],
[
" ",
" |",
" |",
],
[
" _ ",
" _|",
"|_ ",
],
[
" _ ",
" _|",
" _|",
],
[
" ",
"|_|",
" |",
],
[
" _ ",
"|_ ",
" _|",
],
[
" _ ",
"|_ ",
"|_|",
],
[
" _ ",
" |",
" |",
],
[
" _ ",
"|_|",
"|_|",
],
[
" _ ",
"|_|",
" _|",
],
][time]
def guard_inputs(hours: int, minutes: int) -> None:
"""Raise ValueError if input is too large or too small"""
if not (0 < hours <= 24 and 0 <= minutes <= 59):
raise ValueError("Invalid time")
def to_twelve_hours(hours: int) -> int:
"""Convert a 24 hour time to a 12 hour time"""
return (hours - 12) if hours > 12 else hours
def round_to_nearest_minutes(minutes: int) -> str:
"""
Round an amount of minutes to the nearest 5
and return as a formatted string
"""
remainder = minutes % 5
return str(minutes + 5 - remainder if remainder > 2 else minutes - remainder).rjust(
2, "0"
)
def print_ascii(ascii_array: list[str]) -> None:
"""Print out an ASCII array line by line"""
for row in ascii_array:
print(row)
def ncurses_ascii(ascii_array: list[str]) -> None:
"""Output an ASCII array line by line using ncurses"""
from curses import use_default_colors, window, wrapper # pylint: disable=import-outside-toplevel
def _inner(stdscr: window):
use_default_colors()
stdscr.clear()
for row in ascii_array:
stdscr.addstr(row + "\n")
stdscr.refresh()
stdscr.getkey()
wrapper(_inner)
def main(args: str = "", xtime: tuple[int, int] = (0, 0)) -> None:
"""Parse args and display either the current time or the passed in `xtime`"""
if "h" in args:
print(
"\n".join(
md_table_to_lines(
7,
17,
f"{os.path.dirname(__file__)}/README.md",
frozenset(
"**",
),
)
)
)
return
if "x" in args:
time = xtime
args = args.replace("x", "")
else:
time = cast(tuple[int, int], tuple(map(int, datetime.now().strftime("%H %M").split())))
if "t" in args:
print(to_twelve_hours(time[0]), str(time[1]).ljust(2, "0"), sep=":")
return
if "n" in args:
display = ncurses_ascii
args = args.replace("n", "")
else:
display = print_ascii
clock = {"d": digital_watch, "c": circular_analog}.get(args, analog_clock)
display(clock(*time))
if __name__ == "__main__":
if len(sys.argv) > 3 and sys.argv[1].startswith("-"):
main(sys.argv[1][1:], (int(sys.argv[2]), int(sys.argv[3])))
elif len(sys.argv) > 1 and sys.argv[1].startswith("-"):
main(sys.argv[1][1:])
else:
main()