Class: Canis::Table
- Defined in:
- lib/canis/core/widgets/table.rb
Instance Attribute Summary collapse
-
#table_row_sorter ⇒ Object
attr_reader :columns.
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
-
#_calculate_column_offsets ⇒ Object
This calculates and stores the offset at which each column starts.
-
#_convert_curpos_to_column ⇒ Fixnum
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).
-
#_init_model(array) ⇒ Object
size each column based on widths of this row of data.
- #_invalidate_width_cache ⇒ Object
-
#add(array) ⇒ Object
(also: #<<)
add a row to the table The name add will be removed soon, pls use << instead.
-
#add_column(tc) ⇒ Object
TODO.
- #calculate_column_width(col, maxrows = 99) ⇒ Object
-
#clear ⇒ Object
clear the list completely.
-
#clear_matches ⇒ Object
return @indices.
-
#column_align(colindex, align) ⇒ Object
convenience method to set alignment of a column.
-
#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.
-
#column_model ⇒ Object
returns collection of ColumnInfo objects.
-
#column_width(colindex, width) ⇒ Object
——- column related methods ——#.
-
#columns(*val) ⇒ Object
NOTE Appends columns to array, so it must be set before data, and thus it should clear the list.
-
#columns=(array) ⇒ Object
(also: #headings=)
deprecated
Deprecated.
complicated, just use ‘columns()`
-
#content_cols ⇒ Object
calculate pad width based on widths of columns.
- #contract_column ⇒ Object
-
#create_default_renderer ⇒ Object
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 ++.
- #create_default_sorter ⇒ Object
-
#delete_at(ix) ⇒ Object
delete a data row at index .
-
#each_column ⇒ Object
yields non-hidden columns (ColumnInfo) and the offset/index This is the order in which columns are to be printed.
-
#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.
-
#estimate_column_widths ⇒ Object
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.
- #expand_column ⇒ Object
-
#expand_column_to_max_width ⇒ Object
find the width of the longest item in the current columns and expand the width to that.
- #expand_column_to_width(w = nil) ⇒ Object
-
#filename(name, _config = {}) ⇒ Object
(also: #load)
Takes the name of a file containing delimited data and load it into the table.
-
#fire_action_event ⇒ Object
called when ENTER is pressed.
-
#fire_column_event(eve) ⇒ Object
a column traversal has happened.
-
#get_column(index) ⇒ Object
retrieve the column info structure for the given offset.
-
#get_value_at(row, col) ⇒ Object
get the value at the cell at row and col.
- #header_adjustment ⇒ Object
-
#header_row? ⇒ Boolean
returns true if focus is on header_row.
-
#initialize(form = nil, config = {}, &block) ⇒ Table
constructor
A new instance of Table.
-
#matching_indices ⇒ Object
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.
-
#model_row(index) ⇒ Object
size each column based on widths of this row of data.
-
#move_column(ix, newix) ⇒ Object
should all this move into table column model or somepn move a column from offset ix to offset newix.
-
#next_column ⇒ Object
jump cursor to next column TODO : if cursor goes out of view, then pad should scroll right or left and down.
-
#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.
-
#padrefresh ⇒ Object
refresh pad onto window overrides super due to header_adjustment and the header too.
-
#prev_column ⇒ Object
jump cursor to previous column TODO : if cursor goes out of view, then pad should scroll right or left and down.
-
#print_foot ⇒ Object
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.
-
#remove_column(tc) ⇒ Object
TODO.
-
#render_all ⇒ Object
calls the renderer for all rows of data giving them pad, lineno, and line data.
-
#renderer(r) ⇒ Object
def method_missing(name, *args) @tp.send(name, *args) end.
-
#resultset(columns, data) ⇒ Object
set column array and data array in one shot Erases any existing content.
-
#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.
-
#set_default_selection_model ⇒ Object
set the default selection model as the operational one.
-
#set_value_at(row, col, val) ⇒ Object
set value at the cell at row and col.
-
#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. -
#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. -
#to_searchable(index) ⇒ Object
convert the row into something searchable so that offsets returned by
index
are exactly what is seen on the screen.
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
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.
412 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 |
# File 'lib/canis/core/widgets/table.rb', line 412 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 #1 @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. } bind_key(?=, "expand column to width") { self. } bind_key(?\M-=, "expand column to width") { self. } 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_sorter ⇒ Object
attr_reader :columns
410 411 412 |
# File 'lib/canis/core/widgets/table.rb', line 410 def table_row_sorter @table_row_sorter end |
Instance Method Details
#_calculate_column_offsets ⇒ Object
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
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 |
# File 'lib/canis/core/widgets/table.rb', line 482 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_column ⇒ Fixnum
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)
501 502 503 504 505 506 507 508 509 510 511 512 513 |
# File 'lib/canis/core/widgets/table.rb', line 501 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
647 648 649 650 651 652 653 654 655 656 657 |
# File 'lib/canis/core/widgets/table.rb', line 647 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_cache ⇒ Object
871 872 873 |
# File 'lib/canis/core/widgets/table.rb', line 871 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.
796 797 798 799 800 801 802 803 804 805 |
# File 'lib/canis/core/widgets/table.rb', line 796 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
885 886 887 888 |
# File 'lib/canis/core/widgets/table.rb', line 885 def add_column tc raise "to figure out add_column" _invalidate_width_cache end |
#calculate_column_width(col, maxrows = 99) ⇒ Object
894 895 896 897 898 899 900 901 902 903 904 905 906 907 |
# File 'lib/canis/core/widgets/table.rb', line 894 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 |
#clear ⇒ Object
clear the list completely
823 824 825 826 |
# File 'lib/canis/core/widgets/table.rb', line 823 def clear @selected_indices.clear super end |
#clear_matches ⇒ Object
return @indices
1018 1019 1020 1021 1022 1023 1024 1025 |
# File 'lib/canis/core/widgets/table.rb', line 1018 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
860 861 862 |
# File 'lib/canis/core/widgets/table.rb', line 860 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
866 867 868 869 |
# File 'lib/canis/core/widgets/table.rb', line 866 def column_hidden colindex, hidden get_column(colindex).hidden = hidden _invalidate_width_cache end |
#column_model ⇒ Object
returns collection of ColumnInfo objects
459 460 461 |
# File 'lib/canis/core/widgets/table.rb', line 459 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)
853 854 855 856 |
# File 'lib/canis/core/widgets/table.rb', line 853 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
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 |
# File 'lib/canis/core/widgets/table.rb', line 611 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 } self end end |
#columns=(array) ⇒ Object Also known as: headings=
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.
638 639 640 641 |
# File 'lib/canis/core/widgets/table.rb', line 638 def columns=(array) columns(array) self end |
#content_cols ⇒ Object
calculate pad width based on widths of columns
464 465 466 467 468 469 470 471 472 473 474 475 |
# File 'lib/canis/core/widgets/table.rb', line 464 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_column ⇒ Object
580 581 582 583 584 585 586 587 |
# File 'lib/canis/core/widgets/table.rb', line 580 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_renderer ⇒ Object
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
++
935 936 937 938 |
# File 'lib/canis/core/widgets/table.rb', line 935 def create_default_renderer r = DefaultTableRenderer.new self renderer(r) end |
#create_default_sorter ⇒ Object
926 927 928 929 |
# File 'lib/canis/core/widgets/table.rb', line 926 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+
814 815 816 817 818 819 820 |
# File 'lib/canis/core/widgets/table.rb', line 814 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_column ⇒ Object
yields non-hidden columns (ColumnInfo) and the offset/index This is the order in which columns are to be printed
1040 1041 1042 1043 1044 1045 |
# File 'lib/canis/core/widgets/table.rb', line 1040 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.
1032 1033 1034 1035 1036 |
# File 'lib/canis/core/widgets/table.rb', line 1032 def ensure_visible row = @current_index unless is_visible? row @prow = @current_index - @_header_adjustment end end |
#estimate_column_widths ⇒ Object
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.
673 674 675 676 677 678 |
# File 'lib/canis/core/widgets/table.rb', line 673 def estimate_column_widths each_column {|c,i| c.width = suggest_column_width(i) } self end |
#expand_column ⇒ Object
555 556 557 558 559 560 561 |
# File 'lib/canis/core/widgets/table.rb', line 555 def 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_width ⇒ Object
find the width of the longest item in the current columns and expand the width to that.
575 576 577 578 579 |
# File 'lib/canis/core/widgets/table.rb', line 575 def x = _convert_curpos_to_column w = calculate_column_width x w end |
#expand_column_to_width(w = nil) ⇒ Object
562 563 564 565 566 567 568 569 570 571 572 |
# File 'lib/canis/core/widgets/table.rb', line 562 def 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
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 |
# File 'lib/canis/core/widgets/table.rb', line 740 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_event ⇒ Object
called when ENTER is pressed. Takes into account if user is on header_row
947 948 949 950 951 952 953 954 955 956 957 958 959 960 |
# File 'lib/canis/core/widgets/table.rb', line 947 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
550 551 552 553 554 |
# File 'lib/canis/core/widgets/table.rb', line 550 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.
449 450 451 452 453 454 455 456 |
# File 'lib/canis/core/widgets/table.rb', line 449 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
830 831 832 833 |
# File 'lib/canis/core/widgets/table.rb', line 830 def get_value_at row,col actrow = row + @_header_adjustment @list[actrow, col] end |
#header_adjustment ⇒ Object
598 599 600 |
# File 'lib/canis/core/widgets/table.rb', line 598 def header_adjustment @_header_adjustment end |
#header_row? ⇒ Boolean
returns true if focus is on header_row
940 941 942 943 |
# File 'lib/canis/core/widgets/table.rb', line 940 def header_row? #@prow == 0 @prow == @current_index end |
#matching_indices ⇒ Object
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
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 |
# File 'lib/canis/core/widgets/table.rb', line 999 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.
659 660 661 662 663 664 665 666 667 668 669 |
# File 'lib/canis/core/widgets/table.rb', line 659 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
877 878 879 880 881 882 883 |
# File 'lib/canis/core/widgets/table.rb', line 877 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_column ⇒ Object
jump cursor to next column TODO : if cursor goes out of view, then pad should scroll right or left and down
525 526 527 528 529 530 531 532 533 534 |
# File 'lib/canis/core/widgets/table.rb', line 525 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
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 |
# File 'lib/canis/core/widgets/table.rb', line 971 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 |
#padrefresh ⇒ Object
refresh pad onto window overrides super due to header_adjustment and the header too
911 912 913 914 915 916 917 918 919 920 921 922 923 924 |
# File 'lib/canis/core/widgets/table.rb', line 911 def padrefresh top = @window.top left = @window.left sr = @startrow + top sc = @startcol + left # first do header always in first row 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_column ⇒ Object
jump cursor to previous column TODO : if cursor goes out of view, then pad should scroll right or left and down
537 538 539 540 541 542 543 544 545 546 |
# File 'lib/canis/core/widgets/table.rb', line 537 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_foot ⇒ Object
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.
1061 1062 1063 1064 1065 1066 1067 1068 |
# File 'lib/canis/core/widgets/table.rb', line 1061 def print_foot return unless @print_footer ha = @_header_adjustment # ha takes into account whether there are headers or not = "#{@current_index+1-ha} of #{@list.length-ha} " @graphic.printstring( @row + @height -1 , @col+2, , @color_pair || $datacolor, @footer_attrib) @repaint_footer_required = false end |
#remove_column(tc) ⇒ Object
TODO
890 891 892 893 |
# File 'lib/canis/core/widgets/table.rb', line 890 def remove_column tc raise "to figure out add_column" _invalidate_width_cache end |
#render_all ⇒ Object
calls the renderer for all rows of data giving them pad, lineno, and line data
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 |
# File 'lib/canis/core/widgets/table.rb', line 1047 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]) 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()
595 596 597 |
# File 'lib/canis/core/widgets/table.rb', line 595 def renderer r @renderer = r end |
#resultset(columns, data) ⇒ Object
set column array and data array in one shot Erases any existing content
717 718 719 720 721 |
# File 'lib/canis/core/widgets/table.rb', line 717 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.
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 |
# File 'lib/canis/core/widgets/table.rb', line 766 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_model ⇒ Object
set the default selection model as the operational one
440 441 442 443 |
# File 'lib/canis/core/widgets/table.rb', line 440 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
840 841 842 843 844 845 |
# File 'lib/canis/core/widgets/table.rb', line 840 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
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 |
# File 'lib/canis/core/widgets/table.rb', line 682 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
704 705 706 707 708 709 710 711 712 |
# File 'lib/canis/core/widgets/table.rb', line 704 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.
516 517 518 519 520 521 522 |
# File 'lib/canis/core/widgets/table.rb', line 516 def to_searchable index if @renderer @renderer.to_searchable(@list[index]) else @list[index].to_s end end |