Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix 'rake pry' and refactor Pry.load_file_through_repl()

  • Loading branch information...
commit f21fc706054a4cb878d70ad45b0a0f80ed1d19e4 1 parent 5396811
@banister banister authored
Showing with 20 additions and 9 deletions.
  1. +1 −1  lib/pry/cli.rb
  2. +19 −8 lib/pry/pry_class.rb
View
2  lib/pry/cli.rb
@@ -147,7 +147,7 @@ def parse_options(args=ARGV.dup)
# create the actual context
context = Pry.binding_for(eval(opts[:context]))
- if Pry::CLI.input_args.any?
+ if Pry::CLI.input_args.any? && Pry::CLI.input_args != ["pry"]
full_name = File.expand_path(Pry::CLI.input_args.first)
Pry.load_file_through_repl(full_name)
exit
View
27 lib/pry/pry_class.rb
@@ -145,6 +145,7 @@ def self.start(target=toplevel_binding, options={})
pry_instance.repl(head)
end
+ # @return [Array<String>] return rc files with expanded path.
def self.rc_files
RC_FILES.collect do |file_name|
full_name = File.expand_path(file_name)
@@ -152,6 +153,8 @@ def self.rc_files
end.uniq.compact
end
+ # Execute the file through the REPL loop, non-interactively.
+ # @param [String] file_name File name to load through the REPL.
def self.load_file_through_repl(file_name)
full_name = File.expand_path(file_name)
raise RuntimeError, "No such file: #{full_name}" if !File.exists?(full_name)
@@ -164,23 +167,34 @@ def self.load_file_through_repl(file_name)
original_print = Pry.config.print
original_exception_handler = Pry.config.exception_handler
+
restore_originals = proc do |_pry_|
_pry_.input = original_input
_pry_.print = original_print
_pry_.exception_handler = original_exception_handler
end
+ non_interactive_mode = proc do |_pry_|
+ _pry_.print = proc {}
+ _pry_.exception_handler = (proc do |o, e, _pry_|
+ _pry_.run_command "cat --ex"
+ o.puts "...exception encountered, going interactive!"
+ restore_originals.call(_pry_)
+ end)
+ end
+
Pry::Commands.command "make-interactive", "Make the session interactive" do
self._pry_.input_stack.push self._pry_.input
- self._pry_.input = original_input
+ restore_originals.call(_pry_)
end
Pry::Commands.command "make-non-interactive", "Make the session non-interactive" do
self._pry_.input = _pry_.input_stack.pop
+ non_interactive_mode.call(_pry_)
end
Pry::Commands.command "load-file", "Load another file through the repl" do |file_name|
- file_input = StringIO.new(File.expand_path(file_name))
+ file_input = StringIO.new(File.read(File.expand_path(file_name)))
_pry_.input_stack.push(_pry_.input)
_pry_.input = file_input
@@ -189,12 +203,9 @@ def self.load_file_through_repl(file_name)
Pry.start(Pry.toplevel_binding,
:input => content,
:input_stack => [StringIO.new("exit-all\n")],
- :print => proc {},
- :exception_handler => (proc do |o, e, _pry_|
- _pry_.run_command "cat --ex"
- o.puts "...exception encountered, going interactive!"
- restore_originals.call(_pry_)
- end))
+ :when_started => proc do |o, t, _pry_|
+ non_interactive_mode.call(_pry_)
+ end)
end
# An inspector that clips the output to `max_length` chars.
Please sign in to comment.
Something went wrong with that request. Please try again.