fancy clock
This commit is contained in:
244
bin/clockcurses
244
bin/clockcurses
@@ -1,151 +1,173 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import curses
|
import curses
|
||||||
import os
|
import datetime
|
||||||
import sys
|
|
||||||
import time,datetime
|
|
||||||
import math
|
import math
|
||||||
|
import os
|
||||||
import signal
|
import signal
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
|
||||||
|
ascii_digits = {
|
||||||
|
"0": "0000 00 00 0000",
|
||||||
|
"1": " 1" * 5,
|
||||||
|
"2": "222 22222 222",
|
||||||
|
"3": "333 3 33 3333",
|
||||||
|
"4": "4 4 4444 4 4",
|
||||||
|
"5": "5555 555 5555",
|
||||||
|
"6": "66 6 6666 6666",
|
||||||
|
"7": "777 7 7 7 7",
|
||||||
|
"8": "8888 88888 8888",
|
||||||
|
"9": "9999 9999 9 99",
|
||||||
|
":": " . . ",
|
||||||
|
}
|
||||||
|
|
||||||
|
fancy_digits = {
|
||||||
|
"0": "╒═╕│ ││ ││ │╘═╛",
|
||||||
|
"1": " ╕ │ │ │ ╧ ",
|
||||||
|
"2": "╒═╕ │╒═╛│ ╘═╛",
|
||||||
|
"3": "╒═╕ │ ═╡ │╘═╛",
|
||||||
|
"4": "╤ │ │╘═╡ │ ╧",
|
||||||
|
"5": "╒═╕│ ╘═╕ │╘═╛",
|
||||||
|
"6": "╒╕ │ ╞═╕│ │╘═╛",
|
||||||
|
"7": "╒═╕ │ ╪ │ ╛",
|
||||||
|
"8": "╒═╕│ │╞═╡│ │╘═╛",
|
||||||
|
"9": "╒═╕│ │╘═╡ │ ╘╛",
|
||||||
|
":": " ° ° ",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def termsize():
|
def termsize():
|
||||||
rows, columns = os.popen('stty size', 'r').read().split()
|
rows, columns = os.popen("stty size", "r").read().split()
|
||||||
return (int(rows),int(columns))
|
return (int(rows), int(columns))
|
||||||
|
|
||||||
def centerpoint(ry,rx):
|
|
||||||
r=int(round(min(ry/1.5,rx/1.5))-1)
|
|
||||||
return (int(round(ry/2)),int(round(rx/2)),r)
|
|
||||||
|
|
||||||
def saddstr(win,y,x,s):
|
def centerpoint(ry, rx):
|
||||||
|
r = int(round(min(ry / 1.5, rx / 1.5)) - 1)
|
||||||
|
return (int(round(ry / 2)), int(round(rx / 2)), r)
|
||||||
|
|
||||||
|
|
||||||
|
def saddstr(win, y, x, s):
|
||||||
try:
|
try:
|
||||||
win.addstr(y,x,s)
|
win.addstr(y, x, s)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def drawcircle(win,cy,cx,r):
|
|
||||||
precision=360
|
def drawcircle(win, cy, cx, r):
|
||||||
|
precision = 360
|
||||||
for a in range(precision):
|
for a in range(precision):
|
||||||
alpha=2.0*math.pi*a/precision
|
alpha = 2.0 * math.pi * a / precision
|
||||||
dy=int(round(cy-float(r)*math.cos(alpha)))
|
dy = int(round(cy - float(r) * math.cos(alpha)))
|
||||||
dx=int(round(cx+2.0*float(r)*math.sin(alpha)))
|
dx = int(round(cx + 2.0 * float(r) * math.sin(alpha)))
|
||||||
saddstr(win,dy,dx,'.')
|
saddstr(win, dy, dx, ".")
|
||||||
return
|
return
|
||||||
|
|
||||||
def drawline(win,cy,cx,a,s,r,char):
|
|
||||||
prec=2
|
def drawline(win, cy, cx, a, s, r, char):
|
||||||
for l in range(int(r*prec)):
|
prec = 2
|
||||||
if l>s:
|
for l in range(int(r * prec)):
|
||||||
ly=int(round(cy-float(l)*math.cos(a)/prec))
|
if l > s:
|
||||||
lx=int(round(cx+2.0*float(l)*math.sin(a)/prec))
|
ly = int(round(cy - float(l) * math.cos(a) / prec))
|
||||||
saddstr(win,ly,lx,char)
|
lx = int(round(cx + 2.0 * float(l) * math.sin(a) / prec))
|
||||||
|
saddstr(win, ly, lx, char)
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
def drawdigit(win,y,x,d):
|
|
||||||
s=' '
|
|
||||||
if d=='0':
|
|
||||||
s='0000 00 00 0000'
|
|
||||||
if d=='1':
|
|
||||||
s=' 1'*5
|
|
||||||
if d=='2':
|
|
||||||
s='222 22222 222'
|
|
||||||
if d=='3':
|
|
||||||
s='333 3 33 3333'
|
|
||||||
if d=='4':
|
|
||||||
s='4 4 4444 4 4'
|
|
||||||
if d=='5':
|
|
||||||
s='5555 555 5555'
|
|
||||||
if d=='6':
|
|
||||||
s='66 6 6666 6666'
|
|
||||||
if d=='7':
|
|
||||||
s='777 7 7 7 7'
|
|
||||||
if d=='8':
|
|
||||||
s='8888 88888 8888'
|
|
||||||
if d=='9':
|
|
||||||
s='9999 9999 9 99'
|
|
||||||
if d==':':
|
|
||||||
s=' . . '
|
|
||||||
drawsplitstr(win,y,x,s)
|
|
||||||
return
|
|
||||||
|
|
||||||
def drawsplitstr(win,y,x,st):
|
def drawdigit(win, y, x, d, ascii=False):
|
||||||
ls=list(st)
|
s = " "
|
||||||
for r in range(5):
|
if ascii:
|
||||||
rs=3*r
|
s = ascii_digits.get(d, s)
|
||||||
saddstr(win,y+r,x,ls[rs]+ls[rs+1]+ls[rs+2])
|
|
||||||
return
|
|
||||||
|
|
||||||
def drawdigital(win,y,x,t):
|
|
||||||
if options.seconds:
|
|
||||||
if t.tm_sec&1:
|
|
||||||
hrs=list('%02d:%02d:%02d' % (t.tm_hour, t.tm_min, t.tm_sec))
|
|
||||||
else:
|
|
||||||
hrs=list('%02d %02d %02d' % (t.tm_hour, t.tm_min, t.tm_sec))
|
|
||||||
else:
|
else:
|
||||||
if t.tm_sec&1:
|
s = fancy_digits.get(d, s)
|
||||||
hrs=list('%02d:%02d' % (t.tm_hour, t.tm_min))
|
drawsplitstr(win, y, x, s)
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def drawsplitstr(win, y, x, st):
|
||||||
|
ls = list(st)
|
||||||
|
for r in range(5):
|
||||||
|
rs = 3 * r
|
||||||
|
saddstr(win, y + r, x, ls[rs] + ls[rs + 1] + ls[rs + 2])
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def drawdigital(win, y, x, t, ascii=False):
|
||||||
|
if options.seconds:
|
||||||
|
if t.tm_sec & 1:
|
||||||
|
hrs = list("%02d:%02d:%02d" % (t.tm_hour, t.tm_min, t.tm_sec))
|
||||||
else:
|
else:
|
||||||
hrs=list('%02d %02d' % (t.tm_hour, t.tm_min))
|
hrs = list("%02d %02d %02d" % (t.tm_hour, t.tm_min, t.tm_sec))
|
||||||
|
else:
|
||||||
|
if t.tm_sec & 1:
|
||||||
|
hrs = list("%02d:%02d" % (t.tm_hour, t.tm_min))
|
||||||
|
else:
|
||||||
|
hrs = list("%02d %02d" % (t.tm_hour, t.tm_min))
|
||||||
for c in range(len(hrs)):
|
for c in range(len(hrs)):
|
||||||
drawdigit(win,y,x+4*c,hrs[c])
|
drawdigit(win, y, x + 4 * c, hrs[c], ascii=ascii)
|
||||||
|
|
||||||
|
|
||||||
def readinput(win):
|
def readinput(win):
|
||||||
try:
|
try:
|
||||||
input=win.getch()
|
input = win.getch()
|
||||||
if input in [ord(x) for x in ['x','X','q','Q']]:
|
if input in [ord(x) for x in ["x", "X", "q", "Q"]]:
|
||||||
return "x"
|
return "x"
|
||||||
except:
|
except:
|
||||||
return ""
|
return ""
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|
||||||
class timer_struct:
|
class timer_struct:
|
||||||
""" Class for storing timer. """
|
"""Class for storing timer."""
|
||||||
def __init__(self,h,m,s):
|
|
||||||
self.tm_hour=int(h)
|
def __init__(self, h, m, s):
|
||||||
self.tm_min=int(m)
|
self.tm_hour = int(h)
|
||||||
self.tm_sec=int(s)
|
self.tm_min = int(m)
|
||||||
|
self.tm_sec = int(s)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
stdscr=curses.initscr()
|
stdscr = curses.initscr()
|
||||||
curses.curs_set(0)
|
curses.curs_set(0)
|
||||||
start_t=time.time()
|
start_t = time.time()
|
||||||
try:
|
try:
|
||||||
#rows,columns = termsize()
|
# rows,columns = termsize()
|
||||||
while (1):
|
while 1:
|
||||||
rows,columns = stdscr.getmaxyx()
|
rows, columns = stdscr.getmaxyx()
|
||||||
cy,cx,r = centerpoint(rows,columns)
|
cy, cx, r = centerpoint(rows, columns)
|
||||||
stdscr.clear()
|
stdscr.clear()
|
||||||
t=time.localtime()
|
t = time.localtime()
|
||||||
if options.timer:
|
if options.timer:
|
||||||
t_new=time.time()-start_t
|
t_new = time.time() - start_t
|
||||||
t_m, t_s = divmod(t_new, 60)
|
t_m, t_s = divmod(t_new, 60)
|
||||||
t_h, t_m = divmod(t_m, 60)
|
t_h, t_m = divmod(t_m, 60)
|
||||||
else:
|
else:
|
||||||
t_s=float(t.tm_sec)
|
t_s = float(t.tm_sec)
|
||||||
t_m=float(t.tm_min)
|
t_m = float(t.tm_min)
|
||||||
t_h=float(t.tm_hour)
|
t_h = float(t.tm_hour)
|
||||||
alphas=math.pi*t_s/30.0
|
alphas = math.pi * t_s / 30.0
|
||||||
alpham=math.pi*t_m/30.0 + alphas/60.0
|
alpham = math.pi * t_m / 30.0 + alphas / 60.0
|
||||||
alphah=math.pi*t_h/6.0 + alpham/12.0
|
alphah = math.pi * t_h / 6.0 + alpham / 12.0
|
||||||
if options.seconds:
|
if options.seconds:
|
||||||
drawline(stdscr,cy,cx,alphas,1,r/2,'s')
|
drawline(stdscr, cy, cx, alphas, 1, r / 2, "s")
|
||||||
drawline(stdscr,cy,cx,alpham,1,int(round(r*0.8)/2),'m')
|
drawline(stdscr, cy, cx, alpham, 1, int(round(r * 0.8) / 2), "m")
|
||||||
drawline(stdscr,cy,cx,alphah,1,int(round(r*0.5)/2),'HH')
|
drawline(stdscr, cy, cx, alphah, 1, int(round(r * 0.5) / 2), "HH")
|
||||||
stdscr.addstr(cy,cx,'o')
|
stdscr.addstr(cy, cx, "o")
|
||||||
drawcircle(stdscr,cy,cx,r/2)
|
drawcircle(stdscr, cy, cx, r / 2)
|
||||||
for h in range(12):
|
for h in range(12):
|
||||||
drawline(stdscr,cy,cx,math.pi*h/6.0,r,1+r/2,'O')
|
drawline(stdscr, cy, cx, math.pi * h / 6.0, r, 1 + r / 2, "O")
|
||||||
if options.timer:
|
if options.timer:
|
||||||
drawdigital(stdscr,1,1,timer_struct(t_h,t_m,t_s))
|
drawdigital(stdscr, 1, 1, timer_struct(t_h, t_m, t_s), options.ascii)
|
||||||
drawdigital(stdscr,7,1,t)
|
drawdigital(stdscr, 7, 1, t, options.ascii)
|
||||||
else:
|
else:
|
||||||
drawdigital(stdscr,1,1,t)
|
drawdigital(stdscr, 1, 1, t, options.ascii)
|
||||||
stdscr.refresh()
|
stdscr.refresh()
|
||||||
#time.sleep(options.refresh)
|
# time.sleep(options.refresh)
|
||||||
signal.signal(signal.SIGALRM, readinput)
|
signal.signal(signal.SIGALRM, readinput)
|
||||||
signal.alarm(int(options.refresh))
|
signal.alarm(int(options.refresh))
|
||||||
userinput=readinput(stdscr)
|
userinput = readinput(stdscr)
|
||||||
if userinput=="x":
|
if userinput == "x":
|
||||||
curses.nocbreak()
|
curses.nocbreak()
|
||||||
stdscr.keypad(0)
|
stdscr.keypad(0)
|
||||||
curses.endwin()
|
curses.endwin()
|
||||||
@@ -154,23 +176,23 @@ def main():
|
|||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
curses.nocbreak()
|
curses.nocbreak()
|
||||||
stdscr.keypad(0)
|
stdscr.keypad(0)
|
||||||
#curses.echo()
|
# curses.echo()
|
||||||
|
|
||||||
curses.endwin()
|
curses.endwin()
|
||||||
|
|
||||||
usage='''Usage: %prog [options]
|
|
||||||
|
usage = """Usage: %prog [options]
|
||||||
|
|
||||||
Display a clockface
|
Display a clockface
|
||||||
'''
|
"""
|
||||||
parser=OptionParser(usage=usage)
|
parser = OptionParser(usage=usage)
|
||||||
parser.add_option("-s",action="store_true",dest="seconds",default=False,
|
parser.add_option("-s", action="store_true", dest="seconds", default=False, help="Show seconds [%default]")
|
||||||
help="Show seconds [%default]")
|
parser.add_option("-r", type="int", dest="refresh", default=3, help="Refresh rate in seconds [%default]")
|
||||||
parser.add_option("-r",type="int",dest="refresh",default=3,
|
parser.add_option(
|
||||||
help="Refresh rate in seconds [%default]")
|
"-t", action="store_true", dest="timer", default=False, help="Timer instead of current time [%default]"
|
||||||
parser.add_option("-t",action="store_true",dest="timer",default=False,
|
)
|
||||||
help="Timer instead of current time [%default]")
|
parser.add_option("-a", action="store_true", dest="ascii", default=False, help="Plain ascii characters only [%default]")
|
||||||
global options
|
global options
|
||||||
(options,args)=parser.parse_args()
|
(options, args) = parser.parse_args()
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user