Class: Canis::TextView

Inherits:
Widget show all
Extended by:
Forwardable
Includes:
ListScrollable
Defined in:
lib/canis/core/widgets/deprecated/rtextview.rb

Overview

A viewable read only box. Can scroll. Intention is to be able to change content dynamically - the entire list. Use set_content to set content, or just update the list attrib TODO -

- goto line - DONE

Instance Attribute Summary collapse

Attributes included from ListScrollable

#find_offset, #find_offset1, #search_found_ix, #show_caret

Attributes inherited from Widget

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

Instance Method Summary collapse

Methods included from ListScrollable

#_convert_index_to_printable_row, #_convert_index_to_visible_row, #_find_next, #_find_prev, #ask_search, #backward_word, #bounds_check, #find_more, #find_next, #find_prev, #focussed_index, #forward_char, #forward_word, #goto_bottom, #goto_last_position, #goto_top, #highlight_focussed_row, #install_keys, #is_visible?, #next_match, #next_row, #previous_row, #scroll_backward, #scroll_forward, #scroll_left, #scroll_right, #selected_item, #set_focus_on, #set_form_row, #set_selection_for_char, #show_caret_func

Methods inherited from Widget

#action_manager, #bgcolor, #color, #color_pair, #command, #destroy, #focus, #focusable, #focusable?, #getvalue_for_paint, #hide, #modified?, #move, #on_leave, #override_graphic, #process_key, #property_set, #remove, #repaint_all, #repaint_required, #set_form, #set_form_row, #set_modified, #setformrowcol, #setrowcol, #show, #unbind_key

Methods included from Io

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

Methods included from Utils

#ORIG_process_key, #ORIGbind_key, #ORIGkeycode_tos, #_process_key, #bind_composite_mapping, #bind_key, #bind_keys, #check_composite_mapping, #create_logger, #define_key, #define_prefix_command, #execute_mapping, #get_attrib, #get_color, #key, #key_tos, #print_key_bindings, #repeatm, #run_command, #shell_out, #shell_output, #suspend, #view, #xxxbind_composite_mapping

Methods included from ConfigSetup

#config_setup, #variable_set

Methods included from EventHandler

#bind, #event?, #event_list, #fire_handler, #fire_property_change, #register_events

Constructor Details

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

Returns a new instance of TextView.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 45

def initialize form = nil, config={}, &block
  @focusable = true
  @editable = false
  @sanitization_required = true
  @suppress_borders = false
  @row_offset = @col_offset = 1 
  @row = 0
  @col = 0
  @show_focus = false  # don't highlight row under focus
  @list = []
  map_keys
  super
  # ideally this should have been 2 to take care of borders, but that would break
  # too much stuff !
  @win = @graphic

  @_events.push :CHANGE # thru vieditable
  @_events << :PRESS # new, in case we want to use this for lists and allow ENTER
  @_events << :ENTER_ROW # new, should be there in listscrollable ??
  install_keys # do something about this nonsense FIXME
  bordertitle_init
  init_vars
  init_actions
end

Instance Attribute Details

#current_indexObject (readonly)

Returns the value of attribute current_index.



42
43
44
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 42

def current_index
  @current_index
end

#toprowObject (readonly)

the toprow in the view (offsets are 0)



39
40
41
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 39

def toprow
  @toprow
end

Instance Method Details

#addcol(num) ⇒ Object

2010-01-23 22:41



409
410
411
412
413
414
415
416
417
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 409

def addcol num #:nodoc:
  #@repaint_required = true
  @repaint_footer_required = true # 2010-01-23 22:41 
  if @form
    @form.addcol num
  else
    @parent_component.form && @parent_component.form.addcol(num)
  end
end

#addrowcol(row, col) ⇒ Object

:nodoc:



418
419
420
421
422
423
424
425
426
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 418

def addrowcol row,col #:nodoc:
  #@repaint_required = true
  @repaint_footer_required = true # 2010-01-23 22:41 
  if @form
  @form.addrowcol row, col
  else
    @parent_component.form.addrowcol num
  end
end

#check_curposObject

newly added to check curpos when moving up or down



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 354

def check_curpos #:nodoc:
  @buffer = @list[@current_index]
  # if the cursor is ahead of data in this row then move it back
  if @pcol+@curpos > row_length
    addcol((@pcol+row_length-@curpos)+1)
    @curpos = row_length 
    maxlen = (@maxlen || @width-@internal_width)

    # even this row is gt maxlen, i.e., scrolled right
    if @curpos > maxlen
      @pcol = @curpos - maxlen
      @curpos = maxlen-1 
    else
      # this row is within maxlen, make scroll 0
      @pcol=0
    end
    set_form_col 
  end
end

#color_parser(f) ⇒ Object

supply with a color parser, if you supplied formatted text



452
453
454
455
456
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 452

def color_parser f
  $log.debug "XXX: parser setting color_parser to #{f} "
  #@window.color_parser f
  @color_parser = f
end

#current_valueObject



243
244
245
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 243

def current_value
  @list[@current_index]
end

#cursor_backwardObject

:nodoc:



427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 427

def cursor_backward  #:nodoc:
  repeatm { 
  if @curpos > 0
    @curpos -= 1
    set_form_col 
    #addcol -1
  elsif @pcol > 0 
    @pcol -= 1   
  end
  }
  #@repaint_required = true
  @repaint_footer_required = true # 2010-01-23 22:41 
end

#cursor_forwardObject

:nodoc:



395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 395

def cursor_forward #:nodoc:
  maxlen = @maxlen || @width-@internal_width
  repeatm { 
  if @curpos < @width and @curpos < maxlen-1 # else it will do out of box
    @curpos += 1
    addcol 1
  else
    @pcol += 1 if @pcol <= row_length
  end
  }
  set_form_col 
  #@repaint_required = true
  @repaint_footer_required = true # 2010-01-23 22:41 
end

#do_relative_row(num) {|| ... } ⇒ Object

Deprecated.

Yields:

  • ()


446
447
448
449
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 446

def do_relative_row num  #:nodoc:
  raise "unused will be removed"
  yield @list[@current_index+num] 
end

#edit_externalObject

edit content of textview in EDITOR and bring back NOTE: does not maintain content_type, so if you edit ansi text, it will come back in as normal text



712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 712

def edit_external
  require 'canis/core/include/appmethods'
  require 'tempfile'
  f = Tempfile.new("canis")
  l = self.text
  l.each { |line| f.puts line }
  fp = f.path
  f.flush

  editor = ENV['EDITOR'] || 'vi'
  vimp = %x[which #{editor}].chomp
  ret = shell_out "#{vimp} #{fp}"
  if ret
    lines = File.open(f,'r').readlines
    set_content(lines, :content_type => @old_content_type)
  end
end

#find_first_match(regex) ⇒ Object

returns row of first match of given regex (or nil if not found)



193
194
195
196
197
198
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 193

def find_first_match regex #:nodoc:
  @list.each_with_index do |row, ix|
    return ix if !row.match(regex).nil?
  end
  return nil
end

#fire_action_eventObject

on pressing ENTER we send user some info, the calling program would bind :PRESS – FIXME we can create this once and reuse ++



640
641
642
643
644
645
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 640

def fire_action_event
  return if @list.nil? || @list.size == 0
  require 'canis/core/include/ractionevent'
  aev = TextActionEvent.new self, :PRESS, current_value().to_s, @current_index, @curpos
  fire_handler :PRESS, aev
end

#formatted_text(text, fmt) ⇒ Object



147
148
149
150
151
152
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 147

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

#get_contentObject

FOR scrollable ###



218
219
220
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 218

def get_content
  @list
end

#get_windowObject

:nodoc:



221
222
223
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 221

def get_window #:nodoc:
  @graphic
end

#getstr(prompt, maxlen = 80) ⇒ Object

this is just a test of prompting user for a string + as an alternative to the dialog.



616
617
618
619
620
621
622
623
624
625
626
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 616

def getstr prompt, maxlen=80  #:nodoc:
  tabc = Proc.new {|str| Dir.glob(str +"*") }
  config={}; config[:tab_completion] = tabc
  config[:default] = "test"
  config[:display_length] = 11
  $log.debug " inside getstr before call "
  ret, str = rbgetstr(@form.window, @row+@height-1, @col+1, prompt, maxlen, config)
  $log.debug " rbgetstr returned #{ret} ,#{str}."
  return "" if ret != 0
  return str
end

#getvalueObject

this sucks and should not be used but is everywhere, should use text()



240
241
242
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 240

def getvalue
  @list
end

#handle_key(ch) ⇒ Object

textview NOTE: i think this should return if list is nil or empty. No need to put

stuff into buffer and continue. will trouble other classes that extend.



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 273

def handle_key ch #:nodoc:
  $log.debug " textview got ch #{ch} "
  @old_pcol = @pcol
  @buffer = @list[@current_index]
  if @buffer.nil? && row_count == 0
    @list << "\r"
    @buffer = @list[@current_index]
  end
  return if @buffer.nil?
  #$log.debug " before: curpos #{@curpos} blen: #{row_length}"
  if @curpos > row_length #@buffer.length
    addcol((row_length-@curpos)+1)
    @curpos = row_length
    set_form_col 
  end
  # We can improve later
  case ch
  when KEY_UP, ?k.getbyte(0)
    #select_prev_row
    ret = up
    # next removed as very irritating, can be configured if required 2011-11-2 
    #get_window.ungetch(KEY_BTAB) if ret == :NO_PREVIOUS_ROW
    check_curpos
    
  when KEY_DOWN, ?j.getbyte(0)
    ret = down
    # This should be configurable, or only if all rows are visible
    #get_window.ungetch(KEY_TAB) if ret == :NO_NEXT_ROW
    check_curpos
  #when KEY_LEFT, ?h.getbyte(0)
    #cursor_backward
  #when KEY_RIGHT, ?l.getbyte(0)
    #cursor_forward
  when ?\C-a.getbyte(0) #, ?0.getbyte(0)
    # take care of data that exceeds maxlen by scrolling and placing cursor at start
    @repaint_required = true if @pcol > 0 # tried other things but did not work
    set_form_col 0
    @pcol = 0
  when ?\C-e.getbyte(0), ?$.getbyte(0)
    # take care of data that exceeds maxlen by scrolling and placing cursor at end
    # This use to actually pan the screen to actual end of line, but now somewhere
    # it only goes to end of visible screen, set_form probably does a sanity check
    blen = row_length # @buffer.rstrip.length FIXME
    set_form_col blen
  when KEY_ENTER, FFI::NCurses::KEY_ENTER
    #fire_handler :PRESS, self
    fire_action_event
  when ?0.getbyte(0)..?9.getbyte(0)
    # FIXME the assumption here was that if numbers are being entered then a 0 is a number
    # not a beg-of-line command.
    # However, after introducing universal_argument, we can enters numbers using C-u and then press another
    # C-u to stop. In that case a 0 should act as a command, even though multiplier has been set
    if ch == ?0.getbyte(0) and $multiplier == 0
      # copy of C-a - start of line
      @repaint_required = true if @pcol > 0 # tried other things but did not work
      set_form_col 0
      @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
    rescue => err
      $log.error " TEXTVIEW ERROR #{err} "
      $log.debug(err.backtrace.join("\n"))
      textdialog [err.to_s, *err.backtrace], :title => "Exception"
    end
    return :UNHANDLED if ret == :UNHANDLED
  end
  $multiplier = 0 # you must reset if you've handled a key. if unhandled, don't reset since parent could use
  set_form_row
  return 0 # added 2010-01-12 22:17 else down arrow was going into next field
end

#init_actionsObject



729
730
731
732
733
734
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 729

def init_actions
  editor = ENV['EDITOR'] || 'vi'
  am = action_manager()
  am.add_action( Action.new("&Edit in #{editor} ") { edit_external } )
  am.add_action( Action.new("&Saveas") { saveas() })
end

#init_varsObject

:nodoc:



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 69

def init_vars #:nodoc:
  @curpos = @pcol = @toprow = @current_index = 0
  @repaint_all=true 
  @repaint_required=true 
  @widget_scrolled = true
  ## 2010-02-10 20:20 RFED16 taking care if no border requested
  @row_offset = @col_offset = 0 if @suppress_borders == true
  # added 2010-02-11 15:11 RFED16 so we don't need a form.
  $error_message_row ||= 23
  $error_message_col ||= 1
  # currently i scroll right only if  current line is longer than display width, i should use 
  # longest line on screen.
  @longest_line = 0 # the longest line printed on this page, used to determine if scrolling shd work
  @internal_width = 2
  @internal_width = 0 if @suppress_borders # NOTE bordertitle says 1

  @search_found_ix = nil # so old searches don't get highlighted
end

#load_module(requirename, includename) ⇒ Object

dynamically load a module and execute init method. Hopefully, we can get behavior like this such as vieditable or multibuffers



630
631
632
633
634
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 630

def load_module requirename, includename
  require "canis/#{requirename}"
  extend Object.const_get("#{includename}")
  send("#{requirename}_init") #if respond_to? "#{includename}_init"
end

#map_keysObject



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 87

def map_keys
  require 'canis/core/include/deprecated/listbindings'
  bindings()
  #bind_key([?\C-x, ?\C-s], :saveas)
  #bind_key([?\C-x, ?e], :edit_external)
  bind_key(32, 'scroll forward'){ scroll_forward() }
  # have placedhere so multi-bufer can override BS to prev buffer
  bind_keys([KEY_BACKSPACE,KEY_BSPACE,KEY_DELETE], :cursor_backward)
  #bind_key(?r) { getstr("Enter a word: ") } if $log.debug?
  #bind_key(?m, :disp_menu)                  if $log.debug?
end

#next_lineObject

Deprecated.

gives offset of next line, does not move



442
443
444
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 442

def next_line  #:nodoc:
  @list[@current_index+1]
end

#on_enterObject

added 2010-09-30 18:48 so standard with other components, esp on enter NOTE: the on_enter repaint required causes this to be repainted 2 times if its the first object, once with the entire form, then with on_enter.



654
655
656
657
658
659
660
661
662
663
664
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 654

def on_enter
  if @list.nil? || @list.size == 0
    Ncurses.beep
    return :UNHANDLED
  end
  on_enter_row @current_index
  set_form_row 
  @repaint_required = true
  super
  true
end

#on_enter_row(arow) ⇒ Object

called by listscrollable, used by scrollbar ENTER_ROW



647
648
649
650
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 647

def on_enter_row arow
  fire_handler :ENTER_ROW, self
  @repaint_required = true
end

#paintObject

NOTE: earlier print_border was called only once in constructor, but when + a window is resized, and destroyed, then this was never called again, so the + border would not be seen in splitpane unless the width coincided exactly with + what is calculated in divider_location.



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 464

def paint  #:nodoc:

  $log.debug "XXX TEXTVIEW repaint HAPPENING #{@current_index} "
  my_win = nil
  if @form
    my_win = @form.window
  else
    my_win = @target_window
  end
  @graphic = my_win unless @graphic
  if @formatted_text
    $log.debug "XXX:  INSIDE FORMATTED TEXT "

    # I don't want to do this in 20 places and then have to change
    # it and retest. Let me push it to util.
    l = Canis::Utils.parse_formatted_text(@color_parser,
                                           @formatted_text)

    #cp = Chunks::ColorParser.new @color_parser
    #l = []
    #@formatted_text.each { |e| l << cp.convert_to_chunk(e) }

    @old_content_type = @content_type
    text(l)
    @formatted_text = nil

  end

  print_borders if (@suppress_borders == false && @repaint_all) # do this once only, unless everything changes
  rc = row_count
  maxlen = @maxlen || @width-@internal_width
  #$log.debug " #{@name} textview repaint width is #{@width}, height is #{@height} , maxlen #{maxlen}/ #{@maxlen}, #{@graphic.name} roff #{@row_offset} coff #{@col_offset}" 
  tm = get_content
  tr = @toprow
  acolor = get_color $datacolor
  h = scrollatrow() 
  r,c = rowcol
  @longest_line = @width-@internal_width #maxlen
  0.upto(h) do |hh|
    crow = tr+hh
    if crow < rc
        #focussed = @current_index == crow ? true : false 
        #selected = is_row_selected crow
        content = tm[crow]
        # next call modified string. you may wanna dup the string.
        # rlistbox does
        # scrolling fails if you do not dup, since content gets truncated
        if content.is_a? String
          content = content.dup
          sanitize(content) if @sanitization_required
          truncate content
          @graphic.printstring  r+hh, c, "%-*s" % [@width-@internal_width,content], 
            acolor, @attr
        elsif content.is_a? Chunks::ChunkLine
          # clear the line first
          @graphic.printstring  r+hh, c, " "* (@width-@internal_width), 
            acolor, @attr
          # move back
          @graphic.wmove r+hh, c
          # either we have to loop through and put in default color and attr
          # or pass it to show_col
          a = get_attrib @attrib
          # FIXME this does not clear till the eol
          @graphic.show_colored_chunks content, acolor, a, @width-@internal_width, @pcol
        elsif content.is_a? Chunks::Chunk
          raise "TODO chunk in textview"
        elsif content.is_a? Array
            # several chunks in one row - NOTE Very experimental may change
          if content[0].is_a? Array
            # clearing the line since colored_chunks does not yet XXX FIXME if possible
            @graphic.printstring  r+hh, c, " "* (@width-@internal_width), 
              acolor, @attr
            @graphic.wmove r+hh, c
            # either we have to loop through and put in default color and attr
            # or pass it to show_col
            a = get_attrib @attrib
            # FIXME this does not clear till the eol
            # # NOTE 2013-03-08 - 17:37 pls add width to avoid overflow
            @graphic.show_colored_chunks content, acolor, a, @width-@internal_width
            #@graphic.show_colored_chunks content, acolor, a
          else
            # a single row chunk - NOTE Very experimental may change
            text = content[1].dup
            sanitize(text) if @sanitization_required
            truncate text
            @graphic.printstring  r+hh, c, "%-*s" % [@width-@internal_width,text], 
              content[0] || acolor, content[2] || @attr
          end
        end

        # highlighting search results.
        if @search_found_ix == tr+hh
          if !@find_offset.nil?
            # handle exceed bounds, and if scrolling
            if @find_offset1 < maxlen+@pcol and @find_offset > @pcol
            @graphic.mvchgat(y=r+hh, x=c+@find_offset-@pcol, @find_offset1-@find_offset, Ncurses::A_NORMAL, $reversecolor, nil)
            end
          end
        end

    else
      # clear rows
      @graphic.printstring r+hh, c, " " * (@width-@internal_width), acolor,@attr
    end
  end


  @repaint_required = false
  @repaint_footer_required = true
  @repaint_all = false 
  # 2011-10-15 
  @widget_scrolled = false
  @record_changed = false
  @property_changed = false
  @old_pcol = @pcol

end

#pipe_fileObject



665
666
667
668
669
670
671
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 665

def pipe_file
  # TODO ask process name from user
  output = pipe_output 'munpack', @list
  if output && !output.empty?
    set_content output
  end
end

#pipe_output(pipeto, str) ⇒ Object

returns array of lines after running command on string passed TODO: need to close pipe other’s we’ll have a process lying around forever.



675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 675

def pipe_output (pipeto, str)
  case str
  when String
    #str = str.split "\n"
    # okay
  when Array
    str = str.join "\n"
  end
  #pipeto = '/usr/sbin/sendmail -t'
  #pipeto = %q{mail -s "my title" rahul}
  if pipeto != nil  # i was taking pipeto from a hash, so checking
    proc = IO.popen(pipeto, "w+")
    proc.puts str
    proc.close_write
    proc.readlines
  end
end

:nodoc:



210
211
212
213
214
215
216
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 210

def print_foot #:nodoc:
  @footer_attrib ||= Ncurses::A_REVERSE
  footer = "R: #{@current_index+1}, C: #{@curpos+@pcol}, #{@list.length} lines  "
  $log.debug " print_foot calling printstring with #{@row} + #{@height} -1, #{@col}+2"
  @graphic.printstring( @row + @height -1 , @col+2, footer, @color_pair || $datacolor, @footer_attrib) 
  @repaint_footer_required = false # 2010-01-23 22:55 
end

#remove_allObject



166
167
168
169
170
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 166

def remove_all
  @list = []
  init_vars
  @repaint_required = true
end

#repaintObject

textview :nodoc:



225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 225

def repaint # textview :nodoc:
  #$log.debug "TEXTVIEW repaint r c #{@row}, #{@col}, key: #{$current_key}, reqd #{@repaint_required} "  

  #return unless @repaint_required # 2010-02-12 19:08  TRYING - won't let footer print for col move
  # TRYING OUT dangerous 2011-10-13 
  @repaint_required = false
  @repaint_required = true if @widget_scrolled || @pcol != @old_pcol || @record_changed || @property_changed

  paint if @repaint_required

  @repaint_footer_required = true if @oldrow != @current_index # 2011-10-15 
  print_foot if @print_footer && !@suppress_borders && @repaint_footer_required
end

#row_countObject



188
189
190
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 188

def row_count
  @list.length
end

#row_lengthObject

determine length of row since we have chunks now. Since chunk implements length, so not required except for the old cases of demos that use an array.



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 250

def row_length
  case @buffer
  when String
    @buffer.length
  when Chunks::ChunkLine
    return @buffer.length
  when Array
    # this is for those old cases like rfe.rb which sent in an array
    # (before we moved to chunks) 
    # line is an array of arrays
    if @buffer[0].is_a? Array
      result = 0
      @buffer.each {|e| result += e[1].length  }
      return result
    end
    # line is one single chunk
    return @buffer[1].length
  end
end

#rowcolObject

returns the position where cursor was to be positioned by default It may no longer work like that.



201
202
203
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 201

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

#sanitize(content) ⇒ Object

takes a block, this way anyone extending this class can just pass a block to do his job This modifies the string



583
584
585
586
587
588
589
590
591
592
593
594
595
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 583

def sanitize content  #:nodoc:

  if content.is_a? String
    content.chomp!
    # trying out since gsub giving #<ArgumentError: invalid byte sequence in UTF-8> 2011-09-11 
    
    content.replace(content.encode("ASCII-8BIT", :invalid => :replace, :undef => :replace, :replace => "?")) if content.respond_to?(:encode)
    content.gsub!(/[\t\n\r]/, '  ') # don't display tab or newlines
    content.gsub!(/[^[:print:]]/, '')  # don't display non print characters
  else
    content
  end
end

#saveas(name = nil, config = {}) ⇒ Object



692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 692

def saveas name=nil, config={}
  unless name
    name = rb_gets "File to save as: "
    return if name.nil? || name == ""
  end
  exists = File.exists? name
  if exists # need to prompt
    return unless rb_confirm("Overwrite existing file? ")
  end
  l = getvalue
  File.open(name, "w"){ |f|
    l.each { |line| f.puts line }
    #l.each { |line| f.write line.gsub(/\r/,"\n") }
  }
  rb_puts "#{name} written."
end

#scrollatrowObject

—- for listscrollable —- ##



181
182
183
184
185
186
187
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 181

def scrollatrow #:nodoc:
  if @suppress_borders
    @height - 1  # should be 2 FIXME but erasing lower line. see appemail
  else
    @height - 3 
  end
end

#set_content(list, config = {}) ⇒ Object

send in a list e.g. set_content File.open(“README.txt”,“r”).readlines set wrap at time of passing :WRAP_NONE :WRAP_WORD XXX if we widen the textview later, as in a vimsplit that data will sti1ll be wrapped at this width !!

2011-12-3 changed wrap to hash, so we can use content_type :ansi, :tmux


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 105

def set_content list, config = {} #wrap = :WRAP_NONE
  @content_type = config[:content_type]
  _title = config[:title]
  self.title = _title if _title
  if @content_type
    formatted_text list, @content_type
    return
  end
  # please note that content type is lost once formatted text does it's work
  @wrap_policy = config[:wrap]
  if list.is_a? String
    if @wrap_policy == :WRAP_WORD
      data = wrap_text list
      @list = data.split("\n")
    else
      @list = list.split("\n")
    end
  elsif list.is_a? Array
    if @wrap_policy == :WRAP_WORD
      data = wrap_text list.join(" ")
      @list = data.split("\n")
    else
      @list = list
    end
  else
    raise "set_content expects Array not #{list.class}"
  end
  init_vars
end

#set_form_col(col1 = @curpos) ⇒ Object

set cursor on correct column tview



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 374

def set_form_col col1=@curpos #:nodoc:
  @cols_panned ||= 0
  @pad_offset ||= 0 # added 2010-02-11 21:54 since padded widgets get an offset.
  @curpos = col1
  maxlen = @maxlen || @width-@internal_width
  #@curpos = maxlen if @curpos > maxlen
  if @curpos > maxlen
    @pcol = @curpos - maxlen
    @curpos = maxlen - 1
    @repaint_required = true # this is required so C-e can pan screen
  else
    @pcol = 0
  end
  # the rest only determines cursor placement
  win_col = 0 # 2010-02-07 23:19 new cursor stuff
  col2 = win_col + @col + @col_offset + @curpos + @cols_panned + @pad_offset
  $log.debug "TV SFC #{@name} setting c to #{col2} #{win_col} #{@col} #{@col_offset} #{@curpos} "
  #@form.setrowcol @form.row, col
  setrowcol nil, col2
  @repaint_footer_required = true
end

#text(*val) ⇒ Object

for consistency with other objects that respect text alias :text :set_content



136
137
138
139
140
141
142
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 136

def text(*val)
  if val.empty?
    return @list
  end
  set_content(*val)
  self
end

#text=(val) ⇒ Object



143
144
145
146
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 143

def text=(val)
  return unless val # added 2010-11-17 20:11, dup will fail on nil
  set_content(val)
end

#top_row(*val) ⇒ Object

display this row on top



172
173
174
175
176
177
178
179
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 172

def top_row(*val) #:nodoc:
  if val.empty?
    @toprow
  else
    @toprow = val[0] || 0
  end
  @repaint_required = true
end

#truncate(content) ⇒ Object

returns only the visible portion of string taking into account display length and horizontal scrolling. MODIFIES STRING



598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 598

def truncate content  #:nodoc:
  _maxlen = @maxlen || @width-@internal_width
  _maxlen = @width-@internal_width if _maxlen > @width-@internal_width # take care of decrease in width
  if !content.nil? 
    if content.length > _maxlen # only show maxlen
      @longest_line = content.length if content.length > @longest_line
      #content = content[@pcol..@pcol+maxlen-1] 
      content.replace(content[@pcol..@pcol+_maxlen-1] || "")
    else
      if @pcol > 0
          content.replace(content[@pcol..-1]  || "")
      end
    end
  end
  content
end

#wrap_text(txt, col = @maxlen) ⇒ Object

:nodoc:



204
205
206
207
208
209
# File 'lib/canis/core/widgets/deprecated/rtextview.rb', line 204

def wrap_text(txt, col = @maxlen) #:nodoc:
  col ||= @width-@internal_width
  #$log.debug "inside wrap text for :#{txt}"
  txt.gsub(/(.{1,#{col}})( +|$\n?)|(.{1,#{col}})/,
           "\\1\\3\n") 
end