Class: Canis::Table

Inherits:
TextPad show all
Defined in:
lib/canis/core/widgets/table.rb

Instance Attribute Summary collapse

Attributes inherited from TextPad

#cols, #current_index, #document, #key_handler, #lastcol, #lastrow, #pad, #text_patterns

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 inherited from TextPad

#DEADhighlight_row, #ORIG_text, #ORIGnext_match, #[]=, #__calc_dimensions, #_do_borders, #_getarray, #_handle_key, #append, #ask_search, #backward_regex, #backward_word, #bottom_of_window, #bounds_check, #check_prow, #clear_pad, #clear_row, #content, #create_default_keyhandler, #current_value, #cursor_backward, #cursor_bol, #cursor_eol, #cursor_forward, #destroy, #down, #find_more, #fire_dimension_changed, #fire_row_changed, #forward_regex, #forward_word, #goto_end, #goto_last_position, #goto_line, #goto_start, #handle_key, #height=, #init_vars, #is_visible?, #lastcurpos, #map_keys, #middle_of_window, #next_match, #next_regex, #on_enter, #on_enter_row, #pad_cols, #pad_rows, #preprocess_text, #render, #repaint, #row_count, #rowcol, #scroll_backward, #scroll_forward, #scroll_left, #scroll_right, #scroll_window_down, #scroll_window_up, #scrollatrows, #set_form_col, #set_form_row, #text_action_event, #top_of_window, #up, #visual_index, #width=

Methods included from BorderTitle

#bordertitle_init, #print_borders, #print_title

Methods inherited from Widget

#action_manager, #bgcolor, #color, #color_pair, #command, #destroy, #focus, #focusable, #focusable?, #getvalue, #getvalue_for_paint, #handle_key, #hide, #init_vars, #modified?, #move, #on_enter, #on_leave, #override_graphic, #process_key, #property_set, #remove, #repaint, #repaint_all, #repaint_required, #rowcol, #set_form, #set_form_col, #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) ⇒ Table

Returns a new instance of Table.



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/canis/core/widgets/table.rb', line 413

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

  # array of column info objects
  @chash = []
  # chash should be an array which is basically the order of rows to be printed
  #  it contains index, which is the offset of the row in the data @list
  #  When printing we should loop through chash and get the index in data
  #
  # should be zero here, but then we won't get textpad correct
  @_header_adjustment = 0
  @col_min_width = 3

  self.extend DefaultListSelection
  super
  create_default_renderer unless @renderer # 2014-04-10 - 11:01
  # NOTE listselection takes + and - for ask_select
  bind_key(?w, "next column") { self.next_column }
  bind_key(?b, "prev column") { self.prev_column }
  bind_key(?\M-\-, "contract column") { self.contract_column }
  bind_key(?\M-+, "expand column") { self.expand_column }
  bind_key(?=, "expand column to width") { self.expand_column_to_width }
  bind_key(?\M-=, "expand column to width") { self.expand_column_to_max_width }
  bind_key(?\C-s, "Save as") { self.save_as(nil) }
  #@list_selection_model ||= DefaultListSelectionModel.new self
  set_default_selection_model unless @list_selection_model
end

Instance Attribute Details

#table_row_sorterObject

attr_reader :columns



411
412
413
# File 'lib/canis/core/widgets/table.rb', line 411

def table_row_sorter
  @table_row_sorter
end

Instance Method Details

#_calculate_column_offsetsObject

This calculates and stores the offset at which each column starts. Used when going to next column or doing a find for a string in the table. TODO store this inside the hash so it’s not calculated again in renderer



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/canis/core/widgets/table.rb', line 483

def _calculate_column_offsets
  @coffsets = []
  total = 0

  #@chash.each_pair { |i, c|
  #@chash.each_with_index { |c, i|
    #next if c.hidden
  each_column {|c,i|
    w = c.width
    @coffsets[i] = total
    c.offset = total
    # if you use prepare_format then use w+2 due to separator symbol
    total += w + 1
  }
end

#_convert_curpos_to_columnInteger

Convert current cursor position to a table column calculate column based on curpos since user may not have user w and b keys (:next_column)

Returns:

  • (Integer)

    column index base 0



502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'lib/canis/core/widgets/table.rb', line 502

def _convert_curpos_to_column  #:nodoc:
  _calculate_column_offsets unless @coffsets
  x = 0
  @coffsets.each_with_index { |i, ix|
    if @curpos < i
      break
    else
      x += 1
    end
  }
  x -= 1 # since we start offsets with 0, so first auto becoming 1
  return x
end

#_init_model(array) ⇒ Object

size each column based on widths of this row of data. Only changed width if no width for that column



649
650
651
652
653
654
655
656
657
658
659
# File 'lib/canis/core/widgets/table.rb', line 649

def _init_model array
  # clear the column data -- this line should be called otherwise previous tables stuff will remain.
  @chash.clear
  array.each_with_index { |e,i|
    # if columns added later we could be overwriting the width
    c = get_column(i)
    c.width ||= 10
  }
  # maintains index in current pointer and gives next or prev
  @column_pointer = Circular.new array.size()-1
end

#_invalidate_width_cacheObject



873
874
875
# File 'lib/canis/core/widgets/table.rb', line 873

def _invalidate_width_cache    #:nodoc:
  @coffsets = nil
end

#add(array) ⇒ Object Also known as: <<

add a row to the table The name add will be removed soon, pls use << instead.



798
799
800
801
802
803
804
805
806
807
# File 'lib/canis/core/widgets/table.rb', line 798

def add array
  unless @list
    # columns were not added, this most likely is the title
    @list ||= []
    _init_model array
  end
  @list << array
  fire_dimension_changed
  self
end

#add_column(tc) ⇒ Object

TODO



887
888
889
890
# File 'lib/canis/core/widgets/table.rb', line 887

def add_column tc
  raise "to figure out add_column"
  _invalidate_width_cache
end

#calculate_column_width(col, maxrows = 99) ⇒ Object



896
897
898
899
900
901
902
903
904
905
906
907
908
909
# File 'lib/canis/core/widgets/table.rb', line 896

def calculate_column_width col, maxrows=99
  ret = 3
  ctr = 0
  @list.each_with_index { |r, i|
    #next if i < @toprow # this is also a possibility, it checks visible rows
    break if ctr > maxrows
    ctr += 1
    #next if r == :separator
    c = r[col]
    x = c.to_s.length
    ret = x if x > ret
  }
  ret
end

#clearObject

clear the list completely



825
826
827
828
# File 'lib/canis/core/widgets/table.rb', line 825

def clear
  @selected_indices.clear
  super
end

#clear_matchesObject

return @indices



1032
1033
1034
1035
1036
1037
1038
1039
# File 'lib/canis/core/widgets/table.rb', line 1032

def clear_matches
  # clear previous match so all data can show again
  if @indices && @indices.count > 0
    fire_dimension_changed
    init_vars
  end
  @indices = nil
end

#column_align(colindex, align) ⇒ Object

convenience method to set alignment of a column

Parameters:

  • index

    of column

  • align
    • :right (any other value is taken to be left)



862
863
864
# File 'lib/canis/core/widgets/table.rb', line 862

def column_align colindex, align
  get_column(colindex).align = align
end

#column_hidden(colindex, hidden) ⇒ Object

convenience method to hide or unhide a column Provided since column offsets need to be recalculated in the case of a width change or visibility change



868
869
870
871
# File 'lib/canis/core/widgets/table.rb', line 868

def column_hidden colindex, hidden
  get_column(colindex).hidden = hidden
  _invalidate_width_cache
end

#column_modelObject

returns collection of ColumnInfo objects



460
461
462
# File 'lib/canis/core/widgets/table.rb', line 460

def column_model
  @chash
end

#column_width(colindex, width) ⇒ Object

——- column related methods ——#

convenience method to set width of a column For setting other attributes, use get_column(index)

Parameters:

  • index

    of column

  • width


855
856
857
858
# File 'lib/canis/core/widgets/table.rb', line 855

def column_width colindex, width
  get_column(colindex).width = width
  _invalidate_width_cache
end

#columns(*val) ⇒ Object

NOTE

Appends columns to array, so it must be set before data, and thus it should
clear the list


612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
# File 'lib/canis/core/widgets/table.rb', line 612

def columns(*val)
  if val.empty?
    # returns array of column names as Strings
    @list[0]
  else
    array = val[0]
    @_header_adjustment = 1
    @list ||= []
    @list.clear
    @list << array
    _init_model array

    # update the names in column model
    array.each_with_index { |n,i|
      c = get_column(i)
      #c.name = name     ## 2018-05-19 - seems to be a bug
      c.name = n
    }
    self
  end
end

#columns=(array) ⇒ Object Also known as: headings=

Deprecated.

complicated, just use ‘columns()`

Set column titles with given array of strings. NOTE: This is only required to be called if first row of file or content does not contain titles. In that case, this should be called before setting the data as the array passed is appended into the content array.



640
641
642
643
# File 'lib/canis/core/widgets/table.rb', line 640

def columns=(array)
  columns(array)
  self
end

#content_colsObject

calculate pad width based on widths of columns



465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/canis/core/widgets/table.rb', line 465

def content_cols
  total = 0
  #@chash.each_pair { |i, c|
  #@chash.each_with_index { |c, i|
    #next if c.hidden
  each_column {|c,i|
    w = c.width
    # if you use prepare_format then use w+2 due to separator symbol
    total += w + 1
  }
  return total
end

#contract_columnObject



581
582
583
584
585
586
587
588
# File 'lib/canis/core/widgets/table.rb', line 581

def contract_column
  x = _convert_curpos_to_column
  w = get_column(x).width
  return if w <= @col_min_width
  column_width x, w-1 if w
  @coffsets = nil
  fire_dimension_changed
end

#create_default_rendererObject

set a default renderer –

we were not doing this automatically, so repaint was going to TP and failing on mvaddstr
2014-04-10 - 10:57

++



949
950
951
952
# File 'lib/canis/core/widgets/table.rb', line 949

def create_default_renderer
  r = DefaultTableRenderer.new self
  renderer(r)
end

#create_default_sorterObject



940
941
942
943
# File 'lib/canis/core/widgets/table.rb', line 940

def create_default_sorter
  raise "Data not sent in." unless @list
  @table_row_sorter = DefaultTableRowSorter.new @list
end

#delete_at(ix) ⇒ Object

delete a data row at index

NOTE : This does not adjust for header_adjustment. So zero will refer to the header if there is one.

This is to keep consistent with textpad which does not know of header_adjustment and uses the actual
index. Usually, programmers will be dealing with +@current_index+

Raises:

  • (ArgumentError)


816
817
818
819
820
821
822
# File 'lib/canis/core/widgets/table.rb', line 816

def delete_at ix
  return unless @list
  raise ArgumentError, "Argument must be within 0 and #{@list.length}" if ix < 0 or ix >=  @list.length
  fire_dimension_changed
  #@list.delete_at(ix + @_header_adjustment)
  @list.delete_at(ix)
end

#each_columnObject

yields non-hidden columns (ColumnInfo) and the offset/index This is the order in which columns are to be printed



1054
1055
1056
1057
1058
1059
# File 'lib/canis/core/widgets/table.rb', line 1054

def each_column
  @chash.each_with_index { |c, i|
    next if c.hidden
    yield c,i if block_given?
  }
end

#ensure_visible(row = @current_index) ⇒ Object

Ensure current row is visible, if not make it first row

This overrides textpad due to header_adjustment, otherwise
during next_match, the header overrides the found row.

Parameters:

  • current_index (default if not given)


1046
1047
1048
1049
1050
# File 'lib/canis/core/widgets/table.rb', line 1046

def ensure_visible row = @current_index
  unless is_visible? row
      @prow = @current_index - @_header_adjustment
  end
end

#estimate_column_widthsObject

estimate columns widths based on data in first 10 or so rows This will override any previous widths, so put custom widths after calling this.



675
676
677
678
679
680
# File 'lib/canis/core/widgets/table.rb', line 675

def estimate_column_widths
  each_column {|c,i|
    c.width  = suggest_column_width(i)
  }
  self
end

#expand_columnObject



556
557
558
559
560
561
562
# File 'lib/canis/core/widgets/table.rb', line 556

def expand_column
  x = _convert_curpos_to_column
  w = get_column(x).width
  column_width x, w+1 if w
  @coffsets = nil
  fire_dimension_changed
end

#expand_column_to_max_widthObject

find the width of the longest item in the current columns and expand the width to that.



576
577
578
579
580
# File 'lib/canis/core/widgets/table.rb', line 576

def expand_column_to_max_width
  x = _convert_curpos_to_column
  w = calculate_column_width x
  expand_column_to_width w
end

#expand_column_to_width(w = nil) ⇒ Object



563
564
565
566
567
568
569
570
571
572
573
# File 'lib/canis/core/widgets/table.rb', line 563

def expand_column_to_width w=nil
  x = _convert_curpos_to_column
  unless w
    # expand to width of current cell
    s = @list[@current_index][x]
    w = s.to_s.length + 1
  end
  column_width x, w
  @coffsets = nil
  fire_dimension_changed
end

#filename(name, _config = {}) ⇒ Object Also known as: load

Takes the name of a file containing delimited data

and load it into the table.

This method will load and split the file into the table.

NOTE

if columns is not mentioned, then it defaults to false

Example

table = Table.new ...
table.filename 'contacts.tsv', :separator => '|', :columns => true

Parameters:

  • name

    is the file name

  • config

    is a hash containing:

    • :separator - field separator, default is TAB

    • :columns - array of column names

      or true - first row is column names
      or false - no columns.
      


742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
# File 'lib/canis/core/widgets/table.rb', line 742

def filename name, _config = {}
  arr = File.open(name,"r").read.split("\n")
  lines = []
  sep = _config[:separator] || _config[:delimiter] || '\t'
  arr.each { |l| lines << l.split(sep) }
  cc = _config[:columns]
  if cc.is_a? Array
    columns(cc)
    text(lines)
  elsif cc
    # cc is true, use first row as column names
    columns(lines[0])
    text(lines[1..-1])
  else
    # cc is false - no columns
    # XXX since columns() is not called, so chash is not cleared.
    _init_model lines[0]
    text(lines)
  end
end

#fire_action_eventObject

called when ENTER is pressed. Takes into account if user is on header_row



961
962
963
964
965
966
967
968
969
970
971
972
973
974
# File 'lib/canis/core/widgets/table.rb', line 961

def fire_action_event
  if header_row?
    if @table_row_sorter
      x = _convert_curpos_to_column
      c = @chash[x]
      # convert to index in data model since sorter only has data_model
      index = c.index
      @table_row_sorter.toggle_sort_order index
      @table_row_sorter.sort
      fire_dimension_changed
    end
  end
  super
end

#fire_column_event(eve) ⇒ Object

a column traversal has happened. FIXME needs to be looked into. is this consistent naming wise and are we using the correct object In old system it was TABLE_TRAVERSAL_EVENT



551
552
553
554
555
# File 'lib/canis/core/widgets/table.rb', line 551

def fire_column_event eve
  require 'canis/core/include/ractionevent'
  aev = TextActionEvent.new self, eve, get_column(@column_pointer.current_index), @column_pointer.current_index, @column_pointer.last_index
  fire_handler eve, aev
end

#get_column(index) ⇒ Object

retrieve the column info structure for the given offset. The offset pertains to the visible offset not actual offset in data model. These two differ when we move a column.

Returns:

  • ColumnInfo object containing width align color bgcolor attrib hidden



450
451
452
453
454
455
456
457
# File 'lib/canis/core/widgets/table.rb', line 450

def get_column index
  return @chash[index] if @chash[index]
  # create a new entry since none present
  c = ColumnInfo.new
  c.index = index
  @chash[index] = c
  return c
end

#get_value_at(row, col) ⇒ Object

get the value at the cell at row and col

Returns:

  • String



832
833
834
835
# File 'lib/canis/core/widgets/table.rb', line 832

def get_value_at row,col
  actrow = row + @_header_adjustment
  @list[actrow, col]
end

#header_adjustmentObject



599
600
601
# File 'lib/canis/core/widgets/table.rb', line 599

def header_adjustment
  @_header_adjustment
end

#header_row?Boolean

returns true if focus is on header_row

Returns:

  • (Boolean)


954
955
956
957
# File 'lib/canis/core/widgets/table.rb', line 954

def header_row?
  #@prow == 0
  @prow == @current_index
end

#matching_indicesObject

yields each column to caller method if yield returns true, collects index of row into array and returns the array Value yielded can be fixnum or date etc



1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
# File 'lib/canis/core/widgets/table.rb', line 1013

def matching_indices
  raise "block required for matching_indices" unless block_given?
  @indices = []
  ## content can be string or Chunkline, so we had to write <tt>index</tt> for this.
  @list.each_with_index do |fields, ix|
    flag = yield ix, fields
    if flag
      @indices << ix
    end
  end
  #$log.debug "XXX:  INDICES found #{@indices}"
  if @indices.count > 0
    fire_dimension_changed
    init_vars
  else
    @indices = nil
  end
  #return @indices
end

#model_row(index) ⇒ Object

size each column based on widths of this row of data.



661
662
663
664
665
666
667
668
669
670
671
# File 'lib/canis/core/widgets/table.rb', line 661

def model_row index
  array = @list[index]
  array.each_with_index { |c,i|
    # if columns added later we could be overwriting the width
    ch = get_column(i)
    ch.width = c.to_s.length + 2
  }
  # maintains index in current pointer and gives next or prev
  @column_pointer = Circular.new array.size()-1
  self
end

#move_column(ix, newix) ⇒ Object

should all this move into table column model or somepn move a column from offset ix to offset newix



879
880
881
882
883
884
885
# File 'lib/canis/core/widgets/table.rb', line 879

def move_column ix, newix
  acol = @chash.delete_at ix
  @chash.insert newix, acol
  _invalidate_width_cache
  #tmce = TableColumnModelEvent.new(ix, newix, self, :MOVE)
  #fire_handler :TABLE_COLUMN_MODEL_EVENT, tmce
end

#next_columnObject

jump cursor to next column TODO : if cursor goes out of view, then pad should scroll right or left and down



526
527
528
529
530
531
532
533
534
535
# File 'lib/canis/core/widgets/table.rb', line 526

def next_column
  # TODO take care of multipliers
  _calculate_column_offsets unless @coffsets
  c = @column_pointer.next
  cp = @coffsets[c]
  #$log.debug " next_column #{c} , #{cp} "
  @curpos = cp if cp
  down() if c < @column_pointer.last_index
  fire_column_event :ENTER_COLUMN
end

#OLDnext_match(str) ⇒ Object

Find the next row that contains given string Overrides textpad since each line is an array NOTE does not go to next match within row NOTE: FIXME ensure_visible puts prow = current_index so in this case, the header

overwrites the matched row.

@ deprecate since it does not get second match in line. textpad does however, the offset textpad shows is wrong

Parameters:

  • String

    to find

Returns:

  • row and col offset of match, or nil



985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
# File 'lib/canis/core/widgets/table.rb', line 985

def OLDnext_match str
  _calculate_column_offsets unless @coffsets
  first = nil
  ## content can be string or Chunkline, so we had to write <tt>index</tt> for this.
  @list.each_with_index do |fields, ix|
    #col = line.index str
    #fields.each_with_index do |f, jx|
    #@chash.each_with_index do |c, jx|
      #next if c.hidden
    each_column do |c,jx|
      f = fields[c.index]
      # value can be numeric
      col = f.to_s.index str
      if col
        col += @coffsets[jx]
        first ||= [ ix, col ]
        if ix > @current_index
          return [ix, col]
        end
      end
    end
  end
  return first
end

#padrefreshObject

refresh pad onto window overrides super due to header_adjustment and the header too



913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
# File 'lib/canis/core/widgets/table.rb', line 913

def padrefresh
  top = @window.top
  left = @window.left
  sr = @startrow + top
  sc = @startcol + left
  # first do header always in first row
  # -- prefresh arguments are:
  # 1. pad
  # 2. pminrow
  # 3. pmincol (pad upper left)
  # 4, sminrow (screen upper left row)
  # 5, smincol
  # 6, smaxrow
  # 7, smaxcol

  # 2019-03-12 - fixed bug in table, only printed header if table on row 1
  retval = FFI::NCurses.prefresh(@pad, @prow, @pcol, sr, sc, sr + 1, @cols + sc)
  $log.warn "XXX:  PADREFRESH HEADER #{retval}, #{@prow}, #{@pcol}, #{sr}, #{sc}, #{1+sr}, #{@cols+sc}." if retval == -1
  # retval = FFI::NCurses.prefresh(@pad,0,@pcol, sr , sc , 2 , @cols+ sc );
  # now print rest of data
  # h is header_adjustment
  h = 1
  retval = FFI::NCurses.prefresh(@pad,@prow + h,@pcol, sr + h , sc , @rows + sr  , @cols+ sc );
  $log.warn "XXX:  PADREFRESH #{retval}, #{@prow}, #{@pcol}, #{sr}, #{sc}, #{@rows+sr}, #{@cols+sc}." if retval == -1
  # padrefresh can fail if width is greater than NCurses.COLS
end

#prev_columnObject

jump cursor to previous column TODO : if cursor goes out of view, then pad should scroll right or left and down



538
539
540
541
542
543
544
545
546
547
# File 'lib/canis/core/widgets/table.rb', line 538

def prev_column
  # TODO take care of multipliers
  _calculate_column_offsets unless @coffsets
  c = @column_pointer.previous
  cp = @coffsets[c]
  #$log.debug " prev #{c} , #{cp} "
  @curpos = cp if cp
  up() if c > @column_pointer.last_index
  fire_column_event :ENTER_COLUMN
end

print footer containing line and total, overriding textpad which prints column offset also This is called internally by repaint() but can be overridden for more complex printing.



1075
1076
1077
1078
1079
1080
1081
1082
# File 'lib/canis/core/widgets/table.rb', line 1075

def print_foot
  return unless @print_footer
  ha = @_header_adjustment
  # ha takes into account whether there are headers or not
  footer = "#{@current_index+1-ha} of #{@list.length-ha} "
  @graphic.printstring( @row + @height -1 , @col+2, footer, @color_pair || $datacolor, @footer_attrib)
  @repaint_footer_required = false
end

#remove_column(tc) ⇒ Object

TODO



892
893
894
895
# File 'lib/canis/core/widgets/table.rb', line 892

def remove_column tc
  raise "to figure out add_column"
  _invalidate_width_cache
end

#render_allObject

calls the renderer for all rows of data giving them pad, lineno, and line data



1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
# File 'lib/canis/core/widgets/table.rb', line 1061

def render_all
  if @indices && @indices.count > 0
    @indices.each_with_index do |ix, jx|
      render @pad, jx, @list[ix]
    end
  else
    @list.each_with_index { |line, ix|
      # FFI::NCurses.mvwaddstr(@pad,ix, 0, @list[ix].to_s)
      render @pad, ix, line
    }
  end
end

#renderer(r) ⇒ Object

def method_missing(name, *args) @tp.send(name, *args) end

supply a custom renderer that implements render()



596
597
598
# File 'lib/canis/core/widgets/table.rb', line 596

def renderer r
  @renderer = r
end

#resultset(columns, data) ⇒ Object

set column array and data array in one shot Erases any existing content



719
720
721
722
723
# File 'lib/canis/core/widgets/table.rb', line 719

def resultset columns, data
  @list = []
  columns(columns)
  text(data)
end

#save_as(outfile) ⇒ Object

save the table as a file Currently, tabs are used as delimiter, but this could be based on input separator, or prompted.

Parameters:

  • String

    name of output file. If nil, user is prompted



768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
# File 'lib/canis/core/widgets/table.rb', line 768

def save_as outfile
  _t = "(all rows)"
  if @selected_indices.size > 0
    _t = "(selected rows)"
  end
  unless outfile
    outfile = get_string "Enter file name to save #{_t} as "
    return unless outfile
  end

  # if there is a selection, then write only selected rows
  l = nil
  if @selected_indices.size > 0
    l = []
    @list.each_with_index { |v,i| l << v if @selected_indices.include? i }
  else
    l = @list
  end

  File.open(outfile, 'w') {|f|
    l.each {|r|
      line = r.join "\t"
      f.puts line
    }
  }
end

#set_default_selection_modelObject

set the default selection model as the operational one



441
442
443
444
# File 'lib/canis/core/widgets/table.rb', line 441

def set_default_selection_model
  @list_selection_model = nil
  @list_selection_model = Canis::DefaultListSelectionModel.new self
end

#set_value_at(row, col, val) ⇒ Object

set value at the cell at row and col

Parameters:

  • int

    row

  • int

    col

  • String

    value

Returns:

  • self



842
843
844
845
846
847
# File 'lib/canis/core/widgets/table.rb', line 842

def set_value_at row,col,val
  actrow = row + @_header_adjustment
  @list[actrow , col] = val
  fire_row_changed actrow
  self
end

#suggest_column_width(col) ⇒ Object

calculates and returns a suggested columns width for given column based on data (first 10 rows) called by estimate_column_widths in a loop



684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
# File 'lib/canis/core/widgets/table.rb', line 684

def suggest_column_width col
  #ret = @cw[col] || 2
  ret = get_column(col).width || 2
  ctr = 0
  @list.each_with_index { |r, i|
    #next if i < @toprow # this is also a possibility, it checks visible rows
    break if ctr > 10
    ctr += 1
    next if r == :separator
    c = r[col]
    x = c.to_s.length
    ret = x if x > ret
  }
  ret
end

#text(lines, fmt = :none) ⇒ Object

I am assuming the column has been set using columns= Now only data is being sent in NOTE : calling set_content sends to TP’s text() which resets @list

Parameters:

  • lines

    is an array or arrays



706
707
708
709
710
711
712
713
714
# File 'lib/canis/core/widgets/table.rb', line 706

def text lines, fmt=:none
  # maybe we can check this out
  # should we not erase data, will user keep one column and resetting data ?
  # set_content assumes data is gone.
  @list ||= []  # this would work if no columns
  @list.concat( lines)
  fire_dimension_changed
  self
end

#to_searchable(index) ⇒ Object

convert the row into something searchable so that offsets returned by index

are exactly what is seen on the screen.


517
518
519
520
521
522
523
# File 'lib/canis/core/widgets/table.rb', line 517

def to_searchable index
  if @renderer
    @renderer.to_searchable(@list[index])
  else
    @list[index].to_s
  end
end