From ec8f9ec71f88924eab0ed8a43f921abf131e017e Mon Sep 17 00:00:00 2001 From: John Mair Date: Sun, 19 Dec 2010 01:54:16 +1300 Subject: [PATCH] Added out parameter to Output class, so can redirect standard output to any valid IO object. Renamed show_instance_method to show_imethod and method_doc to show_doc, and instance_method_doc to show_idoc, updated README. --- README.markdown | 8 ++--- lib/pry.rb | 6 ++-- lib/pry/output.rb | 82 +++++++++++++++++++++++++---------------------- 3 files changed, 51 insertions(+), 45 deletions(-) diff --git a/README.markdown b/README.markdown index c21b2609..1c288296 100644 --- a/README.markdown +++ b/README.markdown @@ -144,7 +144,7 @@ end. * Pry sessions can nest arbitrarily deeply -- to go back one level of nesting type 'exit' or 'quit' or 'back' * Use `_` to recover last result. * Pry has multi-line support built in. -* Pry has unique commands not found in any other REPL: `show_method`, `method_doc` +* Pry has unique commands not found in any other REPL: `show_method`, `show_doc` `jump_to`, `ls`, `cd`, `cat` * Pry gives good control over nested sessions (important when exploring complicated runtime state) * Pry is not based on the IRB codebase. @@ -206,10 +206,10 @@ If you want to access a method of the same name, prefix the invocation by whites * `cd ` starts a `Pry` session on the variable . E.g `cd @x` * `show_method ` Displays the sourcecode for the method . E.g `show_method hello` -* `show_instance_method ` Displays the sourcecode for the +* `show_imethod ` Displays the sourcecode for the instance method . E.g `show_instance_method goodbye` -* `method_doc ` Displays comments for `` -* `instance_method_doc ` Displays comments for instance +* `show_doc ` Displays comments for `` +* `show_idoc ` Displays comments for instance method `` * `exit_program` or `quit_program` will end the currently running program. diff --git a/lib/pry.rb b/lib/pry.rb index 72355f89..d949cf3e 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -165,12 +165,12 @@ class Pry obj = $~.captures.first target.eval("#{obj}.pry") eval_string.clear - when /^method_doc\s*(.+)/ + when /^show_doc\s*(.+)/ meth_name = ($~.captures).first doc = target.eval("method(:#{meth_name})").comment output.show_doc doc eval_string.clear - when /^instance_method_doc\s*(.+)/ + when /^show_idoc\s*(.+)/ meth_name = ($~.captures).first doc = target.eval("instance_method(:#{meth_name})").comment output.show_doc doc @@ -180,7 +180,7 @@ class Pry code = target.eval("method(:#{meth_name})").source output.show_method code eval_string.clear - when /^show_instance_method\s*(.+)/ + when /^show_instance_method\s*(.+)/, /^show_imethod\s*(.+)/ meth_name = ($~.captures).first code = target.eval("instance_method(:#{meth_name})").source output.show_method code diff --git a/lib/pry/output.rb b/lib/pry/output.rb index 2c416c88..d083e443 100644 --- a/lib/pry/output.rb +++ b/lib/pry/output.rb @@ -1,90 +1,96 @@ class Pry class Output + attr_reader :out + + def initialize(out=STDOUT) + @out = out + end + def refresh - puts "Refreshed REPL" + out.puts "Refreshed REPL" end def session_start(obj) - puts "Beginning Pry session for #{Pry.view(obj)}" + out.puts "Beginning Pry session for #{Pry.view(obj)}" end def session_end(obj) - puts "Ending Pry session for #{Pry.view(obj)}" + out.puts "Ending Pry session for #{Pry.view(obj)}" end # the print component of READ-EVAL-PRINT-LOOP def print(value) case value when Exception - puts "#{value.class}: #{value.message}" + out.puts "#{value.class}: #{value.message}" else - puts "=> #{Pry.view(value)}" + out.puts "=> #{Pry.view(value)}" end end def show_help - puts "Command list:" - puts "--" - puts "help This menu" - puts "status Show status information" - puts "! Refresh the REPL" - puts "nesting Show nesting information" - puts "ls Show the list of variables in the current scope" - puts "cat Show output of .inspect" - puts "cd Start a Pry session on (use `cd ..` to go back)" - puts "show_method Show the sourcecode for the method " - puts "show_instance_method Show the sourcecode for the instance method " - puts "method_doc Show the comments above " - puts "instance_method_doc Show the comments above instance method " - puts "exit/quit/back End the current Pry session" - puts "exit_all End all nested Pry sessions" - puts "exit_program/quit_program End the current program" - puts "jump_to Jump to a Pry session further up the stack, exiting all sessions below" + out.puts "Command list:" + out.puts "--" + out.puts "help This menu" + out.puts "status Show status information" + out.puts "! Refresh the REPL" + out.puts "nesting Show nesting information" + out.puts "ls Show the list of variables in the current scope" + out.puts "cat Show output of .inspect" + out.puts "cd Start a Pry session on (use `cd ..` to go back)" + out.puts "show_method Show the sourcecode for the method " + out.puts "show_imethod Show the sourcecode for the instance method " + out.puts "show_doc Show the comments above " + out.puts "show_idoc Show the comments above instance method " + out.puts "exit/quit/back End the current Pry session" + out.puts "exit_all End all nested Pry sessions" + out.puts "exit_program/quit_program End the current program" + out.puts "jump_to Jump to a Pry session further up the stack, exiting all sessions below" end def show_nesting(nesting) - puts "Nesting status:" - puts "--" + out.puts "Nesting status:" + out.puts "--" nesting.each do |level, obj| if level == 0 - puts "#{level}. #{Pry.view(obj)} (Pry top level)" + out.puts "#{level}. #{Pry.view(obj)} (Pry top level)" else - puts "#{level}. #{Pry.view(obj)}" + out.puts "#{level}. #{Pry.view(obj)}" end end end def show_status(nesting, target) - puts "Status:" - puts "--" - puts "Receiver: #{Pry.view(target.eval('self'))}" - puts "Nesting level: #{nesting.level}" - puts "Local variables: #{target.eval('Pry.view(local_variables)')}" - puts "Last result: #{Pry.view(Pry.last_result)}" + out.puts "Status:" + out.puts "--" + out.puts "Receiver: #{Pry.view(target.eval('self'))}" + out.puts "Nesting level: #{nesting.level}" + out.puts "Local variables: #{target.eval('Pry.view(local_variables)')}" + out.puts "Last result: #{Pry.view(Pry.last_result)}" end def ls(target) - puts "#{target.eval('Pry.view(local_variables + instance_variables)')}" + out.puts "#{target.eval('Pry.view(local_variables + instance_variables)')}" end def cat(target, var) - puts target.eval("#{var}.inspect") + out.puts target.eval("#{var}.inspect") end def show_method(code) - code.display + out.puts code end def show_doc(doc) - doc.display + out.puts doc end def warn_already_at_level(nesting_level) - puts "Already at nesting level #{nesting_level}" + out.puts "Already at nesting level #{nesting_level}" end def err_invalid_nest_level(nest_level, max_nest_level) - puts "Invalid nest level. Must be between 0 and #{max_nest_level}. Got #{nest_level}." + out.puts "Invalid nest level. Must be between 0 and #{max_nest_level}. Got #{nest_level}." end def exit() end