Skip to content

Commit

Permalink
Add --no-multiline options to disable multiline with Reline
Browse files Browse the repository at this point in the history
  • Loading branch information
andrehjr committed Oct 15, 2024
1 parent 2615de7 commit cfd1904
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
4 changes: 4 additions & 0 deletions lib/pry/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ def start(opts)
Pry.config.color = false
end

on "no-multiline", "Disables multiline (defaults to true with Reline)" do
Pry.config.multiline = false
end

on :f, "Suppress loading of pryrc" do
Pry.config.should_load_rc = false
Pry.config.should_load_local_rc = false
Expand Down
4 changes: 4 additions & 0 deletions lib/pry/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class Config
# @return [Boolean]
attribute :pager

# @return [Boolean]
attribute :multiline

# @return [Boolean] whether the global ~/.pryrc should be loaded
attribute :should_load_rc

Expand Down Expand Up @@ -164,6 +167,7 @@ def initialize
hooks: Pry::Hooks.default,
pager: true,
system: Pry::SystemCommandHandler.method(:default),
multiline: true,
color: Pry::Helpers::BaseHelpers.use_ansi_codes?,
default_window_size: 5,
editor: Pry::Editor.default,
Expand Down
52 changes: 29 additions & 23 deletions lib/pry/repl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def read
# Return nil for EOF, :no_more_input for error, or :control_c for <Ctrl-C>
return val unless val.is_a?(String)

if pry.config.auto_indent && !reline_available?
if pry.config.auto_indent && !input_multiline?
original_val = "#{indentation}#{val}"
indented_val = @indent.indent(val)

Expand Down Expand Up @@ -180,7 +180,29 @@ def read_line(current_prompt)
end

if reline_available?
input_reline(current_prompt)
Reline.output_modifier_proc = lambda do |text, _|
if pry.color
SyntaxHighlighter.highlight(text)
else
text
end
end

if pry.config.auto_indent
Reline.auto_indent_proc = lambda do |lines, line_index, _byte_ptr, _newline|
if line_index == 0
0
else
pry_indentation = Pry::Indent.new
pry_indentation.indent(lines.join("\n"))
pry_indentation.last_indent_level.length
end
end
end
end

if input_multiline?
input_readmultiline(current_prompt)
elsif readline_available?
set_readline_output
input_readline(current_prompt, false) # false since we'll add it manually
Expand All @@ -194,27 +216,7 @@ def read_line(current_prompt)
end
end

def input_reline(*args)
Reline.output_modifier_proc = lambda do |text, _|
if pry.color
SyntaxHighlighter.highlight(text)
else
text
end
end

if pry.config.auto_indent
Reline.auto_indent_proc = lambda do |lines, line_index, _byte_pointer, _newline|
if line_index == 0
0
else
pry_indentation = Pry::Indent.new
pry_indentation.indent(lines.join("\n"))
pry_indentation.last_indent_level.length
end
end
end

def input_readmultiline(*args)
Pry::InputLock.for(:all).interruptible_region do
input.readmultiline(*args) do |multiline_input|
Pry.commands.find_command(multiline_input) ||
Expand All @@ -229,6 +231,10 @@ def input_readline(*args)
end
end

def input_multiline?
!!pry.config.multiline && reline_available?
end

def reline_available?
defined?(Reline) && input == Reline && prism_available?
end
Expand Down

0 comments on commit cfd1904

Please sign in to comment.