#!python # -*- mode: python; Encoding: utf-8; coding: utf-8 -*- # Last updated: <2019/09/18 14:16:28 +0900> """ display time with curses. Windows10 x64 + Python 2.7.16 32bit """ import curses import os import time from datetime import datetime import sys numpat = [ [ "#####", "# #", "# #", "# #", "#####", ], [ " #", " #", " #", " #", " #", ], [ "#####", " #", "#####", "# ", "#####", ], [ "#####", " #", "#####", " #", "#####", ], [ "# #", "# #", "#####", " #", " #", ], [ "#####", "# ", "#####", " #", "#####", ], [ "#####", "# ", "#####", "# #", "#####", ], [ "#####", " #", " #", " #", " #", ], [ "#####", "# #", "#####", "# #", "#####", ], [ "#####", "# #", "#####", " #", "#####", ], [ " ", " # ", " ", " # ", " ", ], [ " ", " ", "#####", " ", " ", ], ] def draw_number(win, y, x, n): """Draw 1 character.""" dispchr = curses.ACS_CKBOARD # dispchr = curses.ACS_BLOCK lst = numpat[n] for i, s in enumerate(lst): for j, c in enumerate(s): cc = " " if c == " " else dispchr win.addch(y + i, x + j, cc) def draw_numbers(win, y, x, s): """Draw large string.""" for c in s: n = 0 if c == ":": n = 10 elif c == "-": n = 11 else: n = int(c) draw_number(win, y, x, n) x += 7 def get_hms(t): """Get HH:MM:SS.""" h = int(t / 3600) m = int((t / 60) % 60) s = int(t % 60) return h, m, s def win_main(win): """Main.""" curses.curs_set(0) win.nodelay(True) win.clear() # win.border() tm = time.time() while True: height, width = win.getmaxyx() win.addstr(1, 2, startmsg) win.addstr(1, width - 8, "Q)uit") n = time.time() win.addstr(2, 2, "%s" % datetime.fromtimestamp(n)) d = n - tm if d >= 1.0: # # Please write your action. # win.addstr(3, 2, "action") tm = time.time() elif d >= 0.5: # win.move(3, 2) # win.clrtoeol() win.addstr(3, 2, " ") try: key = win.getkey() if key == os.linesep or key == "q" or ord(key) == 27: break except Exception as e: # No input pass w = (len(numpat[0][0]) + 2) * 8 x0 = int((width - w) / 2) h, m, s = get_hms(n - starttime) ss = "%02d:%02d:%02d" % (h, m, s) draw_numbers(win, 5, x0, ss) time.sleep(0.25) starttime = time.time() startmsg = "%s - start" % datetime.fromtimestamp(starttime) curses.wrapper(win_main) # print result print(startmsg) endtime = time.time() t = "%s" % datetime.fromtimestamp(endtime) print("%s - end" % t) h, m, s = get_hms(endtime - starttime) print("%d:%02d:%02d" % (h, m, s)) sys.exit()