From 3e137e8b1ba9ff9c2c80420fb383ef3dcff68882 Mon Sep 17 00:00:00 2001 From: John Mair Date: Tue, 30 Aug 2011 02:21:51 +1200 Subject: [PATCH] added Pry.config.control_d_handler and also changed way control d (^D) works inside multi-line expressions (it clears input buffer) --- CHANGELOG | 3 +++ lib/pry.rb | 20 +++++++++++++++++++- lib/pry/config.rb | 4 ++++ lib/pry/pry_class.rb | 2 ++ lib/pry/pry_instance.rb | 17 +++++------------ 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index e8a8bbfe..0f829cd3 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -12,6 +12,9 @@ * changed `exit` command so that it now called Kernel#exit (after saving history) * 'quit' now behaves like 'exit-all' (and aliased to exit-all) - it breaks out of the repl loop and sets empties the binding_stack * 'cd ..' just pops a binding off the binding_stack with special behaviour when only one binding in stack - it breaks out of the repl loop +* show-method and show-doc now accept multiple method names +* control_d hook added (Pry.config.control_d_handler) +* behaviour of ^d is now to break out of current expr if in multi-line expr, or break out of current context if nested, or break out of pry repl loop if at top-level */7/2011 version 0.9.3 * cat --ex (cats 5 lines above and below line in file where exception was raised) diff --git a/lib/pry.rb b/lib/pry.rb index 5fd8618c..2fcfd83e 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -58,7 +58,25 @@ class Pry "pry(#{Pry.view_clip(target_self)}):#{Pry.view_clip(nest_level)}* " end } - ] + ] + # Deal with the ^D key being pressed, different behaviour in + # different cases: + # 1) In an expression - behave like `!` command (clear input buffer) + # 2) At top-level session - behave like `exit command (break out of repl loop) + # 3) In a nested session - behave like `cd ..` (pop a binding) + DEFAULT_CONTROL_D_HANDLER = proc do |eval_string, _pry_| + if !eval_string.empty? + # clear input buffer + eval_string.replace("") + elsif _pry_.binding_stack.one? + # ^D at top-level breaks out of loop + _pry_.binding_stack.clear + throw(:breakout) + else + # otherwise just pops a binding + _pry_.binding_stack.pop + end + end # A simple prompt - doesn't display target or nesting level SIMPLE_PROMPT = [proc { ">> " }, proc { " | " }] diff --git a/lib/pry/config.rb b/lib/pry/config.rb index c93b3964..0c39f46b 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -107,6 +107,10 @@ class Pry # @return [Integer] Amount of results that will be stored into out attr_accessor :memory_size + + # @return [Proc] The proc that manages ^D presses in the REPL. + # The proc is passed the current eval_string and the current pry instance. + attr_accessor :control_d_handler end end diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index 421601e2..16d23877 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -236,6 +236,8 @@ class Pry config.history.should_load = true config.history.file = File.expand_path("~/.pry_history") + config.control_d_handler = DEFAULT_CONTROL_D_HANDLER + config.memory_size = 100 end diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index 984e0170..def6a5af 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -257,19 +257,12 @@ class Pry # exit session if we receive EOF character (^D) if !val - output.puts - if binding_stack.one? - # ^D at top-level breaks out of loop - binding_stack.clear - throw(:breakout) - else - # otherwise just pops a binding - binding_stack.pop - val = "" - end + output.puts "" + Pry.config.control_d_handler.call(eval_string, self) + "" + else + val end - - val end # Process the line received.