Class: Canis::Tabular

Inherits:
Object show all
Defined in:
lib/canis/core/widgets/tabular.rb

Defined Under Namespace

Classes: ColumnInfo

Constant Summary collapse

GUESSCOLUMNS =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cols = nil, *args) { ... } ⇒ Tabular

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

Yields:

  • self



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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

Instance Attribute Details

#columnsObject

an array of column titles



43
44
45
# File 'lib/canis/core/widgets/tabular.rb', line 43

def columns
  @columns
end

#numberingObject

boolean, does user want lines numbered



45
46
47
# File 'lib/canis/core/widgets/tabular.rb', line 45

def numbering
  @numbering
end

#xObject

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



48
49
50
# File 'lib/canis/core/widgets/tabular.rb', line 48

def x
  @x
end

#yObject

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



48
49
50
# File 'lib/canis/core/widgets/tabular.rb', line 48

def y
  @y
end

Instance Method Details

#add(array) ⇒ Object Also known as: <<, add_row

add a row of data

Parameters:

  • an (Array)

    array containing entries for each column



94
95
96
97
98
# 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

#add_separatorObject



176
177
178
# File 'lib/canis/core/widgets/tabular.rb', line 176

def add_separator
  @list << :separator
end

#align_column(colindex, lrc) ⇒ Object

set alignment of given column offset

Parameters:

  • column (Number)

    offset, starting 0

  • :left, (Symbol)

    :right

Raises:

  • (ArgumentError)


118
119
120
121
122
123
124
125
126
127
# 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) ⇒ Object

set width of a given column

Parameters:

  • column (Number)

    offset, starting 0

  • width (Number)


105
106
107
108
109
110
111
112
113
# 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

#convert_value_to_text(r, count) ⇒ Object



161
162
163
164
165
166
167
168
169
# 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) ⇒ Object

set data as an array of arrays

Parameters:

  • data (Array<Array>)

    as array of arrays



86
87
88
89
90
# 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

#renderArray<String>

Now returns an array with formatted data

Returns:

  • (Array<String>)

    array of formatted data



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# 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

#separatorObject



179
180
181
182
183
184
185
186
187
# 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_sObject

use this for printing out on terminal

Examples:

puts t.to_s


173
174
175
# File 'lib/canis/core/widgets/tabular.rb', line 173

def to_s
  render().join "\n"
end

#yield_or_eval(&block) ⇒ Object



31
32
33
34
35
36
37
38
# 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