lib/terminal-table/cell.rb in terminal-table-1.4.5 vs lib/terminal-table/cell.rb in terminal-table-1.5.0
- old
+ new
@@ -1,16 +1,10 @@
module Terminal
class Table
class Cell
-
##
- # Cell width.
-
- attr_reader :width
-
- ##
# Cell value.
attr_reader :value
##
@@ -34,11 +28,11 @@
def alignment?
!@alignment.nil?
end
def alignment
- @alignment || :left
+ @alignment || @table.style.alignment || :left
end
def alignment=(val)
supported = %w(left center right)
if supported.include?(val.to_s)
@@ -46,10 +40,14 @@
else
raise "Aligment must be one of: #{supported.join(' ')}"
end
end
+ def align(val, position, length)
+ positions = { :left => :ljust, :right => :rjust, :center => :center }
+ val.public_send(positions[position], length)
+ end
def lines
@value.to_s.split(/\n/)
end
##
@@ -57,11 +55,11 @@
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
- "#{left}#{lines[line]}#{right}".align(alignment, render_width + @table.cell_padding)
+ align("#{left}#{lines[line]}#{right}", alignment, render_width + @table.cell_padding)
end
alias :to_s :render
##
# Returns the longest line in the cell and
@@ -85,10 +83,10 @@
##
# removes all ANSI escape sequences (e.g. color)
def escape(line)
line.to_s.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '').
gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '').
- gsub(/[\x03|\x1a]/, '')
+ gsub(/(\x03|\x1a)/, '')
end
end
end
end