Skip to content

Commit

Permalink
Reopen ansi escape sequences after wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
smarterclayton committed Apr 8, 2013
1 parent 9fd74e6 commit a202c7b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
22 changes: 14 additions & 8 deletions lib/rhc/core_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/rhc/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'] }
Expand Down

0 comments on commit a202c7b

Please sign in to comment.