class Umbra::Table

A table of columnar data. This uses Tabular as a table model and extends Multiline.

Attributes

header_attr[RW]

color pair and attribute for header row

header_color_pair[RW]

color pair and attribute for header row

rendered[RW]
tabular[RW]

tabular is the data model for Table. It may be passed in in the constructor, or else is created when columns and data are passed in.

Public Class Methods

new(config={}) click to toggle source

Create a Table object passing either a Tabular object or columns and list e.g. Table.new tabular: tabular

Table.new columns: cols, list: mylist
Calls superclass method Umbra::Multiline::new
# File lib/umbra/table.rb, line 58
def initialize config={}, &block
  if config.key? :tabular
    @tabular = config.delete(:tabular)
  else
    cols = config.delete(:columns)
    data = config.delete(:list)
    @tabular = Tabular.new cols
    if data
      @tabular.data = data
    end
  end
  @rendered = nil
  super

  bind_key(?w, "next column") { self.next_column }
  bind_key(?b, "prev column") { self.prev_column }
  bind_key(KEY_RETURN, :fire_action_event)
  ## NOTE: a tabular object should be existing at this point.
end

Public Instance Methods

_print_data(win, row, col, str, index, state, data_index) click to toggle source

Print the data. index is index into visual row, starting 0 for headings, and 1 for separator data_index is index into actual data object. Use this if checking actual data array

# File lib/umbra/table.rb, line 151
def _print_data(win, row, col, str, index, state, data_index)
  data_index = index - @data_offset  ## index into actual data object
  arr = color_of_data_row(index, state, data_index)

  win.printstring(row, col, str, arr[0], arr[1])
end
_print_headings(win, row, col, str, index, state) click to toggle source

Print the header row index [Integer] - should be 0 or 1 (1 for optional separator)

# File lib/umbra/table.rb, line 141
def _print_headings(win, row, col, str, index, state)
  arr = color_of_header_row(index, state)
  win.printstring(row, col, str, arr[0], arr[1])
end
color_of_column(ix, value, defaultcolor) click to toggle source
# File lib/umbra/table.rb, line 157
def color_of_column ix, value, defaultcolor
  raise "unused yet"
end
color_of_data_row(index, state, data_index) click to toggle source

Specify how the data rows are to be coloured. Override this to have customised row coloring. @return array of color_pair and attrib.

# File lib/umbra/table.rb, line 124
def color_of_data_row index, state, data_index
  color_of_row(index, state)         ## calling superclass here
end
color_of_header_row(index, state) click to toggle source

Specify how to print the header and separator. index can be 0 or 1 returns an array of color_pair and attribute

# File lib/umbra/table.rb, line 115
def color_of_header_row index, state
    arr =  [ @header_color_pair || CP_MAGENTA, @header_attr || REVERSE ] 
    return arr if index == 0
    [ arr[0], NORMAL ]
end
current_column_offset() click to toggle source

Convert current cursor position to a table column calculate column based on curpos since user may not have used w and b keys (:next_column) @return [Integer] column index base 0

# File lib/umbra/table.rb, line 226
def current_column_offset
  _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
current_id() click to toggle source

return rowid (assumed to be first column)

# File lib/umbra/table.rb, line 169
def current_id
  data = current_row_as_array()
  return nil unless data
  data.first
end
current_row_as_array() click to toggle source

How do I deal with separators and headers here - return nil

This returns all columns including hidden so rowid can be accessed
# File lib/umbra/table.rb, line 176
def current_row_as_array
  data_index = @current_index - @data_offset  ## index into actual data object
  return nil if data_index < 0                ## separator and heading
  data()[data_index]
end
current_row_as_hash() click to toggle source

returns the current row as a hash with column name as key.

# File lib/umbra/table.rb, line 183
def current_row_as_hash
  data = current_row_as_array
  return nil unless data
  columns = @tabular.columns
  hash = columns.zip(data).to_h
end
data() click to toggle source

returns the raw data as array of arrays in tabular

# File lib/umbra/table.rb, line 79
def data
  @tabular.list
end
data=(list) click to toggle source
# File lib/umbra/table.rb, line 84
def data=(list)
  @rendered = false
  @tabular.data = list
  @repaint_required = true
  self.focusable = true
  @pstart = @current_index = 0
  @pcol               = 0
  #$log.debug "  before table data= CHANGED "
  #fire_handler(:CHANGED, self)    ## added 2018-05-08 -
end
fire_action_event() click to toggle source

Handle case where ENTER/RETURN pressed on header row (so sorting can be done).

Calls superclass method Umbra::Multiline#fire_action_event
# File lib/umbra/table.rb, line 245
def fire_action_event
  if header_row?
    # TODO sorting here
    $log.debug "  PRESSED ENTER on header row, TODO sorting here"
  end
  super
end
header_row?() click to toggle source
# File lib/umbra/table.rb, line 240
def header_row?
  @current_index == 0 and @data_offset > 0
end
next_column() click to toggle source

Move cursor to next column

# File lib/umbra/table.rb, line 191
def next_column
  @coffsets = @tabular._calculate_column_offsets unless @coffsets
  #c = @column_pointer.next
  current_column = current_column_offset() +1
  if current_column > @tabular.column_count-1
    current_column = 0
  end
  cp = @coffsets[current_column] 
  @curpos = cp if cp
  $log.debug " next_column #{@coffsets} :::: #{cp}, curpos=#{@curpos} "
  set_col_offset @curpos
  #down() if c < @column_pointer.last_index
  #fire_column_event :ENTER_COLUMN
end
prev_column() click to toggle source

Move cursor to previous column

# File lib/umbra/table.rb, line 207
def prev_column
  @coffsets = @tabular._calculate_column_offsets unless @coffsets
  #c = @column_pointer.next
  current_column = current_column_offset() -1
  if current_column < 0 #
    current_column = @tabular.column_count-1
  end
  cp = @coffsets[current_column] 
  @curpos = cp if cp
  $log.debug " next_column #{@coffsets} :::: #{cp}, curpos=#{@curpos} "
  set_col_offset @curpos
  #down() if c < @column_pointer.last_index
  #fire_column_event :ENTER_COLUMN
end
print_row(win, row, col, str, index, state) click to toggle source

Print the row which could be header or data @param index [Integer] - index of list, starting with header and separator

render() click to toggle source

render the two-dimensional array of data as an array of Strings. Calculates data_offset which is the row offset from which data starts.

# File lib/umbra/table.rb, line 97
def render
  @data_offset = 0
  @data_offset +=1 if @tabular.use_separator
  @data_offset +=1 if @tabular.columns
  self.list = @tabular.render
end
repaint() click to toggle source

paint the table

Calls superclass method Umbra::Multiline#repaint
# File lib/umbra/table.rb, line 105
def repaint
  render if !@rendered
  super

  @rendered = true
end
row_count() click to toggle source
# File lib/umbra/table.rb, line 163
def row_count
  @tabular.list.size
end