Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/table_print/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ def data_width
if multibyte_count
[
name.each_char.collect{|c| c.bytesize == 1 ? 1 : 2}.inject(0, &:+),
Array(data).compact.collect(&:to_s).collect{|m| m.each_char.collect{|n| n.bytesize == 1 ? 1 : 2}.inject(0, &:+)}.max
Array(data).compact.collect{|s| escape_strip(s.to_s)}.collect{|m| m.each_char.collect{|n| n.bytesize == 1 ? 1 : 2}.inject(0, &:+)}.max
].compact.max || 0
else
[
name.length,
Array(data).compact.collect(&:to_s).collect(&:length).max
Array(data).compact.collect{|s| escape_strip(s.to_s)}.collect(&:length).max
].compact.max || 0
end
end
Expand All @@ -59,5 +59,10 @@ def max_width
def multibyte_count
TablePrint::Config.multibyte
end

def escape_strip(string)
return string unless string.class == String
string.gsub(/\e\[([0-9]{1,2};){0,2}[0-9]{1,2}m/,'')
end
end
end
20 changes: 17 additions & 3 deletions lib/table_print/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def initialize(width)
end

def format(value)
padding = width - length(value.to_s)
padding = width - length(escape_strip(value).to_s)
truncate(value) + (padding < 0 ? '' : " " * padding)
end

Expand All @@ -34,9 +34,12 @@ def truncate(value)
return "" unless value

value = value.to_s
return value unless value.length > width

"#{value[0..width-4]}..."
value_stripped, stripped_stuff = escape_strip(value, true)

return value unless value_stripped.length > width

"#{value[0..(width + stripped_stuff.length)-4]}..."
end

def length(str)
Expand All @@ -46,5 +49,16 @@ def length(str)
str.length
end
end

def escape_strip(string, return_stripped_stuff = false)
return string unless string.class == String
stripped_stuff = ''
string_stripped = string.gsub(/\e\[([0-9]{1,2};){0,2}[0-9]{1,2}m/) do |s|
stripped_stuff << s
s = ''
end
return string_stripped, stripped_stuff if return_stripped_stuff == true
return string_stripped
end
end
end
6 changes: 6 additions & 0 deletions spec/column_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,10 @@
end
end
end

describe "#escape_strip" do
it "should strip shell escape characters" do
c.send(:escape_strip, "\e[0;32;49mGREEN\e[0m").should == "GREEN"
end
end
end
14 changes: 14 additions & 0 deletions spec/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,19 @@
it "turns objects into strings before trying to format them" do
@f.format(123).should == "123 "
end

it "ignores shell escape characters" do
@f.format("\e[0;32;49mGREEN\e[0m").should == "\e[0;32;49mGREEN\e[0m "
end
end

describe "#escape_strip" do
it "should strip shell escape characters" do
@f.send(:escape_strip, "\e[0;32;49mGREEN\e[0m").should == "GREEN"
end

it "return stripped characters if requested" do
@f.send(:escape_strip, "\e[0;32;49mGREEN\e[0m", true).should == ["GREEN", "\e[0;32;49m\e[0m"]
end
end
end