ncursesw 0.9.1.a

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,129 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # $Id: example.rb,v 1.4 2002/03/04 13:24:29 t-peters Exp $
4
+
5
+ # This file provides an example for the usage of the ncurses-ruby module.
6
+ # Copyright (C) 2002 Tobias Peters <t-peters@users.berlios.de>
7
+ #
8
+ # The following license applys only to this file. It is less restrictive
9
+ # than the license for the rest of the ncurses-ruby distribution.
10
+ # I've adapted this file from someone else, see below.
11
+ #
12
+ # Permission is hereby granted, free of charge, to any person
13
+ # obtaining a copy of this file
14
+ # (the "Software"), to deal in the Software without restriction,
15
+ # including without limitation the rights to use, copy, modify, merge,
16
+ # publish, distribute, sublicense, and/or sell copies of the Software,
17
+ # and to permit persons to whom the Software is furnished to do so,
18
+ # subject to the following conditions:
19
+ #
20
+ # The above copyright notice and this permission notice shall be
21
+ # included in all copies or substantial portions of the Software.
22
+ #
23
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27
+ # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28
+ # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29
+ # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
+ # SOFTWARE.
31
+
32
+
33
+
34
+ # this file is adapted from an example of the python ncurses binding
35
+ # pyncurses (http://pyncurses.sf.net/), which has the following copyright
36
+ # statement:
37
+
38
+ # Copyright (c) 2000 by Harry Henry Gebel
39
+ #
40
+ # Permission is hereby granted, free of charge, to any person
41
+ # obtaining a copy of this software and associated documentation files
42
+ # (the "Software"), to deal in the Software without restriction,
43
+ # including without limitation the rights to use, copy, modify, merge,
44
+ # publish, distribute, sublicense, and/or sell copies of the Software,
45
+ # and to permit persons to whom the Software is furnished to do so,
46
+ # subject to the following conditions:
47
+ #
48
+ # The above copyright notice and this permission notice shall be
49
+ # included in all copies or substantial portions of the Software.
50
+ #
51
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
52
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
53
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
54
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
55
+ # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
56
+ # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
57
+ # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
58
+ # SOFTWARE.
59
+
60
+
61
+
62
+ require "ncurses"
63
+
64
+ def moving(scr)
65
+ scr.clear() # clear screen
66
+ scr.move(5,5) # move cursor
67
+ scr.addstr("move(5,5)")
68
+ scr.refresh() # update screen
69
+ sleep(2)
70
+ scr.move(2,2)
71
+ scr.addstr("move(2,2)")
72
+ scr.refresh()
73
+ sleep(2)
74
+ scr.move(10, 2)
75
+ scr.addstr("Press a key to continue")
76
+ scr.getch()
77
+ end
78
+
79
+ def border(scr)
80
+ scr.clear()
81
+ scr.border(*([0]*8)) # calls WINDOW#border(0, 0, 0, 0, 0, 0, 0, 0)
82
+ scr.move(3,3)
83
+ scr.addstr("Press a key to continue")
84
+ scr.getch()
85
+ end
86
+
87
+ def two_borders()
88
+ # make a new window as tall as the screen and half as wide, in the left half
89
+ # of the screen
90
+ one = Ncurses::WINDOW.new(0, Ncurses.COLS() / 2, 0, 0)
91
+ # make one for the right half
92
+ two = Ncurses::WINDOW.new(0, Ncurses.COLS() - (Ncurses.COLS() / 2),
93
+ 0, Ncurses.COLS() / 2)
94
+ one.border(*([0]*8))
95
+ two.border(*([0]*8))
96
+ one.move(3,3)
97
+ two.move(2,5)
98
+ one.addstr("move(3,3)")
99
+ two.addstr("move(2,5)")
100
+ two.move(5,3)
101
+ two.addstr("Press a key to continue")
102
+ one.noutrefresh() # copy window to virtual screen, don't update real screen
103
+ two.noutrefresh()
104
+ Ncurses.doupdate() # update read screen
105
+ two.getch()
106
+ end
107
+
108
+ begin
109
+ # initialize ncurses
110
+ Ncurses.initscr
111
+ Ncurses.cbreak # provide unbuffered input
112
+ Ncurses.noecho # turn off input echoing
113
+ Ncurses.nonl # turn off newline translation
114
+ Ncurses.stdscr.intrflush(false) # turn off flush-on-interrupt
115
+ Ncurses.stdscr.keypad(true) # turn on keypad mode
116
+
117
+ Ncurses.stdscr.addstr("Press a key to continue") # output string
118
+ Ncurses.stdscr.getch # get a charachter
119
+
120
+ moving(Ncurses.stdscr) # demo of moving cursor
121
+ border(Ncurses.stdscr) # demo of borders
122
+ two_borders() # demo of two windows with borders
123
+
124
+ ensure
125
+ Ncurses.echo
126
+ Ncurses.nocbreak
127
+ Ncurses.nl
128
+ Ncurses.endwin
129
+ end
data/examples/form.rb ADDED
@@ -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
+
data/examples/form2.rb ADDED
@@ -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
+