diff --git a/lib/table_print/column.rb b/lib/table_print/column.rb index ed997ce..b69c21f 100644 --- a/lib/table_print/column.rb +++ b/lib/table_print/column.rb @@ -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 @@ -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 diff --git a/lib/table_print/formatter.rb b/lib/table_print/formatter.rb index 8c254f5..17a4f51 100644 --- a/lib/table_print/formatter.rb +++ b/lib/table_print/formatter.rb @@ -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 @@ -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) @@ -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 diff --git a/spec/column_spec.rb b/spec/column_spec.rb index 7b9fe91..f1856e9 100644 --- a/spec/column_spec.rb +++ b/spec/column_spec.rb @@ -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 diff --git a/spec/formatter_spec.rb b/spec/formatter_spec.rb index 07c464c..4ecc26c 100644 --- a/spec/formatter_spec.rb +++ b/spec/formatter_spec.rb @@ -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