From 60e84ee1d80919b0cc41268a878ffc9e78f903ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Luis=20Leal=20Cardoso=20Junior?= Date: Sun, 30 Aug 2020 19:56:16 -0300 Subject: [PATCH 1/2] 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. --- lib/pry/helpers/text.rb | 2 +- spec/helpers/text_spec.rb | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 spec/helpers/text_spec.rb diff --git a/lib/pry/helpers/text.rb b/lib/pry/helpers/text.rb index b1fee03f1..4ccedf833 100644 --- a/lib/pry/helpers/text.rb +++ b/lib/pry/helpers/text.rb @@ -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. diff --git a/spec/helpers/text_spec.rb b/spec/helpers/text_spec.rb new file mode 100644 index 000000000..e45bc8abc --- /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 From 8e8f2d53d6972e3a96fd0c63b64b9e626d438b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Luis=20Leal=20Cardoso=20Junior?= Date: Sun, 30 Aug 2020 19:59:46 -0300 Subject: [PATCH 2/2] Fix RegexpError: premature end of char-class --- spec/pry_repl_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/pry_repl_spec.rb b/spec/pry_repl_spec.rb index 946b92e81..b7f5870a6 100644 --- a/spec/pry_repl_spec.rb +++ b/spec/pry_repl_spec.rb @@ -135,7 +135,7 @@ def output.tty? break #note the tab here end TAB - output("do\n break #note the tab here\nend\n\e[1B\e[0G=> nil") + output("do\n break #note the tab here\nend\n\e\\[1B\e\\[0G=> nil") end end end