class Canis::Tabular

Constants

GUESSCOLUMNS

Attributes

columns[R]

an array of column titles

numbering[RW]

boolean, does user want lines numbered

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/canis/core/widgets/tabular.rb, line 54
def initialize cols=nil, *args, &block
  @chash = {}
  @cw = {}
  @calign = {}
  @separ = @columns = @numbering =  nil
  @y = '|'
  @x = '+'
  self.columns = cols if cols
  if !args.empty?
    #puts "ARGS after shift #{args} "
    if !args.empty?
      self.data = args
    end
  end
  yield_or_eval(&block) if block_given?
end

Public Instance Methods

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

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

# File lib/canis/core/widgets/tabular.rb, line 94
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/canis/core/widgets/tabular.rb, line 176
def add_separator
  @list << :separator
end
align_column(colindex, lrc) click to toggle source

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

# File lib/canis/core/widgets/tabular.rb, line 118
def align_column colindex, lrc
  raise ArgumentError, "wrong alignment value sent" if ![:right, :left, :center].include? lrc
  @calign[colindex] ||= lrc
  if @chash[colindex].nil?
    @chash[colindex] = ColumnInfo.new("", nil, lrc)
  else
    @chash[colindex].align = lrc
  end
  @chash
end
column_width(colindex, width) click to toggle source

set width of a given column @param [Number] column offset, starting 0 @param [Number] width

# File lib/canis/core/widgets/tabular.rb, line 105
def column_width colindex, width
  @cw[colindex] ||= width
  if @chash[colindex].nil?
    @chash[colindex] = ColumnInfo.new("", width) 
  else
    @chash[colindex].w = width
  end
  @chash
end
columns=(array) click to toggle source

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

# File lib/canis/core/widgets/tabular.rb, line 73
def columns=(array)
  $log.debug "tabular got columns #{array.count} #{array.inspect} " if $log
  @columns = array
  @columns.each_with_index { |c,i| 
    @chash[i] = ColumnInfo.new(c, c.to_s.length) 
    @cw[i] ||= c.to_s.length
    #@calign[i] ||= :left # 2011-09-27 prevent setting later on
  }
end
Also aliased as: headings=
convert_value_to_text(r, count) click to toggle source
# File lib/canis/core/widgets/tabular.rb, line 161
def convert_value_to_text r, count
  if r == :separator
    return separator
  end
  if @numbering
    r.insert 0, count+1
  end
  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/canis/core/widgets/tabular.rb, line 86
def data=(list)
  #puts "got data: #{list.size} " if !$log
  #puts list if !$log
  @list = list
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/canis/core/widgets/tabular.rb, line 132
def render
  buffer = []
  _guess_col_widths
  rows = @list.size.to_s.length
  @rows = rows
  _prepare_format
  
  str = ""
  if @numbering
    str = " "*(rows+1)+@y
  end
  str <<  @fmstr % @columns
  buffer << str
  #puts "-" * str.length
  buffer << separator
  if @list
    if @numbering
      @fmstr = "%#{rows}d "+ @y + @fmstr
    end
    #@list.each { |e| puts e.join(@y) }
    count = 0
    @list.each_with_index { |r,i|  
      value = convert_value_to_text r, count
      buffer << value
      count += 1
    }
  end
  buffer
end
separator() click to toggle source
# File lib/canis/core/widgets/tabular.rb, line 179
def separator
  return @separ if @separ
  str = ""
  if @numbering
    str = "-"*(@rows+1)+@x
  end
  @cw.each_pair { |k,v| str << "-" * (v+1) + @x }
  @separ = str.chop
end
to_s() click to toggle source

use this for printing out on terminal @example

puts t.to_s
# File lib/canis/core/widgets/tabular.rb, line 173
def to_s
  render().join "\n"
end
yield_or_eval() { |self| ... } click to toggle source
# File lib/canis/core/widgets/tabular.rb, line 31
def yield_or_eval &block
  return unless block
  if block.arity > 0
    yield self
  else
    self.instance_eval(&block)
  end
end