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