Files

64 lines
2.5 KiB
Python
Executable File

#!/usr/bin/env python3
# clock - big ASCII clock, live, with a duration limit
# Usage: clock [--duration SECONDS] [--date] [--no-clear]
import argparse, datetime, sys, time
DIGITS = {
'0': (" ███ ", "█ █", "█ █", "█ █", " ███ "),
'1': (" █ ", " ██ ", " █ ", " █ ", " ███ "),
'2': (" ███ ", " █", " ███ ", "█ ", " ███ "),
'3': (" ███ ", " █", " ███ ", " █", " ███ "),
'4': ("█ █", "█ █", " ████", " █", " █"),
'5': (" ████", "█ ", " ███ ", " █", " ███ "),
'6': (" ███ ", "█ ", " ███ ", "█ █", " ███ "),
'7': ("█████", " █", " █ ", " █ ", " █ "),
'8': (" ███ ", "█ █", " ███ ", "█ █", " ███ "),
'9': (" ███ ", "█ █", " ████", " █", " ███ "),
':': (" ", " █ ", " ", " █ ", " "),
' ': (" ", " ", " ", " ", " "),
}
def render(hms):
rows = [""] * 5
for ch in hms:
glyph = DIGITS.get(ch, DIGITS[" "])
for i in range(5):
rows[i] += glyph[i] + " "
return "\n".join(rows)
def selftest():
out = render("12:34:56")
assert out.count("\n") == 4 and "█" in out
assert render("00:00:00")
now = datetime.datetime.now().strftime("%H:%M:%S")
assert len(render(now).splitlines()) == 5
print(render("12:34:56"))
print("clock selftest OK")
def main():
p = argparse.ArgumentParser(description="Big ASCII clock, live, with a duration limit.")
p.add_argument("--duration", type=float, default=5, help="seconds to run (default 5)")
p.add_argument("--date", action="store_true", help="also show the date")
p.add_argument("--no-clear", action="store_true", help="do not clear the screen each second")
p.add_argument("--selftest", action="store_true", help="run a built-in self-test and exit")
a = p.parse_args()
if a.selftest:
selftest(); return 0
end = time.time() + max(0.0, a.duration)
while True:
now = datetime.datetime.now()
if not a.no_clear:
print("\033[2J\033[H", end="")
print(render(now.strftime("%H:%M:%S")))
if a.date:
print(now.strftime("%A, %B %d, %Y"))
if time.time() >= end:
break
time.sleep(1)
if time.time() >= end:
break
return 0
if __name__ == "__main__":
sys.exit(main())