Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Restore original behaviour of Table#align_column; Fix some tests

  • Loading branch information...
commit 5e9a7f12d3bc541ff1bfdcabeaf5ae01b6cb666a 1 parent 9e8940d
@rrrene rrrene authored
Showing with 21 additions and 8 deletions.
  1. +19 −7 lib/terminal-table/cell.rb
  2. +2 −1  lib/terminal-table/table.rb
View
26 lib/terminal-table/cell.rb
@@ -14,11 +14,6 @@ class Cell
attr_reader :value
##
- # Cell alignment.
-
- attr_accessor :alignment
-
- ##
# Column span.
attr_reader :colspan
@@ -29,13 +24,30 @@ class Cell
def initialize options = nil
@value, options = options, {} unless Hash === options
@value = options.fetch :value, value
- @alignment = options.fetch :alignment, :left
+ @alignment = options.fetch :alignment, nil
@colspan = options.fetch :colspan, 1
@width = options.fetch :width, @value.to_s.size
@index = options.fetch :index
@table = options.fetch :table
end
+ def alignment?
+ !@alignment.nil?
+ end
+
+ def alignment
+ @alignment || :left
+ end
+
+ def alignment=(val)
+ supported = %w(left center right)
+ if supported.include?(val.to_s)
+ @alignment = val
+ else
+ raise "Aligment must be one of: #{supported.join(' ')}"
+ end
+ end
+
def lines
@value.to_s.split(/\n/)
end
@@ -44,7 +56,7 @@ def lines
# Render the cell.
def render(line = 0)
- " #{lines[line]} ".align alignment, width + 2
+ " #{lines[line]} ".align(alignment, width + 2)
end
alias :to_s :render
View
3  lib/terminal-table/table.rb
@@ -35,7 +35,8 @@ def initialize options = {}, &block
def align_column n, alignment
r = rows
column(n).each_with_index do |col, i|
- r[i][n].alignment = alignment
+ cell = r[i][n]
+ cell.alignment = alignment unless cell.alignment?
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.