Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape non-printing characters #2154

Merged
merged 2 commits into from Mar 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/pry/helpers/text.rb
Expand Up @@ -21,20 +21,20 @@ module Text

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
Expand All @@ -52,7 +52,7 @@ def strip_color(text)
# @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.
Expand Down
38 changes: 19 additions & 19 deletions spec/commands/wtf_spec.rb
Expand Up @@ -38,7 +38,7 @@
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" \
Expand All @@ -55,7 +55,7 @@
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" \
Expand All @@ -73,7 +73,7 @@
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" \
Expand Down Expand Up @@ -116,14 +116,14 @@
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" \
Expand All @@ -140,15 +140,15 @@
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" \
"2: /bin/pry:23:in `<main>'\n" \
"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" \
Expand Down Expand Up @@ -177,17 +177,17 @@
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
Expand All @@ -200,13 +200,13 @@
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
Expand Down