1 | #!/usr/bin/env python
|
---|
2 | #
|
---|
3 | # $Id: tclock.py 46626 2006-06-03 23:07:21Z andrew.kuchling $
|
---|
4 | #
|
---|
5 | # From tclock.c, Copyright Howard Jones <ha.jones@ic.ac.uk>, September 1994.
|
---|
6 |
|
---|
7 | from math import *
|
---|
8 | import curses, time
|
---|
9 |
|
---|
10 | ASPECT = 2.2
|
---|
11 |
|
---|
12 | def sign(_x):
|
---|
13 | if _x < 0: return -1
|
---|
14 | return 1
|
---|
15 |
|
---|
16 | def A2XY(angle, radius):
|
---|
17 | return (int(round(ASPECT * radius * sin(angle))),
|
---|
18 | int(round(radius * cos(angle))))
|
---|
19 |
|
---|
20 | def plot(x, y, col):
|
---|
21 | stdscr.addch(y, x, col)
|
---|
22 |
|
---|
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))
|
---|
27 |
|
---|
28 | dx = x2 - from_x
|
---|
29 | dy = y2 - from_y
|
---|
30 |
|
---|
31 | ax = abs(dx * 2)
|
---|
32 | ay = abs(dy * 2)
|
---|
33 |
|
---|
34 | sx = sign(dx)
|
---|
35 | sy = sign(dy)
|
---|
36 |
|
---|
37 | x = from_x
|
---|
38 | y = from_y
|
---|
39 |
|
---|
40 | if ax > ay:
|
---|
41 | d = ay - ax // 2
|
---|
42 |
|
---|
43 | while True:
|
---|
44 | plot(x, y, ch)
|
---|
45 | if x == x2:
|
---|
46 | return
|
---|
47 |
|
---|
48 | if d >= 0:
|
---|
49 | y += sy
|
---|
50 | d -= ax
|
---|
51 | x += sx
|
---|
52 | d += ay
|
---|
53 | else:
|
---|
54 | d = ax - ay // 2
|
---|
55 |
|
---|
56 | while True:
|
---|
57 | plot(x, y, ch)
|
---|
58 | if y == y2:
|
---|
59 | return
|
---|
60 |
|
---|
61 | if d >= 0:
|
---|
62 | x += sx
|
---|
63 | d -= ay
|
---|
64 | y += sy
|
---|
65 | d += ax
|
---|
66 |
|
---|
67 | def main(win):
|
---|
68 | global stdscr
|
---|
69 | stdscr = win
|
---|
70 |
|
---|
71 | lastbeep = -1
|
---|
72 | my_bg = curses.COLOR_BLACK
|
---|
73 |
|
---|
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)
|
---|
81 |
|
---|
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
|
---|
88 |
|
---|
89 | for i in range(0, 12):
|
---|
90 | sangle = (i + 1) * 2.0 * pi / 12.0
|
---|
91 | sdx, sdy = A2XY(sangle, sradius)
|
---|
92 |
|
---|
93 | stdscr.addstr(cy - sdy, cx + sdx, "%d" % (i + 1))
|
---|
94 |
|
---|
95 | stdscr.addstr(0, 0,
|
---|
96 | "ASCII Clock by Howard Jones <ha.jones@ic.ac.uk>, 1994")
|
---|
97 |
|
---|
98 | sradius = max(sradius-4, 8)
|
---|
99 |
|
---|
100 | while True:
|
---|
101 | curses.napms(1000)
|
---|
102 |
|
---|
103 | tim = time.time()
|
---|
104 | t = time.localtime(tim)
|
---|
105 |
|
---|
106 | hours = t[3] + t[4] / 60.0
|
---|
107 | if hours > 12.0:
|
---|
108 | hours -= 12.0
|
---|
109 |
|
---|
110 | mangle = t[4] * 2 * pi / 60.0
|
---|
111 | mdx, mdy = A2XY(mangle, mradius)
|
---|
112 |
|
---|
113 | hangle = hours * 2 * pi / 12.0
|
---|
114 | hdx, hdy = A2XY(hangle, hradius)
|
---|
115 |
|
---|
116 | sangle = t[5] * 2 * pi / 60.0
|
---|
117 | sdx, sdy = A2XY(sangle, sradius)
|
---|
118 |
|
---|
119 | dline(3, cx, cy, cx + mdx, cy - mdy, ord('#'))
|
---|
120 |
|
---|
121 | stdscr.attrset(curses.A_REVERSE)
|
---|
122 | dline(2, cx, cy, cx + hdx, cy - hdy, ord('.'))
|
---|
123 | stdscr.attroff(curses.A_REVERSE)
|
---|
124 |
|
---|
125 | if curses.has_colors():
|
---|
126 | stdscr.attrset(curses.color_pair(1))
|
---|
127 |
|
---|
128 | plot(cx + sdx, cy - sdy, ord('O'))
|
---|
129 |
|
---|
130 | if curses.has_colors():
|
---|
131 | stdscr.attrset(curses.color_pair(0))
|
---|
132 |
|
---|
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()
|
---|
138 |
|
---|
139 | ch = stdscr.getch()
|
---|
140 | if ch == ord('q'):
|
---|
141 | return 0
|
---|
142 |
|
---|
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(' '))
|
---|
146 |
|
---|
147 | curses.wrapper(main)
|
---|