rbcurse 1.4.0 → 1.4.1.pre2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. data/NOTES +68 -0
  2. data/README.markdown +73 -330
  3. data/TODO2.txt +2 -2
  4. data/VERSION +1 -1
  5. data/examples/abasiclist.rb +8 -2
  6. data/examples/alpmenu.rb +1 -1
  7. data/examples/app.rb +1 -24
  8. data/examples/appdirtree.rb +1 -1
  9. data/examples/appemail.rb +8 -14
  10. data/examples/appemaillb.rb +2 -2
  11. data/examples/appgcompose.rb +7 -5
  12. data/examples/common/file.rb +40 -0
  13. data/examples/{rmail.rb → common/rmail.rb} +0 -0
  14. data/examples/data/README.markdown +9 -0
  15. data/examples/data/brew.txt +38 -0
  16. data/examples/data/color.2 +37 -0
  17. data/examples/data/gemlist.txt +60 -0
  18. data/examples/data/lotr.txt +12 -0
  19. data/examples/data/ports.txt +136 -0
  20. data/examples/data/tasks.txt +27 -0
  21. data/examples/{todocsv.csv → data/todocsv.csv} +0 -0
  22. data/examples/data/unix1.txt +21 -0
  23. data/examples/data/unix2.txt +11 -0
  24. data/examples/dbdemo.rb +49 -14
  25. data/examples/{appgmail.rb → deprecated/appgmail.rb} +1 -1
  26. data/examples/{splitp.rb → deprecated/splitp.rb} +0 -0
  27. data/examples/{testscrolllb.rb → deprecated/testscrolllb.rb} +0 -0
  28. data/examples/{testscrollp.rb → deprecated/testscrollp.rb} +0 -0
  29. data/examples/{testscrollta.rb → deprecated/testscrollta.rb} +0 -0
  30. data/examples/{testscrolltable.rb → deprecated/testscrolltable.rb} +0 -0
  31. data/examples/{testsplit.rb → deprecated/testsplit.rb} +0 -0
  32. data/examples/{testsplit2.rb → deprecated/testsplit2.rb} +0 -0
  33. data/examples/{testsplit3.rb → deprecated/testsplit3.rb} +0 -0
  34. data/examples/{testsplit3_1.rb → deprecated/testsplit3_1.rb} +0 -0
  35. data/examples/{testsplit3a.rb → deprecated/testsplit3a.rb} +0 -0
  36. data/examples/{testsplit3b.rb → deprecated/testsplit3b.rb} +0 -0
  37. data/examples/{testsplitta.rb → deprecated/testsplitta.rb} +0 -0
  38. data/examples/{testsplittv.rb → deprecated/testsplittv.rb} +0 -0
  39. data/examples/{testsplittvv.rb → deprecated/testsplittvv.rb} +0 -0
  40. data/examples/{testtpane.rb → deprecated/testtpane.rb} +0 -0
  41. data/examples/{testtpane2.rb → deprecated/testtpane2.rb} +0 -0
  42. data/examples/{testtpanetable.rb → deprecated/testtpanetable.rb} +0 -0
  43. data/examples/dirtree.rb +17 -7
  44. data/examples/experimental/resultsetdemo.rb +280 -0
  45. data/examples/experimental/testmform.rb +35 -0
  46. data/examples/{testscroller.rb → experimental/testscroller.rb} +1 -19
  47. data/examples/experimental/teststackflow.rb +111 -0
  48. data/examples/menu1.rb +1 -0
  49. data/examples/multispl.rb +4 -4
  50. data/examples/newmessagebox.rb +130 -0
  51. data/examples/newtabbedwindow.rb +100 -0
  52. data/examples/newtesttabp.rb +0 -3
  53. data/examples/qdfilechooser.rb +0 -3
  54. data/examples/rfe.rb +134 -18
  55. data/examples/rfe_renderer.rb +48 -2
  56. data/examples/sqlc.rb +2 -1
  57. data/examples/sqlm.rb +0 -2
  58. data/examples/table1.rb +1 -7
  59. data/examples/term2.rb +4 -1
  60. data/examples/test1.rb +0 -5
  61. data/examples/test2.rb +42 -31
  62. data/examples/testapp2.rb +8 -1
  63. data/examples/testchars.rb +0 -4
  64. data/examples/testcombo.rb +5 -9
  65. data/examples/testkeypress.rb +0 -3
  66. data/examples/testmenu.rb +0 -4
  67. data/examples/testmulticomp.rb +3 -5
  68. data/examples/testmulticontainer.rb +94 -0
  69. data/examples/testtable.rb +0 -5
  70. data/examples/testtabp.rb +16 -18
  71. data/examples/testtodo.rb +1 -5
  72. data/examples/testwsshortcuts.rb +64 -0
  73. data/examples/testwsshortcuts2.rb +126 -0
  74. data/examples/viewtodo.rb +1 -4
  75. data/lib/rbcurse.rb +1 -1
  76. data/lib/rbcurse/app.rb +92 -69
  77. data/lib/rbcurse/applicationheader.rb +46 -6
  78. data/lib/rbcurse/celleditor.rb +1 -9
  79. data/lib/rbcurse/comboboxcellrenderer.rb +0 -4
  80. data/lib/rbcurse/common/ansiparser.rb +117 -0
  81. data/{examples → lib/rbcurse/common}/appmethods.rb +25 -0
  82. data/lib/rbcurse/common/basestack.rb +407 -0
  83. data/lib/rbcurse/common/bordertitle.rb +41 -0
  84. data/lib/rbcurse/common/chunk.rb +177 -0
  85. data/lib/rbcurse/common/colorparser.rb +71 -0
  86. data/lib/rbcurse/common/keydefs.rb +30 -0
  87. data/lib/rbcurse/common/widgetshortcuts.rb +302 -0
  88. data/lib/rbcurse/deprecated/README.markdown +12 -0
  89. data/lib/rbcurse/{rscrollpane.rb → deprecated/rscrollpane.rb} +0 -0
  90. data/lib/rbcurse/{rsplitpane.rb → deprecated/rsplitpane.rb} +0 -0
  91. data/lib/rbcurse/{rsplitpane2.rb → deprecated/rsplitpane2.rb} +0 -0
  92. data/lib/rbcurse/{rviewport.rb → deprecated/rviewport.rb} +0 -0
  93. data/lib/rbcurse/experimental/README.markdown +14 -0
  94. data/lib/rbcurse/experimental/resultsettextview.rb +585 -0
  95. data/lib/rbcurse/experimental/stackflow.rb +478 -0
  96. data/lib/rbcurse/extras/bottomline.rb +85 -16
  97. data/lib/rbcurse/extras/box.rb +58 -0
  98. data/lib/rbcurse/extras/directorylist.rb +1 -1
  99. data/lib/rbcurse/extras/horizlist.rb +203 -0
  100. data/lib/rbcurse/extras/listselectable.rb +8 -0
  101. data/lib/rbcurse/extras/multiform.rb +330 -0
  102. data/lib/rbcurse/extras/multilinelabel.rb +142 -0
  103. data/lib/rbcurse/extras/newmessagebox.rb +328 -0
  104. data/lib/rbcurse/extras/newtabbedpane.rb +612 -0
  105. data/lib/rbcurse/extras/newtabbedwindow.rb +68 -0
  106. data/lib/rbcurse/extras/padreader.rb +189 -0
  107. data/lib/rbcurse/extras/rcomboedit.rb +256 -0
  108. data/lib/rbcurse/extras/resultsetbrowser.rb +281 -0
  109. data/lib/rbcurse/extras/statusline.rb +44 -6
  110. data/lib/rbcurse/extras/tabularwidget.rb +141 -104
  111. data/lib/rbcurse/extras/textpad.rb +516 -0
  112. data/lib/rbcurse/extras/viewer.rb +2 -0
  113. data/lib/rbcurse/io.rb +120 -3
  114. data/lib/rbcurse/listcellrenderer.rb +2 -1
  115. data/lib/rbcurse/listkeys.rb +1 -1
  116. data/lib/rbcurse/listscrollable.rb +72 -7
  117. data/lib/rbcurse/rbasiclistbox.rb +64 -12
  118. data/lib/rbcurse/rchangeevent.rb +0 -1
  119. data/lib/rbcurse/rcombo.rb +54 -59
  120. data/lib/rbcurse/rcommandwindow.rb +46 -10
  121. data/lib/rbcurse/rcontainer.rb +415 -0
  122. data/lib/rbcurse/rdialogs.rb +242 -165
  123. data/lib/rbcurse/rinputdataevent.rb +0 -1
  124. data/lib/rbcurse/rlistbox.rb +19 -9
  125. data/lib/rbcurse/rmessagebox.rb +1 -5
  126. data/lib/rbcurse/rmulticontainer.rb +64 -61
  127. data/lib/rbcurse/rmultitextview.rb +0 -2
  128. data/lib/rbcurse/rpopupmenu.rb +0 -2
  129. data/lib/rbcurse/rscrollform.rb +61 -12
  130. data/lib/rbcurse/rtabbedpane.rb +89 -73
  131. data/lib/rbcurse/rtabbedwindow.rb +53 -211
  132. data/lib/rbcurse/rtable.rb +0 -2
  133. data/lib/rbcurse/rtextarea.rb +14 -14
  134. data/lib/rbcurse/rtextview.rb +165 -50
  135. data/lib/rbcurse/rvimsplit.rb +15 -12
  136. data/lib/rbcurse/rwidget.rb +404 -150
  137. data/lib/rbcurse/table/tablecellrenderer.rb +1 -4
  138. data/lib/rbcurse/table/tabledatecellrenderer.rb +0 -3
  139. data/lib/ver/ncurses.rb +1 -9
  140. data/lib/ver/rpad.rb +375 -0
  141. data/lib/ver/window.rb +262 -440
  142. metadata +73 -31
@@ -0,0 +1,281 @@
1
+ require 'rbcurse/rscrollform'
2
+ require 'fileutils'
3
+
4
+ # See tabbed pane, and add_cols nad add_rows, on_entr and set_form_row to see cursor issue
5
+ # remove main form
6
+ # don't make a widget just an object
7
+ # let it create own form.
8
+
9
+ # NOTE: experimental, not yet firmed up
10
+ # If you use in application, please copy to some application folder in case i change this.
11
+ # Can be used for print_help_page
12
+ # SUGGESTIONS WELCOME.
13
+ # @since 1.4.1
14
+ module RubyCurses
15
+
16
+ class ResultsetBrowser #< Widget
17
+ #include EventHandler
18
+ include ConfigSetup
19
+ include RubyCurses::Utils
20
+ #dsl_property :xxx
21
+ # I don't think width is actually used, windows width matters. What of height ?
22
+ # THATS WRONG , i cannot eat up window. I should use object dimentsion for pad,
23
+ # not window dimensions
24
+ dsl_accessor :row, :col, :height, :width
25
+ dsl_accessor :should_print_border
26
+
27
+ def initialize win, config={}, &block
28
+ @should_print_border = true
29
+ @v_window = win #form.window
30
+ @window = @v_window
31
+ @focusable = true
32
+ @editable = true
33
+ @old_index = @current_index = 0
34
+ @fields = nil
35
+ config_setup config
36
+ instance_eval &block if block_given?
37
+ init_vars
38
+ end
39
+ def map_keys
40
+ @v_form.bind_key(?\C-n) { @current_index += 1 if @current_index < @rows.count-1 }
41
+ @v_form.bind_key(?\C-p) { @current_index -= 1 if @current_index > 0 }
42
+ @mapped = true
43
+ end
44
+ def init_vars
45
+ @row ||= 0
46
+ @col ||= 0
47
+ @height ||= 15
48
+ @width ||= 50
49
+ @field_offset = 14 # where actual Field should start (leaving space for label)
50
+ @row_offset ||= 0
51
+ @col_offset ||= 1
52
+ @border_offset = 0
53
+ if @should_print_border
54
+ @border_offset = 1
55
+ end
56
+ @v_form = RubyCurses::ScrollForm.new @v_window
57
+ @v_form.display_h(@height-1-@border_offset*2) if @height
58
+ @v_form.display_w(@width-1-@border_offset*2) if @width
59
+ end
60
+ def data=(rows)
61
+ @rows = rows
62
+ end
63
+ def columns=(columns)
64
+ @columns = columns
65
+ h = @columns.count + 2
66
+ w = 150
67
+ #row = 1
68
+ #col = 1
69
+ row = @row + @border_offset
70
+ col = @col + @border_offset
71
+ @v_form.set_pad_dimensions(row, col, h, w)
72
+ @v_form.should_print_border(false) # this should use dimensions of object not window. # 2011-10-12 15:48:14
73
+ # currently I don't have space for any buttons or anything. The form takes all space of the window
74
+ # not of the object defined.
75
+ # I should be able to tell Scrollform to use only thismuch of window.
76
+ end
77
+ def set_form_row
78
+ f = @v_form.get_current_field
79
+ f.set_form_row
80
+ end
81
+ def handle_key ch
82
+ map_keys unless @mapped
83
+ $log.debug "XXX: RB HK got ch "
84
+ ret = @v_form.handle_key ch
85
+ #set_form_row
86
+ if ret == :UNHANDLED
87
+ @v_form.process_key ch, self
88
+ end
89
+ repaint
90
+ @v_window.wrefresh
91
+ end
92
+ def repaint
93
+ @fields ||= _create_fields
94
+ #alert "old #{@old_index} , #{@current_index} "
95
+ if @old_index != @current_index
96
+ #alert "index change"
97
+ row = @rows[@current_index]
98
+ @columns.each_with_index { |e, i|
99
+ value = row[i]
100
+ len = value.to_s.length
101
+ type=@rows[0].types[i]
102
+ if type == "TEXT"
103
+ value = value.gsub(/\n/," ") if value
104
+ end
105
+ f = @fields[i]
106
+ @fields[i].set_buffer(value)
107
+ if f.display_length < len && len < (@width - @field_offset)
108
+ @fields[i].display_length len
109
+ end
110
+ }
111
+ @v_form.repaint
112
+ @window.wrefresh
113
+ Ncurses::Panel.update_panels
114
+ @old_index = @current_index
115
+ end
116
+ end
117
+ # maybe not required since we don't have 2 forms now
118
+ def unused_on_enter
119
+ if $current_key == KEY_BTAB
120
+ c = @v_form.widgets.count-1
121
+ @v_form.select_field c
122
+ else
123
+ @v_form.select_field 0
124
+ end
125
+ end
126
+ private
127
+ def _create_fields
128
+ color = $datacolor
129
+ if @should_print_border
130
+ @v_window.print_border @row, @col, @height-1, @width, color #, Ncurses::A_REVERSE
131
+ @row_offset += 1
132
+ @col_offset += 1
133
+ end
134
+ $log.debug "XXX: ROWS#{@rows}"
135
+ $log.debug "XXX: COLS#{@columns}"
136
+ $log.debug "XXX: row#{@rows[@current_index]}"
137
+ fields = []
138
+ r = @row + @row_offset # row was for where to print the total object, not this
139
+ c = @col + @col_offset + @field_offset # 14 is to leave space for labels
140
+ v_form = @v_form
141
+ @columns.each_with_index { |e, index|
142
+ #break if index >= @height-1 # create only as much space we have, SUCKS but just trying till be scroll
143
+ #$log.debug "XXX: #{r} #{c} EACH #{e}, #{index}, #{@rows[@current_index][index]}"
144
+ value=@rows[@current_index][index]
145
+ type=@rows[0].types[index]
146
+ if type == "TEXT"
147
+ value = value.gsub(/\n/," ") if value
148
+ end
149
+ len = [value.to_s.length, (@width - @field_offset)].min
150
+ f = Field.new v_form do
151
+ name e
152
+ row r
153
+ col c
154
+ bgcolor 'blue'
155
+ highlight_background 'cyan'
156
+ set_buffer value
157
+ display_length len
158
+ set_label Label.new v_form, {'text' => e, 'color'=>'cyan'}
159
+ end
160
+ fields << f
161
+ r += 1
162
+ }
163
+ @v_form.repaint
164
+ @window.wrefresh
165
+ Ncurses::Panel.update_panels
166
+ #$log.debug "XXX: created fields "
167
+ return fields
168
+ end
169
+
170
+ # ADD HERE
171
+
172
+ end # class
173
+ end # module
174
+ module RubyCurses
175
+ # a data viewer for viewing some text or filecontents
176
+ # view filename, :close_key => KEY_RETURN
177
+ # send data in an array
178
+ # view Array, :close_key => KEY_RETURN, :layout => [0,0,23,80]
179
+ # when passing layout reserve 4 rows for window and border. So for 2 lines of text
180
+ # give 6 rows.
181
+ class Browser
182
+ def self.browse_sql dbname, tablename, sql, config={} #:yield: ???
183
+ raise "file not found" unless File.exist? dbname
184
+ require 'sqlite3'
185
+ db = SQLite3::Database.new(dbname)
186
+ columns, *rows = db.execute2(sql)
187
+ #$log.debug "XXX COLUMNS #{sql} "
188
+ content = rows
189
+ return nil if content.nil? or content[0].nil?
190
+ datatypes = content[0].types
191
+ self.browse db, tablename, columns, rows, config
192
+ end
193
+ # @param filename as string or content as array
194
+ # @yield textview object for further configuration before display
195
+ # NOTE: i am experimentally yielding textview object so i could supress borders
196
+ # just for kicks, but on can also bind_keys or events if one wanted.
197
+ #def self.view what, config={} #:yield: textview
198
+ def self.browse dbconn, tablename, columns, rows, config={} #:yield: ???
199
+ wt = 0 # top margin
200
+ wl = 0 # left margin
201
+ wh = Ncurses.LINES-wt-3 # height, goes to bottom of screen
202
+ ww = Ncurses.COLS-wl-3 # width, goes to right end
203
+ wt, wl, wh, ww = config[:layout] if config.has_key? :layout
204
+
205
+ fp = config[:title] || ""
206
+ pf = config.fetch(:print_footer, true)
207
+ ta = config.fetch(:title_attrib, 'bold')
208
+ fa = config.fetch(:footer_attrib, 'bold')
209
+
210
+ wh = Ncurses.LINES-0
211
+ ww = Ncurses.COLS-0
212
+ layout = { :height => wh, :width => ww, :top => wt, :left => wl }
213
+ #v_window = config[:window] # using previous window cause crash seg fault
214
+ #v_window ||= VER::Window.new(layout) # copywin gives -1 and prints nothing
215
+ v_window = VER::Window.root_window
216
+ v_window.printstring 0, 30, "Database Browser Demo", $datacolor
217
+ @form = RubyCurses::Form.new v_window # only for some widgets that are not editable
218
+ header = app_header "rbcurse ", :text_center => "ResultsetBrowser Demo", :text_right =>"New Improved!", :color => :black, :bgcolor => :white, :attr => :bold
219
+ sl = status_line :row => v_window.height == 0 ? Ncurses.LINES-1 : v.window.height-1
220
+ sl.command { "Record Navigation: C-n C-p. Scrolling M-n, M-p, M-l, M-h" }
221
+
222
+ @form.repaint
223
+
224
+ #rb = ResultsetBrowser.new v_form, :row => 2, :col => 2
225
+ rb = ResultsetBrowser.new v_window, :row => 2, :col => 2, :height => 20, :width => 95
226
+ rb.columns = columns
227
+ rb.data = rows
228
+ rb.repaint
229
+
230
+
231
+ # yielding textview so you may further configure or bind keys or events
232
+ begin
233
+ #v_form.repaint
234
+ v_window.wrefresh
235
+ Ncurses::Panel.update_panels
236
+ # allow closing using q and Ctrl-q in addition to any key specified
237
+ # user should not need to specify key, since that becomes inconsistent across usages
238
+ while((ch = v_window.getchar()) != ?\C-q.getbyte(0) )
239
+ break if ch == config[:close_key]
240
+ rb.handle_key ch
241
+ #v_form.handle_key ch
242
+ end
243
+ rescue => err
244
+ $log.error err.to_s
245
+ $log.error err.backtrace.join("\n")
246
+ alert err.to_s
247
+
248
+ ensure
249
+ v_window.destroy if !v_window.nil?
250
+ end
251
+ end
252
+ end # class
253
+ end # module
254
+ if __FILE__ == $PROGRAM_NAME
255
+ require 'rbcurse/app'
256
+
257
+ App.new do
258
+ header = app_header "rbcurse ", :text_center => "ResultsetBrowser Demo", :text_right =>"New Improved!", :color => :black, :bgcolor => :white, :attr => :bold
259
+ message "Press F10 to exit from here"
260
+ columns = ["Name","Age","City", "Country"]
261
+ data = [
262
+ [ "Rahul",31, "Delhi","India"],
263
+ [ "Dev",35, "Mumbai","India"],
264
+ [ "Jobs",56, "L.A","U.S.A"],
265
+ [ "Matz",40, "Tokyo","Nippon"]
266
+ ]
267
+
268
+ #RubyCurses::Browser.browse("dummy", "atable", columns, data, :close_key => FFI::NCurses::KEY_F10, :title => "Enter to close") do |t|
269
+ sql = "select id, type, priority, title from bugs"
270
+ sql = "select * from bugs"
271
+ RubyCurses::Browser.browse_sql("../../../bugzy.sqlite", "bugs", sql, :close_key => FFI::NCurses::KEY_F10, :title => "Enter to close", :window => @window) do |t|
272
+ # you may configure textview further here.
273
+ #t.suppress_borders true
274
+ #t.color = :black
275
+ #t.bgcolor = :white
276
+ # or
277
+ #t.attr = :reverse
278
+ end
279
+
280
+ end # app
281
+ end
@@ -9,7 +9,12 @@ module RubyCurses
9
9
  class StatusLine < Widget
10
10
 
11
11
  def initialize form, config={}, &block
12
- @row = Ncurses.LINES-3
12
+ if form.window.height == 0
13
+ @row = Ncurses.LINES-3 # fix, what about smaller windows, use window dimensions and watch out for 0,0
14
+ else
15
+ @row = form.window.height-3 # fix, what about smaller windows, use window dimensions and watch out for 0,0
16
+ end
17
+ # in root windows FIXME
13
18
  @col = 0
14
19
  super
15
20
  @focusable = false
@@ -19,7 +24,8 @@ module RubyCurses
19
24
  bind(:PROPERTY_CHANGE) { |e| @color_pair = nil ; }
20
25
  end
21
26
  #
22
- # command that returns a string that populates the status line.
27
+ # command that returns a string that populates the status line (left aligned)
28
+ # @see :right
23
29
  # See dbdemo.rb
24
30
  # e.g.
25
31
  # @l.command { "%-20s [DB: %-s | %-s ]" % [ Time.now, $current_db || "None", $current_table || "----"] }
@@ -28,23 +34,55 @@ module RubyCurses
28
34
  @command = blk
29
35
  @args = args
30
36
  end
37
+ alias :left :command
38
+
39
+ #
40
+ # Procudure for text to be right aligned in statusline
41
+ def right *args, &blk
42
+ @right_text = blk
43
+ @right_args = args
44
+ end
31
45
 
32
46
  # NOTE: I have not put a check of repaint_required, so this will print on each key-stroke OR
33
47
  # rather whenever form.repaint is called.
34
48
  def repaint
35
49
  @color_pair ||= get_color($datacolor, @color, @bgcolor)
50
+ len = @form.window.width
51
+ len = Ncurses.COLS if len == 0
36
52
 
37
53
  # first print dashes through
38
- @form.window.printstring @row, @col, "%s" % "-" * Ncurses.COLS, @color_pair, Ncurses::A_REVERSE
54
+ @form.window.printstring @row, @col, "%s" % "-" * len, @color_pair, Ncurses::A_REVERSE
39
55
 
40
56
  # now call the block to get current values
41
57
  if @command
42
- ftext = @command.call(self, @args) if @command
58
+ ftext = @command.call(self, @args)
43
59
  else
44
60
  status = $status_message ? $status_message.value : ""
45
- ftext = " %-20s | %s" % [Time.now, status] # should we print a default value just in case user doesn't
61
+ #ftext = " %-20s | %s" % [Time.now, status] # should we print a default value just in case user doesn't
62
+ ftext = status # should we print a default value just in case user doesn't
63
+ end
64
+ if ftext =~ /#\[/
65
+ @form.window.printstring_formatted @row, @col, ftext, $datacolor, Ncurses::A_REVERSE
66
+ else
67
+ @form.window.printstring @row, @col, ftext, $datacolor, Ncurses::A_REVERSE
68
+ end
69
+ # move this to formatted FIXME
70
+ #@form.window.printstring_or_chunks @row, @col, ftext, $datacolor, Ncurses::A_REVERSE
71
+
72
+ if @right_text
73
+ ftext = @right_text.call(self, @right_args)
74
+ if ftext =~ /#\[/
75
+ @form.window.printstring_formatted_right @row, nil, ftext, $datacolor, Ncurses::A_REVERSE
76
+ else
77
+ c = len - ftext.length
78
+ @form.window.printstring @row, c, ftext, $datacolor, Ncurses::A_REVERSE
79
+ end
80
+ else
81
+ t = Time.now
82
+ tt = t.strftime "%F %H:%M:%S"
83
+ ftext = "#[fg=green,bg=blue] %-20s" % [tt] # print a default
84
+ @form.window.printstring_formatted_right @row, nil, ftext, $datacolor, Ncurses::A_REVERSE
46
85
  end
47
- @form.window.printstring @row, @col, ftext, $datacolor, Ncurses::A_REVERSE
48
86
 
49
87
  @repaint_required = false
50
88
  end
@@ -71,11 +71,11 @@ module RubyCurses
71
71
  attr_accessor :numbering
72
72
  # default or custom sorter
73
73
  attr_reader :table_row_sorter
74
-
74
+
75
75
  # @group select related
76
76
  dsl_accessor :selection_mode
77
77
  dsl_accessor :selected_color, :selected_bgcolor, :selected_attr
78
-
78
+
79
79
  dsl_property :show_selector # boolean
80
80
  dsl_property :row_selected_symbol
81
81
  dsl_property :row_unselected_symbol
@@ -172,30 +172,39 @@ module RubyCurses
172
172
  @column_pointer = Circular.new @columns.size()-1
173
173
  end
174
174
  alias :headings= :columns=
175
- ##
176
- # send in a list of data
177
- #
178
- # @param [Array / Tabular] data to be displayed
179
- def set_content list
180
- if list.is_a? RubyCurses::Tabular
181
- @list = list
182
- elsif list.is_a? Array
183
- @list = list
184
- else
185
- raise "set_content expects Array not #{list.class}"
186
- end
187
- if @table_row_sorter
188
- @table_row_sorter.model=@list
189
- else
190
- @table_row_sorter = TableRowSorter.new @list
175
+ ##
176
+ # send in a list of data
177
+ # sorting will only happen if data passed using set_content
178
+ # NOTE: why doesn't set_content take in columns
179
+ # @param [Array / Tabular] data to be displayed
180
+ def set_content list, columns=nil
181
+ if list.is_a? RubyCurses::Tabular
182
+ @list = list
183
+ elsif list.is_a? Array
184
+ @list = list
185
+ else
186
+ raise "set_content expects Array not #{list.class}"
187
+ end
188
+ if @table_row_sorter
189
+ @table_row_sorter.model=@list
190
+ else
191
+ @table_row_sorter = TableRowSorter.new @list
192
+ end
193
+ # adding columns setting here 2011-10-16
194
+ self.columns = columns if columns
195
+ @current_index = @_header_adjustment # but this is set when columns passed
196
+ @toprow = 0
197
+ @second_time = false # so that reestimation of column_widths
198
+ @repaint_required = true
199
+ @recalc_required = true
200
+ self
191
201
  end
192
- @current_index = @_header_adjustment
193
- @toprow = 0
194
- @second_time = false # so that reestimation of column_widths
195
- @repaint_required = true
196
- @recalc_required = true
202
+ def data=(data)
203
+ set_content(data, nil)
197
204
  end
205
+
198
206
  # add a row of data
207
+ # NOTE: this is not creating a table sorter
199
208
  # @param [Array] an array containing entries for each column
200
209
  def add array
201
210
  @list ||= []
@@ -206,6 +215,10 @@ module RubyCurses
206
215
  alias :<< :add
207
216
  alias :add_row :add
208
217
  alias :append :add
218
+ def create_default_sorter
219
+ raise "Data not sent in." unless @list
220
+ @table_row_sorter = TableRowSorter.new @list
221
+ end
209
222
  def remove_all
210
223
  @list = []
211
224
  init_vars
@@ -301,7 +314,7 @@ module RubyCurses
301
314
  raise "#{self.class} needs height" unless @height
302
315
 
303
316
  $log.debug " #{@name} print_borders, #{@graphic.name} "
304
-
317
+
305
318
  bordercolor = @border_color || $datacolor
306
319
  borderatt = @border_attrib || Ncurses::A_NORMAL
307
320
  @graphic.print_border @row, @col, @height-1, @width, bordercolor, borderatt
@@ -313,11 +326,25 @@ module RubyCurses
313
326
  @graphic.printstring( @row, @col+(@width-@title.length)/2, @title, $datacolor, @title_attrib) unless @title.nil?
314
327
  end
315
328
  def print_foot #:nodoc:
316
- @footer_attrib ||= Ncurses::A_REVERSE
317
- footer = "R: #{@current_index+1}, C: #{@curpos+@pcol}, #{@list.length} lines "
318
- #$log.debug " print_foot calling printstring with #{@row} + #{@height} -1, #{@col}+2"
319
- @graphic.printstring( @row + @height -1 , @col+2, footer, $datacolor, @footer_attrib)
329
+ @footer_attrib ||= Ncurses::A_DIM
330
+ gb = get_color($datacolor, 'green','black')
331
+ if @current_index == @toprow
332
+ footer = "%15s" % " [ header row ]"
333
+ else
334
+ footer = "%15s" % " [#{@current_index}/ #{@list.length} ]"
335
+ end
336
+ pos = @col + 2
337
+ right = true
338
+ if right
339
+ pos = @col + @width - footer.length - 1
340
+ end
341
+ @graphic.printstring( @row + @height -1 , pos, footer, gb, @footer_attrib)
320
342
  @repaint_footer_required = false # 2010-01-23 22:55
343
+ #@footer_attrib ||= Ncurses::A_REVERSE
344
+ #footer = "R: #{@current_index+1}, C: #{@curpos+@pcol}, #{@list.length} lines "
345
+ ##$log.debug " print_foot calling printstring with #{@row} + #{@height} -1, #{@col}+2"
346
+ #@graphic.printstring( @row + @height -1 , @col+2, footer, $datacolor, @footer_attrib)
347
+ #@repaint_footer_required = false # 2010-01-23 22:55
321
348
  end
322
349
  ### FOR scrollable ###
323
350
  def get_content
@@ -368,10 +395,10 @@ module RubyCurses
368
395
  ret = up
369
396
  get_window.ungetch(KEY_BTAB) if ret == :NO_PREVIOUS_ROW
370
397
  check_curpos
371
-
398
+
372
399
  when KEY_DOWN, ?j.getbyte(0)
373
400
  ret = down
374
- get_window.ungetch(KEY_TAB) if ret == :NO_NEXT_ROW
401
+ #get_window.ungetch(KEY_TAB) if ret == :NO_NEXT_ROW
375
402
  check_curpos
376
403
  when KEY_LEFT, ?h.getbyte(0)
377
404
  cursor_backward
@@ -413,7 +440,7 @@ module RubyCurses
413
440
  # storing digits entered so we can multiply motion actions
414
441
  $multiplier *= 10 ; $multiplier += (ch-48)
415
442
  return 0
416
- #when ?\C-u.getbyte(0)
443
+ #when ?\C-u.getbyte(0)
417
444
  ## multiplier. Series is 4 16 64
418
445
  #@multiplier = (@multiplier == 0 ? 4 : @multiplier *= 4)
419
446
  #return 0
@@ -430,7 +457,7 @@ module RubyCurses
430
457
  ret = process_key ch, self
431
458
  rescue => err
432
459
  $error_message.value = err.to_s
433
- # @form.window.print_error_message # changed 2011 dts
460
+ # @form.window.print_error_message # changed 2011 dts
434
461
  alert err.to_s
435
462
  $log.error " Tabularwidget ERROR #{err} "
436
463
  $log.debug(err.backtrace.join("\n"))
@@ -515,12 +542,12 @@ module RubyCurses
515
542
  def cursor_forward #:nodoc:
516
543
  maxlen = @maxlen || @width-@internal_width
517
544
  repeatm {
518
- if @curpos < @width and @curpos < maxlen-1 # else it will do out of box
519
- @curpos += 1
520
- addcol 1
521
- else
522
- @pcol += 1 if @pcol <= @buffer.length
523
- end
545
+ if @curpos < @width and @curpos < maxlen-1 # else it will do out of box
546
+ @curpos += 1
547
+ addcol 1
548
+ else
549
+ @pcol += 1 if @pcol <= @buffer.length
550
+ end
524
551
  }
525
552
  set_form_col
526
553
  #@repaint_required = true
@@ -539,20 +566,20 @@ module RubyCurses
539
566
  #@repaint_required = true
540
567
  @repaint_footer_required = true # 2010-01-23 22:41
541
568
  if @form
542
- @form.addrowcol row, col
569
+ @form.addrowcol row, col
543
570
  else
544
571
  @parent_component.form.addrowcol num
545
572
  end
546
573
  end
547
574
  def cursor_backward #:nodoc:
548
575
  repeatm {
549
- if @curpos > 0
550
- @curpos -= 1
551
- set_form_col
552
- #addcol -1
553
- elsif @pcol > 0
554
- @pcol -= 1
555
- end
576
+ if @curpos > 0
577
+ @curpos -= 1
578
+ set_form_col
579
+ #addcol -1
580
+ elsif @pcol > 0
581
+ @pcol -= 1
582
+ end
556
583
  }
557
584
  #@repaint_required = true
558
585
  @repaint_footer_required = true # 2010-01-23 22:41
@@ -594,27 +621,27 @@ module RubyCurses
594
621
  0.upto(h - @_header_adjustment) do |hh|
595
622
  crow = tr+hh
596
623
  if crow < rc
597
- #focussed = @current_index == crow ? true : false
598
- content = tm[crow]
624
+ #focussed = @current_index == crow ? true : false
625
+ content = tm[crow]
599
626
 
600
- columnrow = false
601
- if content == :columns
602
- columnrow = true
603
- end
627
+ columnrow = false
628
+ if content == :columns
629
+ columnrow = true
630
+ end
604
631
 
605
- value = convert_value_to_text content, crow
632
+ value = convert_value_to_text content, crow
606
633
 
607
- @buffer = value if crow == @current_index
608
- # next call modified string. you may wanna dup the string.
609
- # rlistbox does
610
- sanitize value if @sanitization_required
611
- truncate value
612
- ## set the selector symbol if requested
613
- paint_selector crow, r+hh, c, acolor, @attr
634
+ @buffer = value if crow == @current_index
635
+ # next call modified string. you may wanna dup the string.
636
+ # rlistbox does
637
+ sanitize value if @sanitization_required
638
+ truncate value
639
+ ## set the selector symbol if requested
640
+ paint_selector crow, r+hh, c, acolor, @attr
614
641
 
615
- #@graphic.printstring r+hh, c, "%-*s" % [@width-@internal_width,value], acolor, @attr
616
- #print_data_row( r+hh, c, "%-*s" % [@width-@internal_width,value], acolor, @attr)
617
- print_data_row( r+hh, c+@left_margin, @width-@internal_width-@left_margin, value, acolor, @attr)
642
+ #@graphic.printstring r+hh, c, "%-*s" % [@width-@internal_width,value], acolor, @attr
643
+ #print_data_row( r+hh, c, "%-*s" % [@width-@internal_width,value], acolor, @attr)
644
+ print_data_row( r+hh, c+@left_margin, @width-@internal_width-@left_margin, value, acolor, @attr)
618
645
 
619
646
  else
620
647
  # clear rows
@@ -853,9 +880,11 @@ module RubyCurses
853
880
  end
854
881
 
855
882
  # returns true if cursor is on header row
883
+ # NOTE: I have no idea why row was used here. it is not working
856
884
  def header_row?
857
885
  return false if @columns.nil?
858
- 1 == @row + (@current_index-@toprow)
886
+ #1 == @row + (@current_index-@toprow)
887
+ @current_index == @toprow
859
888
  end
860
889
  # on pressing ENTER we send user some info, the calling program
861
890
  # would bind :PRESS
@@ -937,7 +966,7 @@ module RubyCurses
937
966
  end
938
967
 
939
968
  # Some supporting classes
940
-
969
+
941
970
  # This is our default table row sorter.
942
971
  # It does a multiple sort and allows for reverse sort also.
943
972
  # It's a pretty simple sorter and uses sort, not sort_by.
@@ -976,6 +1005,7 @@ module RubyCurses
976
1005
  def sort
977
1006
  return unless @model
978
1007
  return if @sort_keys.empty?
1008
+ $log.debug "TABULAR SORT KEYS #{sort_keys} "
979
1009
  @model.sort!{|x,y|
980
1010
  res = 0
981
1011
  @sort_keys.each { |ee|
@@ -1003,7 +1033,10 @@ module RubyCurses
1003
1033
  @sort_keys.delete index # in case its already there
1004
1034
  @sort_keys.delete(index*-1) # in case its already there
1005
1035
  @sort_keys.unshift index
1006
- # TODO delete beyond a max, default 3
1036
+ # don't let it go on increasing
1037
+ if @sort_keys.size > 3
1038
+ @sort_keys.pop
1039
+ end
1007
1040
  end
1008
1041
  end
1009
1042
  def set_sort_keys list
@@ -1071,43 +1104,47 @@ module RubyCurses
1071
1104
 
1072
1105
  end # modul
1073
1106
  if __FILE__ == $PROGRAM_NAME
1074
-
1075
- require 'rbcurse/app'
1076
- App.new do
1077
- t = TabularWidget.new @form, :row => 2, :col => 2, :height => 20, :width => 30
1078
- t.columns = ["Name ", "Age ", " Email "]
1079
- t.add %w{ rahul 33 r@ruby.org }
1080
- t << %w{ _why 133 j@gnu.org }
1081
- t << ["jane", "1331", "jane@gnu.org" ]
1082
- t.column_align 1, :right
1083
- s = TabularWidget.new @form, :row => 2, :col =>32 do |b|
1084
- b.columns = %w{ country continent text }
1085
- b << ["india","asia","a warm country" ]
1086
- b << ["japan","asia","a cool country" ]
1087
- b << ["russia","europe","a hot country" ]
1088
- #b.column_width 2, 30
1089
- end
1090
- s = TabularWidget.new @form , :row => 12, :col => 32 do |b|
1091
- b.columns = %w{ place continent text }
1092
- b << ["india","asia","a warm country" ]
1093
- b << ["japan","asia","a cool country" ]
1094
- b << ["russia","europe","a hot country" ]
1095
- b << ["sydney","australia","a dry country" ]
1096
- b << ["canberra","australia","a dry country" ]
1097
- b << ["ross island","antarctica","a dry country" ]
1098
- b << ["mount terror","antarctica","a windy country" ]
1099
- b << ["mt erebus","antarctica","a cold place" ]
1100
- b << ["siberia","russia","an icy city" ]
1101
- b << ["new york","USA","a fun place" ]
1102
- b.column_width 0, 12
1103
- b.column_width 1, 12
1104
- b.column_hidden 1, true
1105
- b.numbering = true ## FIXME BROKEN
1107
+
1108
+ require 'rbcurse/app'
1109
+ App.new do
1110
+ t = TabularWidget.new @form, :row => 2, :col => 2, :height => 20, :width => 30
1111
+ t.columns = ["Name ", "Age ", " Email "]
1112
+ t.add %w{ rahul 33 r@ruby.org }
1113
+ t << %w{ _why 133 j@gnu.org }
1114
+ t << ["jane", "1331", "jane@gnu.org" ]
1115
+ t.column_align 1, :right
1116
+ t.create_default_sorter
1117
+
1118
+ s = TabularWidget.new @form, :row => 2, :col =>32 do |b|
1119
+ b.columns = %w{ country continent text }
1120
+ b << ["india","asia","a warm country" ]
1121
+ b << ["japan","asia","a cool country" ]
1122
+ b << ["russia","europe","a hot country" ]
1123
+ #b.column_width 2, 30
1124
+ end
1125
+ s.create_default_sorter
1126
+ s = TabularWidget.new @form , :row => 12, :col => 32 do |b|
1127
+ b.columns = %w{ place continent text }
1128
+ b << ["india","asia","a warm country" ]
1129
+ b << ["japan","asia","a cool country" ]
1130
+ b << ["russia","europe","a hot country" ]
1131
+ b << ["sydney","australia","a dry country" ]
1132
+ b << ["canberra","australia","a dry country" ]
1133
+ b << ["ross island","antarctica","a dry country" ]
1134
+ b << ["mount terror","antarctica","a windy country" ]
1135
+ b << ["mt erebus","antarctica","a cold place" ]
1136
+ b << ["siberia","russia","an icy city" ]
1137
+ b << ["new york","USA","a fun place" ]
1138
+ b.column_width 0, 12
1139
+ b.column_width 1, 12
1140
+ b.column_hidden 1, true
1141
+ b.numbering = true ## FIXME BROKEN
1142
+ end
1143
+ s.create_default_sorter
1144
+ require 'rbcurse/extras/scrollbar'
1145
+ sb = Scrollbar.new @form, :parent => s
1146
+ #t.column_align 1, :right
1147
+ #puts t.to_s
1148
+ #puts
1106
1149
  end
1107
- require 'rbcurse/extras/scrollbar'
1108
- sb = Scrollbar.new @form, :parent => s
1109
- #t.column_align 1, :right
1110
- #puts t.to_s
1111
- #puts
1112
- end
1113
1150
  end