Class: AsciiParadise::AsciiTable::Cell
- Inherits:
-
Object
- Object
- AsciiParadise::AsciiTable::Cell
- Defined in:
- lib/ascii_paradise/asciitable/cell.rb
Instance Attribute Summary collapse
-
#colspan ⇒ Object
readonly
attr_reader :width # Cell width.
Instance Method Summary collapse
-
#alignment ⇒ Object
# === alignment ========================================================================= #.
-
#alignment=(i) ⇒ Object
# === alignment= ========================================================================= #.
-
#alignment? ⇒ Boolean
# === alignment? ========================================================================= #.
-
#escape(i) ⇒ Object
# === escape.
-
#initialize(options = nil) ⇒ Cell
constructor
# === initialize.
-
#lines ⇒ Object
# === lines ========================================================================= #.
-
#render(line = 0) ⇒ Object
(also: #to_s)
# === render.
-
#reset ⇒ Object
# === reset ========================================================================= #.
-
#value? ⇒ Boolean
(also: #value)
# === value? ========================================================================= #.
-
#value_for_column_width_recalc ⇒ Object
# === value_for_column_width_recalc.
-
#width ⇒ Object
# === width.
Constructor Details
#initialize(options = nil) ⇒ Cell
#
initialize
Initialize with options.
#
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/ascii_paradise/asciitable/cell.rb', line 21 def initialize( = nil) reset @value, = , {} unless Hash === @value = .fetch :value, value @alignment = .fetch :alignment, nil @alignment = :center if @alignment == :middle @colspan = .fetch :colspan, 1 @width = .fetch :width, @value.to_s.size @index = .fetch :index @table = .fetch :table end |
Instance Attribute Details
#colspan ⇒ Object (readonly)
attr_reader :width # Cell width.
14 15 16 |
# File 'lib/ascii_paradise/asciitable/cell.rb', line 14 def colspan @colspan end |
Instance Method Details
#alignment ⇒ Object
#
alignment
#
114 115 116 |
# File 'lib/ascii_paradise/asciitable/cell.rb', line 114 def alignment @alignment || :left end |
#alignment=(i) ⇒ Object
#
alignment=
#
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ascii_paradise/asciitable/cell.rb', line 50 def alignment=(i) supported_alignments = %w( left center right ) case i when :middle i = :center end if supported_alignments.include?(i.to_s) @alignment = i else raise "Aligment must be one of: #{supported_alignments.join(' ')}" end end |
#alignment? ⇒ Boolean
#
alignment?
#
107 108 109 |
# File 'lib/ascii_paradise/asciitable/cell.rb', line 107 def alignment? ! @alignment.nil? end |
#escape(i) ⇒ Object
#
escape
This method removes all ANSI escape sequences (e.g. color).
#
98 99 100 101 102 |
# File 'lib/ascii_paradise/asciitable/cell.rb', line 98 def escape(i) i.to_s.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, ''). gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, ''). gsub(/[\x03|\x1a]/, '') end |
#lines ⇒ Object
#
lines
#
66 67 68 |
# File 'lib/ascii_paradise/asciitable/cell.rb', line 66 def lines @value.to_s.split(/\n/) end |
#render(line = 0) ⇒ Object Also known as: to_s
#
render
Render the cell.
alignment can be :left or :right or :center.
#
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/ascii_paradise/asciitable/cell.rb', line 125 def render(line = 0) left = ' ' * @table.style.padding_left right = ' ' * @table.style.padding_right render_width = lines[line].to_s.size - escape(lines[line]).size + width line = "#{left}#{lines[line]}#{right}" horizontal_length = render_width + @table.cell_padding case alignment when :left line.ljust(horizontal_length) when :right line.rjust(horizontal_length) when :center line.center(horizontal_length) end end |
#reset ⇒ Object
#
reset
#
36 37 38 |
# File 'lib/ascii_paradise/asciitable/cell.rb', line 36 def reset @value = 0 end |
#value? ⇒ Boolean Also known as: value
#
value?
#
43 44 45 |
# File 'lib/ascii_paradise/asciitable/cell.rb', line 43 def value? @value end |
#value_for_column_width_recalc ⇒ Object
#
value_for_column_width_recalc
Returns the longest line in the cell and removes all ANSI escape sequences (e.g. color).
#
76 77 78 |
# File 'lib/ascii_paradise/asciitable/cell.rb', line 76 def value_for_column_width_recalc lines.map{ |s| escape(s) }.max_by{ |s| s.size } end |
#width ⇒ Object
#
width
Returns the width of this cell
#
85 86 87 88 89 90 91 |
# File 'lib/ascii_paradise/asciitable/cell.rb', line 85 def width padding = (colspan - 1) * @table.cell_spacing inner_width = (1..@colspan).to_a.inject(0) { |w, counter| w + @table.column_width(@index + counter - 1) } return inner_width + padding end |