Class: Terminal::Table::Style

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/terminal-table/style.rb

Overview

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}

Constant Summary collapse

@@defaults =
{
  :border => AsciiBorder.new,
  :padding_left => 1, :padding_right => 1,
  :margin_left => '',
  :width => nil, :alignment => nil,
  :all_separators => false,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Style

Returns a new instance of Style.



248
249
250
# File 'lib/terminal-table/style.rb', line 248

def initialize options = {}
  apply self.class.defaults.merge(options)
end

Instance Attribute Details

#alignmentObject

Returns the value of attribute alignment.



243
244
245
# File 'lib/terminal-table/style.rb', line 243

def alignment
  @alignment
end

#all_separatorsObject

Returns the value of attribute all_separators.



245
246
247
# File 'lib/terminal-table/style.rb', line 245

def all_separators
  @all_separators
end

#borderObject

Accessor for instance of Border



210
211
212
# File 'lib/terminal-table/style.rb', line 210

def border
  @border
end

#margin_leftObject

Returns the value of attribute margin_left.



240
241
242
# File 'lib/terminal-table/style.rb', line 240

def margin_left
  @margin_left
end

#padding_leftObject

Returns the value of attribute padding_left.



237
238
239
# File 'lib/terminal-table/style.rb', line 237

def padding_left
  @padding_left
end

#padding_rightObject

Returns the value of attribute padding_right.



238
239
240
# File 'lib/terminal-table/style.rb', line 238

def padding_right
  @padding_right
end

#widthObject

Returns the value of attribute width.



242
243
244
# File 'lib/terminal-table/style.rb', line 242

def width
  @width
end

Class Method Details

.defaultsObject



259
260
261
262
263
264
265
# 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) ⇒ Object



267
268
269
# File 'lib/terminal-table/style.rb', line 267

def defaults= options
  @@defaults = defaults.merge(options)
end

Instance Method Details

#apply(options) ⇒ Object



252
253
254
255
256
# File 'lib/terminal-table/style.rb', line 252

def apply options
  options.each do |m, v|
    __send__ "#{m}=", v
  end
end

#border_bottomObject



232
# File 'lib/terminal-table/style.rb', line 232

def border_bottom ; @border.bottom ; end

#border_bottom=(val) ⇒ Object



227
# File 'lib/terminal-table/style.rb', line 227

def border_bottom=(val) ; @border.bottom = val ; end

#border_i=(val) ⇒ Object



205
# File 'lib/terminal-table/style.rb', line 205

def border_i=(val) ; @border[:i] = val ; end

#border_leftObject



233
# File 'lib/terminal-table/style.rb', line 233

def border_left ; @border.left ; end

#border_left=(val) ⇒ Object



228
# File 'lib/terminal-table/style.rb', line 228

def border_left=(val) ; @border.left = val ; end

#border_rightObject



234
# File 'lib/terminal-table/style.rb', line 234

def border_right ; @border.right ; end

#border_right=(val) ⇒ Object



229
# File 'lib/terminal-table/style.rb', line 229

def border_right=(val) ; @border.right = val ; end

#border_topObject



231
# File 'lib/terminal-table/style.rb', line 231

def border_top ; @border.top ; end

#border_top=(val) ⇒ Object



226
# File 'lib/terminal-table/style.rb', line 226

def border_top=(val) ; @border.top = val ; end

#border_x=(val) ⇒ Object

settors/gettor for legacy ascii borders



203
# File 'lib/terminal-table/style.rb', line 203

def border_x=(val) ; @border[:x] = val ; end

#border_yObject



206
# File 'lib/terminal-table/style.rb', line 206

def border_y ; @border[:y] ; end

#border_y=(val) ⇒ Object



204
# File 'lib/terminal-table/style.rb', line 204

def border_y=(val) ; @border[:y] = val ; end

#border_y_widthObject



207
# File 'lib/terminal-table/style.rb', line 207

def border_y_width ; Util::ansi_escape(@border[:y]).length ; end

#on_change(attr) ⇒ Object



273
274
275
276
277
278
279
280
# 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