Skip to content

Commit

Permalink
Apply arches#45
Browse files Browse the repository at this point in the history
  • Loading branch information
ccorn90 committed Jan 14, 2018
1 parent 50d1dfa commit 43619f2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 6 additions & 2 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(strip_escape(value.to_s))
truncate(value) + (padding < 0 ? '' : " " * padding)
end

Expand All @@ -34,11 +34,15 @@ def truncate(value)
return "" unless value

value = value.to_s
return value unless value.length > width
return value unless strip_escape(value).length > width

"#{value[0..width-4]}..."
end

def strip_escape(value)
value.gsub(%r{\e[^m]*m}, '')
end

def length(str)
if TablePrint::Config.multibyte
str.each_char.collect{|c| c.bytesize == 1 ? 1 : 2}.inject(0, &:+)
Expand Down
4 changes: 4 additions & 0 deletions spec/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
@f.format("1234567890123456").should == "1234567..."
end

it "truncate colorized values correctly" do
@f.format("\e[0;31;49masdf\e[0m").should == "\e[0;31;49masdf\e[0m "
end

it "uses an empty string in place of nils" do
@f.format(nil).should == " "
end
Expand Down

0 comments on commit 43619f2

Please sign in to comment.