Fixed issue-number mistake in NEWS update.
[python.git] / Demo / curses / tclock.py
blob8058d9a42089ce2852ebfe5417eae729eb2ff4a2
1 #!/usr/bin/env python
3 # $Id$
5 # From tclock.c, Copyright Howard Jones <ha.jones@ic.ac.uk>, September 1994.
7 from math import *
8 import curses, time
10 ASPECT = 2.2
12 def sign(_x):
13 if _x < 0: return -1
14 return 1
16 def A2XY(angle, radius):
17 return (int(round(ASPECT * radius * sin(angle))),
18 int(round(radius * cos(angle))))
20 def plot(x, y, col):
21 stdscr.addch(y, x, col)
23 # draw a diagonal line using Bresenham's algorithm
24 def dline(pair, from_x, from_y, x2, y2, ch):
25 if curses.has_colors():
26 stdscr.attrset(curses.color_pair(pair))
28 dx = x2 - from_x
29 dy = y2 - from_y
31 ax = abs(dx * 2)
32 ay = abs(dy * 2)
34 sx = sign(dx)
35 sy = sign(dy)
37 x = from_x
38 y = from_y
40 if ax > ay:
41 d = ay - ax // 2
43 while True:
44 plot(x, y, ch)
45 if x == x2:
46 return
48 if d >= 0:
49 y += sy
50 d -= ax
51 x += sx
52 d += ay
53 else:
54 d = ax - ay // 2
56 while True:
57 plot(x, y, ch)
58 if y == y2:
59 return
61 if d >= 0:
62 x += sx
63 d -= ay
64 y += sy
65 d += ax
67 def main(win):
68 global stdscr
69 stdscr = win
71 lastbeep = -1
72 my_bg = curses.COLOR_BLACK
74 stdscr.nodelay(1)
75 stdscr.timeout(0)
76 # curses.curs_set(0)
77 if curses.has_colors():
78 curses.init_pair(1, curses.COLOR_RED, my_bg)
79 curses.init_pair(2, curses.COLOR_MAGENTA, my_bg)
80 curses.init_pair(3, curses.COLOR_GREEN, my_bg)
82 cx = (curses.COLS - 1) // 2
83 cy = curses.LINES // 2
84 ch = min( cy-1, int(cx // ASPECT) - 1)
85 mradius = (3 * ch) // 4
86 hradius = ch // 2
87 sradius = 5 * ch // 6
89 for i in range(0, 12):
90 sangle = (i + 1) * 2.0 * pi / 12.0
91 sdx, sdy = A2XY(sangle, sradius)
93 stdscr.addstr(cy - sdy, cx + sdx, "%d" % (i + 1))
95 stdscr.addstr(0, 0,
96 "ASCII Clock by Howard Jones <ha.jones@ic.ac.uk>, 1994")
98 sradius = max(sradius-4, 8)
100 while True:
101 curses.napms(1000)
103 tim = time.time()
104 t = time.localtime(tim)
106 hours = t[3] + t[4] / 60.0
107 if hours > 12.0:
108 hours -= 12.0
110 mangle = t[4] * 2 * pi / 60.0
111 mdx, mdy = A2XY(mangle, mradius)
113 hangle = hours * 2 * pi / 12.0
114 hdx, hdy = A2XY(hangle, hradius)
116 sangle = t[5] * 2 * pi / 60.0
117 sdx, sdy = A2XY(sangle, sradius)
119 dline(3, cx, cy, cx + mdx, cy - mdy, ord('#'))
121 stdscr.attrset(curses.A_REVERSE)
122 dline(2, cx, cy, cx + hdx, cy - hdy, ord('.'))
123 stdscr.attroff(curses.A_REVERSE)
125 if curses.has_colors():
126 stdscr.attrset(curses.color_pair(1))
128 plot(cx + sdx, cy - sdy, ord('O'))
130 if curses.has_colors():
131 stdscr.attrset(curses.color_pair(0))
133 stdscr.addstr(curses.LINES - 2, 0, time.ctime(tim))
134 stdscr.refresh()
135 if (t[5] % 5) == 0 and t[5] != lastbeep:
136 lastbeep = t[5]
137 curses.beep()
139 ch = stdscr.getch()
140 if ch == ord('q'):
141 return 0
143 plot(cx + sdx, cy - sdy, ord(' '))
144 dline(0, cx, cy, cx + hdx, cy - hdy, ord(' '))
145 dline(0, cx, cy, cx + mdx, cy - mdy, ord(' '))
147 curses.wrapper(main)