From 753a39340938da4b9d7b5a520c879242ca9b6661 Mon Sep 17 00:00:00 2001 From: Travis Thompson Date: Thu, 1 Oct 2015 16:36:54 -0700 Subject: [PATCH 1/4] Issue #69: Ignore shell escape --- lib/table_print/column.rb | 12 ++++++++++-- lib/table_print/formatter.rb | 19 ++++++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/table_print/column.rb b/lib/table_print/column.rb index ed997ce..9128239 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| strip_escape(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| strip_escape(s.to_s)}.collect(&:length).max ].compact.max || 0 end end @@ -59,5 +59,13 @@ def max_width def multibyte_count TablePrint::Config.multibyte end + + def strip_escape(string) + if string.class == String + string.gsub(/\e\[([0-9]{1,2};){0,2}[0-9]{1,2}m/,'') + else + string + end + end end end diff --git a/lib/table_print/formatter.rb b/lib/table_print/formatter.rb index 8c254f5..cf5ba27 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,15 @@ def length(str) str.length end end + + def escape_strip(string, return_stripped_stuff = false) + 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 From 19459131c8b07e47c3670c7dee78847d38a32ba3 Mon Sep 17 00:00:00 2001 From: Travis Thompson Date: Thu, 1 Oct 2015 16:42:33 -0700 Subject: [PATCH 2/4] Fix code to pass unit tests --- lib/table_print/column.rb | 7 ++----- lib/table_print/formatter.rb | 1 + 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/table_print/column.rb b/lib/table_print/column.rb index 9128239..88ba944 100644 --- a/lib/table_print/column.rb +++ b/lib/table_print/column.rb @@ -61,11 +61,8 @@ def multibyte_count end def strip_escape(string) - if string.class == String - string.gsub(/\e\[([0-9]{1,2};){0,2}[0-9]{1,2}m/,'') - else - string - end + 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 cf5ba27..17a4f51 100644 --- a/lib/table_print/formatter.rb +++ b/lib/table_print/formatter.rb @@ -51,6 +51,7 @@ def length(str) 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 From e2d4ac6fc497e6ddd180b6fc5c088bc7586999e8 Mon Sep 17 00:00:00 2001 From: Travis Thompson Date: Thu, 1 Oct 2015 17:04:32 -0700 Subject: [PATCH 3/4] Use consistant function naming --- lib/table_print/column.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/table_print/column.rb b/lib/table_print/column.rb index 88ba944..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{|s| strip_escape(s.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{|s| strip_escape(s.to_s)}.collect(&:length).max + Array(data).compact.collect{|s| escape_strip(s.to_s)}.collect(&:length).max ].compact.max || 0 end end @@ -60,7 +60,7 @@ def multibyte_count TablePrint::Config.multibyte end - def strip_escape(string) + 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 From ae9269671e72127282de88a9584196bed4060cc5 Mon Sep 17 00:00:00 2001 From: Travis Thompson Date: Thu, 1 Oct 2015 17:04:42 -0700 Subject: [PATCH 4/4] Add unit tests --- spec/column_spec.rb | 6 ++++++ spec/formatter_spec.rb | 14 ++++++++++++++ 2 files changed, 20 insertions(+) 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