ncumbra 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/umbra/listbox.rb CHANGED
@@ -5,7 +5,7 @@ require 'umbra/multiline'
5
5
  # Author: j kepler http://github.com/mare-imbrium/umbra
6
6
  # Date: 2018-03-19
7
7
  # License: MIT
8
- # Last update: 2018-05-18 14:16
8
+ # Last update: 2018-05-30 10:08
9
9
  # ----------------------------------------------------------------------------- #
10
10
  # listbox.rb Copyright (C) 2012-2018 j kepler
11
11
  # == TODO
@@ -17,13 +17,14 @@ require 'umbra/multiline'
17
17
  module Umbra
18
18
 
19
19
  ## Display a list of items.
20
- ## Adds selection capability to the Scrollable widget.
20
+ ## Adds selection capability to the Multiline widget.
21
+ ## Adds event :SELECT_ROW which fires on selection and unselection.
21
22
  #
22
23
  class Listbox < Multiline
23
24
 
24
25
  attr_accessor :selection_allowed # does this class allow row selection (should be class level)
25
26
  attr_accessor :selection_key # key used to select a row
26
- attr_accessor :selected_index # row selected, may change to plural
27
+ attr_reader :selected_index # row selected, may change to plural
27
28
  attr_property :selected_color_pair # row selected color_pair
28
29
  attr_property :selected_attr # row selected color_pair
29
30
  attr_accessor :selected_mark # row selected character
@@ -40,7 +41,8 @@ module Umbra
40
41
  @selected_mark = 'x' # row selected character
41
42
  @unselected_mark = ' ' # row unselected character (usually blank)
42
43
  @current_mark = '>' # row current character (default is >)
43
- register_events([:LIST_SELECTION_EVENT])
44
+ #register_events([:LIST_SELECTION_EVENT])
45
+ register_events([:SELECT_ROW])
44
46
  super
45
47
  end
46
48
 
@@ -64,14 +66,27 @@ module Umbra
64
66
  end
65
67
 
66
68
  ## Toggle current row's selection status.
67
- def toggle_selection
69
+ def toggle_selection _row=@current_index
68
70
  @repaint_required = true
69
- if @selected_index == @current_index
70
- @selected_index = nil
71
+ if @selected_index == _row
72
+ unselect_row _row
71
73
  else
72
- @selected_index = @current_index
74
+ select_row _row
75
+ end
76
+ end
77
+
78
+ ## select given row
79
+ def select_row _row=@current_index
80
+ @selected_index = _row
81
+ fire_handler :SELECT_ROW, self # use selected_index to know which one
82
+ end
83
+
84
+ ## unselect given row
85
+ def unselect_row _row=@current_index
86
+ if _row == @selected_index
87
+ @selected_index = nil
88
+ fire_handler :SELECT_ROW, self # use selected_index to know which one
73
89
  end
74
- fire_handler :LIST_SELECTION_EVENT, self # use selected_index to know which one
75
90
  end
76
91
 
77
92
  ## Paint the row.
@@ -142,16 +157,7 @@ module Umbra
142
157
  end
143
158
 
144
159
 
145
- =begin
146
- def cursor_forward
147
- blen = current_row().size-1
148
- @pcol += 1 if @pcol < blen
149
- end
150
- def cursor_backward
151
- @pcol -= 1 if @pcol > 0
152
- end
153
- =end
154
-
155
160
 
156
- end
161
+ end # class
157
162
  end # module
163
+ # vim: comments=sr\:##,mb\:##,el\:#/,\:## :
data/lib/umbra/menu.rb CHANGED
@@ -4,7 +4,7 @@
4
4
  # Author: j kepler http://github.com/mare-imbrium/canis/
5
5
  # Date: 2018-03-13
6
6
  # License: MIT
7
- # Last update: 2018-04-16 15:20
7
+ # Last update: 2018-06-01 12:38
8
8
  # ----------------------------------------------------------------------------- #
9
9
  # menu.rb Copyright (C) 2012-2018 j kepler
10
10
 
@@ -13,7 +13,7 @@
13
13
  # menu will only accept keys or arrow keys or C-c Esc to cancel
14
14
  # returns nil if C-c or Esc pressed.
15
15
  # Otherwise returns character pressed.
16
- # == TODO
16
+ # == Todo
17
17
  # depends on our window class which is minimal.
18
18
  # [ ] cursor should show on the row that is highlighted
19
19
  # [ ] Can we remove that dependency so this is independent
@@ -4,7 +4,7 @@
4
4
  # Author: j kepler http://github.com/mare-imbrium/canis/
5
5
  # Date: 2018-04-13 - 23:10
6
6
  # License: MIT
7
- # Last update: 2018-05-17 12:33
7
+ # Last update: 2018-06-01 14:37
8
8
  # ----------------------------------------------------------------------------- #
9
9
  # messagebox.rb Copyright (C) 2012-2018 j kepler
10
10
  # BUGS:
@@ -152,8 +152,8 @@ module Umbra
152
152
  def run
153
153
  repaint
154
154
  @form.pack # needs window
155
- @form.select_first_field ## otherwise on_enter of first won't fire
156
155
  @form.repaint
156
+ @form.select_first_field ## otherwise on_enter of first won't fire
157
157
  @window.wrefresh
158
158
  return handle_keys
159
159
  end
@@ -6,18 +6,26 @@ require 'umbra/widget'
6
6
  # Author: j kepler http://github.com/mare-imbrium/canis/
7
7
  # Date: 2018-05-08 - 11:54
8
8
  # License: MIT
9
- # Last update: 2018-05-22 12:27
9
+ # Last update: 2018-06-01 14:36
10
10
  # ----------------------------------------------------------------------------- #
11
11
  # multiline.rb Copyright (C) 2012-2018 j kepler
12
12
  #
13
13
  ## TODO search through text and put cursor on next result.
14
+ ## TODO allow setting of current_index programmatically
15
+ ## TODO is a row visible. visible? row. and make a row visible. programmatically
16
+ ## TODO insert delete a row (if editable)
14
17
  #/
15
18
 
16
19
 
17
20
  module Umbra
21
+
22
+ ## Base class for widgets that have multiple lines and are scrollable.
23
+ ## Preferably, use a concrete class such as Listbox, Table or Textbox.
24
+ ## This is not editable by default. To delete or insert rows, set editable to true.
18
25
  class Multiline < Widget
19
26
 
20
27
  attr_reader :list # array containing data (usually Strings)
28
+ attr_reader :panned_cols ## How may columns has widget panned to right
21
29
 
22
30
  # index of focussed row, starting 0, index into the list supplied
23
31
  attr_reader :current_index
@@ -27,7 +35,6 @@ module Umbra
27
35
  @editable = false
28
36
  @pstart = 0 # which row does printing start from
29
37
  @current_index = 0 # index of row on which cursor is
30
- #register_events([:LEAVE_ROW, :ENTER_ROW, :LIST_SELECTION_EVENT])
31
38
 
32
39
  ## PRESS event relates to pressing RETURN/ENTER (10)
33
40
  register_events([:LEAVE_ROW, :ENTER_ROW, :PRESS])
@@ -36,8 +43,9 @@ module Umbra
36
43
 
37
44
  map_keys
38
45
  @row_offset = 0
39
- @pcol = 0
40
- @curpos = 0
46
+ @panned_cols = 0 ## how many columns the view has panned.
47
+ ## The name means panned_cols
48
+ @curpos = 0 ## Widget defines an accessor on this.
41
49
  @repaint_required = true
42
50
  end
43
51
 
@@ -53,7 +61,7 @@ module Umbra
53
61
  @list = alist
54
62
  @repaint_required = true
55
63
  @pstart = @current_index = 0
56
- @pcol = 0
64
+ @panned_cols = 0
57
65
  $log.debug " before multiline list= CHANGED "
58
66
  fire_handler(:CHANGED, self) ## added 2018-05-08 -
59
67
  end
@@ -88,6 +96,7 @@ module Umbra
88
96
  return unless @repaint_required
89
97
  return unless @list
90
98
  win = @graphic
99
+ raise "window nil in multiline" unless win
91
100
  r,c = self.row, self.col
92
101
  _attr = @attr || NORMAL
93
102
  _color = @color_pair || CP_WHITE
@@ -230,15 +239,15 @@ module Umbra
230
239
  _width = self.width
231
240
  if ff
232
241
  if ff.size > _width
233
- # pcol can be greater than width then we get null
234
- if @pcol < ff.size
235
- ff = ff[@pcol..@pcol+_width-1]
242
+ # panned_cols can be greater than width then we get null
243
+ if @panned_cols < ff.size
244
+ ff = ff[@panned_cols..@panned_cols+_width-1]
236
245
  else
237
246
  ff = ""
238
247
  end
239
248
  else
240
- if @pcol < ff.size
241
- ff = ff[@pcol..-1]
249
+ if @panned_cols < ff.size
250
+ ff = ff[@panned_cols..-1]
242
251
  else
243
252
  ff = ""
244
253
  end
@@ -276,7 +285,8 @@ module Umbra
276
285
  super
277
286
  on_enter_row @current_index
278
287
  # basically I need to only highlight the current index, not repaint all OPTIMIZE
279
- touch ; repaint
288
+ #touch ; repaint ## 2018 why was i calling repaint here ??? causing error in messagebox since window nil
289
+ touch
280
290
  end
281
291
 
282
292
  # on leave of this multiline
@@ -284,7 +294,8 @@ module Umbra
284
294
  super
285
295
  on_leave_row @current_index
286
296
  # basically I need to only unhighlight the current index, not repaint all OPTIMIZE
287
- touch ; repaint
297
+ #touch ; repaint
298
+ touch ##; repaint ## why repaint here ?? when was this necessary ?
288
299
  end
289
300
 
290
301
  ## called when user leaves a row and when object is exited.
@@ -297,13 +308,15 @@ module Umbra
297
308
  fire_handler(:ENTER_ROW, [@current_index]) # 2018-03-26 - improve this
298
309
  # if cursor ahead of blen then fix it
299
310
  blen = current_row().size-1
311
+ ## why -1 on above line. Empty lines will give -1
312
+ blen = 0 if blen < 0
300
313
  if @curpos > blen
301
- @col_offset = blen - @pcol
314
+ @col_offset = blen - @panned_cols
302
315
  @curpos = blen
303
- if @pcol > blen
304
- @pcol = blen - self.width ## @int_width 2018-05-22 -
305
- @pcol = 0 if @pcol < 0
306
- @col_offset = blen - @pcol
316
+ if @panned_cols > blen
317
+ @panned_cols = blen - self.width ## @int_width 2018-05-22 -
318
+ @panned_cols = 0 if @panned_cols < 0
319
+ @col_offset = blen - @panned_cols
307
320
  end
308
321
  end
309
322
  @col_offset = 0 if @col_offset < 0
@@ -318,7 +331,7 @@ module Umbra
318
331
  # position cursor at start of field
319
332
  def cursor_home
320
333
  @curpos = 0
321
- @pcol = 0
334
+ @panned_cols = 0
322
335
  set_col_offset 0
323
336
  end
324
337
  # goto end of line.
@@ -327,13 +340,13 @@ module Umbra
327
340
  blen = current_row().length
328
341
  if blen < self.width
329
342
  set_col_offset blen # just after the last character
330
- @pcol = 0
343
+ @panned_cols = 0
331
344
  else
332
- @pcol = blen-self.width #+2 # 2 is due to mark and space XXX could be a problem with textbox
345
+ @panned_cols = blen-self.width #+2 # 2 is due to mark and space XXX could be a problem with textbox
333
346
  set_col_offset blen # just after the last character
334
347
  end
335
348
  @curpos = blen # this is position in array where editing or motion is to happen regardless of what you see
336
- # regardless of pcol (panning)
349
+ # regardless of panned_cols (panning)
337
350
  end
338
351
  # returns current row as String
339
352
  # 2018-04-11 - NOTE this may not be a String so we convert it to string before returning
@@ -347,8 +360,8 @@ module Umbra
347
360
  blen = current_row().size # -1
348
361
  if @curpos < blen
349
362
  if add_col_offset(1)==-1 # go forward if you can, else scroll
350
- #@pcol += 1 if @pcol < self.width
351
- @pcol += 1 if @pcol < blen
363
+ #@panned_cols += 1 if @panned_cols < self.width
364
+ @panned_cols += 1 if @panned_cols < blen
352
365
  end
353
366
  @curpos += 1
354
367
  end
@@ -360,8 +373,8 @@ module Umbra
360
373
  add_col_offset -1
361
374
  else
362
375
  # cur is on the first col, then scroll left
363
- if @pcol > 0
364
- @pcol -= 1
376
+ if @panned_cols > 0
377
+ @panned_cols -= 1
365
378
  @curpos -= 1
366
379
  else
367
380
  # do nothing
@@ -379,7 +392,7 @@ module Umbra
379
392
  @col_offset += num
380
393
  end
381
394
  # sets the visual cursor on the window at correct place
382
- # NOTE be careful of curpos - pcol being less than 0
395
+ # NOTE be careful of curpos - panned_cols being less than 0
383
396
  # @param [Integer] position in data on the line
384
397
  private def set_col_offset x=@curpos
385
398
  @curpos = x || 0 # NOTE we set the index of cursor here - WHY TWO THINGS ??? XXX
@@ -397,21 +410,21 @@ module Umbra
397
410
  end
398
411
  def scroll_right ## cursor_forward
399
412
  blen = current_row().size-1
400
- @pcol += 1 if @pcol < blen
413
+ @panned_cols += 1 if @panned_cols < blen
401
414
  end
402
415
  def scroll_left ##cursor_backward
403
- @pcol -= 1 if @pcol > 0
416
+ @panned_cols -= 1 if @panned_cols > 0
404
417
  end
405
418
  # go to start of file (first line)
406
419
  def goto_start
407
420
  @current_index = 0
408
- @pcol = @curpos = 0
421
+ @panned_cols = @curpos = 0
409
422
  set_col_offset 0
410
423
  end
411
424
  # go to end of file (last line)
412
425
  def goto_end
413
426
  @current_index = @list.size-1
414
- @pcol = @curpos = 0
427
+ @panned_cols = @curpos = 0
415
428
  end
416
429
  def scroll_down
417
430
  @current_index += @scroll_lines
@@ -433,7 +446,7 @@ module Umbra
433
446
  ## @param [Integer] ch: key caught by getch of window
434
447
  def handle_key ch
435
448
  old_current_index = @current_index
436
- old_pcol = @pcol
449
+ old_panned_cols = @panned_cols
437
450
  old_col_offset = @col_offset
438
451
 
439
452
  ret = super
@@ -449,7 +462,7 @@ module Umbra
449
462
  on_enter_row @current_index
450
463
  @repaint_required = true
451
464
  end
452
- @repaint_required = true if old_pcol != @pcol or old_col_offset != @col_offset
465
+ @repaint_required = true if old_panned_cols != @panned_cols or old_col_offset != @col_offset
453
466
  end
454
467
 
455
468
  ## convenience method for calling most used event of a widget
@@ -471,6 +484,56 @@ module Umbra
471
484
  #fire_handler :PRESS, aev
472
485
  fire_handler :PRESS, self
473
486
  end
487
+
488
+
489
+ ##### TO TEST THE REST ############
490
+
491
+ ## Is the given row visible
492
+ ## UNTESTED
493
+ def is_visible? _row
494
+ j = _row - @pstart
495
+ j >= 0 && j <= (self.height - 1)
496
+ end
497
+
498
+ # Ensure current row is visible, if not make it first row
499
+ # NOTE - need to check if its at end and then reduce scroll at rows, check_prow does that
500
+ #
501
+ # @param current_index (default if not given)
502
+ #
503
+ ## UNTESTED
504
+ def ensure_visible _row = @current_index
505
+ unless is_visible? _row
506
+ @pstart = _row
507
+ end
508
+ end
509
+
510
+ ## NOTE what about firing handlers when moving rows, i suppose that these will be called from a binding.
511
+ ## UNTESTED
512
+ def goto_line line
513
+ return if line < 0 or line >= self.row_count
514
+ @current_index = line
515
+ ensure_visible line
516
+ end
517
+
518
+ ## UNTESTED
519
+ def delete_at index
520
+ return unless @list
521
+ return unless @editable
522
+ @repaint_all = true
523
+ @list.delete_at index
524
+ end
525
+
526
+ ## UNTESTED
527
+ def insert index, line
528
+ return unless @list
529
+ return unless @editable
530
+ @repaint_all = true
531
+ @list.insert index, line
532
+ end
533
+
534
+ ## delegate << []= etc but we need to fire CHANGED
535
+
474
536
  end # class
475
537
  end # module
476
538
 
539
+ # vim: comments=sr\:##,mb\:##,el\:#/,\:## :
data/lib/umbra/pad.rb CHANGED
@@ -7,10 +7,10 @@
7
7
  * Author: jkepler
8
8
  * Date: 2018-03-28 14:30
9
9
  * License: MIT
10
- * Last update: 2018-04-26 08:29
10
+ * Last update: 2018-06-01 12:40
11
11
 
12
12
  == CHANGES
13
- == TODO
13
+ == Todo
14
14
  - should have option to wrap text
15
15
  - / search ?
16
16
  NOTE:
@@ -35,7 +35,7 @@ class Pad
35
35
  @config = config
36
36
  @rows = FFI::NCurses.LINES-1
37
37
  @cols = FFI::NCurses.COLS-1
38
- @prow = @pcol = 0 # show many cols we are panning
38
+ @prow = @pcol = 0 # how many cols we are panning
39
39
  @startrow = 0
40
40
  @startcol = 0
41
41
 
@@ -76,6 +76,8 @@ class Pad
76
76
  self.list=(config[:list])
77
77
  end
78
78
  end
79
+
80
+
79
81
  # minimum window creator method, not using a class.
80
82
  # However, some methods do require windows width and ht etc
81
83
  def create_window h, w, t, l
@@ -4,7 +4,7 @@
4
4
  # Author: j kepler http://github.com/mare-imbrium/canis/
5
5
  # Date: 2018-04-02 - 10:37
6
6
  # License: MIT
7
- # Last update: 2018-05-14 14:31
7
+ # Last update: 2018-06-01 12:31
8
8
  # ----------------------------------------------------------------------------- #
9
9
  # radiobutton.rb Copyright (C) 2012-2018 j kepler
10
10
 
@@ -27,7 +27,7 @@ module Umbra
27
27
  def initialize config={}, &block
28
28
  @surround_chars = ['(', ')'] if @surround_chars.nil?
29
29
  super
30
- $log.warn "XXX: FIXME Please set 'value' for radiobutton. If not sure, try setting it to the same value as 'text'" unless @value
30
+ $log.warn "XXX: FIXMe Please set 'value' for radiobutton. If not sure, try setting it to the same value as 'text'" unless @value
31
31
  @value ||= @text
32
32
  end
33
33