Skip to content

Commit

Permalink
Make #strip_color only remove colors.
Browse files Browse the repository at this point in the history
The previous implementation was removing more than that. When Pry.color = false, it was messing up the output of a few commands.
  • Loading branch information
andrehjr committed Oct 6, 2020
1 parent 22c3597 commit 60e84ee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/pry/helpers/text.rb
Expand Up @@ -44,7 +44,7 @@ module Text
# @param [String, #to_s] text
# @return [String] _text_ stripped of any color codes.
def strip_color(text)
text.to_s.gsub(/(\001)?\e\[.*?(\d)+m(\002)?/, '')
text.to_s.gsub(/(\001)?(\e\[(\d[;\d]?)*m)(\002)?/, '')
end

# Returns _text_ as bold text for use on a terminal.
Expand Down
18 changes: 18 additions & 0 deletions spec/helpers/text_spec.rb
@@ -0,0 +1,18 @@
# frozen_string_literal: true

describe Pry::Helpers::Text do
describe "#strip_color" do
[
["\e[1A\e[0G[2] pry(main)> puts \e[31m\e[1;31m'\e[0m\e[31m"\
"hello\e[1;31m'\e[0m\e[31m\e[0m\e[1B\e[0G",
"\e[1A\e[0G[2] pry(main)> puts 'hello'\e[1B\e[0G"],
["\e[31m\e[1;31m'\e[0m\e[31mhello\e[1;31m'\e[0m\e[31m\e[0m\e[1B\e[0G",
"'hello'\e[1B\e[0G"],
%w[string string]
].each do |(text, text_without_color)|
it "removes color code from text #{text.inspect}" do
expect(subject.strip_color(text)).to eql(text_without_color)
end
end
end
end

0 comments on commit 60e84ee

Please sign in to comment.