Class: AsciiParadise::AsciiTable::Cell

Inherits:
Object
  • Object
show all
Defined in:
lib/ascii_paradise/asciitable/cell.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = nil)
  reset
  @value, options = options, {} unless Hash === options
  @value = options.fetch :value, value
  @alignment = options.fetch :alignment, nil
  @alignment = :center if @alignment == :middle
  @colspan = options.fetch :colspan, 1
  @width = options.fetch :width, @value.to_s.size
  @index = options.fetch :index
  @table = options.fetch :table
end

Instance Attribute Details

#colspanObject (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

#alignmentObject

#

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?

#

Returns:

  • (Boolean)


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

#linesObject

#

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

#resetObject

#

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?

#

Returns:

  • (Boolean)


43
44
45
# File 'lib/ascii_paradise/asciitable/cell.rb', line 43

def value?
  @value
end

#value_for_column_width_recalcObject

#

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

#widthObject

#

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