Class: RubyCurses::TextPad

Inherits:
Widget show all
Includes:
BorderTitle
Defined in:
lib/rbcurse/core/widgets/textpad.rb

Instance Attribute Summary

Attributes inherited from Widget

#_object_created, #col_offset, #cols_panned, #config, #curpos, #focussed, #form, #id, #key_label, #parent_component, #row_offset, #rows_panned, #state

Instance Method Summary collapse

Methods included from BorderTitle

#bordertitle_init, #print_borders, #print_title

Methods inherited from Widget

#action_manager, #changed, #click, #color_pair, #command, #enter, #event_list, #focus, #get_preferred_size, #getvalue, #getvalue_for_paint, #height, #height=, #hide, #leave, #modified?, #move, #on_enter, #on_leave, #override_graphic, #process_key, #remove, #repaint_all, #repaint_required, #set_buffer_modified, #set_buffering, #set_form, #set_form_col, #set_form_row, #set_modified, #setformrowcol, #setrowcol, #show, #text_variable, #unbind_key, #width, #width=

Methods included from Io

#__create_footer_window, #clear_this, #get_file, #print_this, #rb_getchar, #rb_gets, #rbgetstr, #warn

Methods included from Utils

#OLDdefine_key, #_process_key, #bind_key, #bind_keys, #clean_string!, #define_key, #define_prefix_command, #display_app_help, #get_attrib, #get_color, #keycode_tos, #last_line, #one_line_window, #parse_formatted_text, #print_key_bindings, #repeatm, #run_command, #shell_out, #shell_output, #suspend, #view, #wrap_text

Methods included from ConfigSetup

#cget, #config_setup, #configure, #variable_set

Methods included from EventHandler

#bind, #fire_handler, #fire_property_change

Constructor Details

#initialize(form = nil, config = {}, &block) ⇒ TextPad

You may pass height, width, row and col for creating a window otherwise a fullscreen window will be created. If you pass a window from caller then that window will be used. Some keys are trapped, jkhl space, pgup, pgdown, end, home, t b This is currently very minimal and was created to get me started to integrating pads into other classes such as textview.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rbcurse/core/widgets/textpad.rb', line 41

def initialize form=nil, config={}, &block

  @editable = false
  @focusable = true
  @config = config
  #@rows = FFI::NCurses.LINES-1
  #@cols = FFI::NCurses.COLS-1
  @prow = @pcol = 0
  @startrow = 0
  @startcol = 0
  @list = []
  super

  # FIXME 0 as height craps out. need to make it LINES

  @height = @height.ifzero(FFI::NCurses.LINES)
  @width = @width.ifzero(FFI::NCurses.COLS)
  @rows = @height
  @cols = @width
  @startrow = @row
  @startcol = @col
  #@suppress_border = config[:suppress_border]
  @row_offset = @col_offset = 1
  unless @suppress_border
    @startrow += 1
    @startcol += 1
    @rows -=3  # 3 is since print_border_only reduces one from width, to check whether this is correct
    @cols -=3
  end
  @row_offset = @col_offset = 0 if @suppress_borders
  @top = @row
  @left = @col
  init_vars
end

Instance Method Details

#destroyObject

Now since we use get_pad from window, upon the window being destroyed, it will call this. Else it will destroy pad



425
426
427
428
# File 'lib/rbcurse/core/widgets/textpad.rb', line 425

def destroy
  FFI::NCurses.delwin(@pad) if @pad # when do i do this ? FIXME
  @pad = nil
end

#down(num = (($multiplier.nil? or $multiplier == 0) ? 1 : $multiplier)) ⇒ Object

move down a line mimicking vim’s j key

Parameters:

  • multiplier (int)

    entered prior to invoking key



286
287
288
289
290
291
292
293
294
295
# File 'lib/rbcurse/core/widgets/textpad.rb', line 286

def down num=(($multiplier.nil? or $multiplier == 0) ? 1 : $multiplier)
  @oldindex = @current_index if num > 10
  @current_index += num
  unless is_visible? @current_index
    if @current_index > @scrollatrows
      @prow += 1
    end
  end
  $multiplier = 0
end

#filename(filename) ⇒ Object

supply a filename as source for textpad Reads up file into @content



137
138
139
140
141
142
# File 'lib/rbcurse/core/widgets/textpad.rb', line 137

def filename(filename)
  @file = filename
  @filetype = File.extname filename
  @content = File.open(filename,"r").readlines
  @_populate_needed = true
end

#formatted_text(text, fmt) ⇒ Object



189
190
191
192
193
194
# File 'lib/rbcurse/core/widgets/textpad.rb', line 189

def formatted_text text, fmt
  require 'rbcurse/core/include/chunk'
  @formatted_text = text
  @color_parser = fmt
  #remove_all
end

#goto_endObject

goto last line of file



278
279
280
281
282
# File 'lib/rbcurse/core/widgets/textpad.rb', line 278

def goto_end
  @oldindex = @current_index
  @current_index = @content_rows-1
  @prow = @current_index - @scrollatrows
end

#goto_last_positionObject



348
349
350
351
352
353
354
# File 'lib/rbcurse/core/widgets/textpad.rb', line 348

def goto_last_position
  return unless @oldindex
  tmp = @current_index
  @current_index = @oldindex
  @oldindex = tmp
  bounds_check
end

#goto_startObject

goto first line of file



271
272
273
274
275
# File 'lib/rbcurse/core/widgets/textpad.rb', line 271

def goto_start
  @oldindex = @current_index
  @current_index = 0
  @prow = 0
end

#handle_key(ch) ⇒ Object



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/rbcurse/core/widgets/textpad.rb', line 355

def handle_key ch
  return :UNHANDLED unless @content
  map_keys unless @mapped_keys

  @maxrow = @content_rows - @rows
  @maxcol = @content_cols - @cols 
  @oldrow = @prow
  @oldcol = @pcol
  $log.debug "XXX: PAD got #{ch} "
  begin
    case ch
    when key(?l)
      # TODO take multipler
      @pcol += 1
    when key(?$)
      @pcol = @maxcol - 1
    when key(?h)
      # TODO take multipler
      if @pcol > 0
        @pcol -= 1
      end
    when key(?q)
      #throw :close
  when ?0.getbyte(0)..?9.getbyte(0)
    if ch == ?0.getbyte(0) && $multiplier == 0
      # copy of C-a - start of line
      @repaint_required = true if @pcol > 0 # tried other things but did not work
      @pcol = 0
      return 0
    end
    # storing digits entered so we can multiply motion actions
    $multiplier *= 10 ; $multiplier += (ch-48)
    return 0
    when ?\C-c.getbyte(0)
      $multiplier = 0
      return 0
    else
      # check for bindings, these cannot override above keys since placed at end
      begin
        ret = process_key ch, self
        ## If i press C-x > i get an alert from rwidgets which blacks the screen
        # if i put a padrefresh here it becomes okay but only for one pad,
        # i still need to do it for all pads.
      rescue => err
        $log.error " TEXTPAD ERROR INS #{err} "
        $log.debug(err.backtrace.join("\n"))
        textdialog ["Error in TextPad: #{err} ", *err.backtrace], :title => "Exception"
        # FIXME why does this result in a blank spot on screen till its refreshed again
        # should not happen if its deleting its panel and doing an update panel
      end
      return :UNHANDLED if ret == :UNHANDLED
    end
    bounds_check
  rescue => err
    $log.error " TEXTPAD ERROR 111 #{err} "
    $log.debug( err) if err
    $log.debug(err.backtrace.join("\n")) if err
    textdialog ["Error in TextPad: #{err} ", *err.backtrace], :title => "Exception"
    $error_message.value = ""
  ensure
  end
  return 0
end

#init_varsObject



75
76
77
78
79
# File 'lib/rbcurse/core/widgets/textpad.rb', line 75

def init_vars
  @scrollatrows = @height - 3
  @oldindex = @current_index = 0
  @repaint_required = true
end

#is_visible?(index) ⇒ Boolean

Returns:

  • (Boolean)


429
430
431
432
# File 'lib/rbcurse/core/widgets/textpad.rb', line 429

def is_visible? index
  j = index - @prow #@toprow
  j >= 0 && j <= @scrollatrows
end

#map_keysObject

key mappings



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/rbcurse/core/widgets/textpad.rb', line 247

def map_keys
  @mapped_keys = true
  bind_key([?g,?g]){ goto_start } # mapping double keys like vim
  bind_key(279){ goto_start } 
  bind_keys([?G,277]){ goto_end } 
  bind_keys([?k,KEY_UP]){ up } 
  bind_keys([?j,KEY_DOWN]){ down } 
  bind_key(?\C-e){ scroll_window_down } 
  bind_key(?\C-y){ scroll_window_up } 
  bind_keys([32,338, ?\C-d]){ scroll_forward } 
  bind_keys([?\C-b,339]){ scroll_backward } 
  bind_key([?',?']){ goto_last_position } # vim , goto last row position (not column)
  #bind_key(?/, :ask_search)
  #bind_key(?n, :find_more)
  bind_key([?\C-x, ?>], :scroll_right)
  bind_key([?\C-x, ?<], :scroll_left)
  bind_key(?\M-l, :scroll_right)
  bind_key(?\M-h, :scroll_left)
  bind_key([?\C-x, ?\C-s], :saveas)
  #bind_key(?r) { getstr("Enter a word: ") }
  bind_key(?m, :disp_menu)
end

—- the next 2 methods deal with printing chunks we should put it int a common module and include it in Window and Pad stuff and perhaps include it conditionally.



156
157
158
159
160
# File 'lib/rbcurse/core/widgets/textpad.rb', line 156

def print(string, width = width)
  #return unless visible?
  w = width == 0? Ncurses.COLS : width
  FFI::NCurses.waddnstr(@pad,string.to_s, w) # changed 2011 dts  
end

#render(pad, lineno, text) ⇒ Object

default method for rendering a line



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rbcurse/core/widgets/textpad.rb', line 119

def render pad, lineno, text
  if text.is_a? Chunks::ChunkLine
    FFI::NCurses.wmove @pad, lineno, 0
    a = get_attrib @attrib
  
    show_colored_chunks text, nil, a
    return
  end
  if @renderer
    @renderer.render @pad, lineno, text
  else
    FFI::NCurses.mvwaddstr(@pad,lineno, 0, @content[lineno])
  end
end

#renderer(r) ⇒ Object

supply a custom renderer that implements render()

See Also:



113
114
115
# File 'lib/rbcurse/core/widgets/textpad.rb', line 113

def renderer r
  @renderer = r
end

#repaintObject



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/rbcurse/core/widgets/textpad.rb', line 215

def repaint
  return unless @repaint_required
  if @formatted_text
    $log.debug "XXX:  INSIDE FORMATTED TEXT "

    l = RubyCurses::Utils.parse_formatted_text(@color_parser,
                                           @formatted_text)

    text(l)
    @formatted_text = nil
  end

  ## moved this line up or else create_p was crashing
  @window ||= @graphic
  populate_pad if @_populate_needed
  #HERE we need to populate once so user can pass a renderer
  unless @suppress_border
    if @repaint_all
      @window.print_border_only @top, @left, @height-1, @width, $datacolor
      print_title
      @window.wrefresh
    end
  end

  padrefresh
  @repaint_required = false
  @repaint_all = false
end

#rowcolObject

:nodoc:



80
81
82
# File 'lib/rbcurse/core/widgets/textpad.rb', line 80

def rowcol #:nodoc:
  return @row+@row_offset, @col+@col_offset
end

#scroll_backwardObject

scrolls lines backward a window full at a time, on pressing pageup C-u may not work since it is trapped by form earlier. Need to fix



343
344
345
346
347
# File 'lib/rbcurse/core/widgets/textpad.rb', line 343

def scroll_backward
  @oldindex = @current_index
  @current_index -= @scrollatrows
  @prow = @current_index - @scrollatrows
end

#scroll_forwardObject

scrolls lines a window full at a time, on pressing ENTER or C-d or pagedown



335
336
337
338
339
# File 'lib/rbcurse/core/widgets/textpad.rb', line 335

def scroll_forward
  @oldindex = @current_index
  @current_index += @scrollatrows
  @prow = @current_index - @scrollatrows
end

#scroll_window_down(num = (($multiplier.nil? or $multiplier == 0) ? 1 : $multiplier)) ⇒ Object

scrolls window down mimicking vim C-e

Parameters:

  • multiplier (int)

    entered prior to invoking key



314
315
316
317
318
319
320
321
# File 'lib/rbcurse/core/widgets/textpad.rb', line 314

def scroll_window_down num=(($multiplier.nil? or $multiplier == 0) ? 1 : $multiplier)
  @prow += num
    if @prow > @current_index
      @current_index += 1
    end
  #check_prow
  $multiplier = 0
end

#scroll_window_up(num = (($multiplier.nil? or $multiplier == 0) ? 1 : $multiplier)) ⇒ Object

scrolls window up mimicking vim C-y

Parameters:

  • multiplier (int)

    entered prior to invoking key



325
326
327
328
329
330
331
332
# File 'lib/rbcurse/core/widgets/textpad.rb', line 325

def scroll_window_up num=(($multiplier.nil? or $multiplier == 0) ? 1 : $multiplier)
  @prow -= num
  unless is_visible? @current_index
    # one more check may be needed here TODO
    @current_index -= num
  end
  $multiplier = 0
end

#show_colored_chunks(chunks, defcolor = nil, defattr = nil) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rbcurse/core/widgets/textpad.rb', line 162

def show_colored_chunks(chunks, defcolor = nil, defattr = nil)
  #return unless visible?
  chunks.each do |chunk| #|color, chunk, attrib|
    case chunk
    when Chunks::Chunk
      color = chunk.color
      attrib = chunk.attrib
      text = chunk.text
    when Array
      # for earlier demos that used an array
      color = chunk[0]
      attrib = chunk[2]
      text = chunk[1]
    end

    color ||= defcolor
    attrib ||= defattr || NORMAL

    #cc, bg = ColorMap.get_colors_for_pair color
    #$log.debug "XXX: CHUNK textpad #{text}, cp #{color} ,  attrib #{attrib}. #{cc}, #{bg} "
    FFI::NCurses.wcolor_set(@pad, color,nil) if color
    FFI::NCurses.wattron(@pad, attrib) if attrib
    print(text)
    FFI::NCurses.wattroff(@pad, attrib) if attrib
  end
end

#text(lines) ⇒ Object

Supply an array of string to be displayed This will replace existing text



147
148
149
150
# File 'lib/rbcurse/core/widgets/textpad.rb', line 147

def text lines
  @content = lines
  @_populate_needed = true
end

#up(num = (($multiplier.nil? or $multiplier == 0) ? 1 : $multiplier)) ⇒ Object

move up a line mimicking vim’s k key

Parameters:

  • multiplier (int)

    entered prior to invoking key



299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/rbcurse/core/widgets/textpad.rb', line 299

def up num=(($multiplier.nil? or $multiplier == 0) ? 1 : $multiplier)
  @oldindex = @current_index if num > 10
  @current_index -= num
  unless is_visible? @current_index
    if @prow > @current_index
      $status_message.value = "1 #{@prow} > #{@current_index} "
      @prow -= 1
    else
    end
  end
  $multiplier = 0
end