Skip to content

Commit

Permalink
Reverted pry#2154, in favor or adding \001/\002 control character…
Browse files Browse the repository at this point in the history
…s on prompts only.
  • Loading branch information
SilverPhoenix99 committed Jul 5, 2021
1 parent 74d81f3 commit 68e3392
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 37 deletions.
4 changes: 2 additions & 2 deletions lib/pry/config.rb
Expand Up @@ -68,7 +68,7 @@ class Config
attribute :pager

# @return [Boolean]
attribute :escape_headers
attribute :escape_prompt

# @return [Boolean] whether the global ~/.pryrc should be loaded
attribute :should_load_rc
Expand Down Expand Up @@ -180,7 +180,7 @@ def initialize

hooks: Pry::Hooks.default,
pager: true,
escape_headers: true,
escape_prompt: true,
system: Pry::SystemCommandHandler.method(:default),
color: Pry::Helpers::BaseHelpers.use_ansi_codes?,
default_window_size: 5,
Expand Down
15 changes: 2 additions & 13 deletions lib/pry/helpers/text.rb
Expand Up @@ -7,9 +7,6 @@ module Helpers
module Text
extend self

HEADER_START = "\001"
HEADER_END = "\002"

DECORATIONS = {
"bold" => '1',
"faint" => '2',
Expand Down Expand Up @@ -65,23 +62,15 @@ module Text
# @param text [String]
# @return [String]
def escape_text(color_code, text)

header_start = nil
header_end = nil
if Pry.config.escape_headers
header_start = HEADER_START
header_end = HEADER_END
end

"#{header_start}\e[#{color_code}m#{header_end}#{text}#{header_start}\e[0m#{header_end}"
"\e[#{color_code}m#{text}\e[0m"
end

# Remove any color codes from _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[;\d]?)*m)(\002)?/, '')
text.to_s.gsub(/\001|\002|\e\[[\d;]+m/, '')
end

# Returns `text` in the default foreground colour.
Expand Down
1 change: 0 additions & 1 deletion lib/pry/indent.rb
Expand Up @@ -391,7 +391,6 @@ def module_nesting
#
# @return [String] correctly indented line
def correct_indentation(prompt, code, overhang = 0)
prompt = prompt.delete("\001\002")
line_to_measure = Pry::Helpers::Text.strip_color(prompt) << code
whitespace = ' ' * overhang

Expand Down
2 changes: 1 addition & 1 deletion lib/pry/prompt.rb
Expand Up @@ -177,7 +177,7 @@ def [](key)
add(
:nav,
"A prompt that displays the binding stack as a path and includes information \n" \
"about '_in_' and '_out_'.",
"about #{Helpers::Text.bold('_in_')} and #{Helpers::Text.bold('_out_')}.",
%w[> *]
) do |_context, _nesting, pry_instance, sep|
tree = pry_instance.binding_stack.map { |b| Pry.view_clip(b.eval('self')) }
Expand Down
1 change: 1 addition & 0 deletions lib/pry/pry_instance.rb
Expand Up @@ -685,6 +685,7 @@ def ensure_correct_encoding!(val)
end
end

# @return [String]
def generate_prompt(prompt_proc, conf)
if prompt_proc.arity == 1
prompt_proc.call(conf)
Expand Down
3 changes: 3 additions & 0 deletions lib/pry/repl.rb
Expand Up @@ -92,7 +92,10 @@ def epilogue
# @return [:no_more_input] On EOF.
def read
@indent.reset if pry.eval_string.empty?

current_prompt = pry.select_prompt
current_prompt = current_prompt.gsub(/(\e\[[\d;]+m)/, "\001\\1\002") if pry.config.escape_prompt

indentation = pry.config.auto_indent ? @indent.current_prefix : ''

val = read_line("#{current_prompt}#{indentation}")
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(
"\001\e[1m\002Exception:\001\e[0m\002 RuntimeError: oops\n" \
"\e[1mException:\e[0m 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(
"\001\e[1m\002Exception:\001\e[0m\002 RuntimeError: oops\n" \
"\e[1mException:\e[0m 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(
"\001\e[1m\002Exception:\001\e[0m\002 RuntimeError: oops\n" \
"\e[1mException:\e[0m 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(
"\001\e[1m\002Exception:\001\e[0m\002 RuntimeError: outer\n" \
"\e[1mException:\e[0m 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" \
"\001\e[1m\002Caused by:\001\e[0m\002 RuntimeError: inner\n" \
"\e[1mCaused by:\e[0m 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(
"\001\e[1m\002Exception:\001\e[0m\002 RuntimeError: outer\n" \
"\e[1mException:\e[0m 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" \
"\001\e[1m\002Caused by:\001\e[0m\002 RuntimeError: inner\n" \
"\e[1mCaused by:\e[0m 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(
"\001\e[1m\002Exception:\001\e[0m\002 RuntimeError: oops\n" \
"\e[1mException:\e[0m RuntimeError: oops\n" \
"--\n" \
"0: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n" \
"0: \e[1m#{__FILE__}:168:in `<main>'\e[0m\n" \
" Array.new(6) { \"\#{__FILE__}:\#{__LINE__}:in `<main>'\" }\n" \
"1: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n" \
"1: \e[1m#{__FILE__}:168:in `<main>'\e[0m\n" \
" Array.new(6) { \"\#{__FILE__}:\#{__LINE__}:in `<main>'\" }\n" \
"2: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n" \
"2: \e[1m#{__FILE__}:168:in `<main>'\e[0m\n" \
" Array.new(6) { \"\#{__FILE__}:\#{__LINE__}:in `<main>'\" }\n" \
"3: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n" \
"3: \e[1m#{__FILE__}:168:in `<main>'\e[0m\n" \
" Array.new(6) { \"\#{__FILE__}:\#{__LINE__}:in `<main>'\" }\n" \
"4: \001\e[1m\002#{__FILE__}:168:in `<main>'\001\e[0m\002\n" \
"4: \e[1m#{__FILE__}:168:in `<main>'\e[0m\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(
"\001\e[1m\002Exception:\001\e[0m\002 RuntimeError: oops\n" \
"\e[1mException:\e[0m RuntimeError: oops\n" \
"--\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"
"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"
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/config_spec.rb
Expand Up @@ -13,7 +13,7 @@
specify { expect(subject.unrescued_exceptions).to be_an(Array) }
specify { expect(subject.hooks).to be_a(Pry::Hooks) }
specify { expect(subject.pager).to be(true).or be(false) }
specify { expect(subject.escape_headers).to be(true).or be(false) }
specify { expect(subject.escape_prompt).to be(true).or be(false) }
specify { expect(subject.system).to be_a(Method) }
specify { expect(subject.color).to be(true).or be(false) }
specify { expect(subject.default_window_size).to be_a(Numeric) }
Expand Down

0 comments on commit 68e3392

Please sign in to comment.