diff --git a/lib/rhc/core_ext.rb b/lib/rhc/core_ext.rb index 1b4a82ef4..6ec70d9fb 100644 --- a/lib/rhc/core_ext.rb +++ b/lib/rhc/core_ext.rb @@ -85,21 +85,27 @@ def textwrap_ansi(limit, breakword=true) (?:\s+|$)? ) /x + escapes = [] split("\n",-1).inject([]) do |a, line| if line.length < limit a << line else line.scan(re) do |segment, other| - # short escape sequence matches have whitespace from regex - a << segment.rstrip - # find any escape sequences after the last 0m reset, in order - escapes = segment.scan(ANSI_ESCAPE_SEQUENCE).map{ |e| e.first }.reverse.take_while{ |e| e != '0m' }.uniq.reverse - if escapes.present? - a[-1] << "\e[0m" - # TODO: Apply the unclosed sequences to the beginning of the - # next string + if escapes.present? + a << escapes.map{ |s| "\e[#{s}"}.join + a[-1] << segment.rstrip + else + a << segment.rstrip end + + segment.scan(ANSI_ESCAPE_SEQUENCE).map{ |e| e.first }.each do |e| + case e + when '0m' then escapes.clear + else escapes << e + end + end + a[-1] << "\e[0m" if escapes.present? end end a diff --git a/spec/rhc/helpers_spec.rb b/spec/rhc/helpers_spec.rb index 39f9521d9..f91dce2e8 100644 --- a/spec/rhc/helpers_spec.rb +++ b/spec/rhc/helpers_spec.rb @@ -332,10 +332,10 @@ def options it{ "\e[31;1m".textwrap_ansi(1).should == ["\e[31;1m\e[0m"] } it{ "\e[1ma".textwrap_ansi(1).should == ["\e[1ma\e[0m"] } it{ "a\e[12m".textwrap_ansi(1).should == ["a\e[12m\e[0m"] } - it{ "a\e[12m\e[34mb".textwrap_ansi(1).should == ["a\e[12m\e[34m\e[0m","b"] } + it{ "a\e[12m\e[34mb".textwrap_ansi(1).should == ["a\e[12m\e[34m\e[0m","\e[12m\e[34mb\e[0m"] } it{ "\e[12;34ma".textwrap_ansi(1).should == ["\e[12;34ma\e[0m"] } it{ "\e[1m\e[1m".textwrap_ansi(1).should == ["\e[1m\e[1m\e[0m"] } - it{ "\e[1m \e[1m".textwrap_ansi(1).should == ["\e[1m\e[0m", "\e[1m\e[0m"] } + it{ "\e[1m \e[1m".textwrap_ansi(1).should == ["\e[1m\e[0m", "\e[1m\e[1m\e[0m"] } it{ "ab".textwrap_ansi(1,false).should == ['ab'] } it{ " abc".textwrap_ansi(3,false).should == [' abc'] }