blob: f423e9a54be45db5d38cd4d5da16eaa986f21f33 [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
Andrew M. Kuchling994d8532000-12-23 14:50:18 +000083 ch = min( cy-1, int(cx / ASPECT) - 1)
84 mradius = (3 * ch) / 4
85 hradius = ch / 2
86 sradius = 5 * ch / 6
Andrew M. Kuchling15c3c2b2000-12-15 00:41:48 +000087
88 for i in range(0, 12):
89 sangle = (i + 1) * 2.0 * pi / 12.0
Andrew M. Kuchling15c3c2b2000-12-15 00:41:48 +000090 sdx, sdy = A2XY(sangle, sradius)
91
92 stdscr.addstr(cy - sdy, cx + sdx, "%d" % (i + 1))
93
94 stdscr.addstr(0, 0,
95 "ASCII Clock by Howard Jones <ha.jones@ic.ac.uk>, 1994")
96
Andrew M. Kuchling994d8532000-12-23 14:50:18 +000097 sradius = max(sradius-4, 8)
98
Andrew M. Kuchling15c3c2b2000-12-15 00:41:48 +000099 while 1:
100 curses.napms(1000)
101
102 tim = time.time()
103 t = time.localtime(tim)
104
105 hours = t[3] + t[4] / 60.0
106 if hours > 12.0:
107 hours -= 12.0
108
109 mangle = t[4] * 2 * pi / 60.0
110 mdx, mdy = A2XY(mangle, mradius)
111
112 hangle = hours * 2 * pi / 12.0
113 hdx, hdy = A2XY(hangle, hradius)
114
115 sangle = t[5] * 2 * pi / 60.0
116 sdx, sdy = A2XY(sangle, sradius)
117
118 dline(3, cx, cy, cx + mdx, cy - mdy, ord('#'))
119
120 stdscr.attrset(curses.A_REVERSE)
121 dline(2, cx, cy, cx + hdx, cy - hdy, ord('.'))
122 stdscr.attroff(curses.A_REVERSE)
123
124 if curses.has_colors():
125 stdscr.attrset(curses.color_pair(1))
126
127 plot(cx + sdx, cy - sdy, ord('O'))
128
129 if curses.has_colors():
130 stdscr.attrset(curses.color_pair(0))
131
132 stdscr.addstr(curses.LINES - 2, 0, time.ctime(tim))
133 stdscr.refresh()
134 if (t[5] % 5) == 0 and t[5] != lastbeep:
135 lastbeep = t[5]
136 curses.beep()
137
138 ch = stdscr.getch()
139 if ch == ord('q'):
140 return 0
141
142 plot(cx + sdx, cy - sdy, ord(' '))
143 dline(0, cx, cy, cx + hdx, cy - hdy, ord(' '))
144 dline(0, cx, cy, cx + mdx, cy - mdy, ord(' '))
145
146curses.wrapper(main)