Files
q-tools/clockcurses.py
2013-07-16 18:38:01 +03:00

147 lines
3.8 KiB
Python
Executable File

#!/usr/bin/python
import curses
import os,sys
import time
import math
import signal
from optparse import OptionParser
def termsize():
rows, columns = os.popen('stty size', 'r').read().split()
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):
try:
win.addstr(y,x,s)
except:
pass
def drawcircle(win,cy,cx,r):
precision=360
for a in range(precision):
alpha=2.0*math.pi*a/precision
dy=int(round(cy-float(r)*math.cos(alpha)))
dx=int(round(cx+2.0*float(r)*math.sin(alpha)))
saddstr(win,dy,dx,'.')
return
def drawline(win,cy,cx,a,r,char):
prec=2
for l in range(r*prec):
if l>1:
ly=int(round(cy-float(l)*math.cos(a)/prec))
lx=int(round(cx+1.8*float(l)*math.sin(a)/prec))
saddstr(win,ly,lx,char)
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):
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):
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:
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)):
drawdigit(win,y,x+4*c,hrs[c])
def readinput(win):
try:
return win.getch()
except:
return ""
def main():
stdscr=curses.initscr()
curses.curs_set(0)
try:
#rows,columns = termsize()
while (1):
rows,columns = stdscr.getmaxyx()
cy,cx,r = centerpoint(rows,columns)
stdscr.clear()
t=time.localtime()
alphas=math.pi*float(t.tm_sec)/30.0
alpham=math.pi*float(t.tm_min)/30.0 + alphas/60.0
alphah=math.pi*float(t.tm_hour)/6.0 + alpham/12.0
if options.seconds:
drawline(stdscr,cy,cx,alphas,r/2,'s')
drawline(stdscr,cy,cx,alpham,int(round(r*0.8)/2),'m')
drawline(stdscr,cy,cx,alphah,int(round(r*0.5)/2),'HH')
stdscr.addstr(cy,cx,'o')
drawcircle(stdscr,cy,cx,r/2)
drawdigital(stdscr,1,1,t)
stdscr.refresh()
#time.sleep(options.refresh)
signal.signal(signal.SIGALRM, readinput)
signal.alarm(int(options.refresh))
userinput=readinput(stdscr)
if userinput!="":
curses.nocbreak()
stdscr.keypad(0)
curses.endwin()
sys.exit(0)
except KeyboardInterrupt:
curses.nocbreak()
stdscr.keypad(0)
#curses.echo()
curses.endwin()
usage='''Usage: %prog [options]
'''
parser=OptionParser(usage=usage)
parser.add_option("-s",action="store_true",dest="seconds",default=False,
help="Show seconds [%default]")
parser.add_option("-r",type="int",dest="refresh",default=3,
help="Refresh rate in seconds [%default]")
global options
(options,args)=parser.parse_args()
main()