Andrew M. Kuchling | 15c3c2b | 2000-12-15 00:41:48 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # $Id$ |
| 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))), int(round(radius * cos(angle))) |
| 18 | |
| 19 | def plot(x, y, col): |
| 20 | stdscr.addch(y, x, col) |
| 21 | |
| 22 | # draw a diagonal line using Bresenham's algorithm |
| 23 | def 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 | |
| 66 | def main(win): |
| 67 | global stdscr |
| 68 | stdscr = win |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 69 | |
Andrew M. Kuchling | 15c3c2b | 2000-12-15 00:41:48 +0000 | [diff] [blame] | 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 |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 83 | ch = min( cy-1, int(cx / ASPECT) - 1) |
Andrew M. Kuchling | 994d853 | 2000-12-23 14:50:18 +0000 | [diff] [blame] | 84 | mradius = (3 * ch) / 4 |
| 85 | hradius = ch / 2 |
| 86 | sradius = 5 * ch / 6 |
Andrew M. Kuchling | 15c3c2b | 2000-12-15 00:41:48 +0000 | [diff] [blame] | 87 | |
| 88 | for i in range(0, 12): |
| 89 | sangle = (i + 1) * 2.0 * pi / 12.0 |
Andrew M. Kuchling | 15c3c2b | 2000-12-15 00:41:48 +0000 | [diff] [blame] | 90 | 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. Kuchling | 994d853 | 2000-12-23 14:50:18 +0000 | [diff] [blame] | 97 | sradius = max(sradius-4, 8) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 98 | |
Andrew M. Kuchling | 15c3c2b | 2000-12-15 00:41:48 +0000 | [diff] [blame] | 99 | 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')) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 128 | |
Andrew M. Kuchling | 15c3c2b | 2000-12-15 00:41:48 +0000 | [diff] [blame] | 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 | |
| 146 | curses.wrapper(main) |