1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
#!/usr/bin/ruby
# Copyright (c) 2004 by Simon Kaczor <skaczor@cox.net>
# Simple example of a form in action, based on the NCURSES Programming HOWTO:
# http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/
#
# All standard field types are created in the form.
# Additionnally a custom field is created to illustrate
# custom field validation using Ruby Proc objects, as shown in the example.
#
# The original example contained the following copyright:
# Copyright (c) 2001 by Pradeep Padala. This document may be distributed
# under the terms set forth in the LDP license at linuxdoc.org/COPYRIGHT.html.
require 'ncursesw'
include Ncurses
include Ncurses::Form
def print_in_middle(win, starty, startx, width, string, color)
if(win == nil)
win = stdscr;
end
x = Array.new
y = Array.new
Ncurses.getyx(win, y, x);
if(startx != 0)
x[0] = startx;
end
if(starty != 0)
y[0] = starty;
end
if(width == 0)
width = 80;
end
length = string.length;
temp = (width - length)/ 2;
x[0] = startx + temp.floor;
win.attron(color);
win.mvprintw(y[0], x[0], "%s", string);
win.attroff(color);
Ncurses.refresh();
end
fields = Array.new
states = {"MI" => "Michigan",
"VA" => "Virginia",
"VE" => "Vermont"}
fieldcheck = proc { |afield|
val = afield.field_buffer(0)
val.strip!
if (states[val] != nil)
afield.set_field_buffer(0,states[val])
return true
else
return false
end
}
charcheck = proc { |ch|
if (('A'..'Z').include?(ch))
return true
else
return false
end
}
# Initialize curses
begin
stdscr = Ncurses.initscr();
Ncurses.start_color();
Ncurses.cbreak();
Ncurses.noecho();
Ncurses.keypad(stdscr, true);
# Initialize few color pairs
Ncurses.init_pair(1, COLOR_RED, COLOR_BLACK);
Ncurses.init_pair(2, COLOR_BLACK, COLOR_WHITE);
Ncurses.init_pair(3, COLOR_BLACK, COLOR_BLUE);
stdscr.bkgd(Ncurses.COLOR_PAIR(2));
# Initialize the fields
(1..9).each { |i|
field = FIELD.new(1, 10, i*2, 1, 0, 0)
field.set_field_back(A_UNDERLINE)
fields.push(field)
}
customtype = FIELDTYPE.new(fieldcheck, charcheck);
fields[1].set_field_type(TYPE_ALNUM, 0);
fields[2].set_field_type(TYPE_ALPHA, 0);
fields[3].set_field_type(TYPE_INTEGER, 0, 0, 1000);
fields[4].set_field_type(TYPE_NUMERIC, 2, 0, 1000);
fields[5].set_field_type(TYPE_ENUM, ["one","two","three"], false, false);
fields[6].set_field_type(TYPE_REGEXP, "^ *[0-9]* *$");
fields[7].set_field_type(TYPE_IPV4);
fields[8].set_field_type(customtype);
# Create the form and post it
my_form = FORM.new(fields);
my_form.user_object = "My identifier"
# Calculate the area required for the form
rows = Array.new()
cols = Array.new()
my_form.scale_form(rows, cols);
# Create the window to be associated with the form
my_form_win = WINDOW.new(rows[0] + 3, cols[0] + 14, 1, 1);
my_form_win.bkgd(Ncurses.COLOR_PAIR(3));
my_form_win.keypad(TRUE);
# Set main window and sub window
my_form.set_form_win(my_form_win);
my_form.set_form_sub(my_form_win.derwin(rows[0], cols[0], 2, 12));
# Print a border around the main window and print a title */
my_form_win.box(0, 0);
print_in_middle(my_form_win, 1, 0, cols[0] + 14, "My Form", Ncurses.COLOR_PAIR(1));
my_form.post_form();
# Print field types
my_form_win.mvaddstr(4, 2, "No Type")
my_form_win.mvaddstr(6, 2, "Alphanum")
my_form_win.mvaddstr(8, 2, "Alpha")
my_form_win.mvaddstr(10, 2, "Integer")
my_form_win.mvaddstr(12, 2, "Numeric")
my_form_win.mvaddstr(14, 2, "Enum")
my_form_win.mvaddstr(16, 2, "Regexp")
my_form_win.mvaddstr(18, 2, "IP")
my_form_win.mvaddstr(20, 2, "Custom")
my_form_win.wrefresh();
stdscr.mvprintw(Ncurses.LINES - 2, 28, "Use UP, DOWN arrow keys to switch between fields");
stdscr.mvprintw(Ncurses.LINES - 1, 28, "Press F1 to quit");
stdscr.refresh();
# Loop through to get user requests
while((ch = my_form_win.getch()) != KEY_F1)
case ch
when KEY_DOWN
# Go to next field */
my_form.form_driver(REQ_VALIDATION);
my_form.form_driver(REQ_NEXT_FIELD);
# Go to the end of the present buffer
# Leaves nicely at the last character
my_form.form_driver(REQ_END_LINE);
when KEY_UP
# Go to previous field
my_form.form_driver(REQ_VALIDATION);
my_form.form_driver(REQ_PREV_FIELD);
my_form.form_driver(REQ_END_LINE);
when KEY_LEFT
# Go to previous field
my_form.form_driver(REQ_PREV_CHAR);
when KEY_RIGHT
# Go to previous field
my_form.form_driver(REQ_NEXT_CHAR);
when KEY_BACKSPACE
my_form.form_driver(REQ_DEL_PREV);
else
# If this is a normal character, it gets Printed
my_form.form_driver(ch);
end
end
# Un post form and free the memory
my_form.unpost_form();
my_form.free_form();
fields.each {|f| f.free_field()}
ensure
Ncurses.endwin();
end
|