diff options
| author | Ryan Fitzgerald <rwfitzge@gmail.com> | 2013-01-05 19:39:36 -0800 |
|---|---|---|
| committer | Ryan Fitzgerald <rwfitzge@gmail.com> | 2013-08-25 17:00:42 -0700 |
| commit | c873c0bda25b75ac0e67d74109b08e665a2e199e (patch) | |
| tree | 3ae484f5e39635b494e0c9c6346c88210877c8e4 | |
| parent | 7b2d30faddfb1dfed7f6b86b0aa0043aede4ee13 (diff) | |
| download | pry-c873c0bda25b75ac0e67d74109b08e665a2e199e.tar.gz | |
Split EOF handling up from other error handling
| -rw-r--r-- | lib/pry/repl.rb | 98 |
1 files changed, 55 insertions, 43 deletions
diff --git a/lib/pry/repl.rb b/lib/pry/repl.rb index fa1caf04..84364361 100644 --- a/lib/pry/repl.rb +++ b/lib/pry/repl.rb @@ -118,31 +118,54 @@ class Pry indented_val end - # Manage switching of input objects on encountering `EOFError`s. + # Returns the next line of input to be sent to the {Pry} instance. + # @param [String] current_prompt The prompt to use for input. + # @return [String?] The next line of input, or `nil` on <Ctrl-D>. + def read_line(current_prompt) + handle_read_errors do + handle_eof do + if defined? Coolline and input.is_a? Coolline + input.completion_proc = proc do |cool| + completions = @pry.complete cool.completed_word + completions.compact + end + elsif input.respond_to? :completion_proc= + input.completion_proc = proc do |input| + @pry.complete input + end + end + + if input == Readline + if !$stdout.tty? && $stdin.tty? && !Pry::Helpers::BaseHelpers.windows? + Readline.output = File.open('/dev/tty', 'w') + end + input_readline(current_prompt, false) # false since we'll add it manually + elsif defined? Coolline and input.is_a? Coolline + input_readline(current_prompt) + else + if input.method(:readline).arity == 1 + input_readline(current_prompt) + else + input_readline + end + end + end + end + end + + # Deal with any random errors that happen while trying to get user input. # @return [Object] Whatever the given block returns. # @return [:no_more_input] Indicates that no more input can be read. def handle_read_errors - should_retry = true exception_count = 0 - begin - yield - rescue EOFError - 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 - # 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. + begin + yield rescue Interrupt - return :control_c - + raise # 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. @@ -163,35 +186,24 @@ class Pry end end - # Returns the next line of input to be sent to the {Pry} instance. - # @param [String] current_prompt The prompt to use for input. - # @return [String?] The next line of input, or `nil` on <Ctrl-D>. - def read_line(current_prompt) - handle_read_errors do - if defined? Coolline and input.is_a? Coolline - input.completion_proc = proc do |cool| - completions = @pry.complete cool.completed_word - completions.compact - end - elsif input.respond_to? :completion_proc= - input.completion_proc = proc do |input| - @pry.complete input - end - end + # 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. + def handle_eof + should_retry = true - if input == Readline - if !$stdout.tty? && $stdin.tty? && !Pry::Helpers::BaseHelpers.windows? - Readline.output = File.open('/dev/tty', 'w') - end - input_readline(current_prompt, false) # false since we'll add it manually - elsif defined? Coolline and input.is_a? Coolline - input_readline(current_prompt) + begin + yield + rescue EOFError + pry.input = Pry.config.input + + if should_retry + should_retry = false + retry else - if input.method(:readline).arity == 1 - input_readline(current_prompt) - else - input_readline - end + output.puts "Error: Pry ran out of things to read from! " \ + "Attempting to break out of REPL." + return :no_more_input end end end |
