summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md5
-rw-r--r--lib/pry/helpers/documentation_helpers.rb3
-rw-r--r--lib/pry/helpers/text.rb12
-rw-r--r--spec/commands/wtf_spec.rb38
-rw-r--r--spec/documentation_helper_spec.rb4
-rw-r--r--spec/helpers/text_spec.rb18
-rw-r--r--spec/pry_repl_spec.rb2
7 files changed, 55 insertions, 27 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f3d6e4c5..1abf18ac 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,10 @@
### master
+#### Bug fixes
+
+* Fixed bad coloring of some RDoc-style docs
+ (([#2182](https://github.com/pry/pry/pull/2182))
+
### [v0.14.0][v0.14.0] (February 8, 2021)
#### Features
diff --git a/lib/pry/helpers/documentation_helpers.rb b/lib/pry/helpers/documentation_helpers.rb
index 1ce6be2b..024e1477 100644
--- a/lib/pry/helpers/documentation_helpers.rb
+++ b/lib/pry/helpers/documentation_helpers.rb
@@ -17,12 +17,13 @@ class Pry
last_match_ruby = proc do
SyntaxHighlighter.highlight(Regexp.last_match(1))
end
+
comment.gsub(%r{<code>(?:\s*\n)?(.*?)\s*</code>}m, &last_match_ruby)
.gsub(%r{<em>(?:\s*\n)?(.*?)\s*</em>}m) { "\e[1m#{Regexp.last_match(1)}\e[0m" }
.gsub(%r{<i>(?:\s*\n)?(.*?)\s*</i>}m) { "\e[1m#{Regexp.last_match(1)}\e[0m" }
.gsub(%r{<tt>(?:\s*\n)?(.*?)\s*</tt>}m, &last_match_ruby)
.gsub(/\B\+(\w+?)\+\B/) { "\e[32m#{Regexp.last_match(1)}\e[0m" }
- .gsub(/((?:^[ \t]+.+(?:\n+|\Z))+)/, &last_match_ruby)
+ .gsub(/((?:^[ \t]+(?:(?!.+\e\[)).+(?:\n+|\Z))+)/, &last_match_ruby)
.gsub(/`(?:\s*\n)?([^\e]*?)\s*`/) { "`#{last_match_ruby.call}`" }
end
diff --git a/lib/pry/helpers/text.rb b/lib/pry/helpers/text.rb
index b1fee03f..e004c961 100644
--- a/lib/pry/helpers/text.rb
+++ b/lib/pry/helpers/text.rb
@@ -21,20 +21,20 @@ class Pry
COLORS.each_pair do |color, value|
define_method color do |text|
- "\033[0;#{30 + value}m#{text}\033[0m"
+ "\001\033[0;#{30 + value}m\002#{text}\001\033[0m\002"
end
define_method "bright_#{color}" do |text|
- "\033[1;#{30 + value}m#{text}\033[0m"
+ "\001\033[1;#{30 + value}m\002#{text}\001\033[0m\002"
end
COLORS.each_pair do |bg_color, bg_value|
define_method "#{color}_on_#{bg_color}" do |text|
- "\033[0;#{30 + value};#{40 + bg_value}m#{text}\033[0m"
+ "\001\033[0;#{30 + value};#{40 + bg_value}m\002#{text}\001\033[0m\002"
end
define_method "bright_#{color}_on_#{bg_color}" do |text|
- "\033[1;#{30 + value};#{40 + bg_value}m#{text}\033[0m"
+ "\001\033[1;#{30 + value};#{40 + bg_value}m\002#{text}\001\033[0m\002"
end
end
end
@@ -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.
@@ -52,7 +52,7 @@ class Pry
# @param [String, #to_s] text
# @return [String] _text_
def bold(text)
- "\e[1m#{text}\e[0m"
+ "\001\e[1m\002#{text}\001\e[0m\002"
end
# Returns `text` in the default foreground colour.
diff --git a/spec/commands/wtf_spec.rb b/spec/commands/wtf_spec.rb
index 52ad0251..c4af0cc5 100644
--- a/spec/commands/wtf_spec.rb
+++ b/spec/commands/wtf_spec.rb
@@ -38,7 +38,7 @@ RSpec.describe Pry::Command::Wtf do
it "prints only a part of the exception backtrace" do
subject.process
expect(subject.output.string).to eq(
- "\e[1mException:\e[0m RuntimeError: oops\n" \
+ "\001\e[1m\002Exception:\001\e[0m\002 RuntimeError: oops\n" \
"--\n" \
"0: /bin/pry:23:in `<main>'\n" \
"1: /bin/pry:23:in `<main>'\n" \
@@ -55,7 +55,7 @@ RSpec.describe Pry::Command::Wtf do
it "prints full exception backtrace" do
subject.process
expect(subject.output.string).to eq(
- "\e[1mException:\e[0m RuntimeError: oops\n" \
+ "\001\e[1m\002Exception:\001\e[0m\002 RuntimeError: oops\n" \
"--\n" \
"0: /bin/pry:23:in `<main>'\n" \
"1: /bin/pry:23:in `<main>'\n" \
@@ -73,7 +73,7 @@ RSpec.describe Pry::Command::Wtf do
it "prints more of backtrace" do
subject.process
expect(subject.output.string).to eq(
- "\e[1mException:\e[0m RuntimeError: oops\n" \
+ "\001\e[1m\002Exception:\001\e[0m\002 RuntimeError: oops\n" \
"--\n" \
"0: /bin/pry:23:in `<main>'\n" \
"1: /bin/pry:23:in `<main>'\n" \
@@ -116,14 +116,14 @@ RSpec.describe Pry::Command::Wtf do
it "prints parts of both original and nested exception backtrace" do
subject.process
expect(subject.output.string).to eq(
- "\e[1mException:\e[0m RuntimeError: outer\n" \
+ "\001\e[1m\002Exception:\001\e[0m\002 RuntimeError: outer\n" \
"--\n" \
"0: /bin/pry:23:in `<main>'\n" \
"1: /bin/pry:23:in `<main>'\n" \
"2: /bin/pry:23:in `<main>'\n" \
"3: /bin/pry:23:in `<main>'\n" \
"4: /bin/pry:23:in `<main>'\n" \
- "\e[1mCaused by:\e[0m RuntimeError: inner\n" \
+ "\001\e[1m\002Caused by:\001\e[0m\002 RuntimeError: inner\n" \
"--\n" \
"0: /bin/pry:23:in `<main>'\n" \
"1: /bin/pry:23:in `<main>'\n" \
@@ -140,7 +140,7 @@ RSpec.describe Pry::Command::Wtf do
it "prints both original and nested exception backtrace" do
subject.process
expect(subject.output.string).to eq(
- "\e[1mException:\e[0m RuntimeError: outer\n" \
+ "\001\e[1m\002Exception:\001\e[0m\002 RuntimeError: outer\n" \
"--\n" \
"0: /bin/pry:23:in `<main>'\n" \
"1: /bin/pry:23:in `<main>'\n" \
@@ -148,7 +148,7 @@ RSpec.describe Pry::Command::Wtf do
"3: /bin/pry:23:in `<main>'\n" \
"4: /bin/pry:23:in `<main>'\n" \
"5: /bin/pry:23:in `<main>'\n" \
- "\e[1mCaused by:\e[0m RuntimeError: inner\n" \
+ "\001\e[1m\002Caused by:\001\e[0m\002 RuntimeError: inner\n" \
"--\n" \
"0: /bin/pry:23:in `<main>'\n" \
"1: /bin/pry:23:in `<main>'\n" \
@@ -177,17 +177,17 @@ RSpec.describe Pry::Command::Wtf do
it "prints lines of code that exception frame references" do
subject.process
expect(subject.output.string).to eq(
- "\e[1mException:\e[0m RuntimeError: oops\n" \
+ "\001\e[1m\002Exception:\001\e[0m\002 RuntimeError: oops\n" \
"--\n" \
- "0: \e[1m#{__FILE__}:168:in `<main>'\e[0m\n" \
+ "0: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n" \
" Array.new(6) { \"\#{__FILE__}:\#{__LINE__}:in `<main>'\" }\n" \
- "1: \e[1m#{__FILE__}:168:in `<main>'\e[0m\n" \
+ "1: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n" \
" Array.new(6) { \"\#{__FILE__}:\#{__LINE__}:in `<main>'\" }\n" \
- "2: \e[1m#{__FILE__}:168:in `<main>'\e[0m\n" \
+ "2: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n" \
" Array.new(6) { \"\#{__FILE__}:\#{__LINE__}:in `<main>'\" }\n" \
- "3: \e[1m#{__FILE__}:168:in `<main>'\e[0m\n" \
+ "3: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n" \
" Array.new(6) { \"\#{__FILE__}:\#{__LINE__}:in `<main>'\" }\n" \
- "4: \e[1m#{__FILE__}:168:in `<main>'\e[0m\n" \
+ "4: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n" \
" Array.new(6) { \"\#{__FILE__}:\#{__LINE__}:in `<main>'\" }\n"
)
end
@@ -200,13 +200,13 @@ RSpec.describe Pry::Command::Wtf do
it "skips code and prints only the backtrace frame" do
subject.process
expect(subject.output.string).to eq(
- "\e[1mException:\e[0m RuntimeError: oops\n" \
+ "\001\e[1m\002Exception:\001\e[0m\002 RuntimeError: oops\n" \
"--\n" \
- "0: \e[1m#{__FILE__}:168:in `<main>'\e[0m\n" \
- "1: \e[1m#{__FILE__}:168:in `<main>'\e[0m\n" \
- "2: \e[1m#{__FILE__}:168:in `<main>'\e[0m\n" \
- "3: \e[1m#{__FILE__}:168:in `<main>'\e[0m\n" \
- "4: \e[1m#{__FILE__}:168:in `<main>'\e[0m\n"
+ "0: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n" \
+ "1: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n" \
+ "2: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n" \
+ "3: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n" \
+ "4: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n"
)
end
end
diff --git a/spec/documentation_helper_spec.rb b/spec/documentation_helper_spec.rb
index 8da62a29..715e101d 100644
--- a/spec/documentation_helper_spec.rb
+++ b/spec/documentation_helper_spec.rb
@@ -72,5 +72,9 @@ describe Pry::Helpers::DocumentationHelpers do
it "should not remove ++" do
expect(@helper.process_rdoc("--\n comment in a bubble\n++")).to match(/\+\+/)
end
+
+ it "should not syntax highlight already highlighted code" do
+ expect(@helper.process_rdoc(" \e\[31mFOO\e\[0m")).to match(/ \e\[31mFOO\e\[0m/)
+ end
end
end
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
diff --git a/spec/pry_repl_spec.rb b/spec/pry_repl_spec.rb
index 946b92e8..b7f5870a 100644
--- a/spec/pry_repl_spec.rb
+++ b/spec/pry_repl_spec.rb
@@ -135,7 +135,7 @@ loop do
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