summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndré Luis Leal Cardoso Junior <andrehjr@gmail.com>2020-08-30 19:56:16 -0300
committerAndré Luis Leal Cardoso Junior <andrehjr@gmail.com>2020-10-05 21:57:25 -0300
commit60e84ee1d80919b0cc41268a878ffc9e78f903ac (patch)
treeccadb8dcb0edc067e07d1d7299066cd03d35037b
parent22c35975ece86707a5c867a79862fd0816f69bbb (diff)
downloadpry-60e84ee1d80919b0cc41268a878ffc9e78f903ac.tar.gz
Make #strip_color only remove colors.
The previous implementation was removing more than that. When Pry.color = false, it was messing up the output of a few commands.
-rw-r--r--lib/pry/helpers/text.rb2
-rw-r--r--spec/helpers/text_spec.rb18
2 files changed, 19 insertions, 1 deletions
diff --git a/lib/pry/helpers/text.rb b/lib/pry/helpers/text.rb
index b1fee03f..4ccedf83 100644
--- a/lib/pry/helpers/text.rb
+++ b/lib/pry/helpers/text.rb
@@ -44,7 +44,7 @@ class Pry
# @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.
diff --git a/spec/helpers/text_spec.rb b/spec/helpers/text_spec.rb
new file mode 100644
index 00000000..e45bc8ab
--- /dev/null
+++ b/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