rkumar / rbcurse

ruby based curses widgets: fields, buttons, textarea. menus, message boxes, tabbed panes, tables, listboxes. Event based, MVC architecture.

This URL has Read+Write access

rbcurse / README.txt
100644 313 lines (227 sloc) 9.229 kb
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
= rbcurse
 
* http://totalrecall.wordpress.com << CORRECTED !!!
 
* rbcurse on rubyforge: http://rbcurse.rubyforge.org/
 
* See latest changes on http://github.com/rkumar/rbcurse/tree/master/CHANGELOG
 
* Many working demos in examples folder, such as:
 
  * test2.rb (exit with F1, Or Cancel button/Alt-C) contains various
    widgets. F2 for menubar toggling
 
  * rfe.rb is a ruby file explorer
 
  * sqlc.rb is a ruby sql client demo (using testd.db at
     http://www.benegal.org/files/screen/testd.db)
 
  * testtodo.rb is a test TODO application
 
* Screenshots on
  http://www.benegal.org/files/screen/?M=D (new)
  and on blog, http://totalrecall.wordpress.com
  and http://github.com/rkumar/rbcurse/wikis/screenshots (old)
 
* Todo (for 0.1.2): http://rubyforge.org/pm/task.php?group_id=7775&group_project_id=13812&func=browse
 
* Next Major Release: http://rubyforge.org/pm/task.php?group_project_id=13813&group_id=7775&func=browse
 
== DESCRIPTION:
 
A small but comprehensive widget library written in ruby for creating ncurses
applications.
 
== FEATURES
 
* entry fields in ruby
* scrollable list box (new, editable with Field, checkbox and combos)
* multi-line editable area
* togglebutton, radio and check buttons (with mnemonics)
* message box
* menubar - with submenu and CheckBoxMenuItem
* popup list
* tabbedpane (multiple forms using tabbed metaphor)
* combobox
* labels with mnemonics (hotkeys)
* multi-column table - with cell selection and editing
 
Above may be created using DSL like syntax, or hashes.
 
== Sample programs:
 
* sqlc.rb is a ruby sql client demo
* rfe : file explorer or Finder like app
* testtodo.rb : a todo app based on a yaml file (now csv)
* test2.rb most widgets (including menus)
* test1.rb various kinds of messageboxes (input, list, custom)
* testtabp.rb tabbed pane
* testcombo.rb combos with various insert policies and vertical
   alignments
 
== PROBLEMS, ISSUES
TextArea not bug free. Some situations wrapping errors could exist.
 
== Terminal related issues.
 
* Some terminals may not show underlines (e.g screen).
 
* Some terminals (xterm-color) do not process Function keys, avoid declaring F1 etc if
  unsure of client terminals. I have put in fixes for xterm-color F1 and
  backtab.
 
* To use ALT/META keys on a Mac OS X, in Terminal preferences, under
  Keyboard, select
  "use Option as Meta key". All hotkeys are automatically, ALT combinations.
 
* Some screens do not display window background color under spaces.
  This is okay under "screen" and "xterm-color" but not under "xterm". You will notice
  this in the message box samples.
 
I am developing and testing under "screen" under OS X Leopard.
 
== SYNOPSIS:
 
See lib/rbcurse/rwidgets.rb.
For test programs, see test1, test2, testcombo etc in examples folder.
 
This depends only on "window" provided by ncurses. Does not use forms
and fields. Minor changes and improvements may have happened to sample
code below. See test programs for latest, working code.
 
=== create a window and a form based on window
 
      @layout = { :height => 0, :width => 0, :top => 0, :left => 0 }
      @win = VER::Window.new(@layout)
 
      @form = Form.new @win
 
 
=== create a bunch of fields with dependent labels
 
      r = 1; c = 22;
      %w[ name age company].each do |w|
        field = Field.new @form do
          name w
          row r
          col c
          display_length 30
          set_buffer "abcd #{w}"
          set_label Label.new @form, {'text' => w}
        end
        r += 1
      end
 
=== create a variable (like TkVariable) and tie a label to it.
 
      $results = Variable.new
      $results.value = "A variable"
      var = RubyCurses::Label.new @form, {'text_variable' => $results, "row" => r, "col" => 22}
        r += 1
 
=== create a list and a list box based on the list.
 
        mylist = []
        0.upto(100) { |v| mylist << "#{v} scrollable data" }
 
        field = Listbox.new @form do
          name "mylist"
          row r
          col 1
          width 40
          height 10
          list mylist
        end
        field.insert 5, "hello ruby", "so long python", "farewell java", "RIP .Net"
 
=== create a textarea for entry
 
        texta = TextArea.new @form do
          name "mytext"
          row 1
          col 52
          width 40
          height 20
        end
        texta << "hello there" << "we are testing deletes in this application"
        texta << "HELLO there" << "WE ARE testing deletes in this application"
 
=== create a check box, updates a Variable
 
      checkbutton = CheckBox.new @form do
        text_variable $results
        #value = true
        onvalue "selected cb"
        offvalue "UNselected cb"
        text "Please click me"
        row 17
        col 22
      end
 
=== change field properties at any time by referring to them by name
 
      @form.by_name["age"].display_length = 3
      @form.by_name["age"].maxlen = 3
      @form.by_name["age"].set_buffer "24"
      @form.by_name["name"].set_buffer "Not focusable"
      @form.by_name["age"].chars_allowed = /\d/
      @form.by_name["company"].type(:ALPHA)
      @form.by_name["name"].set_focusable(false)
 
      @form.by_name["password"].color 'red'
      @form.by_name["password"].bgcolor 'blue'
 
      # restrict entry to some values
      password.values(%w[ scotty tiger secret qwerty])
 
      # validation using ruby's regular expressions
 
      field.valid_regex(/^[A-Z]\d+/)
 
=== bind events to forms, and fields
 
      @form.bind(:ENTER) { |f| f.label.bgcolor = $promptcolor if f.instance_of? RubyCurses::Field}
      @form.bind(:LEAVE) { |f| f.label.bgcolor = $datacolor if f.instance_of? RubyCurses::Field}
 
=== create buttons
 
      ok_button = Button.new @form do
        text "OK"
        name "OK"
        row 18
        col 22
      end
      ok_button.command { |form| $results.value = "OK PRESS:";form.printstr(@window, 23,45, "OK CALLED") }
        #text "Cancel"
      cancel_button = Button.new @form do
        text_variable $results
        row 18
        col 28
      end
      cancel_button.command { |form| form.printstr(@window, 23,45, "Cancel CALLED"); throw(:close); }
 
=== create radio buttons
 
      colorlabel = Label.new @form, {'text' => "Select a color:", "row" => 20,
          "col" => 22, "color"=> "cyan"}
      $radio = Variable.new
      radio1 = RadioButton.new @form do
        text_variable $radio
        text "red"
        value "red"
        color "red"
        row 21
        col 22
      end
      radio2 = RadioButton.new @form do
        text_variable $radio
        text "green"
        value "green"
        color "green"
        row 22
        col 22
      end
 
=== create a messagebox
 
      @mb = RubyCurses::MessageBox.new do
        title "Enter your name"
        message "Enter your name"
        type :input
        default_value "rahul"
        default_button 0
      end
        #title "Color selector"
        #type :custom
        #buttons %w[red green blue yellow]
        #underlines [0,0,0,0]
      
     $log.debug "MBOX : #{@mb.selected_index} "
     $log.debug "MBOX : #{@mb.input_value} "
 
=== create a read-only scrollable view of data
 
        @textview = TextView.new @form do
          name "myView"
          row 16
          col 52
          width 40
          height 7
        end
        content = File.open("../../README.txt","r").readlines
        @textview.set_content content
 
        ## set it to point to row 21
        @textview.top_row 21
 
 
        # lets scroll the text view as we scroll the listbox
 
        listb.bind(:ENTER_ROW, @textview) { |arow, tview| tview.top_row arow }
        
        # lets scroll the text view to the line you enter in the numeric
        # field
        @form.by_name["line"].bind(:LEAVE, @textview) { |fld, tv| tv.top_row(fld.getvalue.to_i) }
 
        # lets scroll the text view to the first match of the regex you
        # enter
        @form.by_name["regex"].bind(:LEAVE, @textview) { |fld, tv| tv.top_row(tv.find_first_match(fld.getvalue)) }
 
        # change the value of colorlabel to the selected radiobutton
        # (red or green)
 
        $radio.update_command(colorlabel) {|tv, label| label.color tv.value}
 
        # change the attribute of colorlabel to bold or normal
 
        $results.update_command(colorlabel,checkbutton) {|tv, label, cb|
            attrs = cb.value ? 'bold' : nil; label.attrs(attrs)}
 
      # during menu creation, create a checkboxmenuitem
 
      item = RubyCurses::CheckBoxMenuItem.new "CheckMe"
 
      # when selected, make colorlabel attribute reverse.
 
      item.command(colorlabel){|it, label| att = it.getvalue ? 'reverse' :
          nil; label.attrs(att); label.repaint}
 
== REQUIREMENTS:
 
* ruby 1.8.7 (not compatible with 1.9)
 
* ncurses-ruby
 
(following is provided with source)
 
* uses the window class created by "manveru" (michael)
  It is provided with this package, and has some alterations from the
  original. I have added a method getchar() which traps and returns
  ALT/META, META+CTRL, META+SHIFT+CONTROL, ALT+Fn etc.
 
 
== INSTALL:
 
sudo gem install ncurses-ruby
sudo gem install rbcurse
 
== LICENSE:
 
Copyright (c) 2008 rkumar
 
Same as ruby license.