examples/tclock.rb (5349B) - raw
1 #!/usr/bin/env ruby
2
3 # $Id: tclock.rb,v 1.6 2002-02-28 13:50:10 t-peters Exp $
4
5 # tclock - analog/digital clock for curses, translated to ruby
6 # Copyright (C) 2002 Tobias Peters <t-peters@users.berlios.de>
7 # This file was adapted from the C program tclock.c from the ncurses
8 # distribution, which bears the following copyright statement:
9
10 # tclock - analog/digital clock for curses.
11 # If it gives you joy, then
12 # (a) I'm glad
13 # (b) you need to get out more :-)
14 #
15 # This program is copyright Howard Jones, September 1994
16 # (ha.jones@ic.ac.uk). It may be freely distributed as
17 # long as this copyright message remains intact, and any
18 # modifications are clearly marked as such. [In fact, if
19 # you modify it, I wouldn't mind the modifications back,
20 # especially if they add any nice features. A good one
21 # would be a precalc table for the 60 hand positions, so
22 # that the floating point stuff can be ditched. As I said,
23 # it was a 20 hackup minute job.]
24 #
25 # COMING SOON: tfishtank. Be the envy of your mac-owning
26 # colleagues.
27
28 ###########################################################################
29 # The translation of this program to ruby is a modification and is hereby #
30 # clearly marked as such. #
31 ###########################################################################
32
33 require "ncursesw"
34 PI = Math::PI
35
36 def sign(_x)
37 (_x<0?-1:1)
38 end
39
40 ASPECT = 2.2
41
42 def a2x(angle,radius)
43 (ASPECT * radius * Math::sin(angle)).round
44 end
45 def a2y(angle,radius)
46 (radius * Math::cos(angle)).round
47 end
48
49 # Plot a point
50 def plot(x, y, c)
51 Ncurses.mvaddch(y, x, c.ord)
52 end
53
54 # Draw a diagonal(arbitrary) line using Bresenham's alogrithm.
55 def dline(pair, from_x, from_y, x2, y2, ch)
56 if (Ncurses.has_colors?)
57 Ncurses.attrset(Ncurses.COLOR_PAIR(pair))
58 end
59
60 dx = x2 - from_x;
61 dy = y2 - from_y;
62
63 ax = (dx * 2).abs
64 ay = (dy * 2).abs
65
66 sx = sign(dx);
67 sy = sign(dy);
68
69 x = from_x;
70 y = from_y;
71
72 if (ax > ay)
73 d = ay - (ax / 2);
74
75 while (1)
76 plot(x, y, ch);
77 if (x == x2)
78 return nil
79 end
80 if (d >= 0)
81 y += sy;
82 d -= ax;
83 end
84 x += sx;
85 d += ay;
86 end
87 else
88 d = ax - (ay / 2);
89
90 while (1)
91 plot(x, y, ch);
92 if (y == y2)
93 return nil;
94 end
95 if (d >= 0)
96 x += sx;
97 d -= ay;
98 end
99 y += sy;
100 d += ax;
101 end
102 end
103 end
104
105 begin
106 # int i, cx, cy;
107 # double mradius, hradius, mangle, hangle;
108 # double sangle, sradius, hours;
109 # int hdx, hdy;
110 # int mdx, mdy;
111 # int sdx, sdy;
112 # int ch;
113
114
115 lastbeep = -1;
116 my_bg = Ncurses::COLOR_BLACK;
117
118 Ncurses::initscr();
119 Ncurses::noecho();
120 Ncurses::cbreak();
121 Ncurses::nodelay(Ncurses::stdscr, TRUE);
122 Ncurses::curs_set(0);
123
124 if (Ncurses::has_colors?())
125 Ncurses::start_color();
126
127 Ncurses::init_pair(1, Ncurses::COLOR_RED, my_bg);
128 Ncurses::init_pair(2, Ncurses::COLOR_MAGENTA, my_bg);
129 Ncurses::init_pair(3, Ncurses::COLOR_GREEN, my_bg);
130 end
131 Ncurses::keypad(Ncurses::stdscr, TRUE);
132
133 while (:restart)
134 cx = (Ncurses.COLS - 1) / 2; #/* 39 */
135 cy = Ncurses.LINES / 2; #/* 12 */
136 ch = (cx > cy) ? cy : cx; #/* usually cy */
137 mradius = ((3 * cy) / 4).to_f; #/* 9 */
138 hradius = (cy / 2).to_f; #/* 6 */
139 sradius = ((2 * cy) / 3).to_f; #/* 8 */
140
141 for i in (0...12)
142 sangle = (i + 1) * (2.0 * PI) / 12.0;
143 sradius = ((5 * cy) / 6).to_f; #/* 10 */
144 sdx = a2x(sangle, sradius);
145 sdy = a2y(sangle, sradius);
146 szChar = sprintf("%d", i + 1);
147
148 Ncurses::mvaddstr(cy - sdy, cx + sdx, szChar);
149 end
150
151 Ncurses::mvaddstr(0, 0,
152 "ASCII Clock by Howard Jones (ha.jones@ic.ac.uk),1994");
153
154 sradius -=2
155 sradius = 1 if sradius < 1
156 window_size_changed = false
157 while (window_size_changed == false)
158 sleep(0.100);
159
160 hours = Time.now.hour + Time.now.min / 60.0;
161 if (hours > 12.0)
162 hours -= 12.0;
163 end
164
165 mangle = Time.now.min * (2 * PI) / 60.0;
166 mdx = a2x(mangle, mradius);
167 mdy = a2y(mangle, mradius);
168
169 hangle = ((hours) * (2.0 * PI) / 12.0);
170 hdx = a2x(hangle, hradius);
171 hdy = a2y(hangle, hradius);
172
173 sangle = ((Time.now.sec) * (2.0 * PI) / 60.0);
174 sdx = a2x(sangle, sradius);
175 sdy = a2y(sangle, sradius);
176
177 dline(3, cx, cy, cx + mdx, cy - mdy, '#');
178
179 Ncurses::attrset(Ncurses::A_REVERSE);
180 dline(2, cx, cy, cx + hdx, cy - hdy, '.');
181 Ncurses::attroff(Ncurses::A_REVERSE);
182
183 if (Ncurses::has_colors?())
184 Ncurses::attrset(Ncurses::COLOR_PAIR(1));
185 end
186 plot(cx + sdx, cy - sdy, 'O');
187
188 if (Ncurses::has_colors?())
189 Ncurses::attrset(Ncurses::COLOR_PAIR(0));
190 end
191 Ncurses::mvaddstr(Ncurses.LINES - 2, 0, Time.now.to_s);
192 Ncurses::refresh();
193 if ((Time.now.sec % 5) == 0 &&
194 Time.now.sec != lastbeep)
195 lastbeep = Time.now.sec;
196 Ncurses::beep();
197 end
198
199 if ((ch = Ncurses::getch()) != Ncurses::ERR)
200 if (ch == Ncurses::KEY_RESIZE)
201 Ncurses::erase();
202 window_size_changed = true;
203 else
204 break;
205 end
206 end
207
208 plot(cx + sdx, cy - sdy, ' ');
209 dline(0, cx, cy, cx + hdx, cy - hdy, ' ');
210 dline(0, cx, cy, cx + mdx, cy - mdy, ' ');
211
212 end
213
214 if ! window_size_changed
215 $stderr.puts "! window_size_changed"
216 break
217 end
218 end
219 ensure
220 Ncurses::curs_set(1);
221 Ncurses::endwin();
222 end
223
224
225
226
227