Skip to content

Commit 438ce37

Browse files
authored
Merge pull request #2154 from xjunior/patch-1
Escape non-printing characters
2 parents cee7b4d + 8b5a70b commit 438ce37

2 files changed

Lines changed: 24 additions & 24 deletions

File tree

lib/pry/helpers/text.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ module Text
2121

2222
COLORS.each_pair do |color, value|
2323
define_method color do |text|
24-
"\033[0;#{30 + value}m#{text}\033[0m"
24+
"\001\033[0;#{30 + value}m\002#{text}\001\033[0m\002"
2525
end
2626

2727
define_method "bright_#{color}" do |text|
28-
"\033[1;#{30 + value}m#{text}\033[0m"
28+
"\001\033[1;#{30 + value}m\002#{text}\001\033[0m\002"
2929
end
3030

3131
COLORS.each_pair do |bg_color, bg_value|
3232
define_method "#{color}_on_#{bg_color}" do |text|
33-
"\033[0;#{30 + value};#{40 + bg_value}m#{text}\033[0m"
33+
"\001\033[0;#{30 + value};#{40 + bg_value}m\002#{text}\001\033[0m\002"
3434
end
3535

3636
define_method "bright_#{color}_on_#{bg_color}" do |text|
37-
"\033[1;#{30 + value};#{40 + bg_value}m#{text}\033[0m"
37+
"\001\033[1;#{30 + value};#{40 + bg_value}m\002#{text}\001\033[0m\002"
3838
end
3939
end
4040
end
@@ -52,7 +52,7 @@ def strip_color(text)
5252
# @param [String, #to_s] text
5353
# @return [String] _text_
5454
def bold(text)
55-
"\e[1m#{text}\e[0m"
55+
"\001\e[1m\002#{text}\001\e[0m\002"
5656
end
5757

5858
# Returns `text` in the default foreground colour.

spec/commands/wtf_spec.rb

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
it "prints only a part of the exception backtrace" do
3939
subject.process
4040
expect(subject.output.string).to eq(
41-
"\e[1mException:\e[0m RuntimeError: oops\n" \
41+
"\001\e[1m\002Exception:\001\e[0m\002 RuntimeError: oops\n" \
4242
"--\n" \
4343
"0: /bin/pry:23:in `<main>'\n" \
4444
"1: /bin/pry:23:in `<main>'\n" \
@@ -55,7 +55,7 @@
5555
it "prints full exception backtrace" do
5656
subject.process
5757
expect(subject.output.string).to eq(
58-
"\e[1mException:\e[0m RuntimeError: oops\n" \
58+
"\001\e[1m\002Exception:\001\e[0m\002 RuntimeError: oops\n" \
5959
"--\n" \
6060
"0: /bin/pry:23:in `<main>'\n" \
6161
"1: /bin/pry:23:in `<main>'\n" \
@@ -73,7 +73,7 @@
7373
it "prints more of backtrace" do
7474
subject.process
7575
expect(subject.output.string).to eq(
76-
"\e[1mException:\e[0m RuntimeError: oops\n" \
76+
"\001\e[1m\002Exception:\001\e[0m\002 RuntimeError: oops\n" \
7777
"--\n" \
7878
"0: /bin/pry:23:in `<main>'\n" \
7979
"1: /bin/pry:23:in `<main>'\n" \
@@ -116,14 +116,14 @@
116116
it "prints parts of both original and nested exception backtrace" do
117117
subject.process
118118
expect(subject.output.string).to eq(
119-
"\e[1mException:\e[0m RuntimeError: outer\n" \
119+
"\001\e[1m\002Exception:\001\e[0m\002 RuntimeError: outer\n" \
120120
"--\n" \
121121
"0: /bin/pry:23:in `<main>'\n" \
122122
"1: /bin/pry:23:in `<main>'\n" \
123123
"2: /bin/pry:23:in `<main>'\n" \
124124
"3: /bin/pry:23:in `<main>'\n" \
125125
"4: /bin/pry:23:in `<main>'\n" \
126-
"\e[1mCaused by:\e[0m RuntimeError: inner\n" \
126+
"\001\e[1m\002Caused by:\001\e[0m\002 RuntimeError: inner\n" \
127127
"--\n" \
128128
"0: /bin/pry:23:in `<main>'\n" \
129129
"1: /bin/pry:23:in `<main>'\n" \
@@ -140,15 +140,15 @@
140140
it "prints both original and nested exception backtrace" do
141141
subject.process
142142
expect(subject.output.string).to eq(
143-
"\e[1mException:\e[0m RuntimeError: outer\n" \
143+
"\001\e[1m\002Exception:\001\e[0m\002 RuntimeError: outer\n" \
144144
"--\n" \
145145
"0: /bin/pry:23:in `<main>'\n" \
146146
"1: /bin/pry:23:in `<main>'\n" \
147147
"2: /bin/pry:23:in `<main>'\n" \
148148
"3: /bin/pry:23:in `<main>'\n" \
149149
"4: /bin/pry:23:in `<main>'\n" \
150150
"5: /bin/pry:23:in `<main>'\n" \
151-
"\e[1mCaused by:\e[0m RuntimeError: inner\n" \
151+
"\001\e[1m\002Caused by:\001\e[0m\002 RuntimeError: inner\n" \
152152
"--\n" \
153153
"0: /bin/pry:23:in `<main>'\n" \
154154
"1: /bin/pry:23:in `<main>'\n" \
@@ -177,17 +177,17 @@
177177
it "prints lines of code that exception frame references" do
178178
subject.process
179179
expect(subject.output.string).to eq(
180-
"\e[1mException:\e[0m RuntimeError: oops\n" \
180+
"\001\e[1m\002Exception:\001\e[0m\002 RuntimeError: oops\n" \
181181
"--\n" \
182-
"0: \e[1m#{__FILE__}:168:in `<main>'\e[0m\n" \
182+
"0: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n" \
183183
" Array.new(6) { \"\#{__FILE__}:\#{__LINE__}:in `<main>'\" }\n" \
184-
"1: \e[1m#{__FILE__}:168:in `<main>'\e[0m\n" \
184+
"1: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n" \
185185
" Array.new(6) { \"\#{__FILE__}:\#{__LINE__}:in `<main>'\" }\n" \
186-
"2: \e[1m#{__FILE__}:168:in `<main>'\e[0m\n" \
186+
"2: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n" \
187187
" Array.new(6) { \"\#{__FILE__}:\#{__LINE__}:in `<main>'\" }\n" \
188-
"3: \e[1m#{__FILE__}:168:in `<main>'\e[0m\n" \
188+
"3: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n" \
189189
" Array.new(6) { \"\#{__FILE__}:\#{__LINE__}:in `<main>'\" }\n" \
190-
"4: \e[1m#{__FILE__}:168:in `<main>'\e[0m\n" \
190+
"4: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n" \
191191
" Array.new(6) { \"\#{__FILE__}:\#{__LINE__}:in `<main>'\" }\n"
192192
)
193193
end
@@ -200,13 +200,13 @@
200200
it "skips code and prints only the backtrace frame" do
201201
subject.process
202202
expect(subject.output.string).to eq(
203-
"\e[1mException:\e[0m RuntimeError: oops\n" \
203+
"\001\e[1m\002Exception:\001\e[0m\002 RuntimeError: oops\n" \
204204
"--\n" \
205-
"0: \e[1m#{__FILE__}:168:in `<main>'\e[0m\n" \
206-
"1: \e[1m#{__FILE__}:168:in `<main>'\e[0m\n" \
207-
"2: \e[1m#{__FILE__}:168:in `<main>'\e[0m\n" \
208-
"3: \e[1m#{__FILE__}:168:in `<main>'\e[0m\n" \
209-
"4: \e[1m#{__FILE__}:168:in `<main>'\e[0m\n"
205+
"0: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n" \
206+
"1: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n" \
207+
"2: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n" \
208+
"3: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n" \
209+
"4: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n"
210210
)
211211
end
212212
end

0 commit comments

Comments
 (0)