mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
Remove input_stack and fix REPLFileLoader
This commit is contained in:
parent
a5d72a146f
commit
9ce0397b7a
6 changed files with 19 additions and 130 deletions
|
@ -94,13 +94,6 @@ class Pry
|
|||
end
|
||||
end
|
||||
|
||||
# Get/Set the stack of input objects that a Pry instance switches
|
||||
# to when its current input object encounters EOF.
|
||||
# @return [Array] The array of input objects.
|
||||
# @example
|
||||
# Pry.config.input_stack = [StringIO.new("puts 'hello world'\nexit")]
|
||||
attr_accessor :input_stack
|
||||
|
||||
# Get the array of Procs (or single Proc) to be used for the prompts by default by
|
||||
# all Pry instances.
|
||||
# Three parameters are passed into the prompt procs, (1) the
|
||||
|
|
|
@ -59,7 +59,7 @@ class Pry
|
|||
def_delegators :@plugin_manager, :plugins, :load_plugins, :locate_plugins
|
||||
|
||||
delegate_accessors :@config, :input, :output, :commands, :prompt, :print, :exception_handler,
|
||||
:hooks, :color, :pager, :editor, :memory_size, :input_stack, :extra_sticky_locals
|
||||
:hooks, :color, :pager, :editor, :memory_size, :extra_sticky_locals
|
||||
end
|
||||
|
||||
# Load the given file in the context of `Pry.toplevel_binding`
|
||||
|
@ -255,7 +255,6 @@ class Pry
|
|||
config.exception_whitelist = DEFAULT_EXCEPTION_WHITELIST
|
||||
config.default_window_size = 5
|
||||
config.hooks = DEFAULT_HOOKS
|
||||
config.input_stack = []
|
||||
config.color = Helpers::BaseHelpers.use_ansi_codes?
|
||||
config.pager = true
|
||||
config.system = DEFAULT_SYSTEM
|
||||
|
|
|
@ -27,7 +27,6 @@ class Pry
|
|||
attr_accessor :commands
|
||||
attr_accessor :print
|
||||
attr_accessor :exception_handler
|
||||
attr_accessor :input_stack
|
||||
attr_accessor :quiet
|
||||
alias :quiet? :quiet
|
||||
|
||||
|
@ -125,8 +124,6 @@ class Pry
|
|||
defaults[attribute] = Pry.send attribute
|
||||
end
|
||||
|
||||
defaults[:input_stack] = Pry.input_stack.dup
|
||||
|
||||
defaults.merge!(options).each do |key, value|
|
||||
send("#{key}=", value) if respond_to?("#{key}=")
|
||||
end
|
||||
|
|
|
@ -5,7 +5,7 @@ class Pry
|
|||
extend Forwardable
|
||||
attr_accessor :pry
|
||||
|
||||
def_delegators :pry, :input, :output, :input_stack
|
||||
def_delegators :pry, :input, :output
|
||||
|
||||
def self.start(options)
|
||||
new(Pry.new(options)).start
|
||||
|
@ -112,16 +112,12 @@ class Pry
|
|||
begin
|
||||
yield
|
||||
rescue EOFError
|
||||
if input_stack.empty?
|
||||
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."
|
||||
return :no_more_input
|
||||
end
|
||||
should_retry = false
|
||||
else
|
||||
pry.input = input_stack.pop
|
||||
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."
|
||||
return :no_more_input
|
||||
end
|
||||
should_retry = false
|
||||
|
||||
retry
|
||||
|
||||
|
|
|
@ -15,7 +15,8 @@ class Pry
|
|||
full_name = File.expand_path(file_name)
|
||||
raise RuntimeError, "No such file: #{full_name}" if !File.exists?(full_name)
|
||||
|
||||
@content = StringIO.new(File.read(full_name))
|
||||
define_additional_commands
|
||||
@content = File.read(full_name)
|
||||
end
|
||||
|
||||
# Switch to interactive mode, i.e take input from the user
|
||||
|
@ -25,19 +26,24 @@ class Pry
|
|||
_pry_.input = Pry.config.input
|
||||
_pry_.print = Pry.config.print
|
||||
_pry_.exception_handler = Pry.config.exception_handler
|
||||
Pry::REPL.new(_pry_).start
|
||||
end
|
||||
|
||||
# Switch to non-interactive mode. Essentially
|
||||
# this means there is no result output
|
||||
# and that the session becomes interactive when an exception is encountered.
|
||||
# @param [Pry] _pry_ the Pry instance to make non-interactive.
|
||||
def non_interactive_mode(_pry_)
|
||||
def non_interactive_mode(_pry_, content)
|
||||
_pry_.print = proc {}
|
||||
_pry_.exception_handler = proc do |o, e, _pry_|
|
||||
_pry_.run_command "cat --ex"
|
||||
_pry_.exception_handler = proc do |o, e, _p_|
|
||||
_p_.run_command "cat --ex"
|
||||
o.puts "...exception encountered, going interactive!"
|
||||
interactive_mode(_pry_)
|
||||
end
|
||||
|
||||
content.lines.each do |line|
|
||||
break unless _pry_.eval line, :generated => true
|
||||
end
|
||||
end
|
||||
|
||||
# Define a few extra commands useful for flipping back & forth
|
||||
|
@ -46,35 +52,18 @@ class Pry
|
|||
s = self
|
||||
|
||||
Pry::Commands.command "make-interactive", "Make the session interactive" do
|
||||
_pry_.input_stack.push _pry_.input
|
||||
s.interactive_mode(_pry_)
|
||||
end
|
||||
|
||||
Pry::Commands.command "make-non-interactive", "Make the session non-interactive" do
|
||||
_pry_.input = _pry_.input_stack.pop
|
||||
s.non_interactive_mode(_pry_)
|
||||
end
|
||||
|
||||
Pry::Commands.command "load-file", "Load another file through the repl" do |file_name|
|
||||
content = StringIO.new(File.read(File.expand_path(file_name)))
|
||||
_pry_.input_stack.push(_pry_.input)
|
||||
_pry_.input = content
|
||||
s.non_interactive_mode(_pry_, File.read(File.expand_path(file_name)))
|
||||
end
|
||||
end
|
||||
|
||||
# Actually load the file through the REPL by setting file content
|
||||
# as the REPL input stream.
|
||||
def load
|
||||
Pry.initial_session_setup
|
||||
define_additional_commands
|
||||
|
||||
Pry.config.hooks.add_hook(:when_started, :start_non_interactively) do |o, t, _pry_|
|
||||
non_interactive_mode(_pry_)
|
||||
end
|
||||
|
||||
Pry.start(Pry.toplevel_binding,
|
||||
:input => @content,
|
||||
:input_stack => [StringIO.new("exit-all\n")])
|
||||
non_interactive_mode(Pry.new, @content)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,85 +0,0 @@
|
|||
# coding: utf-8
|
||||
require 'helper'
|
||||
|
||||
describe "Pry#input_stack" do
|
||||
before do
|
||||
@str_output = StringIO.new
|
||||
end
|
||||
|
||||
it 'should accept :input_stack as a config option' do
|
||||
stack = [StringIO.new("test")]
|
||||
Pry.new(:input_stack => stack).input_stack.should == stack
|
||||
end
|
||||
|
||||
it 'should use defaults from Pry.config' do
|
||||
Pry.config.input_stack = [StringIO.new("exit")]
|
||||
Pry.new.input_stack.should == Pry.config.input_stack
|
||||
Pry.config.input_stack = []
|
||||
end
|
||||
|
||||
it 'should read from all input objects on stack and exit session' do
|
||||
stack = [b = StringIO.new(":cloister\nexit\n"), c = StringIO.new(":baron\n")]
|
||||
Object.new.pry :input => StringIO.new(":alex\n"),
|
||||
:output => @str_output,
|
||||
:input_stack => stack
|
||||
|
||||
@str_output.string.should =~ /:alex/
|
||||
@str_output.string.should =~ /:baron/
|
||||
@str_output.string.should =~ /:cloister/
|
||||
end
|
||||
|
||||
it 'input objects should be popped off stack as they are used up' do
|
||||
stack = [StringIO.new(":cloister\n"), StringIO.new(":baron\n")]
|
||||
pry = Pry.new :input => StringIO.new(":alex\n"),
|
||||
:output => @str_output,
|
||||
:input_stack => stack
|
||||
repl = Pry::REPL.new pry
|
||||
stack.size.should == 2
|
||||
repl.send(:read).should == ":alex\n"
|
||||
stack.size.should == 2
|
||||
repl.send(:read).should == ":baron\n"
|
||||
stack.size.should == 1
|
||||
repl.send(:read).should == ":cloister\n"
|
||||
stack.size.should == 0
|
||||
end
|
||||
|
||||
it 'should revert to Pry.config.input when it runs out of input objects in input_stack' do
|
||||
redirect_pry_io(StringIO.new(":rimbaud\nexit\n"), StringIO.new) do
|
||||
stack = [StringIO.new(":cloister\n"), StringIO.new(":baron\n")]
|
||||
Object.new.pry :input => StringIO.new(":alex\n"),
|
||||
:output => @str_output,
|
||||
:input_stack => stack
|
||||
|
||||
@str_output.string.should =~ /:alex/
|
||||
@str_output.string.should =~ /:baron/
|
||||
@str_output.string.should =~ /:cloister/
|
||||
@str_output.string.should =~ /:rimbaud/
|
||||
end
|
||||
end
|
||||
|
||||
it 'should display error and throw(:breakout) if at end of input after using up input_stack objects' do
|
||||
catch(:breakout) do
|
||||
redirect_pry_io(StringIO.new(":rimbaud\n"), @str_output) do
|
||||
Object.new.pry :input_stack => [StringIO.new(":a\n"), StringIO.new(":b\n")]
|
||||
end
|
||||
end
|
||||
@str_output.string.should =~ /Error: Pry ran out of things to read/
|
||||
end
|
||||
|
||||
if "".respond_to?(:encoding)
|
||||
after do
|
||||
Pry.line_buffer = [""]
|
||||
Pry.current_line = 1
|
||||
end
|
||||
it "should pass strings to Pry in the right encoding" do
|
||||
input1 = "'f。。'.encoding.name" # utf-8, see coding declaration
|
||||
input2 = input1.encode('Shift_JIS')
|
||||
|
||||
mock_pry(input1, input2).should == %{=> "UTF-8"\n=> "Shift_JIS"\n}
|
||||
end
|
||||
|
||||
it "should be able to use unicode regexes on a UTF-8 terminal" do
|
||||
mock_pry('":-Þ" =~ /þ/i').should == %{=> 2\n}
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue