Skip to content

Commit

Permalink
Don't recolorize output with ANSI escapes [Fixes pry#751]
Browse files Browse the repository at this point in the history
  • Loading branch information
ConradIrwin authored and rking@sharpsaw.org committed Jan 17, 2013
1 parent 44abff7 commit fbabc4f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/pry.rb
Expand Up @@ -35,7 +35,12 @@ def self.output_with_default_format(output, value, options = {})
nonce = rand(0x100000000).to_s(16) # whatever

stringified.gsub!(/#</, "%<#{nonce}")
colorized = Helpers::BaseHelpers.colorize_code(stringified)
# Don't recolorize output with color (for cucumber, looksee, etc.) [Issue #751]
colorized = if stringified =~ /\e\[/
stringified
else
Helpers::BaseHelpers.colorize_code(stringified)
end

# avoid colour-leak from CodeRay and any of the users' previous output
colorized = colorized.sub(/(\n*)\z/, "\e[0m\\1") if Pry.color
Expand Down
27 changes: 27 additions & 0 deletions spec/pry_output_spec.rb
Expand Up @@ -45,6 +45,33 @@
end
end

describe "color" do
before do
Pry.color = true
end

after do
Pry.color = false
end

it "should colorize strings as though they were ruby" do
accumulator = StringIO.new
Pry.config.print.call(accumulator, [1])
accumulator.string.should == "[\e[1;34m1\e[0m]\e[0m\n"
end

it "should not colorize strings that already include color" do
f = Object.new
def f.inspect
"\e[1;31mFoo\e[0m"
end
accumulator = StringIO.new
Pry.config.print.call(accumulator, f)
# We add an extra \e[0m to prevent color leak
accumulator.string.should == "\e[1;31mFoo\e[0m\e[0m\n"
end
end

describe "output suppression" do
before do
@t = pry_tester
Expand Down

0 comments on commit fbabc4f

Please sign in to comment.