class Canis::Tabular
Constants
- GUESSCOLUMNS
Attributes
an array of column titles
boolean, does user want lines numbered
x is the + character used a field delim in separators y is the field delim used in data rows, default is pipe or bar
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
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
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
# File lib/canis/core/widgets/tabular.rb, line 176 def add_separator @list << :separator end
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
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
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
# 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
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
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
# 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
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
# 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