lib/terminal-table/cell.rb in terminal-table-1.4.4 vs lib/terminal-table/cell.rb in terminal-table-1.4.5
- old
+ new
@@ -56,23 +56,21 @@
# Render the cell.
def render(line = 0)
left = " " * @table.style.padding_left
right = " " * @table.style.padding_right
- "#{left}#{lines[line]}#{right}".align(alignment, width + @table.cell_padding)
+ render_width = lines[line].to_s.size - escape(lines[line]).size + width
+ "#{left}#{lines[line]}#{right}".align(alignment, render_width + @table.cell_padding)
end
alias :to_s :render
##
# Returns the longest line in the cell and
# removes all ANSI escape sequences (e.g. color)
def value_for_column_width_recalc
- str = lines.sort_by { |s| s.size }.last.to_s
- str = str.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '')
- str = str.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '')
- str.gsub(/[\x03|\x1a]/, '')
+ lines.map{ |s| escape(s) }.max_by{ |s| s.size }
end
##
# Returns the width of this cell
@@ -81,8 +79,16 @@
inner_width = (1..@colspan).to_a.inject(0) do |w, counter|
w + @table.column_width(@index + counter - 1)
end
inner_width + padding
end
+
+ ##
+ # 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]/, '')
+ end
end
end
-end
\ No newline at end of file
+end