mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
Pry.accept_line returns a 'should continue?' boolean
This commit is contained in:
parent
37b2f20a34
commit
4d4c0ab6b2
3 changed files with 62 additions and 28 deletions
|
@ -235,7 +235,48 @@ class Pry
|
|||
@eval_string = ""
|
||||
end
|
||||
|
||||
# Pass a line of input to pry.
|
||||
#
|
||||
# This is the equivalent of `binding.eval` but with extra pry!
|
||||
# In particular:
|
||||
# 1. Pry-commands will be executed immediately if the line matches,
|
||||
# 2. Partial lines of input will be queued up until a complete expression has been
|
||||
# accepted,
|
||||
# 3. Output is written to {output} in pretty colours, not returned.
|
||||
#
|
||||
# If this method returns false, that means the user has asked pry to end the session,
|
||||
# probably by invoking the 'exit' command, 'cd ..' out of the top, typing '<ctrl-d>,
|
||||
# or running `raise-up`. You should `return pry.finish` when this happens, so that
|
||||
# the user gets any return value they've asked for (if possible) and any exceptions
|
||||
# they've asked to raise get raised.
|
||||
#
|
||||
# @param [String, nil] line The line of input, nil if the user types <ctrl-d>
|
||||
# @return [Boolean] true if pry is ready for more input, false otherwise
|
||||
def accept_line(line)
|
||||
unless @started
|
||||
@started = true
|
||||
exec_hook :before_session, output, current_binding, self
|
||||
end
|
||||
return false if @stopped
|
||||
|
||||
break_data = nil
|
||||
exception = catch(:raise_up) do
|
||||
break_data = catch(:breakout) do
|
||||
handle_line(line)
|
||||
return true
|
||||
end
|
||||
exception = false
|
||||
end
|
||||
|
||||
@stopped = exception ? :raise : :return
|
||||
@stop_value = exception || break_data
|
||||
return false
|
||||
rescue RescuableException => e
|
||||
@stopped = :raise
|
||||
@stop_value = e
|
||||
end
|
||||
|
||||
def handle_line(line)
|
||||
if line.nil?
|
||||
Pry.config.control_d_handler.call(@eval_string, self)
|
||||
return
|
||||
|
@ -291,6 +332,17 @@ class Pry
|
|||
throw(:breakout) if current_binding.nil?
|
||||
end
|
||||
|
||||
# Called after the user has finished interacting with pry.
|
||||
#
|
||||
# @return [Object, nil] the value the user exited with, if any.
|
||||
# @raise [Exception] the exception the user 'raise-up'd, if any.
|
||||
def finish
|
||||
@stopped ||= :finish
|
||||
exec_hook :after_session, output, current_binding, self
|
||||
raise @stop_value if @stopped == :raise
|
||||
return @stop_value if @stopped == :return
|
||||
end
|
||||
|
||||
def evaluate_ruby(code)
|
||||
inject_sticky_locals!
|
||||
exec_hook :before_eval, code, self
|
||||
|
|
|
@ -19,18 +19,7 @@ class Pry
|
|||
def start
|
||||
repl_prologue
|
||||
|
||||
# FIXME: move these catchers back into Pry#accept_line
|
||||
break_data = nil
|
||||
exception = catch(:raise_up) do
|
||||
break_data = catch(:breakout) do
|
||||
repl
|
||||
end
|
||||
exception = false
|
||||
end
|
||||
|
||||
raise exception if exception
|
||||
|
||||
break_data
|
||||
repl
|
||||
ensure
|
||||
repl_epilogue
|
||||
end
|
||||
|
@ -38,14 +27,11 @@ class Pry
|
|||
private
|
||||
|
||||
def repl_prologue
|
||||
pry.exec_hook :before_session, pry.output, pry.current_binding, pry
|
||||
|
||||
# Clear the line before starting Pry. This fixes the issue discussed here:
|
||||
# https://github.com/pry/pry/issues/566
|
||||
if Pry.config.auto_indent
|
||||
Kernel.print Pry::Helpers::BaseHelpers.windows_ansi? ? "\e[0F" : "\e[0G"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def repl
|
||||
|
@ -56,17 +42,18 @@ class Pry
|
|||
pry.reset_line
|
||||
when :end_of_file
|
||||
output.puts "" if output.tty?
|
||||
pry.accept_line nil
|
||||
return pry.finish unless pry.accept_line(nil)
|
||||
when :no_more_input
|
||||
output.puts "" if output.tty?
|
||||
return pry.finish
|
||||
else
|
||||
pry.accept_line val
|
||||
return pry.finish unless pry.accept_line(val)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Clean-up after the repl session.
|
||||
def repl_epilogue
|
||||
pry.exec_hook :after_session, pry.output, pry.current_binding, pry
|
||||
|
||||
Pry.save_history if Pry.config.history.should_save
|
||||
end
|
||||
|
||||
|
@ -133,7 +120,7 @@ class Pry
|
|||
pry.input = Pry.config.input
|
||||
if !should_retry
|
||||
output.puts "Error: Pry ran out of things to read from! Attempting to break out of REPL."
|
||||
throw(:breakout)
|
||||
return :no_more_input
|
||||
end
|
||||
should_retry = false
|
||||
else
|
||||
|
@ -162,7 +149,7 @@ class Pry
|
|||
puts " Pry.config.input = STDIN"
|
||||
puts " Pry.config.output = STDOUT"
|
||||
puts " binding.pry"
|
||||
throw(:breakout)
|
||||
return :no_more_input
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -19,12 +19,7 @@ describe Pry::DEFAULT_CONTROL_D_HANDLER do
|
|||
it 'should break out of a REPL loop' do
|
||||
instance = Pry.new
|
||||
instance.binding_stack.should.not.be.empty
|
||||
called = false
|
||||
catch(:breakout) {
|
||||
instance.accept_line(nil)
|
||||
called = true
|
||||
}
|
||||
called.should == false
|
||||
instance.accept_line(nil).should.be.false
|
||||
instance.binding_stack.should.be.empty
|
||||
end
|
||||
end
|
||||
|
@ -34,7 +29,7 @@ describe Pry::DEFAULT_CONTROL_D_HANDLER do
|
|||
t = pry_tester
|
||||
t.eval "cd Object.new"
|
||||
t.eval("_pry_.binding_stack.size").should == 2
|
||||
t.eval("_pry_.accept_line(nil)")
|
||||
t.eval("_pry_.accept_line(nil)").should.be.true
|
||||
t.eval("_pry_.binding_stack.size").should == 1
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue