rndk 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/demos/appointment.rb +40 -25
  3. data/demos/clock.rb +22 -11
  4. data/demos/fileview.rb +141 -0
  5. data/examples/01-hello-world.rb +1 -2
  6. data/examples/02-colors.rb +58 -0
  7. data/examples/03-markup.rb +70 -0
  8. data/examples/04-quick-widgets.rb +72 -0
  9. data/examples/05-position-widget.rb +3 -6
  10. data/examples/calendar.rb +104 -0
  11. data/examples/scroll.rb +106 -0
  12. data/lib/rndk/alphalist.rb +14 -14
  13. data/lib/rndk/button.rb +21 -21
  14. data/lib/rndk/buttonbox.rb +18 -18
  15. data/lib/rndk/calendar.rb +333 -240
  16. data/lib/rndk/core/color.rb +136 -0
  17. data/lib/rndk/core/display.rb +23 -12
  18. data/lib/rndk/core/draw.rb +32 -26
  19. data/lib/rndk/core/markup.rb +561 -0
  20. data/lib/rndk/core/quick_widgets.rb +232 -12
  21. data/lib/rndk/core/screen.rb +16 -17
  22. data/lib/rndk/core/utils.rb +143 -0
  23. data/lib/rndk/core/widget.rb +133 -92
  24. data/lib/rndk/dialog.rb +17 -17
  25. data/lib/rndk/entry.rb +21 -21
  26. data/lib/rndk/fselect.rb +16 -16
  27. data/lib/rndk/graph.rb +10 -10
  28. data/lib/rndk/histogram.rb +10 -10
  29. data/lib/rndk/itemlist.rb +20 -20
  30. data/lib/rndk/label.rb +66 -45
  31. data/lib/rndk/marquee.rb +10 -10
  32. data/lib/rndk/matrix.rb +27 -27
  33. data/lib/rndk/mentry.rb +22 -22
  34. data/lib/rndk/menu.rb +14 -14
  35. data/lib/rndk/radio.rb +19 -19
  36. data/lib/rndk/scale.rb +21 -21
  37. data/lib/rndk/scroll.rb +19 -19
  38. data/lib/rndk/scroller.rb +2 -0
  39. data/lib/rndk/selection.rb +20 -20
  40. data/lib/rndk/slider.rb +21 -21
  41. data/lib/rndk/swindow.rb +20 -20
  42. data/lib/rndk/template.rb +21 -21
  43. data/lib/rndk/version.rb +1 -1
  44. data/lib/rndk/viewer.rb +18 -18
  45. data/lib/rndk.rb +99 -777
  46. data/rndk.gemspec +1 -1
  47. metadata +12 -3
@@ -4,15 +4,23 @@
4
4
  # ## Usage
5
5
  #
6
6
  # They're all methods to the RNDK::Screen class.
7
+ #
7
8
  # For example:
8
9
  #
9
- # curses_win = Ncurses.initscr
10
- # rndkscreen = RNDK::Screen.new(curses_win)
10
+ # rndkscreen = RNDK::Screen.new
11
11
  # message = ["awesome quick label!"]
12
- # rndkscreen.popupLabel message
12
+ # rndkscreen.popup_label message
13
13
  #
14
+
15
+ # Since I'm including a lot of widgets here, maybe I
16
+ # should make the user require 'quick_widgets'
17
+ # explicitly?
14
18
  require 'rndk/label'
15
19
  require 'rndk/dialog'
20
+ require 'rndk/viewer'
21
+ require 'rndk/fselect'
22
+ require 'rndk/entry'
23
+ require 'rndk/scroll'
16
24
 
17
25
  module RNDK
18
26
  class Screen
@@ -37,13 +45,11 @@ module RNDK
37
45
  # waits until the user hits a character.
38
46
  #
39
47
  # @note: `message` must be an array of strings.
40
- def popupLabel message
48
+ def popup_label message
41
49
  return if message.class != Array or message.empty?
42
50
 
43
51
  self.cleanly do
44
- count = message.size
45
-
46
- popup = RNDK::LABEL.new(self, CENTER, CENTER, message, count, true, false)
52
+ popup = RNDK::LABEL.new(self, CENTER, CENTER, message, true, false)
47
53
  popup.draw(true)
48
54
 
49
55
  # Wait for some input.
@@ -57,11 +63,11 @@ module RNDK
57
63
  # the background of the dialog.
58
64
  #
59
65
  # @note: `message` must be an array of strings.
60
- def popupLabelAttrib(message, attrib)
66
+ def popup_label_attrib(message, attrib)
61
67
  return if message.class != Array or message.empty?
62
68
 
63
69
  self.cleanly do
64
- popup = RNDK::LABEL.new(self, CENTER, CENTER, message, message.size, true, false)
70
+ popup = RNDK::LABEL.new(self, CENTER, CENTER, message, true, false)
65
71
  popup.setBackgroundAttrib attrib
66
72
  popup.draw(true)
67
73
 
@@ -76,14 +82,15 @@ module RNDK
76
82
  # Shows a centered pop-up Dialog box with `message` label and
77
83
  # each button label on `buttons`.
78
84
  #
79
- # @returns The user choice or `nil` if wrong parameters
80
- # were given.
85
+ # @return The user choice or `nil` if wrong parameters
86
+ # were given.
81
87
  #
82
88
  # @note: `message` and `buttons` must be Arrays of Strings.
83
- def popupDialog(message, buttons)
89
+ def popup_dialog(message, buttons)
84
90
  return nil if message.class != Array or message.empty?
85
91
  return nil if buttons.class != Array or buttons.empty?
86
92
 
93
+ choice = 0
87
94
  self.cleanly do
88
95
  popup = RNDK::DIALOG.new(self,
89
96
  RNDK::CENTER,
@@ -101,6 +108,219 @@ module RNDK
101
108
  choice
102
109
  end
103
110
 
111
+ # Display a long string set `info` in a Viewer Widget.
112
+ #
113
+ # `title` and `buttons` are applied to the Widget.
114
+ #
115
+ # `hide_control_chars` tells if we want to hide those
116
+ # ugly `^J`, `^M` chars.
117
+ #
118
+ # @return The index of the selected button.
119
+ # @note `info` and `buttons` must be Arrays of Strings.
120
+ def view_info(title, info, buttons, hide_control_chars=true)
121
+ return nil if info.class != Array or info.empty?
122
+ return nil if buttons.class != Array or buttons.empty?
123
+
124
+ selected = -1
125
+
126
+ # 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)
137
+
138
+ # 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)
146
+
147
+ selected = viewer.activate([])
148
+
149
+ # Make sure they exited normally.
150
+ if viewer.exit_type != :NORMAL
151
+ viewer.destroy
152
+ return -1
153
+ end
154
+
155
+ # Clean up and return the button index selected
156
+ viewer.destroy
157
+ selected
158
+ end
159
+
160
+ # Reads `filename`'s contents and display it on a Viewer Widget.
161
+ #
162
+ # `title` and `buttons` are applied to the Widget.
163
+ #
164
+ # The viewer shows the contents of the file supplied by the
165
+ # `filename` value.
166
+ #
167
+ # It returns the index of the button selected, or -1 if the file
168
+ # does not exist or if the widget was exited early.
169
+ def view_file(title, filename, buttons)
170
+
171
+ info = []
172
+ result = 0
173
+
174
+ # Open the file and read the contents.
175
+ lines = RNDK.read_file(filename, info)
176
+
177
+ # If we couldn't read the file, return an error.
178
+ if lines == -1
179
+ result = lines
180
+ else
181
+ result = self.view_info(title,
182
+ info,
183
+ buttons,
184
+ true)
185
+ end
186
+ result
187
+ end
188
+
189
+ # Displays a file-selection dialog with `title`.
190
+ #
191
+ # @return The selected filename, or `nil` if none was selected.
192
+ #
193
+ # TODO FIXME This widget is VERY buggy.
194
+ #
195
+ def select_file title
196
+
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([])
215
+
216
+ # Check the way the user exited the selector.
217
+ if fselect.exit_type != :NORMAL
218
+ fselect.destroy
219
+ self.refresh
220
+ return nil
221
+ end
222
+
223
+ # Otherwise...
224
+ fselect.destroy
225
+ self.refresh
226
+ filename
227
+ end
228
+
229
+ # Display a scrollable `list` of strings in a Dialog, allowing
230
+ # the user to select one.
231
+ #
232
+ # @return The index in the list of the value selected.
233
+ #
234
+ # If `numbers` is true, the displayed list items will
235
+ # be numbered.
236
+ #
237
+ # @note `list` must be Arrays of Strings.
238
+ def get_list_index(title, list, numbers)
239
+ return nil if list.class != Array or list.empty?
240
+
241
+ selected = -1
242
+ height = 10
243
+ width = -1
244
+ len = 0
245
+
246
+ # Determine the height of the list.
247
+ if list.size < 10
248
+ height = list.size + if title.size == 0 then 2 else 3 end
249
+ end
250
+
251
+ # Determine the width of the list.
252
+ list.each { |item| width = [width, item.size + 10].max }
253
+
254
+ width = [width, title.size].max
255
+ width += 5
256
+
257
+ scrollp = RNDK::SCROLL.new(self,
258
+ RNDK::CENTER,
259
+ RNDK::CENTER,
260
+ RNDK::RIGHT,
261
+ height,
262
+ width,
263
+ title,
264
+ list,
265
+ list.size,
266
+ numbers,
267
+ Ncurses::A_REVERSE,
268
+ true,
269
+ false)
270
+ if scrollp.nil?
271
+ self.refresh
272
+ return -1
273
+ end
274
+
275
+ selected = scrollp.activate([])
276
+
277
+ # Check how they exited.
278
+ if scrollp.exit_type != :NORMAL
279
+ selected = -1
280
+ end
281
+
282
+ scrollp.destroy
283
+ self.refresh
284
+
285
+ selected
286
+ end
287
+
288
+ # Pops up an Entry Widget and returns the value supplied
289
+ # by the user.
290
+ #
291
+ # `title`, `label` and `initial_value` are passed to the Widget.
292
+ def get_string(title, label, initial_value="")
293
+
294
+ widget = RNDK::ENTRY.new(self,
295
+ RNDK::CENTER,
296
+ RNDK::CENTER,
297
+ title,
298
+ label,
299
+ Ncurses::A_NORMAL,
300
+ '.',
301
+ :MIXED,
302
+ 40,
303
+ 0,
304
+ 5000,
305
+ true,
306
+ false)
307
+
308
+ widget.setValue(initial_value)
309
+
310
+ value = widget.activate([])
311
+
312
+ # Make sure they exited normally.
313
+ if widget.exit_type != :NORMAL
314
+ widget.destroy
315
+ return nil
316
+ end
317
+
318
+ # Return a copy of the string typed in.
319
+ value = widget.getValue.clone
320
+ widget.destroy
321
+
322
+ value
323
+ end
104
324
  end
105
325
  end
106
326
 
@@ -1,3 +1,4 @@
1
+ require 'rndk/core/quick_widgets'
1
2
 
2
3
  module RNDK
3
4
 
@@ -8,10 +9,10 @@ module RNDK
8
9
  #
9
10
  # The only methods you should worry about are:
10
11
  #
11
- # * #initialize
12
- # * #end_rndk
13
- # * #draw or #refresh
14
- # * #erase
12
+ # * Screen#initialize
13
+ # * Screen#end_rndk
14
+ # * Screen#draw or #refresh
15
+ # * Screen#erase
15
16
  #
16
17
  # ## Developer Notes
17
18
  #
@@ -27,6 +28,16 @@ module RNDK
27
28
  EXITOK = 1
28
29
  EXITCANCEL = 2
29
30
 
31
+ # Returns the whole terminal screen width.
32
+ def self.width
33
+ Ncurses.COLS
34
+ end
35
+
36
+ # Returns the whole terminal screen height.
37
+ def self.height
38
+ Ncurses.LINES
39
+ end
40
+
30
41
  # Takes a Ncurses `WINDOW*` pointer and creates a CDKScreen.
31
42
  #
32
43
  # This also starts Ncurses, if it wasn't started before or
@@ -173,16 +184,6 @@ module RNDK
173
184
  end
174
185
  end
175
186
 
176
- # Refresh a raw Ncurses window.
177
- #
178
- # FIXME(original): this should be rewritten to use the panel library, so
179
- # it would not be necessary to touch the window to ensure that it covers
180
- # other windows.
181
- def self.refresh_window win
182
- Ncurses.touchwin win
183
- Ncurses.wrefresh win
184
- end
185
-
186
187
  # Redraws all Widgets inside this Screen.
187
188
  def draw
188
189
  self.refresh
@@ -193,7 +194,7 @@ module RNDK
193
194
  focused = -1
194
195
  visible = -1
195
196
 
196
- RNDK::Screen.refresh_window(@window)
197
+ RNDK.window_refresh(@window)
197
198
 
198
199
  # We erase all the invisible objects, then only draw it all back, so
199
200
  # that the objects can overlap, and the visible ones will always be
@@ -265,5 +266,3 @@ module RNDK
265
266
  end
266
267
  end
267
268
 
268
- require 'rndk/core/quick_widgets'
269
-
@@ -0,0 +1,143 @@
1
+ # Utility functions - misc things.
2
+
3
+ module RNDK
4
+
5
+ # Number of digits `value` has when represented as a String.
6
+ def RNDK.intlen value
7
+ value.to_str.size
8
+ end
9
+
10
+ # Opens the current directory and reads the contents.
11
+ def RNDK.getDirectoryContents(directory, list)
12
+ counter = 0
13
+
14
+ Dir.foreach(directory) do |filename|
15
+ next if filename == '.'
16
+ list << filename
17
+ end
18
+
19
+ list.sort!
20
+ return list.size
21
+ end
22
+
23
+ # This looks for a subset of a word in the given list
24
+ def RNDK.searchList(list, list_size, pattern)
25
+ index = -1
26
+
27
+ if pattern.size > 0
28
+ (0...list_size).each do |x|
29
+ len = [list[x].size, pattern.size].min
30
+ ret = (list[x][0...len] <=> pattern)
31
+
32
+ # If 'ret' is less than 0 then the current word is alphabetically
33
+ # less than the provided word. At this point we will set the index
34
+ # to the current position. If 'ret' is greater than 0, then the
35
+ # current word is alphabetically greater than the given word. We
36
+ # should return with index, which might contain the last best match.
37
+ # If they are equal then we've found it.
38
+ if ret < 0
39
+ index = ret
40
+ else
41
+ if ret == 0
42
+ index = x
43
+ end
44
+ break
45
+ end
46
+ end
47
+ end
48
+ return index
49
+ end
50
+
51
+ # This function checks to see if a link has been requested
52
+ def RNDK.checkForLink (line, filename)
53
+ f_pos = 0
54
+ x = 3
55
+ if line.nil?
56
+ return -1
57
+ end
58
+
59
+ # Strip out the filename.
60
+ if line[0] == L_MARKER && line[1] == 'F' && line[2] == '='
61
+ while x < line.size
62
+ if line[x] == R_MARKER
63
+ break
64
+ end
65
+ if f_pos < RNDK_PATHMAX
66
+ filename << line[x]
67
+ f_pos += 1
68
+ end
69
+ x += 1
70
+ end
71
+ end
72
+ return f_pos != 0
73
+ end
74
+
75
+ # Returns the filename portion of the given pathname, i.e.
76
+ # after the last slash.
77
+ #
78
+ # For now this function is just a wrapper for `File.basename`,
79
+ # being kept for portability.
80
+ def RNDK.baseName (pathname)
81
+ File.basename(pathname)
82
+ end
83
+
84
+ # Returns the directory for the given pathname, i.e. the part before the
85
+ # last slash
86
+ # For now this function is just a wrapper for File.dirname kept for ease of
87
+ # porting and will be completely replaced in the future
88
+ def RNDK.dirName (pathname)
89
+ File.dirname(pathname)
90
+ end
91
+
92
+ # If the dimension is a negative value, the dimension will be the full
93
+ # height/width of the parent window - the value of the dimension. Otherwise,
94
+ # the dimension will be the given value.
95
+ def RNDK.setWidgetDimension (parent_dim, proposed_dim, adjustment)
96
+ # If the user passed in FULL, return the parents size
97
+ if proposed_dim == FULL or proposed_dim == 0
98
+ parent_dim
99
+ elsif proposed_dim >= 0
100
+ # if they gave a positive value, return it
101
+
102
+ if proposed_dim >= parent_dim
103
+ parent_dim
104
+ else
105
+ proposed_dim + adjustment
106
+ end
107
+ else
108
+ # if they gave a negative value then return the dimension
109
+ # of the parent plus the value given
110
+ #
111
+ if parent_dim + proposed_dim < 0
112
+ parent_dim
113
+ else
114
+ parent_dim + proposed_dim
115
+ end
116
+ end
117
+ end
118
+
119
+ # Reads a file and concatenate it's lines into `array`.
120
+ #
121
+ # @note The lines don't end with '\n'.
122
+ def RNDK.read_file(filename, array)
123
+ begin
124
+ fd = File.new(filename, "r")
125
+ rescue
126
+ return nil
127
+ end
128
+
129
+ lines = fd.readlines.map do |line|
130
+ if line.size > 0 && line[-1] == "\n"
131
+ line[0...-1]
132
+ else
133
+ line
134
+ end
135
+ end
136
+
137
+ array.concat lines
138
+ fd.close
139
+ array.size
140
+ end
141
+
142
+ end
143
+