rndk 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +13 -4
  3. data/TODO +21 -1
  4. data/demos/appointment.rb +279 -299
  5. data/demos/clock.rb +13 -8
  6. data/demos/rss-reader.rb +84 -0
  7. data/examples/01-hello-world.rb +13 -11
  8. data/examples/02-colors.rb +14 -21
  9. data/examples/03-markup.rb +7 -7
  10. data/examples/04-quick-widgets.rb +2 -2
  11. data/examples/05-position-widget.rb +50 -31
  12. data/examples/06-callbacks.rb +77 -0
  13. data/examples/07-traverse.rb +90 -0
  14. data/examples/10-all-widgets.rb +165 -0
  15. data/examples/calendar.rb +20 -32
  16. data/examples/entry.rb +15 -20
  17. data/examples/label.rb +11 -11
  18. data/examples/scroll.rb +16 -60
  19. data/examples/slider.rb +18 -19
  20. data/examples/viewer.rb +65 -0
  21. data/lib/rndk.rb +28 -7
  22. data/lib/rndk/alphalist.rb +309 -313
  23. data/lib/rndk/button.rb +239 -157
  24. data/lib/rndk/buttonbox.rb +136 -103
  25. data/lib/rndk/calendar.rb +246 -203
  26. data/lib/rndk/core/color.rb +63 -13
  27. data/lib/rndk/core/display.rb +1 -1
  28. data/lib/rndk/core/draw.rb +11 -11
  29. data/lib/rndk/core/markup.rb +21 -21
  30. data/lib/rndk/core/quick_widgets.rb +75 -96
  31. data/lib/rndk/core/screen.rb +145 -102
  32. data/lib/rndk/core/traverse.rb +150 -136
  33. data/lib/rndk/core/utils.rb +5 -6
  34. data/lib/rndk/core/widget.rb +207 -191
  35. data/lib/rndk/core/widget_bind.rb +108 -0
  36. data/lib/rndk/dialog.rb +88 -56
  37. data/lib/rndk/entry.rb +79 -64
  38. data/lib/rndk/fscale.rb +38 -20
  39. data/lib/rndk/fslider.rb +38 -23
  40. data/lib/rndk/graph.rb +92 -53
  41. data/lib/rndk/itemlist.rb +80 -62
  42. data/lib/rndk/label.rb +111 -77
  43. data/lib/rndk/radio.rb +138 -128
  44. data/lib/rndk/scale.rb +123 -122
  45. data/lib/rndk/scroll.rb +444 -391
  46. data/lib/rndk/scroller.rb +21 -21
  47. data/lib/rndk/slider.rb +149 -140
  48. data/lib/rndk/template.rb +74 -61
  49. data/lib/rndk/version.rb +1 -1
  50. data/lib/rndk/viewer.rb +499 -298
  51. metadata +8 -14
  52. data/demos/fileview.rb +0 -141
  53. data/lib/rndk/dscale.rb +0 -13
  54. data/lib/rndk/fselect.rb +0 -938
  55. data/lib/rndk/histogram.rb +0 -412
  56. data/lib/rndk/marquee.rb +0 -244
  57. data/lib/rndk/matrix.rb +0 -1189
  58. data/lib/rndk/mentry.rb +0 -619
  59. data/lib/rndk/menu.rb +0 -478
  60. data/lib/rndk/selection.rb +0 -630
  61. data/lib/rndk/swindow.rb +0 -766
  62. data/lib/rndk/uscale.rb +0 -14
  63. data/lib/rndk/uslider.rb +0 -14
@@ -22,16 +22,42 @@ module RNDK
22
22
  # color = RNDK::Color[:default_background]
23
23
  # color = RNDK::Color[:foreground]
24
24
  #
25
+ # Also, there are special color modifiers. They change how
26
+ # the color appears and can make some interesting effects:
27
+ #
28
+ # * RNDK::Color[:normal]
29
+ # * RNDK::Color[:bold]
30
+ # * RNDK::Color[:reverse]
31
+ # * RNDK::Color[:underline]
32
+ # * RNDK::Color[:blink]
33
+ # * RNDK::Color[:dim]
34
+ # * RNDK::Color[:invisible]
35
+ # * RNDK::Color[:standout]
36
+ #
37
+ # To apply them, "add" to the regular colors with the
38
+ # pipe ('|'):
39
+ #
40
+ # color = RNDK::Color[:red] | RNDK::Color[:bold]
41
+ #
25
42
  # ## Examples
26
43
  #
27
- # wb = Color[:white_black]
28
- # gm = Color[:green_magenta]
29
- # red = Color[:red]
30
- # bl = Color[:default_black]
44
+ #```
45
+ # wb = Color[:white_black]
46
+ # gm = Color[:green_magenta]
47
+ # red = Color[:red]
48
+ # bl = Color[:default_black]
49
+ # x = RNDK::Color[:blue_yellow] | RNDK::Color[:reverse] # guess what?
50
+ # y = RNDK::Color[:cyan] | RNDK::Color[:invisible] # opps
51
+ #```
31
52
  #
32
53
  # ## Developer Notes
33
54
  #
34
- # Color#init creates 80 color pairs.
55
+ # Color#init creates 80 color pairs. Neat!
56
+ #
57
+ # Also, to extract attributes from a chtype (mix between
58
+ # characters and ncurses colors) use
59
+ #
60
+ # chtype & RNDK::Color[:extract]
35
61
  #
36
62
  module Color
37
63
 
@@ -43,6 +69,27 @@ module RNDK
43
69
  # They're defined on Color#init.
44
70
  @@colors = {}
45
71
 
72
+ # These special attributes exist even if no color
73
+ # were initialized.
74
+ #
75
+ # They modify the way colors show on the screen.
76
+
77
+ @@attributes = {
78
+ :normal => Ncurses::A_NORMAL,
79
+ :bold => Ncurses::A_BOLD,
80
+ :reverse => Ncurses::A_REVERSE,
81
+ :underline => Ncurses::A_UNDERLINE,
82
+ :blink => Ncurses::A_BLINK,
83
+ :dim => Ncurses::A_DIM,
84
+ :invisible => Ncurses::A_INVIS,
85
+ :standout => Ncurses::A_STANDOUT,
86
+
87
+ # To extract attributes from a chtype (mix between
88
+ # characters and ncurses colors) use
89
+ # `chtype & RNDK::Color[:extract]
90
+ :extract => Ncurses::A_ATTRIBUTES
91
+ }
92
+
46
93
  # Start support for colors, initializing all color pairs.
47
94
  def self.init
48
95
  return unless self.has_colors?
@@ -117,18 +164,21 @@ module RNDK
117
164
  Ncurses.has_colors
118
165
  end
119
166
 
120
- # Access individual color pairs.
167
+ # Access individual color pairs or attributes.
121
168
  #
122
- # If colors were not initialized, returns white foreground
123
- # over default background.
169
+ # If colors were not initialized and user request colors,
170
+ # returns **white foreground over default background**.
124
171
  #
125
- # @return Ncurses `COLOR_PAIR` over `label` color.
126
172
  def self.[] label
127
- unless @@colors.include? label
128
- return Ncurses.COLOR_PAIR(0)
129
- end
173
+ if @@attributes.include? label
174
+ @@attributes[label]
130
175
 
131
- Ncurses.COLOR_PAIR @@colors[label]
176
+ elsif @@colors.include? label
177
+ Ncurses.COLOR_PAIR @@colors[label]
178
+
179
+ else
180
+ Ncurses.COLOR_PAIR 0
181
+ end
132
182
  end
133
183
 
134
184
  end
@@ -26,7 +26,7 @@ module RNDK
26
26
  0 => :INVALID
27
27
  }
28
28
 
29
- if table.include?(string)
29
+ if table.include? string
30
30
  table[string]
31
31
  else
32
32
  :INVALID
@@ -71,12 +71,12 @@ module RNDK
71
71
  end
72
72
  end
73
73
 
74
- # Draw a box around the given window using the object's defined
74
+ # Draw a box around the given window using the widget's defined
75
75
  # line-drawing characters
76
- def Draw.drawObjBox(win, object)
76
+ def Draw.drawObjBox(win, widget)
77
77
  Draw.attrbox(win,
78
- object.ULChar, object.URChar, object.LLChar, object.LRChar,
79
- object.HZChar, object.VTChar, object.BXAttr)
78
+ widget.ULChar, widget.URChar, widget.LLChar, widget.LRChar,
79
+ widget.HZChar, widget.VTChar, widget.BXAttr)
80
80
  end
81
81
 
82
82
  # This draws a line on the given window. (odd angle lines not working yet)
@@ -135,14 +135,14 @@ module RNDK
135
135
  y_hi = Ncurses.getmaxy(shadow_win) - 1
136
136
 
137
137
  # Draw the line on the bottom.
138
- Ncurses.mvwhline(shadow_win, y_hi, 1, Ncurses::ACS_HLINE | Ncurses::A_DIM, x_hi)
138
+ Ncurses.mvwhline(shadow_win, y_hi, 1, Ncurses::ACS_HLINE | RNDK::Color[:dim], x_hi)
139
139
 
140
140
  # Draw the line on teh right.
141
- Ncurses.mvwvline(shadow_win, 0, x_hi, Ncurses::ACS_VLINE | Ncurses::A_DIM, y_hi)
141
+ Ncurses.mvwvline(shadow_win, 0, x_hi, Ncurses::ACS_VLINE | RNDK::Color[:dim], y_hi)
142
142
 
143
- Ncurses.mvwaddch(shadow_win, 0, x_hi, Ncurses::ACS_URCORNER | Ncurses::A_DIM)
144
- Ncurses.mvwaddch(shadow_win, y_hi, 0, Ncurses::ACS_LLCORNER | Ncurses::A_DIM)
145
- Ncurses.mvwaddch(shadow_win, y_hi, x_hi, Ncurses::ACS_LRCORNER | Ncurses::A_DIM)
143
+ Ncurses.mvwaddch(shadow_win, 0, x_hi, Ncurses::ACS_URCORNER | RNDK::Color[:dim])
144
+ Ncurses.mvwaddch(shadow_win, y_hi, 0, Ncurses::ACS_LLCORNER | RNDK::Color[:dim])
145
+ Ncurses.mvwaddch(shadow_win, y_hi, x_hi, Ncurses::ACS_LRCORNER | RNDK::Color[:dim])
146
146
  Ncurses.wrefresh shadow_win
147
147
  end
148
148
  end
@@ -160,7 +160,7 @@ module RNDK
160
160
 
161
161
  # This writes out a char string with no attributes
162
162
  def Draw.writeChar(window, xpos, ypos, string, align, start, endn)
163
- Draw.writeCharAttrib(window, xpos, ypos, string, Ncurses::A_NORMAL,
163
+ Draw.writeCharAttrib(window, xpos, ypos, string, RNDK::Color[:normal],
164
164
  align, start, endn)
165
165
  end
166
166
 
@@ -186,7 +186,7 @@ module RNDK
186
186
 
187
187
  # This writes out a chtype string
188
188
  def Draw.writeChtype (window, xpos, ypos, string, align, start, endn)
189
- Draw.writeChtypeAttrib(window, xpos, ypos, string, Ncurses::A_NORMAL,
189
+ Draw.writeChtypeAttrib(window, xpos, ypos, string, RNDK::Color[:normal],
190
190
  align, start, endn)
191
191
  end
192
192
 
@@ -150,6 +150,10 @@
150
150
 
151
151
  module RNDK
152
152
 
153
+ def RNDK.char_of chtype
154
+ (chtype.ord & 255).chr
155
+ end
156
+
153
157
  # Takes a String full of format markers and translates it
154
158
  # into a chtype array.
155
159
  #
@@ -166,7 +170,7 @@ module RNDK
166
170
  # The original code makes two passes since it has to pre-allocate space but
167
171
  # we should be able to make do with one since we can dynamically size it
168
172
  adjust = 0
169
- attrib = Ncurses::A_NORMAL
173
+ attrib = RNDK::Color[:normal]
170
174
  last_char = 0
171
175
  start = 0
172
176
  used = 0
@@ -188,7 +192,7 @@ module RNDK
188
192
 
189
193
  # Pull out the bullet marker.
190
194
  while x < string.size and string[x] != R_MARKER
191
- result << (string[x].ord | Ncurses::A_BOLD)
195
+ result << (string[x].ord | RNDK::Color[:bold])
192
196
  x += 1
193
197
  end
194
198
  adjust = 1
@@ -386,10 +390,6 @@ module RNDK
386
390
  end
387
391
  end
388
392
 
389
- def RNDK.CharOf chtype
390
- (chtype.ord & 255).chr
391
- end
392
-
393
393
  # This returns a string from a chtype array
394
394
  # Formatting codes are omitted.
395
395
  def RNDK.chtype2Char(string)
@@ -397,7 +397,7 @@ module RNDK
397
397
 
398
398
  unless string.nil?
399
399
  string.each do |char|
400
- newstring << RNDK.CharOf(char)
400
+ newstring << RNDK.char_of(char)
401
401
  end
402
402
  end
403
403
 
@@ -448,17 +448,17 @@ module RNDK
448
448
  mask << 0
449
449
  case string[from + 1]
450
450
  when 'B'
451
- mask[0] = Ncurses::A_BOLD
451
+ mask[0] = RNDK::Color[:bold]
452
452
  when 'D'
453
- mask[0] = Ncurses::A_DIM
453
+ mask[0] = RNDK::Color[:dim]
454
454
  when 'K'
455
- mask[0] = Ncurses::A_BLINK
455
+ mask[0] = RNDK::Color[:blink]
456
456
  when 'R'
457
- mask[0] = Ncurses::A_REVERSE
457
+ mask[0] = RNDK::Color[:reverse]
458
458
  when 'S'
459
- mask[0] = Ncurses::A_STANDOUT
459
+ mask[0] = RNDK::Color[:standout]
460
460
  when 'U'
461
- mask[0] = Ncurses::A_UNDERLINE
461
+ mask[0] = RNDK::Color[:underline]
462
462
  end
463
463
 
464
464
  if mask[0] != 0
@@ -494,19 +494,19 @@ module RNDK
494
494
  # Also, alignment markers and tabs are lost.
495
495
  def RNDK.decodeAttribute (string, from, oldattr, newattr)
496
496
  table = {
497
- 'B' => Ncurses::A_BOLD,
498
- 'D' => Ncurses::A_DIM,
499
- 'K' => Ncurses::A_BLINK,
500
- 'R' => Ncurses::A_REVERSE,
501
- 'S' => Ncurses::A_STANDOUT,
502
- 'U' => Ncurses::A_UNDERLINE
497
+ 'B' => RNDK::Color[:bold],
498
+ 'D' => RNDK::Color[:dim],
499
+ 'K' => RNDK::Color[:blink],
500
+ 'R' => RNDK::Color[:reverse],
501
+ 'S' => RNDK::Color[:standout],
502
+ 'U' => RNDK::Color[:underline]
503
503
  }
504
504
 
505
505
  result = if string.nil? then '' else string end
506
506
  base_len = result.size
507
- tmpattr = oldattr & Ncurses::A_ATTRIBUTES
507
+ tmpattr = oldattr & RNDK::Color[:extract]
508
508
 
509
- newattr &= Ncurses::A_ATTRIBUTES
509
+ newattr &= RNDK::Color[:extract]
510
510
  if tmpattr != newattr
511
511
  while tmpattr != newattr
512
512
  found = false
@@ -7,9 +7,9 @@
7
7
  #
8
8
  # For example:
9
9
  #
10
- # rndkscreen = RNDK::Screen.new
10
+ # screen = RNDK::Screen.new
11
11
  # message = ["awesome quick label!"]
12
- # rndkscreen.popup_label message
12
+ # screen.popup_label message
13
13
  #
14
14
 
15
15
  # Since I'm including a lot of widgets here, maybe I
@@ -44,17 +44,19 @@ module RNDK
44
44
  # Creates a centered pop-up Label Widget that
45
45
  # waits until the user hits a character.
46
46
  #
47
- # @note: `message` must be an array of strings.
47
+ # @note: `message` must be a String or an Array of Strings.
48
48
  def popup_label message
49
- return if message.class != Array or message.empty?
50
-
51
49
  self.cleanly do
52
- popup = RNDK::Label.new(self, CENTER, CENTER, message, true, false)
53
- popup.draw(true)
50
+ popup = RNDK::Label.new(self, {
51
+ :x => RNDK::CENTER,
52
+ :y => RNDK::CENTER,
53
+ :text => message
54
+ })
55
+ popup.draw
54
56
 
55
57
  # Wait for some input.
56
58
  Ncurses.keypad(popup.win, true)
57
- popup.getch([])
59
+ popup.getch
58
60
  popup.destroy
59
61
  end
60
62
  end
@@ -63,18 +65,20 @@ module RNDK
63
65
  # the background of the dialog.
64
66
  #
65
67
  # @note: `message` must be an array of strings.
66
- def popup_label_attrib(message, attrib)
67
- return if message.class != Array or message.empty?
68
-
68
+ def popup_label_color(message, attrib)
69
69
  self.cleanly do
70
- popup = RNDK::Label.new(self, CENTER, CENTER, message, true, false)
71
- popup.setBackgroundAttrib attrib
72
- popup.draw(true)
70
+ popup = RNDK::Label.new(self, {
71
+ :x => RNDK::CENTER,
72
+ :y => RNDK::CENTER,
73
+ :text => message
74
+ })
75
+
76
+ popup.set_bg_color attrib
77
+ popup.draw
73
78
 
74
79
  # Wait for some input
75
80
  Ncurses.keypad(popup.win, true)
76
- popup.getch([])
77
-
81
+ popup.getch
78
82
  popup.destroy
79
83
  end
80
84
  end
@@ -87,22 +91,19 @@ module RNDK
87
91
  #
88
92
  # @note: `message` and `buttons` must be Arrays of Strings.
89
93
  def popup_dialog(message, buttons)
90
- return nil if message.class != Array or message.empty?
91
- return nil if buttons.class != Array or buttons.empty?
94
+ return nil if message.empty? or buttons.empty?
92
95
 
93
96
  choice = 0
94
97
  self.cleanly do
95
- popup = RNDK::DIALOG.new(self,
96
- RNDK::CENTER,
97
- RNDK::CENTER,
98
- message,
99
- message.size,
100
- buttons,
101
- buttons.size,
102
- Ncurses::A_REVERSE,
103
- true, true, false)
104
- popup.draw(true)
105
- choice = popup.activate('')
98
+ popup = RNDK::Dialog.new(self, {
99
+ :x => RNDK::CENTER,
100
+ :y => RNDK::CENTER,
101
+ :text => message,
102
+ :buttons => buttons
103
+ })
104
+
105
+ popup.draw
106
+ choice = popup.activate
106
107
  popup.destroy
107
108
  end
108
109
  choice
@@ -124,27 +125,23 @@ module RNDK
124
125
  selected = -1
125
126
 
126
127
  # Create the file viewer to view the file selected.
127
- viewer = RNDK::VIEWER.new(self,
128
- RNDK::CENTER,
129
- RNDK::CENTER,
130
- -6,
131
- -16,
132
- buttons,
133
- buttons.size,
134
- Ncurses::A_REVERSE,
135
- true,
136
- true)
128
+ viewer = RNDK::Viewer.new(self, {
129
+ :x => RNDK::CENTER,
130
+ :y => RNDK::CENTER,
131
+ :width => -6,
132
+ :height => -16,
133
+ :buttons => buttons,
134
+ :shadow => true
135
+ })
137
136
 
138
137
  # Set up the viewer title, and the contents of the widget.
139
- viewer.set(title,
140
- info,
141
- info.size,
142
- Ncurses::A_REVERSE,
143
- hide_control_chars,
144
- true,
145
- true)
138
+ viewer.set({
139
+ :title => title,
140
+ :items => info,
141
+ :hide_control_chars => hide_control_chars
142
+ })
146
143
 
147
- selected = viewer.activate([])
144
+ selected = viewer.activate
148
145
 
149
146
  # Make sure they exited normally.
150
147
  if viewer.exit_type != :NORMAL
@@ -172,7 +169,7 @@ module RNDK
172
169
  result = 0
173
170
 
174
171
  # Open the file and read the contents.
175
- lines = RNDK.read_file(filename, info)
172
+ lines = RNDK.read_file filename
176
173
 
177
174
  # If we couldn't read the file, return an error.
178
175
  if lines == -1
@@ -194,24 +191,16 @@ module RNDK
194
191
  #
195
192
  def select_file title
196
193
 
197
- fselect = RNDK::FSELECT.new(self,
198
- RNDK::CENTER,
199
- RNDK::CENTER,
200
- -4,
201
- -20,
202
- title,
203
- 'File: ',
204
- Ncurses::A_NORMAL,
205
- '_',
206
- Ncurses::A_REVERSE,
207
- '</5>',
208
- '</48>',
209
- '</N>',
210
- '</N>',
211
- true,
212
- false)
213
-
214
- filename = fselect.activate([])
194
+ fselect = RNDK::Fselect.new(self, {
195
+ :x => RNDK::CENTER,
196
+ :y => RNDK::CENTER,
197
+ :width =>-20,
198
+ :height => -4,
199
+ :title => title,
200
+ :label => 'File: '
201
+ })
202
+
203
+ filename = fselect.activate
215
204
 
216
205
  # Check the way the user exited the selector.
217
206
  if fselect.exit_type != :NORMAL
@@ -254,24 +243,21 @@ module RNDK
254
243
  width = [width, title.size].max
255
244
  width += 5
256
245
 
257
- scrollp = RNDK::Scroll.new(self,
258
- RNDK::CENTER,
259
- RNDK::CENTER,
260
- RNDK::RIGHT,
261
- width,
262
- height,
263
- title,
264
- list,
265
- numbers,
266
- Ncurses::A_REVERSE,
267
- true,
268
- false)
246
+ scrollp = RNDK::Scroll.new(self, {
247
+ :x => RNDK::CENTER,
248
+ :y => RNDK::CENTER,
249
+ :width => width,
250
+ :height => height,
251
+ :title => title,
252
+ :items => list,
253
+ :numbers => numbers
254
+ })
269
255
  if scrollp.nil?
270
256
  self.refresh
271
257
  return -1
272
258
  end
273
259
 
274
- selected = scrollp.activate([])
260
+ selected = scrollp.activate
275
261
 
276
262
  # Check how they exited.
277
263
  if scrollp.exit_type != :NORMAL
@@ -290,23 +276,16 @@ module RNDK
290
276
  # `title`, `label` and `initial_text` are passed to the Widget.
291
277
  def get_string(title, label, initial_text="")
292
278
 
293
- widget = RNDK::Entry.new(self,
294
- RNDK::CENTER,
295
- RNDK::CENTER,
296
- title,
297
- label,
298
- Ncurses::A_NORMAL,
299
- '.',
300
- :MIXED,
301
- 40,
302
- 0,
303
- 5000,
304
- true,
305
- false)
306
-
307
- widget.set_text(initial_text)
308
-
309
- value = widget.activate([])
279
+ widget = RNDK::Entry.new(self, {
280
+ :x => RNDK::CENTER,
281
+ :y => RNDK::CENTER,
282
+ :title => title,
283
+ :label => label,
284
+ :field_width => 40,
285
+ :initial_text => initial_text
286
+ })
287
+
288
+ value = widget.activate
310
289
 
311
290
  # Make sure they exited normally.
312
291
  if widget.exit_type != :NORMAL