ncurses 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,82 @@
1
+ #!/usr/bin/ruby
2
+ # Copyright (c) 2004 by Simon Kaczor <skaczor@cox.net>
3
+ # Example from the NCurses Programming HOWTO
4
+ # This example uses module functions that are documented in the ncurses man page.
5
+ # For a more rubyish approach that uses Ruby objects see form2.rb
6
+ #
7
+ # The original example contained the following copyright:
8
+ # Copyright (c) 2001 by Pradeep Padala. This document may be distributed
9
+ # under the terms set forth in the LDP license at linuxdoc.org/COPYRIGHT.html.
10
+
11
+ require 'ncurses.rb'
12
+
13
+ begin
14
+ scr = Ncurses.initscr()
15
+ Ncurses.cbreak()
16
+ Ncurses.noecho()
17
+ Ncurses.keypad(scr, true)
18
+
19
+ #create some fields
20
+ fields = Array.new
21
+ fields.push(Ncurses::Form.new_field(1,10,4,18,0,0))
22
+ fields.push(Ncurses::Form.new_field(1,10,6,18,0,0))
23
+
24
+ # set field options
25
+ Ncurses::Form.set_field_back(fields[0], Ncurses::A_UNDERLINE)
26
+ Ncurses::Form.field_opts_off(fields[0], Ncurses::Form::O_AUTOSKIP)
27
+
28
+ Ncurses::Form.set_field_back(fields[1], Ncurses::A_UNDERLINE)
29
+ Ncurses::Form.field_opts_off(fields[1], Ncurses::Form::O_AUTOSKIP)
30
+
31
+
32
+ # create a form
33
+ form = Ncurses::Form.new_form(fields)
34
+
35
+ # post the form and refresh the screen
36
+ Ncurses::Form.post_form(form)
37
+ scr.refresh()
38
+
39
+ Ncurses.mvprintw(4, 10, "Value 1:")
40
+ Ncurses.mvprintw(6, 10, "Value 2:")
41
+ scr.refresh()
42
+
43
+ # Loop through to get user requests
44
+ while((ch = scr.getch()) != Ncurses::KEY_F1) do
45
+ case(ch)
46
+ when Ncurses::KEY_DOWN
47
+ # Go to next field
48
+ Ncurses::Form.form_driver(form, Ncurses::Form::REQ_NEXT_FIELD)
49
+ # Go to the end of the present buffer
50
+ # Leaves nicely at the last character
51
+ Ncurses::Form.form_driver(form, Ncurses::Form::REQ_END_LINE)
52
+
53
+ when Ncurses::KEY_UP
54
+ #Go to previous field
55
+ Ncurses::Form.form_driver(form, Ncurses::Form::REQ_PREV_FIELD)
56
+ Ncurses::Form.form_driver(form, Ncurses::Form::REQ_END_LINE);
57
+ else
58
+ # If this is a normal character, it gets Printed
59
+ Ncurses::Form.form_driver(form, ch)
60
+ end
61
+ end
62
+
63
+ # unpost and free form
64
+ Ncurses::Form.unpost_form(form);
65
+ Ncurses::Form.free_form(form)
66
+ Ncurses::Form.free_field(fields[0]);
67
+ Ncurses::Form.free_field(fields[1]);
68
+
69
+
70
+ #using class methods this time
71
+ # form = Ncurses::Form::FORM.new(fields)
72
+ # puts "Created form: #{form.inspect}\n"
73
+ # form.free()
74
+
75
+ ensure
76
+ # put the screen back in its normal state
77
+ Ncurses.echo()
78
+ Ncurses.nocbreak()
79
+ Ncurses.nl()
80
+ Ncurses.endwin()
81
+ end
82
+
@@ -0,0 +1,184 @@
1
+ #!/usr/bin/ruby
2
+ # Copyright (c) 2004 by Simon Kaczor <skaczor@cox.net>
3
+ # Simple example of a form in action, based on the NCURSES Programming HOWTO:
4
+ # http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/
5
+ #
6
+ # All standard field types are created in the form.
7
+ # Additionnally a custom field is created to illustrate
8
+ # custom field validation using Ruby Proc objects, as shown in the example.
9
+ #
10
+ # The original example contained the following copyright:
11
+ # Copyright (c) 2001 by Pradeep Padala. This document may be distributed
12
+ # under the terms set forth in the LDP license at linuxdoc.org/COPYRIGHT.html.
13
+
14
+ require 'ncurses'
15
+
16
+ include Ncurses
17
+ include Ncurses::Form
18
+
19
+ def print_in_middle(win, starty, startx, width, string, color)
20
+
21
+ if(win == nil)
22
+ win = stdscr;
23
+ end
24
+ x = Array.new
25
+ y = Array.new
26
+ Ncurses.getyx(win, y, x);
27
+ if(startx != 0)
28
+ x[0] = startx;
29
+ end
30
+ if(starty != 0)
31
+ y[0] = starty;
32
+ end
33
+ if(width == 0)
34
+ width = 80;
35
+ end
36
+ length = string.length;
37
+ temp = (width - length)/ 2;
38
+ x[0] = startx + temp.floor;
39
+ win.attron(color);
40
+ win.mvprintw(y[0], x[0], "%s", string);
41
+ win.attroff(color);
42
+ Ncurses.refresh();
43
+ end
44
+
45
+ fields = Array.new
46
+
47
+ states = {"MI" => "Michigan",
48
+ "VA" => "Virginia",
49
+ "VE" => "Vermont"}
50
+ fieldcheck = proc { |afield|
51
+ val = afield.field_buffer(0)
52
+ val.strip!
53
+ if (states[val] != nil)
54
+ afield.set_field_buffer(0,states[val])
55
+ return true
56
+ else
57
+ return false
58
+ end
59
+ }
60
+ charcheck = proc { |ch|
61
+ if (('A'..'Z').include?(ch))
62
+ return true
63
+ else
64
+ return false
65
+ end
66
+ }
67
+
68
+ # Initialize curses
69
+ begin
70
+ stdscr = Ncurses.initscr();
71
+ Ncurses.start_color();
72
+ Ncurses.cbreak();
73
+ Ncurses.noecho();
74
+ Ncurses.keypad(stdscr, true);
75
+
76
+ # Initialize few color pairs
77
+ Ncurses.init_pair(1, COLOR_RED, COLOR_BLACK);
78
+ Ncurses.init_pair(2, COLOR_BLACK, COLOR_WHITE);
79
+ Ncurses.init_pair(3, COLOR_BLACK, COLOR_BLUE);
80
+ stdscr.bkgd(Ncurses.COLOR_PAIR(2));
81
+
82
+ # Initialize the fields
83
+ (1..9).each { |i|
84
+ field = FIELD.new(1, 10, i*2, 1, 0, 0)
85
+ field.set_field_back(A_UNDERLINE)
86
+ fields.push(field)
87
+ }
88
+
89
+ customtype = FIELDTYPE.new(fieldcheck, charcheck);
90
+
91
+ fields[1].set_field_type(TYPE_ALNUM, 0);
92
+ fields[2].set_field_type(TYPE_ALPHA, 0);
93
+ fields[3].set_field_type(TYPE_INTEGER, 0, 0, 1000);
94
+ fields[4].set_field_type(TYPE_NUMERIC, 2, 0, 1000);
95
+ fields[5].set_field_type(TYPE_ENUM, ["one","two","three"], false, false);
96
+ fields[6].set_field_type(TYPE_REGEXP, "^ *[0-9]* *$");
97
+ fields[7].set_field_type(TYPE_IPV4);
98
+ fields[8].set_field_type(customtype);
99
+
100
+
101
+ # Create the form and post it
102
+ my_form = FORM.new(fields);
103
+
104
+ my_form.user_object = "My identifier"
105
+
106
+ # Calculate the area required for the form
107
+ rows = Array.new()
108
+ cols = Array.new()
109
+ my_form.scale_form(rows, cols);
110
+
111
+ # Create the window to be associated with the form
112
+ my_form_win = WINDOW.new(rows[0] + 3, cols[0] + 14, 1, 1);
113
+ my_form_win.bkgd(Ncurses.COLOR_PAIR(3));
114
+ my_form_win.keypad(TRUE);
115
+
116
+ # Set main window and sub window
117
+ my_form.set_form_win(my_form_win);
118
+ my_form.set_form_sub(my_form_win.derwin(rows[0], cols[0], 2, 12));
119
+
120
+ # Print a border around the main window and print a title */
121
+ my_form_win.box(0, 0);
122
+ print_in_middle(my_form_win, 1, 0, cols[0] + 14, "My Form", Ncurses.COLOR_PAIR(1));
123
+
124
+ my_form.post_form();
125
+
126
+ # Print field types
127
+ my_form_win.mvaddstr(4, 2, "No Type")
128
+ my_form_win.mvaddstr(6, 2, "Alphanum")
129
+ my_form_win.mvaddstr(8, 2, "Alpha")
130
+ my_form_win.mvaddstr(10, 2, "Integer")
131
+ my_form_win.mvaddstr(12, 2, "Numeric")
132
+ my_form_win.mvaddstr(14, 2, "Enum")
133
+ my_form_win.mvaddstr(16, 2, "Regexp")
134
+ my_form_win.mvaddstr(18, 2, "IP")
135
+ my_form_win.mvaddstr(20, 2, "Custom")
136
+
137
+ my_form_win.wrefresh();
138
+
139
+ stdscr.mvprintw(Ncurses.LINES - 2, 28, "Use UP, DOWN arrow keys to switch between fields");
140
+ stdscr.mvprintw(Ncurses.LINES - 1, 28, "Press F1 to quit");
141
+ stdscr.refresh();
142
+
143
+ # Loop through to get user requests
144
+ while((ch = my_form_win.getch()) != KEY_F1)
145
+ case ch
146
+ when KEY_DOWN
147
+ # Go to next field */
148
+ my_form.form_driver(REQ_VALIDATION);
149
+ my_form.form_driver(REQ_NEXT_FIELD);
150
+ # Go to the end of the present buffer
151
+ # Leaves nicely at the last character
152
+ my_form.form_driver(REQ_END_LINE);
153
+
154
+ when KEY_UP
155
+ # Go to previous field
156
+ my_form.form_driver(REQ_VALIDATION);
157
+ my_form.form_driver(REQ_PREV_FIELD);
158
+ my_form.form_driver(REQ_END_LINE);
159
+
160
+ when KEY_LEFT
161
+ # Go to previous field
162
+ my_form.form_driver(REQ_PREV_CHAR);
163
+
164
+ when KEY_RIGHT
165
+ # Go to previous field
166
+ my_form.form_driver(REQ_NEXT_CHAR);
167
+
168
+ when KEY_BACKSPACE
169
+ my_form.form_driver(REQ_DEL_PREV);
170
+ else
171
+ # If this is a normal character, it gets Printed
172
+ my_form.form_driver(ch);
173
+ end
174
+ end
175
+ # Un post form and free the memory
176
+ my_form.unpost_form();
177
+ my_form.free_form();
178
+ fields.each {|f| f.free_field()}
179
+
180
+
181
+ ensure
182
+ Ncurses.endwin();
183
+ end
184
+
@@ -0,0 +1,57 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # $Id: hello_ncurses.rb,v 1.3 2002/02/28 13:50:03 t-peters Exp $
4
+
5
+ # this ncurses-ruby program follows an ancient tradition of example
6
+ # computer programs: When invoked, it prints a friendly greeting on the
7
+ # screen and exits.
8
+ #
9
+ # Copyright (C) 2002 Tobias Peters <t-peters@users.berlios.de>
10
+ #
11
+ # You may use, modify, and redistribute this file without restriction.
12
+
13
+
14
+
15
+ # First, we have to tell Ruby to use the Ncurses extension module:
16
+
17
+ require "ncurses"
18
+
19
+
20
+
21
+ # Second, every program using ncurses must initialize the ncurses library
22
+ # before the first call to any ncurses function:
23
+
24
+ Ncurses.initscr
25
+
26
+
27
+
28
+ # Now the program can use ncurses facilities for screen output. It will print
29
+ # a greeting to the 5th line on the screen, starting at column 20
30
+
31
+ Ncurses.mvaddstr(4, 19, "Hello, world!");
32
+
33
+ # Note that ncurses counts lines and columns starting from 0, and that it
34
+ # expects the line number first and the column number second every time it
35
+ # expects a coordinate pair.
36
+
37
+
38
+
39
+ # The previous function call did not alter the screen at all. Ncurses makes
40
+ # all changes first to an internal buffer. The contents of this buffer is
41
+ # copied to the screen with the following function call:
42
+
43
+ Ncurses.refresh
44
+
45
+
46
+ # Now pause for a short while, enough time for the program user to read the
47
+ # greeting and greet back.
48
+
49
+ sleep(2.5)
50
+
51
+
52
+ # The program has almost finished its task. It only needs to put the screen
53
+ # back to its normal state:
54
+
55
+ Ncurses.endwin
56
+
57
+
@@ -0,0 +1,220 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # $Id: rain.rb,v 1.5 2002/11/16 21:16:32 t-peters Exp $
4
+
5
+ # This program is a translation of the popular rain.c demo program from the
6
+ # ncurses library distribution.
7
+ #
8
+ # Copyright (C) 2002 Tobias Peters <t-peters@users.berlios.de>
9
+ #
10
+ # I do not impose any additional restrictions over the copyright of the
11
+ # ncurses library distribution. It has the following Copyright notice
12
+
13
+ #/****************************************************************************
14
+ # * Copyright (c) 1998 Free Software Foundation, Inc. *
15
+ # * *
16
+ # * Permission is hereby granted, free of charge, to any person obtaining a *
17
+ # * copy of this software and associated documentation files (the *
18
+ # * "Software"), to deal in the Software without restriction, including *
19
+ # * without limitation the rights to use, copy, modify, merge, publish, *
20
+ # * distribute, distribute with modifications, sublicense, and/or sell *
21
+ # * copies of the Software, and to permit persons to whom the Software is *
22
+ # * furnished to do so, subject to the following conditions: *
23
+ # * *
24
+ # * The above copyright notice and this permission notice shall be included *
25
+ # * in all copies or substantial portions of the Software. *
26
+ # * *
27
+ # * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
28
+ # * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
29
+ # * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
30
+ # * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
31
+ # * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
32
+ # * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
33
+ # * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
34
+ # * *
35
+ # * Except as contained in this notice, the name(s) of the above copyright *
36
+ # * holders shall not be used in advertising or otherwise to promote the *
37
+ # * sale, use or other dealings in this Software without prior written *
38
+ # * authorization. *
39
+ # ****************************************************************************/
40
+
41
+
42
+
43
+ require "ncurses"
44
+
45
+
46
+ # A class responsible for raindrop drawing
47
+ class Raindrop
48
+ def initialize (window, color_pair = nil)
49
+ @window = window
50
+ @color_pair = color_pair
51
+ lines = []
52
+ columns = []
53
+ window.getmaxyx(lines,columns)
54
+ lines = (lines[0] <= 4) ? 1 : (lines[0] - 4)
55
+ columns = (columns[0] <= 4) ? 1 : (columns[0] - 4)
56
+ @current_phase = 0
57
+ @x = rand(columns)+2
58
+ @y = rand(lines)+2
59
+ end
60
+
61
+ # draw_next_phase draws the next phase of a raindrop. If this was the last
62
+ # phase, returns 0, otherwise returns the raindrop.
63
+ def draw_next_phase
64
+ if (@color_pair)
65
+ if Ncurses.respond_to?(:color_set)
66
+ @window.color_set(@color_pair, nil)
67
+ else
68
+ @window.attrset(Ncurses.COLOR_PAIR(@color_pair))
69
+ end
70
+ end
71
+ if (DRAWING_PROCS[@current_phase].call(@window,@y,@x))
72
+ @current_phase += 1
73
+ self
74
+ end
75
+ end
76
+
77
+ DRAWING_PROCS = [
78
+ Proc.new{|window,y,x|
79
+ window.mvaddstr(y,x, ".")
80
+ },
81
+ Proc.new{|window,y,x|
82
+ window.mvaddstr(y, x, "o")
83
+ },
84
+ Proc.new{|window,y,x|
85
+ window.mvaddstr(y, x, "O")
86
+ },
87
+ Proc.new{|window,y,x|
88
+ window.mvaddstr(y-1, x, "-")
89
+ window.mvaddstr(y, x-1, "|.|")
90
+ window.mvaddstr(y+1, x, "-")
91
+ },
92
+ Proc.new{|window,y,x|
93
+ window.mvaddstr(y-2, x, "-")
94
+ window.mvaddstr(y-1, x-1, "/ \\")
95
+ window.mvaddstr(y, x-2, "| O |")
96
+ window.mvaddstr(y+1, x-1, "\\ /")
97
+ window.mvaddstr(y+2, x, "-")
98
+ },
99
+ Proc.new{|window,y,x|
100
+ window.mvaddstr(y-2, x, " ")
101
+ window.mvaddstr(y-1, x-1, " ")
102
+ window.mvaddstr(y, x-2, " ")
103
+ window.mvaddstr(y+1, x-1, " ")
104
+ window.mvaddstr(y+2, x, " ")
105
+ nil # signal the last raindrop phase
106
+ }
107
+ ]
108
+ NUMBER_OF_PHASES = DRAWING_PROCS.size - 1
109
+ end
110
+
111
+
112
+ # This class creates raindrops and tells them to draw on the screen
113
+ class Rain
114
+ AVERAGE_RAINDROP_SPACE = 475.1 # 4 simultaneous raindrops in a 80x24 Window
115
+
116
+ def Rain.sigwinch_handler(sig = nil)
117
+ ObjectSpace.each_object(Rain){|rain|
118
+ rain.window_size_changed = true
119
+ }
120
+ end
121
+
122
+ attr_writer :window_size_changed
123
+
124
+ def initialize(window)
125
+ @window = window
126
+ @window_size_changed = true
127
+ @raindrops = []
128
+ @has_colors = Ncurses.has_colors?
129
+ if (@has_colors)
130
+ @current_color = 1
131
+ end
132
+ end
133
+
134
+ def fall_for_a_moment
135
+ adjust_to_new_window_size if (@window_size_changed)
136
+
137
+ current_number_of_new_raindrops.times{
138
+ if (@has_colors)
139
+ @raindrops.push(Raindrop.new(@window, @current_color))
140
+ @current_color = 3 - @current_color # alternate between 1 and 2
141
+ else
142
+ @raindrops.push(Raindrop.new(@window))
143
+ end
144
+ }
145
+
146
+ @raindrops = @raindrops.collect{|raindrop|
147
+ raindrop.draw_next_phase
148
+ }.compact # erase raindrops that have expired from the list
149
+ end
150
+
151
+ def adjust_to_new_window_size
152
+ @window_size_changed = false
153
+ window_size = @window.getmaxx * @window.getmaxy
154
+ average_number_of_raindrops = window_size / AVERAGE_RAINDROP_SPACE
155
+ @average_number_of_new_raindrops =
156
+ average_number_of_raindrops / Raindrop::NUMBER_OF_PHASES
157
+ end
158
+
159
+ def current_number_of_new_raindrops
160
+ num_floor = @average_number_of_new_raindrops.floor
161
+ num_ceil = @average_number_of_new_raindrops.ceil
162
+ chance = @average_number_of_new_raindrops - num_floor
163
+ if (rand > chance)
164
+ num_floor
165
+ else
166
+ num_ceil
167
+ end
168
+ end
169
+
170
+ def fall(pause = 0.1)
171
+ begin
172
+ fall_for_a_moment
173
+ @window.refresh
174
+ sleep(pause)
175
+ end while (true)
176
+ end
177
+ end
178
+
179
+ Ncurses.initscr
180
+ begin
181
+ if (Ncurses.has_colors?)
182
+ bg = Ncurses::COLOR_BLACK
183
+ Ncurses.start_color
184
+ if (Ncurses.respond_to?("use_default_colors"))
185
+ if (Ncurses.use_default_colors == Ncurses::OK)
186
+ bg = -1
187
+ end
188
+ end
189
+ Ncurses.init_pair(1, Ncurses::COLOR_BLUE, bg);
190
+ Ncurses.init_pair(2, Ncurses::COLOR_CYAN, bg);
191
+ end
192
+ Ncurses.nl()
193
+ Ncurses.noecho()
194
+ Ncurses.curs_set(0)
195
+ Ncurses.stdscr.nodelay(true)
196
+ Ncurses.timeout(0)
197
+
198
+ rain = Rain.new(Ncurses.stdscr)
199
+
200
+ begin
201
+ case(Ncurses.getch())
202
+ when 'q'[0], 'Q'[0]
203
+ Ncurses.curs_set(1)
204
+ Ncurses.endwin()
205
+ exit
206
+ when 's'[0]
207
+ Ncurses.stdscr.nodelay(false)
208
+ when ' '[0]
209
+ Ncurses.stdscr.nodelay(true)
210
+ when Ncurses::KEY_RESIZE
211
+ Rain.sigwinch_handler
212
+ end
213
+ sleep(0.050)
214
+ rain.fall_for_a_moment
215
+ Ncurses.refresh
216
+ end while true
217
+ ensure
218
+ Ncurses.curs_set(1)
219
+ Ncurses.endwin()
220
+ end