ncurses 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+ # Emacs: This is -*- ruby -*- code!
3
+ #
4
+ # Unfinished read_line function
5
+ #
6
+ # Written 2003, 2004 by Tobias Peters
7
+ # No warranties
8
+ # Share and enjoy!
9
+
10
+ require "ncurses"
11
+
12
+ # read_line returns an array
13
+ # [string, last_cursor_position_in_string, keycode_of_terminating_enter_key].
14
+ # Complete the "when" clauses before including in your app!
15
+ def read_line(y, x,
16
+ window = Ncurses.stdscr,
17
+ max_len = (window.getmaxx - x - 1),
18
+ string = "",
19
+ cursor_pos = 0)
20
+ loop do
21
+ window.mvaddstr(y,x,string)
22
+ window.move(y,x+cursor_pos)
23
+ ch = window.getch
24
+ case ch
25
+ when Ncurses::KEY_LEFT
26
+ cursor_pos = [0, cursor_pos-1].max
27
+ when Ncurses::KEY_RIGHT
28
+ # similar, implement yourself !
29
+ when Ncurses::KEY_ENTER, ?\n, ?\r
30
+ return string, cursor_pos, ch # Which return key has been used?
31
+ when Ncurses::KEY_BACKSPACE
32
+ string = string[0...([0, cursor_pos-1].max)] + string[cursor_pos..-1]
33
+ cursor_pos = [0, cursor_pos-1].max
34
+ window.mvaddstr(y, x+string.length, " ")
35
+ # when etc...
36
+ when " "[0]..255 # remaining printables
37
+ if (cursor_pos < max_len)
38
+ string[cursor_pos,0] = ch.chr
39
+ cursor_pos += 1
40
+ else
41
+ Ncurses.beep
42
+ end
43
+ else
44
+ Ncurses.beep
45
+ end
46
+ end
47
+ end
48
+
49
+ if (__FILE__ == $0) then begin
50
+ # demo mode
51
+ Ncurses.initscr
52
+ Ncurses.cbreak
53
+ Ncurses.noecho
54
+
55
+ # recognize KEY_ENTER, KEY_BACKSPACE etc
56
+ Ncurses.keypad(Ncurses.stdscr, true)
57
+
58
+ y = 10
59
+ x = 2
60
+ prompt = "Hallo > "
61
+ Ncurses.mvaddstr(y,x, prompt)
62
+ s = read_line(y, x + prompt.length)
63
+
64
+ ensure
65
+ Ncurses.endwin
66
+ end end
67
+ p s
@@ -0,0 +1,227 @@
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 "ncurses"
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[0])
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
+
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # $Id: test_scanw.rb,v 1.1 2003/03/22 22:55:00 t-peters Exp $
4
+ #
5
+ # Test of the scanw function. Should really not be used in any serious curses
6
+ # program. To use it, install scanf for ruby.
7
+
8
+ # Copyright (C) 2003 Tobias Peters <t-peters@users.berlios.de>
9
+ #
10
+ # No warranties. Share and enjoy.
11
+
12
+ require "ncurses"
13
+ begin
14
+ Ncurses.initscr
15
+ Ncurses.mvaddstr(4, 19, "Give me a number: ")
16
+ Ncurses.refresh
17
+ num = []
18
+ Ncurses.scanw("%d", num)
19
+
20
+ Ncurses.mvprintw(6, 19, "You gave: %d", num[0])
21
+ Ncurses.refresh
22
+ sleep 1
23
+ ensure
24
+ Ncurses.endwin
25
+ end
26
+
27
+ puts("You gave: #{num[0]}")
@@ -0,0 +1,130 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # ncurses-ruby is a ruby module for accessing the FSF's ncurses library
4
+ # (C) 2002 Tobias Peters <t-peters@users.berlios.de>
5
+ #
6
+ # This module is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2 of the License, or (at your option) any later version.
10
+ #
11
+ # This module is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this module; if not, write to the Free Software
18
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
+
20
+ # $Id: extconf.rb,v 1.10 2004/05/13 21:54:49 t-peters Exp $
21
+
22
+ require "mkmf"
23
+
24
+ $CFLAGS += " -g"
25
+ $CXXFLAGS = $CFLAGS
26
+
27
+ have_header("unistd.h")
28
+ if have_header("ncurses.h")
29
+ curses_header = "ncurses.h"
30
+ elsif have_header("ncurses/curses.h")
31
+ curses_header = "ncurses/curses.h"
32
+ elsif have_header("curses.h")
33
+ curses_header = "curses.h"
34
+ else
35
+ raise "ncurses header file not found"
36
+ end
37
+
38
+ if have_library("ncurses", "wmove")
39
+ curses_lib = "ncurses"
40
+ elsif have_library("pdcurses", "wmove")
41
+ curses_lib = "pdcurses"
42
+ else
43
+ raise "ncurses library not found"
44
+ end
45
+
46
+ have_func("newscr")
47
+ have_func("TABSIZE")
48
+ have_func("ESCDELAY")
49
+ have_func("keybound")
50
+ have_func("curses_version")
51
+ have_func("tigetstr")
52
+ have_func("getwin")
53
+ have_func("putwin")
54
+ have_func("ungetmouse")
55
+ have_func("mousemask")
56
+ have_func("wenclose")
57
+ have_func("mouseinterval")
58
+ have_func("wmouse_trafo")
59
+ have_func("mcprint")
60
+ have_func("has_key")
61
+
62
+ have_func("delscreen")
63
+ have_func("define_key")
64
+ have_func("keyok")
65
+ have_func("resizeterm")
66
+ have_func("use_default_colors")
67
+ have_func("use_extended_names")
68
+ have_func("wresize")
69
+ have_func("attr_on")
70
+ have_func("attr_off")
71
+ have_func("attr_set")
72
+ have_func("chgat")
73
+ have_func("color_set")
74
+ have_func("filter")
75
+ have_func("intrflush")
76
+ have_func("mvchgat")
77
+ have_func("mvhline")
78
+ have_func("mvvline")
79
+ have_func("mvwchgat")
80
+ have_func("mvwhline")
81
+ have_func("mvwvline")
82
+ have_func("noqiflush")
83
+ have_func("putp")
84
+ have_func("qiflush")
85
+ have_func("scr_dump")
86
+ have_func("scr_init")
87
+ have_func("scr_restore")
88
+ have_func("scr_set")
89
+ have_func("slk_attr_off")
90
+ have_func("slk_attr_on")
91
+ have_func("slk_attr")
92
+ have_func("slk_attr_set")
93
+ have_func("slk_color")
94
+ have_func("tigetflag")
95
+ have_func("tigetnum")
96
+ have_func("use_env")
97
+ have_func("vidattr")
98
+ have_func("vid_attr")
99
+ have_func("wattr_on")
100
+ have_func("wattr_off")
101
+ have_func("wattr_set")
102
+ have_func("wchgat")
103
+ have_func("wcolor_set")
104
+ have_func("getattrs")
105
+
106
+ puts "checking which debugging functions to wrap..."
107
+ have_func("_tracef")
108
+ have_func("_tracedump")
109
+ have_func("_nc_tracebits")
110
+ have_func("_traceattr")
111
+ have_func("_traceattr2")
112
+ have_func("_tracechar")
113
+ have_func("_tracechtype")
114
+ have_func("_tracechtype2")
115
+ have_func("_tracemouse")
116
+
117
+ puts "checking for other functions that appeared after ncurses version 5.0..."
118
+ have_func("assume_default_colors")
119
+ have_func("attr_get")
120
+
121
+ puts "checking for the panel library..."
122
+ if have_header("panel.h")
123
+ have_library("panel", "panel_hidden")
124
+ end
125
+ puts "checking for the form library..."
126
+ if have_header("form.h")
127
+ have_library("form", "new_form")
128
+ end
129
+
130
+ create_makefile('ncurses')