Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Remove magic numbers from width calculations

  • Loading branch information...
commit 9f0da8c6edd8969125bf01f42ef69c8f46d8f73a 1 parent 83a1f90
@rrrene rrrene authored
Showing with 24 additions and 6 deletions.
  1. +4 −2 lib/terminal-table/cell.rb
  2. +20 −4 lib/terminal-table/table.rb
View
6 lib/terminal-table/cell.rb
@@ -56,7 +56,9 @@ def lines
# Render the cell.
def render(line = 0)
- " #{lines[line]} ".align(alignment, width + 2)
+ left = " " * table.cell_padding_left
+ right = " " * table.cell_padding_right
+ "#{left}#{lines[line]}#{right}".align(alignment, width + table.cell_padding)
end
alias :to_s :render
@@ -75,7 +77,7 @@ def value_for_column_width_recalc
# Returns the width of this cell
def width
- padding = (colspan - 1) * (2 + @table.class::Y.length)
+ padding = (colspan - 1) * @table.cell_spacing
inner_width = (1..@colspan).to_a.inject(0) do |w, counter|
w + @table.column_width(@index + counter - 1)
end
View
24 lib/terminal-table/table.rb
@@ -55,7 +55,23 @@ def add_row array
def add_separator
self << :separator
end
-
+
+ def cell_spacing
+ cell_padding + Y.length
+ end
+
+ def cell_padding
+ cell_padding_left + cell_padding_right
+ end
+
+ def cell_padding_left
+ 1
+ end
+
+ def cell_padding_right
+ 1
+ end
+
##
# Return column _n_.
@@ -148,7 +164,7 @@ def rows= array
def separator
@separator ||= begin
I + (0...number_of_columns).to_a.map do |i|
- X * (column_width(i) + 2)
+ X * (column_width(i) + cell_padding)
end.join(I) + I
end
end
@@ -171,7 +187,7 @@ def == other
private
def columns_width
- @column_widths.inject(0) { |s, i| s + i + 2 + Y.length } + Y.length
+ @column_widths.inject(0) { |s, i| s + i + cell_spacing } + Y.length
end
def additional_column_widths
@@ -197,7 +213,7 @@ def recalc_column_widths row
colspan.downto(1) do |j|
cell_length = cell_value.to_s.length
if colspan > 1
- spacing_length = (2 + Y.length) * (colspan - 1)
+ spacing_length = cell_spacing * (colspan - 1)
length_in_columns = (cell_length - spacing_length)
cell_length = (length_in_columns.to_f / colspan).ceil
end
Please sign in to comment.
Something went wrong with that request. Please try again.