class AsciiParadise::AsciiTable
Public Class Methods
disable_colours()
click to toggle source
enable_colours()
click to toggle source
new( i = {}, &block )
click to toggle source
#¶ ↑
initialize¶ ↑
Generates a ASCII table with the given options.
#¶ ↑
# File lib/ascii_paradise/asciitable/table.rb, line 14 def initialize( i = {}, &block ) reset self.style = i.fetch :style, {} self.headings = i.fetch :headings, [] self.rows = i.fetch :rows, [] self.title = i.fetch :title, nil yield_or_eval(&block) if block end
use_colours?()
click to toggle source
use_this_colour(i)
click to toggle source
Public Instance Methods
==(other)
click to toggle source
#¶ ↑
==¶ ↑
Check if other is equal to self. other is considered equal if it contains the same headings and rows.
#¶ ↑
# File lib/ascii_paradise/asciitable/table.rb, line 253 def ==(other) if other.respond_to? :render and other.respond_to? :rows self.headings == other.headings and self.rows == other.rows end end
add(i = :separator)
click to toggle source
add_row(array)
click to toggle source
#¶ ↑
add_row
(row tag, append tag)¶ ↑
Add a row. We add either a separator, or a new row.
You can also use the << operator for this method.
#¶ ↑
# File lib/ascii_paradise/asciitable/table.rb, line 89 def add_row(array) if array == :separator row = Separator.new(self) else row = Row.new(self, array) end @rows << row recalc_column_widths(row) end
add_separator()
click to toggle source
align_column(n, alignment)
click to toggle source
#¶ ↑
align_column
¶ ↑
Align column n to the given alignment of :center, :left, or :right.
#¶ ↑
# File lib/ascii_paradise/asciitable/table.rb, line 69 def align_column(n, alignment) case alignment when :middle alignment = :center end r = rows column(n).each_with_index { |col, i| cell = r[i][n] next unless cell cell.alignment = alignment unless cell.alignment? } end
cell_padding()
click to toggle source
cell_spacing()
click to toggle source
column(n, method = :value, array = rows)
click to toggle source
column_width(n)
click to toggle source
#¶ ↑
column_width
¶ ↑
Return length of column n.
#¶ ↑
# File lib/ascii_paradise/asciitable/table.rb, line 172 def column_width(n) width = @column_widths[n] || 0 width + additional_column_widths[n].to_i end
Also aliased as: length_of_column
column_with_headings(n, method = :value)
click to toggle source
columns()
click to toggle source
headings=(i)
click to toggle source
headings?()
click to toggle source
#¶ ↑
headings?¶ ↑
Reader method for @headings.
#¶ ↑
# File lib/ascii_paradise/asciitable/table.rb, line 40 def headings? @headings end
Also aliased as: headings
number_of_columns()
click to toggle source
render()
click to toggle source
#¶ ↑
render¶ ↑
Render the table.
#¶ ↑
# File lib/ascii_paradise/asciitable/table.rb, line 201 def render separator = Separator.new(self) buffer = [separator] unless @title.nil? buffer << Row.new(self, [title_cell_options]) buffer << separator end unless @headings.cells.empty? buffer << @headings buffer << separator end buffer += @rows buffer << separator buffer.map { |r| r.render }.join("\n") end
Also aliased as: to_s
reset()
click to toggle source
rows()
click to toggle source
rows=(i)
click to toggle source
style()
click to toggle source
style=(i)
click to toggle source
#¶ ↑
style= (style tag)¶ ↑
Set the style setter. We do this by calling apply() on the style object.
Specific example how to use this:
t.style = {:padding_left => 2, :width => 80}
#¶ ↑
# File lib/ascii_paradise/asciitable/table.rb, line 52 def style=(i) style.apply(i) # The method style() returns a @style object - see below. end
title=(i)
click to toggle source
Private Instance Methods
additional_column_widths()
click to toggle source
#¶ ↑
additional_column_widths
¶ ↑
#¶ ↑
# File lib/ascii_paradise/asciitable/table.rb, line 287 def additional_column_widths return [] if style.width.nil? spacing = style.width - columns_width if spacing < 0 raise "Table width exceeds wanted width of `#{style.width}` characters." else per_col = spacing / number_of_columns array = (1...number_of_columns).to_a.map { |i| per_col } other_cols = array.inject(0) { |s, i| s + i } array << spacing - other_cols array end end
columns_width()
click to toggle source
headings_with_rows()
click to toggle source
recalc_column_widths(row)
click to toggle source
#¶ ↑
recalc_column_widths
¶ ↑
#¶ ↑
# File lib/ascii_paradise/asciitable/table.rb, line 304 def recalc_column_widths(row) return if row.is_a? Separator i = 0 row.cells.each { |cell| colspan = cell.colspan cell_value = cell.value_for_column_width_recalc colspan.downto(1) { |j| cell_length = cell_value.to_s.length if colspan > 1 spacing_length = cell_spacing * (colspan - 1) length_in_columns = (cell_length - spacing_length) cell_length = (length_in_columns.to_f / colspan).ceil end if @column_widths[i].to_i < cell_length @column_widths[i] = cell_length end i += 1 } } end
result?()
click to toggle source
#¶ ↑
result?¶ ↑
#¶ ↑
# File lib/ascii_paradise/asciitable/table.rb, line 328 def result? @result end
Also aliased as: result
title?()
click to toggle source
Also aliased as: title