2012-12-20 04:28:04 -05:00
|
|
|
class Pry
|
2012-12-21 03:02:13 -05:00
|
|
|
class REPL
|
2017-06-03 06:53:14 -04:00
|
|
|
extend Pry::Forwardable
|
2012-12-28 22:06:02 -05:00
|
|
|
def_delegators :@pry, :input, :output
|
2012-12-20 04:28:04 -05:00
|
|
|
|
2012-12-28 22:06:02 -05:00
|
|
|
# @return [Pry] The instance of {Pry} that the user is controlling.
|
|
|
|
attr_accessor :pry
|
2012-12-20 04:28:04 -05:00
|
|
|
|
2012-12-28 22:06:02 -05:00
|
|
|
# Instantiate a new {Pry} instance with the given options, then start a
|
|
|
|
# {REPL} instance wrapping it.
|
|
|
|
# @option options See {Pry#initialize}
|
2012-12-20 04:28:04 -05:00
|
|
|
def self.start(options)
|
2012-12-23 03:08:40 -05:00
|
|
|
new(Pry.new(options)).start
|
2012-12-20 04:28:04 -05:00
|
|
|
end
|
|
|
|
|
2012-12-28 22:06:02 -05:00
|
|
|
# Create an instance of {REPL} wrapping the given {Pry}.
|
|
|
|
# @param [Pry] pry The instance of {Pry} that this {REPL} will control.
|
|
|
|
# @param [Hash] options Options for this {REPL} instance.
|
|
|
|
# @option options [Object] :target The initial target of the session.
|
2012-12-23 03:08:40 -05:00
|
|
|
def initialize(pry, options = {})
|
|
|
|
@pry = pry
|
|
|
|
@indent = Pry::Indent.new
|
|
|
|
|
2015-04-05 10:37:50 -04:00
|
|
|
@readline_output = nil
|
|
|
|
|
2012-12-23 03:08:40 -05:00
|
|
|
if options[:target]
|
2012-12-22 01:06:41 -05:00
|
|
|
@pry.push_binding options[:target]
|
|
|
|
end
|
2012-12-20 04:28:04 -05:00
|
|
|
end
|
|
|
|
|
2012-12-28 22:06:02 -05:00
|
|
|
# Start the read-eval-print loop.
|
|
|
|
# @return [Object?] If the session throws `:breakout`, return the value
|
|
|
|
# thrown with it.
|
|
|
|
# @raise [Exception] If the session throws `:raise_up`, raise the exception
|
|
|
|
# thrown with it.
|
2012-12-21 03:02:13 -05:00
|
|
|
def start
|
2012-12-28 01:10:44 -05:00
|
|
|
prologue
|
2013-06-26 16:39:37 -04:00
|
|
|
Pry::InputLock.for(:all).with_ownership { repl }
|
2012-12-20 04:28:04 -05:00
|
|
|
ensure
|
2012-12-28 01:10:44 -05:00
|
|
|
epilogue
|
2012-12-20 04:28:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2012-12-28 22:06:02 -05:00
|
|
|
# Set up the repl session.
|
|
|
|
# @return [void]
|
2012-12-28 01:10:44 -05:00
|
|
|
def prologue
|
2012-12-21 04:38:32 -05:00
|
|
|
pry.exec_hook :before_session, pry.output, pry.current_binding, pry
|
2012-12-28 22:06:02 -05:00
|
|
|
|
|
|
|
# Clear the line before starting Pry. This fixes issue #566.
|
2014-04-30 02:52:17 -04:00
|
|
|
if pry.config.correct_indent
|
2012-12-20 04:28:04 -05:00
|
|
|
Kernel.print Pry::Helpers::BaseHelpers.windows_ansi? ? "\e[0F" : "\e[0G"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-28 22:06:02 -05:00
|
|
|
# The actual read-eval-print loop.
|
2012-12-28 19:24:07 -05:00
|
|
|
#
|
2012-12-28 22:06:02 -05:00
|
|
|
# The {REPL} instance is responsible for reading and looping, whereas the
|
|
|
|
# {Pry} instance is responsible for evaluating user input and printing
|
|
|
|
# return values and command output.
|
2012-12-28 19:24:07 -05:00
|
|
|
#
|
2012-12-28 22:06:02 -05:00
|
|
|
# @return [Object?] If the session throws `:breakout`, return the value
|
|
|
|
# thrown with it.
|
|
|
|
# @raise [Exception] If the session throws `:raise_up`, raise the exception
|
|
|
|
# thrown with it.
|
2012-12-28 17:19:42 -05:00
|
|
|
def repl
|
|
|
|
loop do
|
2012-12-28 01:11:30 -05:00
|
|
|
case val = read
|
2012-12-20 04:28:04 -05:00
|
|
|
when :control_c
|
|
|
|
output.puts ""
|
2012-12-27 22:56:43 -05:00
|
|
|
pry.reset_eval_string
|
2012-12-21 03:40:21 -05:00
|
|
|
when :no_more_input
|
2013-06-26 16:39:37 -04:00
|
|
|
output.puts "" if output.tty?
|
2012-12-21 04:38:32 -05:00
|
|
|
break
|
2012-12-20 04:28:04 -05:00
|
|
|
else
|
2012-12-21 04:38:32 -05:00
|
|
|
output.puts "" if val.nil? && output.tty?
|
2012-12-28 01:06:50 -05:00
|
|
|
return pry.exit_value unless pry.eval(val)
|
2012-12-20 04:28:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-28 22:06:02 -05:00
|
|
|
# Clean up after the repl session.
|
|
|
|
# @return [void]
|
2012-12-28 01:10:44 -05:00
|
|
|
def epilogue
|
2012-12-21 04:38:32 -05:00
|
|
|
pry.exec_hook :after_session, pry.output, pry.current_binding, pry
|
2012-12-20 04:28:04 -05:00
|
|
|
end
|
|
|
|
|
2012-12-28 22:06:02 -05:00
|
|
|
# Read a line of input from the user.
|
|
|
|
# @return [String] The line entered by the user.
|
|
|
|
# @return [nil] On `<Ctrl-D>`.
|
|
|
|
# @return [:control_c] On `<Ctrl+C>`.
|
|
|
|
# @return [:no_more_input] On EOF.
|
2012-12-28 01:11:30 -05:00
|
|
|
def read
|
2012-12-20 04:28:04 -05:00
|
|
|
@indent.reset if pry.eval_string.empty?
|
|
|
|
current_prompt = pry.select_prompt
|
2014-04-30 02:52:17 -04:00
|
|
|
indentation = pry.config.auto_indent ? @indent.current_prefix : ''
|
2012-12-20 04:28:04 -05:00
|
|
|
|
2013-01-18 03:35:59 -05:00
|
|
|
val = read_line("#{current_prompt}#{indentation}")
|
2012-12-20 04:28:04 -05:00
|
|
|
|
2012-12-28 22:05:06 -05:00
|
|
|
# Return nil for EOF, :no_more_input for error, or :control_c for <Ctrl-C>
|
2012-12-28 18:59:41 -05:00
|
|
|
return val unless String === val
|
2012-12-20 04:28:04 -05:00
|
|
|
|
2014-04-30 02:52:17 -04:00
|
|
|
if pry.config.auto_indent
|
2012-12-20 04:28:04 -05:00
|
|
|
original_val = "#{indentation}#{val}"
|
|
|
|
indented_val = @indent.indent(val)
|
|
|
|
|
2014-04-30 02:43:19 -04:00
|
|
|
if output.tty? && pry.config.correct_indent && Pry::Helpers::BaseHelpers.use_ansi_codes?
|
2012-12-28 22:05:06 -05:00
|
|
|
output.print @indent.correct_indentation(
|
2018-10-19 13:25:53 -04:00
|
|
|
current_prompt,
|
|
|
|
indented_val,
|
2018-10-21 16:29:28 -04:00
|
|
|
calculate_overhang(current_prompt, original_val, indented_val)
|
2012-12-28 22:05:06 -05:00
|
|
|
)
|
2012-12-20 04:28:04 -05:00
|
|
|
output.flush
|
|
|
|
end
|
|
|
|
else
|
|
|
|
indented_val = val
|
|
|
|
end
|
|
|
|
|
|
|
|
indented_val
|
|
|
|
end
|
|
|
|
|
2012-12-28 22:06:02 -05:00
|
|
|
# Manage switching of input objects on encountering `EOFError`s.
|
|
|
|
# @return [Object] Whatever the given block returns.
|
|
|
|
# @return [:no_more_input] Indicates that no more input can be read.
|
2012-12-20 04:28:04 -05:00
|
|
|
def handle_read_errors
|
2013-06-26 16:39:37 -04:00
|
|
|
should_retry = true
|
2012-12-20 04:28:04 -05:00
|
|
|
exception_count = 0
|
2012-12-28 22:06:02 -05:00
|
|
|
|
2012-12-20 04:28:04 -05:00
|
|
|
begin
|
|
|
|
yield
|
|
|
|
rescue EOFError
|
2014-04-30 02:52:17 -04:00
|
|
|
pry.config.input = Pry.config.input
|
2013-06-26 16:39:37 -04:00
|
|
|
if !should_retry
|
|
|
|
output.puts "Error: Pry ran out of things to read from! " \
|
|
|
|
"Attempting to break out of REPL."
|
|
|
|
return :no_more_input
|
|
|
|
end
|
|
|
|
should_retry = false
|
|
|
|
retry
|
2012-12-20 04:28:04 -05:00
|
|
|
|
2013-01-18 03:35:59 -05:00
|
|
|
# Handle <Ctrl+C> like Bash: empty the current input buffer, but don't
|
|
|
|
# quit. This is only for MRI 1.9; other versions of Ruby don't let you
|
|
|
|
# send Interrupt from within Readline.
|
2012-12-20 04:28:04 -05:00
|
|
|
rescue Interrupt
|
2013-01-18 03:35:59 -05:00
|
|
|
return :control_c
|
2012-12-20 04:28:04 -05:00
|
|
|
|
2012-12-28 22:06:02 -05:00
|
|
|
# If we get a random error when trying to read a line we don't want to
|
|
|
|
# automatically retry, as the user will see a lot of error messages
|
|
|
|
# scroll past and be unable to do anything about it.
|
2012-12-20 04:28:04 -05:00
|
|
|
rescue RescuableException => e
|
|
|
|
puts "Error: #{e.message}"
|
|
|
|
output.puts e.backtrace
|
|
|
|
exception_count += 1
|
|
|
|
if exception_count < 5
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
puts "FATAL: Pry failed to get user input using `#{input}`."
|
2012-12-28 22:06:02 -05:00
|
|
|
puts "To fix this you may be able to pass input and output file " \
|
|
|
|
"descriptors to pry directly. e.g."
|
2012-12-20 04:28:04 -05:00
|
|
|
puts " Pry.config.input = STDIN"
|
|
|
|
puts " Pry.config.output = STDOUT"
|
|
|
|
puts " binding.pry"
|
2012-12-21 03:40:21 -05:00
|
|
|
return :no_more_input
|
2012-12-20 04:28:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-28 22:06:02 -05:00
|
|
|
# Returns the next line of input to be sent to the {Pry} instance.
|
2012-12-20 04:28:04 -05:00
|
|
|
# @param [String] current_prompt The prompt to use for input.
|
2012-12-28 22:06:02 -05:00
|
|
|
# @return [String?] The next line of input, or `nil` on <Ctrl-D>.
|
2012-12-28 18:19:21 -05:00
|
|
|
def read_line(current_prompt)
|
2012-12-20 04:28:04 -05:00
|
|
|
handle_read_errors do
|
2015-03-02 01:42:59 -05:00
|
|
|
if coolline_available?
|
2012-12-20 04:28:04 -05:00
|
|
|
input.completion_proc = proc do |cool|
|
2012-12-28 18:19:21 -05:00
|
|
|
completions = @pry.complete cool.completed_word
|
2012-12-20 04:28:04 -05:00
|
|
|
completions.compact
|
|
|
|
end
|
|
|
|
elsif input.respond_to? :completion_proc=
|
2015-01-23 08:24:35 -05:00
|
|
|
input.completion_proc = proc do |inp|
|
|
|
|
@pry.complete inp
|
2012-12-28 18:19:21 -05:00
|
|
|
end
|
2012-12-20 04:28:04 -05:00
|
|
|
end
|
|
|
|
|
2015-03-02 01:36:26 -05:00
|
|
|
if readline_available?
|
2015-03-02 05:07:32 -05:00
|
|
|
set_readline_output
|
2013-06-25 15:19:32 -04:00
|
|
|
input_readline(current_prompt, false) # false since we'll add it manually
|
2015-03-02 01:42:59 -05:00
|
|
|
elsif coolline_available?
|
2013-06-25 15:19:32 -04:00
|
|
|
input_readline(current_prompt)
|
2012-12-20 04:28:04 -05:00
|
|
|
else
|
|
|
|
if input.method(:readline).arity == 1
|
2013-06-25 15:19:32 -04:00
|
|
|
input_readline(current_prompt)
|
2012-12-20 04:28:04 -05:00
|
|
|
else
|
2013-06-25 15:19:32 -04:00
|
|
|
input_readline
|
2012-12-20 04:28:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-06-25 15:19:32 -04:00
|
|
|
|
|
|
|
def input_readline(*args)
|
2013-06-26 16:39:37 -04:00
|
|
|
Pry::InputLock.for(:all).interruptible_region do
|
2013-06-25 15:19:32 -04:00
|
|
|
input.readline(*args)
|
|
|
|
end
|
|
|
|
end
|
2015-03-02 01:36:26 -05:00
|
|
|
|
|
|
|
def readline_available?
|
|
|
|
defined?(Readline) && input == Readline
|
|
|
|
end
|
|
|
|
|
2015-03-02 01:42:59 -05:00
|
|
|
def coolline_available?
|
|
|
|
defined?(Coolline) && input.is_a?(Coolline)
|
|
|
|
end
|
|
|
|
|
2015-03-02 01:36:26 -05:00
|
|
|
# If `$stdout` is not a tty, it's probably a pipe.
|
|
|
|
# @example
|
|
|
|
# # `piping?` returns `false`
|
|
|
|
# % pry
|
|
|
|
# [1] pry(main)
|
|
|
|
#
|
|
|
|
# # `piping?` returns `true`
|
|
|
|
# % pry | tee log
|
|
|
|
def piping?
|
2015-08-23 21:56:39 -04:00
|
|
|
return false unless $stdout.respond_to?(:tty?)
|
2018-10-14 09:44:58 -04:00
|
|
|
|
2015-08-23 21:56:39 -04:00
|
|
|
!$stdout.tty? && $stdin.tty? && !Pry::Helpers::BaseHelpers.windows?
|
2015-03-02 01:36:26 -05:00
|
|
|
end
|
2015-03-02 05:07:32 -05:00
|
|
|
|
2015-03-03 10:19:04 -05:00
|
|
|
# @return [void]
|
2015-03-02 05:07:32 -05:00
|
|
|
def set_readline_output
|
2015-03-03 10:19:04 -05:00
|
|
|
return if @readline_output
|
2018-10-14 09:44:58 -04:00
|
|
|
|
2015-03-03 10:19:04 -05:00
|
|
|
if piping?
|
|
|
|
@readline_output = (Readline.output = Pry.config.output)
|
|
|
|
end
|
2015-03-02 05:07:32 -05:00
|
|
|
end
|
2018-10-19 13:25:53 -04:00
|
|
|
|
2018-10-21 16:29:28 -04:00
|
|
|
# Calculates correct overhang for current line. Supports vi Readline
|
|
|
|
# mode and its indicators such as "(ins)" or "(cmd)".
|
|
|
|
#
|
|
|
|
# @return [Integer]
|
|
|
|
# @note This doesn't calculate overhang for Readline's emacs mode with an
|
|
|
|
# indicator because emacs is the default mode and it doesn't use
|
|
|
|
# indicators in 99% of cases.
|
|
|
|
def calculate_overhang(current_prompt, original_val, indented_val)
|
|
|
|
overhang = original_val.length - indented_val.length
|
2018-10-25 16:33:36 -04:00
|
|
|
if readline_available? &&
|
|
|
|
# rb-readline doesn't support this method:
|
|
|
|
# https://github.com/ConnorAtherton/rb-readline/issues/152
|
|
|
|
Readline.respond_to?(:vi_editing_mode?) &&
|
|
|
|
Readline.vi_editing_mode?
|
2018-10-21 16:29:28 -04:00
|
|
|
overhang += current_prompt.length - indented_val.length
|
2018-10-19 13:25:53 -04:00
|
|
|
end
|
2018-10-21 16:29:28 -04:00
|
|
|
[0, overhang].max
|
2018-10-19 13:25:53 -04:00
|
|
|
end
|
2012-12-20 04:28:04 -05:00
|
|
|
end
|
|
|
|
end
|