rbcurse 0.1.0
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/CHANGELOG +1570 -0
- data/History.txt +6 -0
- data/Manifest.txt +54 -0
- data/README.txt +304 -0
- data/Rakefile +28 -0
- data/examples/qdfilechooser.rb +68 -0
- data/examples/rfe.rb +853 -0
- data/examples/rfe_renderer.rb +69 -0
- data/examples/test1.rb +242 -0
- data/examples/test2.rb +498 -0
- data/examples/testcombo.rb +95 -0
- data/examples/testkeypress.rb +61 -0
- data/examples/testmenu.rb +105 -0
- data/examples/testtable.rb +266 -0
- data/examples/testtabp.rb +106 -0
- data/examples/testtodo.rb +532 -0
- data/examples/viewtodo.rb +512 -0
- data/lib/rbcurse/action.rb +31 -0
- data/lib/rbcurse/applicationheader.rb +57 -0
- data/lib/rbcurse/celleditor.rb +120 -0
- data/lib/rbcurse/checkboxcellrenderer.rb +69 -0
- data/lib/rbcurse/colormap.rb +133 -0
- data/lib/rbcurse/comboboxcellrenderer.rb +45 -0
- data/lib/rbcurse/defaultlistselectionmodel.rb +49 -0
- data/lib/rbcurse/keylabelprinter.rb +143 -0
- data/lib/rbcurse/listcellrenderer.rb +99 -0
- data/lib/rbcurse/listkeys.rb +33 -0
- data/lib/rbcurse/listscrollable.rb +216 -0
- data/lib/rbcurse/listselectable.rb +67 -0
- data/lib/rbcurse/mapper.rb +108 -0
- data/lib/rbcurse/orderedhash.rb +77 -0
- data/lib/rbcurse/rcombo.rb +243 -0
- data/lib/rbcurse/rdialogs.rb +183 -0
- data/lib/rbcurse/rform.rb +845 -0
- data/lib/rbcurse/rinputdataevent.rb +36 -0
- data/lib/rbcurse/rlistbox.rb +804 -0
- data/lib/rbcurse/rmenu.rb +666 -0
- data/lib/rbcurse/rmessagebox.rb +325 -0
- data/lib/rbcurse/rpopupmenu.rb +754 -0
- data/lib/rbcurse/rtabbedpane.rb +259 -0
- data/lib/rbcurse/rtable.rb +1296 -0
- data/lib/rbcurse/rtextarea.rb +673 -0
- data/lib/rbcurse/rtextview.rb +335 -0
- data/lib/rbcurse/rwidget.rb +1731 -0
- data/lib/rbcurse/scrollable.rb +301 -0
- data/lib/rbcurse/selectable.rb +94 -0
- data/lib/rbcurse/table/tablecellrenderer.rb +85 -0
- data/lib/rbcurse/table/tabledatecellrenderer.rb +102 -0
- data/lib/rbcurse.rb +7 -0
- data/lib/ver/keyboard.rb +150 -0
- data/lib/ver/keyboard2.rb +170 -0
- data/lib/ver/ncurses.rb +102 -0
- data/lib/ver/window.rb +369 -0
- data/test/test_rbcurse.rb +0 -0
- metadata +118 -0
@@ -0,0 +1,335 @@
|
|
1
|
+
=begin
|
2
|
+
* Name: TextView
|
3
|
+
* $Id$
|
4
|
+
* Description View text in this widget.
|
5
|
+
* Author: rkumar (arunachalesha)
|
6
|
+
TODO
|
7
|
+
* file created 2009-01-08 15:23
|
8
|
+
--------
|
9
|
+
* License:
|
10
|
+
Same as Ruby's License (http://www.ruby-lang.org/LICENSE.txt)
|
11
|
+
|
12
|
+
=end
|
13
|
+
require 'rubygems'
|
14
|
+
require 'ncurses'
|
15
|
+
require 'logger'
|
16
|
+
require 'rbcurse'
|
17
|
+
require 'rbcurse/listscrollable'
|
18
|
+
|
19
|
+
include Ncurses
|
20
|
+
include RubyCurses
|
21
|
+
module RubyCurses
|
22
|
+
extend self
|
23
|
+
|
24
|
+
##
|
25
|
+
# A viewable read only box. Can scroll.
|
26
|
+
# Intention is to be able to change content dynamically - the entire list.
|
27
|
+
# Use set_content to set content, or just update the list attrib
|
28
|
+
# TODO -
|
29
|
+
# - searching, goto line - DONE
|
30
|
+
class TextView < Widget
|
31
|
+
include ListScrollable
|
32
|
+
dsl_accessor :height # height of viewport
|
33
|
+
dsl_accessor :title # set this on top
|
34
|
+
dsl_accessor :title_attrib # bold, reverse, normal
|
35
|
+
dsl_accessor :footer_attrib # bold, reverse, normal
|
36
|
+
dsl_accessor :list # the array of data to be sent by user
|
37
|
+
dsl_accessor :maxlen # max len to be displayed
|
38
|
+
attr_reader :toprow # the toprow in the view (offsets are 0)
|
39
|
+
# attr_reader :prow # the row on which cursor/focus is
|
40
|
+
attr_reader :winrow # the row in the viewport/window
|
41
|
+
dsl_accessor :print_footer
|
42
|
+
|
43
|
+
def initialize form, config={}, &block
|
44
|
+
@focusable = true
|
45
|
+
@editable = false
|
46
|
+
@left_margin = 1
|
47
|
+
@row = 0
|
48
|
+
@col = 0
|
49
|
+
@show_focus = false # don't highlight row under focus
|
50
|
+
@list = []
|
51
|
+
super
|
52
|
+
@row_offset = @col_offset = 1
|
53
|
+
@orig_col = @col
|
54
|
+
# this does result in a blank line if we insert after creating. That's required at
|
55
|
+
# present if we wish to only insert
|
56
|
+
@scrollatrow = @height-2
|
57
|
+
@content_rows = @list.length
|
58
|
+
@win = @form.window
|
59
|
+
#init_scrollable
|
60
|
+
print_borders
|
61
|
+
@maxlen ||= @width-2
|
62
|
+
init_vars
|
63
|
+
end
|
64
|
+
def init_vars
|
65
|
+
@curpos = @pcol = @toprow = @current_index = 0
|
66
|
+
end
|
67
|
+
##
|
68
|
+
# send in a list
|
69
|
+
# e.g. set_content File.open("README.txt","r").readlines
|
70
|
+
# set wrap at time of passing :WRAP_NONE :WRAP_WORD
|
71
|
+
def set_content list, wrap = :WRAP_NONE
|
72
|
+
@wrap_policy = wrap
|
73
|
+
if list.is_a? String
|
74
|
+
if @wrap_policy == :WRAP_WORD
|
75
|
+
data = wrap_text list
|
76
|
+
@list = data.split("\n")
|
77
|
+
else
|
78
|
+
@list = list.split("\n")
|
79
|
+
end
|
80
|
+
elsif list.is_a? Array
|
81
|
+
if @wrap_policy == :WRAP_WORD
|
82
|
+
data = wrap_text list.join(" ")
|
83
|
+
@list = data.split("\n")
|
84
|
+
else
|
85
|
+
@list = list
|
86
|
+
end
|
87
|
+
else
|
88
|
+
raise "set_content expects Array not #{list.class}"
|
89
|
+
end
|
90
|
+
end
|
91
|
+
## display this row on top
|
92
|
+
def top_row(*val)
|
93
|
+
if val.empty?
|
94
|
+
@toprow
|
95
|
+
else
|
96
|
+
@toprow = val[0] || 0
|
97
|
+
#@prow = val[0] || 0
|
98
|
+
end
|
99
|
+
@repaint_required = true
|
100
|
+
end
|
101
|
+
## ---- for listscrollable ---- ##
|
102
|
+
def scrollatrow
|
103
|
+
@height - 2
|
104
|
+
end
|
105
|
+
def row_count
|
106
|
+
@list.length
|
107
|
+
end
|
108
|
+
##
|
109
|
+
# returns row of first match of given regex (or nil if not found)
|
110
|
+
def find_first_match regex
|
111
|
+
@list.each_with_index do |row, ix|
|
112
|
+
return ix if !row.match(regex).nil?
|
113
|
+
end
|
114
|
+
return nil
|
115
|
+
end
|
116
|
+
def rowcol
|
117
|
+
#$log.debug "textarea rowcol : #{@row+@row_offset+@winrow}, #{@col+@col_offset}"
|
118
|
+
#return @row+@row_offset+@winrow, @col+@col_offset
|
119
|
+
#return @row+@row_offset+@winrow, @col+@col_offset
|
120
|
+
return @row+@row_offset, @col+@col_offset
|
121
|
+
end
|
122
|
+
def wrap_text(txt, col = @maxlen)
|
123
|
+
$log.debug "inside wrap text for :#{txt}"
|
124
|
+
txt.gsub(/(.{1,#{col}})( +|$\n?)|(.{1,#{col}})/,
|
125
|
+
"\\1\\3\n")
|
126
|
+
end
|
127
|
+
def print_borders
|
128
|
+
window = @form.window
|
129
|
+
color = $datacolor
|
130
|
+
window.print_border @row, @col, @height, @width, color
|
131
|
+
print_title
|
132
|
+
=begin
|
133
|
+
hline = "+%s+" % [ "-"*(width-((1)*2)) ]
|
134
|
+
hline2 = "|%s|" % [ " "*(width-((1)*2)) ]
|
135
|
+
window.printstring(row=startrow, col=startcol, hline, color)
|
136
|
+
print_title
|
137
|
+
(startrow+1).upto(startrow+height-1) do |row|
|
138
|
+
window.printstring( row, col=startcol, hline2, color)
|
139
|
+
end
|
140
|
+
window.printstring( startrow+height, col=startcol, hline, color)
|
141
|
+
=end
|
142
|
+
|
143
|
+
end
|
144
|
+
def print_title
|
145
|
+
@form.window.printstring( @row, @col+(@width-@title.length)/2, @title, $datacolor, @title_attrib) unless @title.nil?
|
146
|
+
end
|
147
|
+
def print_foot
|
148
|
+
@footer_attrib ||= Ncurses::A_REVERSE
|
149
|
+
footer = "R: #{@current_index+1}, C: #{@curpos+@pcol}, #{@list.length} lines "
|
150
|
+
@form.window.printstring( @row + @height, @col+2, footer, $datacolor, @footer_attrib)
|
151
|
+
end
|
152
|
+
### FOR scrollable ###
|
153
|
+
def get_content
|
154
|
+
@list
|
155
|
+
end
|
156
|
+
def get_window
|
157
|
+
@form.window
|
158
|
+
end
|
159
|
+
### FOR scrollable ###
|
160
|
+
def repaint # textview
|
161
|
+
return unless @repaint_required
|
162
|
+
paint
|
163
|
+
print_foot if @print_footer
|
164
|
+
end
|
165
|
+
def getvalue
|
166
|
+
@list
|
167
|
+
end
|
168
|
+
# textview
|
169
|
+
# [ ] scroll left right DONE
|
170
|
+
def handle_key ch
|
171
|
+
@buffer = @list[@current_index]
|
172
|
+
if @buffer.nil? and row_count == 0
|
173
|
+
@list << "\r"
|
174
|
+
@buffer = @list[@current_index]
|
175
|
+
end
|
176
|
+
return if @buffer.nil?
|
177
|
+
$log.debug " before: curpos #{@curpos} blen: #{@buffer.length}"
|
178
|
+
if @curpos > @buffer.length
|
179
|
+
addcol((@buffer.length-@curpos)+1)
|
180
|
+
@curpos = @buffer.length
|
181
|
+
set_form_col
|
182
|
+
end
|
183
|
+
$log.debug "TV after loop : curpos #{@curpos} blen: #{@buffer.length}"
|
184
|
+
#pre_key
|
185
|
+
case ch
|
186
|
+
when ?\C-n
|
187
|
+
scroll_forward
|
188
|
+
when ?\C-p
|
189
|
+
scroll_backward
|
190
|
+
when ?0, ?\C-[
|
191
|
+
goto_start #start of buffer # cursor_start
|
192
|
+
when ?\C-]
|
193
|
+
goto_end # end / bottom cursor_end
|
194
|
+
when KEY_UP
|
195
|
+
#select_prev_row
|
196
|
+
ret = up
|
197
|
+
check_curpos
|
198
|
+
#addrowcol -1,0 if ret != -1 or @winrow != @oldwinrow # positions the cursor up
|
199
|
+
#@form.row = @row + 1 + @winrow
|
200
|
+
when KEY_DOWN
|
201
|
+
ret = down
|
202
|
+
check_curpos
|
203
|
+
#@form.row = @row + 1 + @winrow
|
204
|
+
when KEY_LEFT
|
205
|
+
cursor_backward
|
206
|
+
when KEY_RIGHT
|
207
|
+
cursor_forward
|
208
|
+
when KEY_BACKSPACE, 127
|
209
|
+
cursor_backward
|
210
|
+
when 330
|
211
|
+
cursor_backward
|
212
|
+
when ?\C-a
|
213
|
+
# take care of data that exceeds maxlen by scrolling and placing cursor at start
|
214
|
+
set_form_col 0
|
215
|
+
@pcol = 0
|
216
|
+
when ?\C-e
|
217
|
+
# take care of data that exceeds maxlen by scrolling and placing cursor at end
|
218
|
+
blen = @buffer.rstrip.length
|
219
|
+
if blen < @maxlen
|
220
|
+
set_form_col blen
|
221
|
+
else
|
222
|
+
@pcol = blen-@maxlen
|
223
|
+
#wrong curpos wiill be reported
|
224
|
+
set_form_col @maxlen-1
|
225
|
+
end
|
226
|
+
else
|
227
|
+
$log.debug("TEXTVIEW XXX ch #{ch}")
|
228
|
+
return :UNHANDLED
|
229
|
+
end
|
230
|
+
#post_key
|
231
|
+
# XXX 2008-11-27 13:57 trying out
|
232
|
+
set_form_row
|
233
|
+
end
|
234
|
+
# newly added to check curpos when moving up or down
|
235
|
+
def check_curpos
|
236
|
+
@buffer = @list[@current_index]
|
237
|
+
# if the cursor is ahead of data in this row then move it back
|
238
|
+
if @pcol+@curpos > @buffer.length
|
239
|
+
addcol((@pcol+@buffer.length-@curpos)+1)
|
240
|
+
@curpos = @buffer.length
|
241
|
+
|
242
|
+
# even this row is gt maxlen, i.e., scrolled right
|
243
|
+
if @curpos > @maxlen
|
244
|
+
@pcol = @curpos - @maxlen
|
245
|
+
@curpos = @maxlen-1
|
246
|
+
else
|
247
|
+
# this row is within maxlen, make scroll 0
|
248
|
+
@pcol=0
|
249
|
+
end
|
250
|
+
set_form_col
|
251
|
+
end
|
252
|
+
end
|
253
|
+
# set cursor on correct column tview
|
254
|
+
def set_form_col col=@curpos
|
255
|
+
@curpos = col
|
256
|
+
@curpos = @maxlen if @curpos > @maxlen
|
257
|
+
@form.col = @orig_col + @col_offset + @curpos
|
258
|
+
@repaint_required = true
|
259
|
+
end
|
260
|
+
def cursor_forward
|
261
|
+
if @curpos < @width and @curpos < @maxlen-1 # else it will do out of box
|
262
|
+
@curpos += 1
|
263
|
+
addcol 1
|
264
|
+
else
|
265
|
+
# XXX 2008-11-26 23:03 trying out
|
266
|
+
@pcol += 1 if @pcol <= @buffer.length
|
267
|
+
end
|
268
|
+
set_form_col
|
269
|
+
@repaint_required = true
|
270
|
+
end
|
271
|
+
def addcol num
|
272
|
+
@repaint_required = true
|
273
|
+
@form.addcol num
|
274
|
+
end
|
275
|
+
def addrowcol row,col
|
276
|
+
@repaint_required = true
|
277
|
+
@form.addrowcol row, col
|
278
|
+
end
|
279
|
+
def cursor_backward
|
280
|
+
if @curpos > 0
|
281
|
+
@curpos -= 1
|
282
|
+
set_form_col
|
283
|
+
#addcol -1
|
284
|
+
elsif @pcol > 0 # XXX added 2008-11-26 23:05
|
285
|
+
@pcol -= 1
|
286
|
+
end
|
287
|
+
@repaint_required = true
|
288
|
+
end
|
289
|
+
# gives offset of next line, does not move
|
290
|
+
def next_line
|
291
|
+
@list[@current_index+1]
|
292
|
+
end
|
293
|
+
def do_relative_row num
|
294
|
+
yield @list[@current_index+num]
|
295
|
+
end
|
296
|
+
def paint
|
297
|
+
print_borders if @to_print_borders == 1 # do this once only, unless everything changes
|
298
|
+
rc = row_count
|
299
|
+
maxlen = @maxlen ||= @width-2
|
300
|
+
tm = get_content
|
301
|
+
tr = @toprow
|
302
|
+
acolor = get_color $datacolor
|
303
|
+
h = scrollatrow()
|
304
|
+
r,c = rowcol
|
305
|
+
0.upto(h) do |hh|
|
306
|
+
crow = tr+hh
|
307
|
+
if crow < rc
|
308
|
+
#focussed = @current_index == crow ? true : false
|
309
|
+
#selected = is_row_selected crow
|
310
|
+
content = tm[crow].chomp
|
311
|
+
content.gsub!(/\t/, ' ') # don't display tab
|
312
|
+
content.gsub!(/[^[:print:]]/, '') # don't display non print characters
|
313
|
+
if !content.nil?
|
314
|
+
if content.length > maxlen # only show maxlen
|
315
|
+
content = content[@pcol..@pcol+maxlen-1]
|
316
|
+
else
|
317
|
+
content = content[@pcol..-1]
|
318
|
+
end
|
319
|
+
end
|
320
|
+
#renderer = get_default_cell_renderer_for_class content.class.to_s
|
321
|
+
#renderer = cell_renderer()
|
322
|
+
#renderer.repaint @form.window, r+hh, c+(colix*11), content, focussed, selected
|
323
|
+
#renderer.repaint @form.window, r+hh, c, content, focussed, selected
|
324
|
+
@form.window.printstring r+hh, c, "%-*s" % [@width-2,content], acolor, @attr
|
325
|
+
|
326
|
+
else
|
327
|
+
# clear rows
|
328
|
+
@form.window.printstring r+hh, c, " " * (@width-2), acolor,@attr
|
329
|
+
end
|
330
|
+
end
|
331
|
+
@table_changed = false
|
332
|
+
@repaint_required = false
|
333
|
+
end
|
334
|
+
end # class textview
|
335
|
+
end # modul
|