class Umbra::Tabular

Constants

GUESSCOLUMNS

Attributes

columns[R]

an array of column titles

list[R]

data which is array of arrays: rows and columns

numbering[RW]

boolean, does user want lines numbered

use_separator[RW]
x[RW]

x is the + character used a field delim in separators y is the field delim used in data rows, default is pipe or bar

y[RW]

x is the + character used a field delim in separators y is the field delim used in data rows, default is pipe or bar

Public Class Methods

new(cols=nil, *args, &block) click to toggle source

takes first optional argument as array of column names second optional argument as array of data arrays @yield self

# File lib/umbra/tabular.rb, line 70
def initialize cols=nil, *args, &block
  @chash             = []                 # hash of column info, not used
  @_skip_columns     = {}                 # internal, which columns not to calc width of since user has specified
  @separ = @columns = @numbering =  nil
  @y = '|'
  @x = '+'
  @use_separator = false
  @_hidden_columns_flag = false
  self.columns = cols if cols
  if !args.empty?
    self.data = args
  end
  yield_or_eval(&block) if block_given?
end

Public Instance Methods

<<(array)
Alias for: add
_calculate_column_offsets() click to toggle source

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.

# File lib/umbra/tabular.rb, line 327
def _calculate_column_offsets
  total = 0
  coffsets = []
  ctr = 0
  ## ix will have gaps in between for hidden fields
  each_column { | c, ix|
    v = c.width
    coffsets[ctr] = total
    ctr += 1
    total += v + 2                         ## blank space plus separator
  }
  return coffsets
end
add(array) click to toggle source

add a row of data @param [Array] an array containing entries for each column

# File lib/umbra/tabular.rb, line 113
def add array
  #$log.debug "tabular got add  #{array.count} #{array.inspect} " if $log
  @list ||= []
  @list << array
end
Also aliased as: <<, add_row
add_row(array)
Alias for: add
add_separator() click to toggle source
# File lib/umbra/tabular.rb, line 305
def add_separator
  @list << :separator
end
column_align(colindex, lrc=:NONE) click to toggle source

set alignment of given column offset @param [Number] column offset, starting 0 @param [Symbol] :left, :right

# File lib/umbra/tabular.rb, line 160
def column_align colindex, lrc=:NONE
  if lrc == :NONE
    return get_column(colindex).align
    #return @calign[colindex]
  end
  raise ArgumentError, "wrong alignment value sent" if ![:right, :left, :center].include? lrc
  get_column(colindex).align = lrc
  self
end
column_count() click to toggle source

returns the count of visible columns based on column names. NOTE: what if no column names gives ???

# File lib/umbra/tabular.rb, line 187
def column_count
  visible_column_names().count
end
column_hidden(colindex, flag=:NONE) click to toggle source
# File lib/umbra/tabular.rb, line 146
def column_hidden colindex, flag=:NONE
  if flag == :NONE
    return get_column(colindex).hidden
    #return @chide[colindex]
  end
  @_hidden_columns_flag = true if flag
  #@chide[colindex] = flag
  get_column(colindex).hidden = flag
  self
end
column_width(colindex, width=:NONE) click to toggle source

set width of a given column, any data beyond this will be truncated at display time. @param [Number] column offset, starting 0 @param [Number] width

# File lib/umbra/tabular.rb, line 136
def column_width colindex, width=:NONE
  if width == :NONE
    #return @cw[colindex]
    return get_column(colindex).width
  end
  @_skip_columns[colindex] = true   ## don't calculate col width for this.
  get_column(colindex).width = width
  self
end
columns=(array) click to toggle source

set columns names .

NOTE that we are not clearing chash here. In case, someone changes table and columns.

@param [Array<String>] column names, preferably padded out to width for column

# File lib/umbra/tabular.rb, line 88
def columns=(array)
  #$log.debug "tabular got columns #{array.count} #{array.inspect} " if $log
  @columns = array
  @columns.each_with_index { |e,i| 
    #@chash[i] = ColumnInfo.new(c, c.to_s.length)
    c = get_column(i)
    c.name = e
    c.width = e.to_s.length
    #@chash[i] = c
    #@cw[i] ||= c.to_s.length
    #@calign[i] ||= :left # 2011-09-27 prevent setting later on
  }
end
Also aliased as: headings=
convert_heading_to_text(r, fmstr) click to toggle source
# File lib/umbra/tabular.rb, line 277
def convert_heading_to_text r, fmstr
  return fmstr % r;  
end
convert_value_to_text(r, fmstr, index) click to toggle source

render_row @param [Array] row as Array @param [String] format string @param [Integer] row offset in data

# File lib/umbra/tabular.rb, line 274
def convert_value_to_text r, fmstr, index
  return fmstr % r;  
end
data=(list) click to toggle source

set data as an array of arrays @param [Array<Array>] data as array of arrays

# File lib/umbra/tabular.rb, line 105
def data=(list)
  #puts "got data: #{list.size} " if !$log
  #puts list if !$log
  @list = list
end
delete_at(ix) click to toggle source
# File lib/umbra/tabular.rb, line 294
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() { |c,i| ... } click to toggle source

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

# File lib/umbra/tabular.rb, line 193
def each_column
  @chash.each_with_index { |c, i| 
    next if c.hidden
    yield c,i if block_given?
  }
end
get_column(index) click to toggle source

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. @return ColumnInfo object containing width align color bgcolor attrib hidden

# File lib/umbra/tabular.rb, line 125
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
headings=(array)
Alias for: columns=
render() click to toggle source

Now returns an array with formatted data @return [Array<String>] array of formatted data

# File lib/umbra/tabular.rb, line 220
def render
  raise "tabular:: list is nil " unless @list
  $log.debug "  render list:: #{@list.size} "
  #$log.debug "  render list:1: #{@list} "
  raise "tabular:: columns is nil " unless @columns
  buffer = []
  @separ = nil
  _guess_col_widths
  rows = @list.size.to_s.length
  #@rows = rows
  fmstr = _prepare_format
  $log.debug "tabular: fmstr:: #{fmstr}"
  $log.debug "tabular: cols: #{@columns}"
  #$log.debug "tabular: data: #{@list}"

  str = ""
  if @numbering
    str = " "*(rows+1)+@y
  end
  #str <<  fmstr % visible_column_names()
  str <<  convert_heading_to_text(visible_column_names(), fmstr)
  buffer << str
  #puts "-" * str.length
  buffer << separator if @use_separator
  if @list    ## XXX why wasn't numbering done in _prepare_format ???? FIXME
    if @numbering
      fmstr = "%#{rows}d "+ @y + fmstr
    end
    #@list.each { |e| puts e.join(@y) }
    count = 0
    @list.each_with_index { |r,i|  
      if r == :separator
        buffer << separator
        next
      end
      if @_hidden_columns_flag
        r = visible_columns(r)
      end
      if @numbering
        r.insert 0, count+1
      end
      #value = convert_value_to_text r, count
      value = convert_value_to_text r, fmstr, i
      buffer << value
      count += 1
    }
  end
  buffer
end
separator() click to toggle source

This refers to a separator line after the heading and not a field separator. Badly named !

# File lib/umbra/tabular.rb, line 311
def separator
  return @separ if @separ
  str = ""
  if @numbering
    str = "-"*(rows+1)+@x
  end
  each_column { | c, ix|
    v = c.width
    next if v == 0     ## hidden column
    str << "-" * (v+1) + @x 
  }
  @separ = str.chop
end
to_string() click to toggle source

use this for printing out on terminal NOTE: Do not name this to_s as it will print the entire content in many places in debug statements @example

puts t.to_s
# File lib/umbra/tabular.rb, line 284
def to_string
  render().join "\n"
end
value_at(x,y, value=:NONE) click to toggle source
# File lib/umbra/tabular.rb, line 288
def value_at x,y, value=:NONE
  if value == :NONE
    return @list[x, y]
  end
  @list[x, y] = value
end
visible_column_names() { |name, ix| ... } click to toggle source

return an array of visible columns names

# File lib/umbra/tabular.rb, line 171
def visible_column_names
  visible = []
  @chash.each_with_index do |c, ix|
    if !c.hidden
      if block_given?
        yield c.name, ix 
      else
        visible << c.name
      end
    end
  end
  return visible unless block_given?
end
visible_columns(row) { |e, ix| ... } click to toggle source

for the given row, return visible columns as an array @yield column and index

# File lib/umbra/tabular.rb, line 202
def visible_columns(row)
  visible = []
  row.each_with_index do |e, ix|
    hid = @chash[ix].hidden
    if !hid
      if block_given?
        yield e, ix
      else
        visible << e 
      end
    end
  end
  return visible if !block_given?
end
yield_or_eval() { |self| ... } click to toggle source
# File lib/umbra/tabular.rb, line 34
def yield_or_eval &block
  return unless block
  if block.arity > 0
    yield self
  else
    self.instance_eval(&block)
  end
end