class AsciiParadise::Table

Public Class Methods

disable_colours() click to toggle source
#

AsciiParadise::AsciiTable.disable_colours

#
# File lib/ascii_paradise/asciitable/toplevel_methods.rb, line 28
def self.disable_colours
  @use_colours = false
end
enable_colours() click to toggle source
#

AsciiParadise::AsciiTable.enable_colours

#
# File lib/ascii_paradise/asciitable/toplevel_methods.rb, line 35
def self.enable_colours
  @use_colours = true
end
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
#

AsciiParadise::AsciiTable.use_colours?

#
# File lib/ascii_paradise/asciitable/toplevel_methods.rb, line 42
def self.use_colours?
  @use_colours
end
use_this_colour(i) click to toggle source
#

AsciiParadise::AsciiTable.use_this_colour

#
# File lib/ascii_paradise/asciitable/toplevel_methods.rb, line 58
def self.use_this_colour(i)
  @use_this_colour = i
end
use_which_colours?() click to toggle source
#

AsciiParadise::AsciiTable.use_which_colours?

Currently hardcoded. Determines which colour to use.

#
# File lib/ascii_paradise/asciitable/toplevel_methods.rb, line 51
def self.use_which_colours?
  @use_this_colour
end

Public Instance Methods

<<(array)
Alias for: add_row
==(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

#
# File lib/ascii_paradise/asciitable/table.rb, line 103
def add(i = :separator)
  case i
  when :separator
    add_separator
  else # Else assume a row.
    add_row(i)
  end
end
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
Also aliased as: <<, append
add_separator() click to toggle source
#

add_separator

Add a separator. A separator has —- and +.

#
# File lib/ascii_paradise/asciitable/table.rb, line 117
def add_separator
  add_row(:separator)
end
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
append(array)
Alias for: add_row
cell_padding() click to toggle source
#

cell_padding

Return the total padding used here.

#
# File lib/ascii_paradise/asciitable/table.rb, line 133
def cell_padding
  style.padding_left + style.padding_right
end
cell_spacing() click to toggle source
#

cell_spacing

#
# File lib/ascii_paradise/asciitable/table.rb, line 124
def cell_spacing
  cell_padding + style.border_y.length
end
column(n, method = :value, array = rows) click to toggle source
#

column

Return column n.

#
# File lib/ascii_paradise/asciitable/table.rb, line 142
def column(n, method = :value, array = rows)
  array.map { |row| 
   cell = row[n]
   cell && method ? cell.__send__(method) : cell
  }.compact 
end
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
#

column_with_headings

Return n column including headings.

#
# File lib/ascii_paradise/asciitable/table.rb, line 154
def column_with_headings(n, method = :value)
  column(n, method, headings_with_rows)
end
columns() click to toggle source
#

columns

Return columns.

#
# File lib/ascii_paradise/asciitable/table.rb, line 163
def columns
  (0...number_of_columns).map { |n| column n } 
end
display(&block)
Alias for: yield_or_eval
header=(i)
Alias for: title=
headings()
Alias for: headings?
headings=(i) click to toggle source
#

headings=

Set the headings

#
# File lib/ascii_paradise/asciitable/table.rb, line 191
def headings=(i)
  @headings = Row.new(self, i)
  recalc_column_widths @headings
end
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
length_of_column(n)
Alias for: column_width
number_of_columns() click to toggle source
#

number_of_columns

Return total number of columns available.

#
# File lib/ascii_paradise/asciitable/table.rb, line 182
def number_of_columns
  headings_with_rows.map { |r| r.cells.size }.max
end
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
#

reset

#
# File lib/ascii_paradise/asciitable/table.rb, line 28
def reset
  # === @column_widths
  @column_widths = []
  # === @result
  @result = nil
end
rows() click to toggle source
#

rows

Return rows without separator rows.

#
# File lib/ascii_paradise/asciitable/table.rb, line 222
def rows
  @rows.reject { |row| row.is_a? Separator }
end
rows=(i) click to toggle source
#

rows

Input to this should be an array.

#
# File lib/ascii_paradise/asciitable/table.rb, line 231
def rows=(i)
  @rows = []
  i.each { |entry| self << entry }
end
run(&block)
Alias for: yield_or_eval
set_title(i)
Alias for: title=
style() click to toggle source
#

style

#
# File lib/ascii_paradise/asciitable/table.rb, line 59
def style
  @style ||= Style.new # Can be found in the file style.rb.
end
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
#

title= (title tag)

Set the title here.

#
# File lib/ascii_paradise/asciitable/table.rb, line 241
def title=(i)
  @title = i
  recalc_column_widths( Row.new(self, [title_cell_options]) )
end
Also aliased as: header=, set_title
to_s()
Alias for: render
yield_or_eval() { |self| ... } click to toggle source
#

yield_or_eval (run tag)

#
# File lib/ascii_paradise/asciitable/table.rb, line 262
def yield_or_eval(&block)
  return unless block
  if block.arity > 0
    yield self
  else
    @result = self.instance_eval(&block)
  end
  return @result
end
Also aliased as: display, run

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
#

columns_width

#
# File lib/ascii_paradise/asciitable/table.rb, line 278
def columns_width
  @column_widths.inject(0) { |s, i|
    s + i + cell_spacing
  } + style.border_y.length
end
headings_with_rows() click to toggle source
#

headings_with_rows

Return headings combined with rows.

#
# File lib/ascii_paradise/asciitable/table.rb, line 344
def headings_with_rows
  [@headings] + rows
end
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()
Alias for: result?
result?() click to toggle source
#

result?

#
# File lib/ascii_paradise/asciitable/table.rb, line 328
def result?
  @result
end
Also aliased as: result
title()
Alias for: title?
title?() click to toggle source
#

title?

#
# File lib/ascii_paradise/asciitable/table.rb, line 335
def title?
  @title
end
Also aliased as: title
title_cell_options() click to toggle source
#

title_cell_options

#
# File lib/ascii_paradise/asciitable/table.rb, line 351
def title_cell_options
  {
    :value     => @title,
    :alignment => :center,
    :colspan   => number_of_columns
  }
end