class Terminal::Table::Style

A Style object holds all the formatting information for a Table object

To create a table with a certain style, use either the constructor option :style, the Table#style object or the Table#style= method

All these examples have the same effect:

# by constructor
@table = Table.new(:style => {:padding_left => 2, :width => 40})

# by object
@table.style.padding_left = 2
@table.style.width = 40

# by method
@table.style = {:padding_left => 2, :width => 40}

To set a default style for all tables created afterwards use Style.defaults=

Terminal::Table::Style.defaults = {:width => 80}

Attributes

alignment[RW]
all_separators[RW]
border[R]

Accessor for instance of Border

margin_left[RW]
padding_left[RW]
padding_right[RW]
width[RW]

Public Class Methods

defaults() click to toggle source
# File lib/terminal-table/style.rb, line 259
def defaults
  klass_defaults = @@defaults.dup
  # border is an object that needs to be duplicated on instantiation,
  # otherwise everything will be referencing the same object-id.
  klass_defaults[:border] = klass_defaults[:border].dup
  klass_defaults
end
defaults=(options) click to toggle source
# File lib/terminal-table/style.rb, line 267
def defaults= options
  @@defaults = defaults.merge(options)
end
new(options = {}) click to toggle source
# File lib/terminal-table/style.rb, line 248
def initialize options = {}
  apply self.class.defaults.merge(options)
end

Public Instance Methods

apply(options) click to toggle source
# File lib/terminal-table/style.rb, line 252
def apply options
  options.each do |m, v|
    __send__ "#{m}=", v
  end
end
border=(val) click to toggle source
# File lib/terminal-table/style.rb, line 211
def border=(val)
  if val.is_a? Symbol
    # convert symbol name like :foo_bar to get class FooBarBorder
    klass_str = val.to_s.split('_').collect(&:capitalize).join + "Border"
    begin
      klass = Terminal::Table::const_get(klass_str)
      @border = klass.new
    rescue NameError
      raise "Cannot lookup class Terminal::Table::#{klass_str} from symbol #{val.inspect}"
    end
  else
    @border = val
  end
end
border_bottom() click to toggle source
# File lib/terminal-table/style.rb, line 232
def border_bottom ; @border.bottom ; end
border_bottom=(val) click to toggle source
# File lib/terminal-table/style.rb, line 227
def border_bottom=(val) ; @border.bottom = val ; end
border_i=(val) click to toggle source
# File lib/terminal-table/style.rb, line 205
def border_i=(val) ; @border[:i] = val ; end
border_left() click to toggle source
# File lib/terminal-table/style.rb, line 233
def border_left ; @border.left ; end
border_left=(val) click to toggle source
# File lib/terminal-table/style.rb, line 228
def border_left=(val) ; @border.left = val ; end
border_right() click to toggle source
# File lib/terminal-table/style.rb, line 234
def border_right ; @border.right ; end
border_right=(val) click to toggle source
# File lib/terminal-table/style.rb, line 229
def border_right=(val) ; @border.right = val ; end
border_top() click to toggle source
# File lib/terminal-table/style.rb, line 231
def border_top ; @border.top ; end
border_top=(val) click to toggle source
# File lib/terminal-table/style.rb, line 226
def border_top=(val) ; @border.top = val ; end
border_x=(val) click to toggle source

settors/gettor for legacy ascii borders

# File lib/terminal-table/style.rb, line 203
def border_x=(val) ; @border[:x] = val ; end
border_y() click to toggle source
# File lib/terminal-table/style.rb, line 206
def border_y ; @border[:y] ; end
border_y=(val) click to toggle source
# File lib/terminal-table/style.rb, line 204
def border_y=(val) ; @border[:y] = val ; end
border_y_width() click to toggle source
# File lib/terminal-table/style.rb, line 207
def border_y_width ; Util::ansi_escape(@border[:y]).length ; end
on_change(attr) { |to_sym, value| ... } click to toggle source
# File lib/terminal-table/style.rb, line 273
def on_change attr
  method_name = :"#{attr}="
  old_method = method method_name
  define_singleton_method(method_name) do |value|
    old_method.call value
    yield attr.to_sym, value
  end
end