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.

This commit is contained in:
John Mair 2010-12-19 01:54:16 +13:00
parent d2009e6de1
commit ec8f9ec71f
3 changed files with 51 additions and 45 deletions

View File

@ -144,7 +144,7 @@ end.
* Pry sessions can nest arbitrarily deeply -- to go back one level of nesting type 'exit' or 'quit' or 'back' * Pry sessions can nest arbitrarily deeply -- to go back one level of nesting type 'exit' or 'quit' or 'back'
* Use `_` to recover last result. * Use `_` to recover last result.
* Pry has multi-line support built in. * 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` `jump_to`, `ls`, `cd`, `cat`
* Pry gives good control over nested sessions (important when exploring complicated runtime state) * Pry gives good control over nested sessions (important when exploring complicated runtime state)
* Pry is not based on the IRB codebase. * 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 <var>` starts a `Pry` session on the variable <var>. E.g `cd @x` * `cd <var>` starts a `Pry` session on the variable <var>. E.g `cd @x`
* `show_method <methname>` Displays the sourcecode for the method * `show_method <methname>` Displays the sourcecode for the method
<methname>. E.g `show_method hello` <methname>. E.g `show_method hello`
* `show_instance_method <methname>` Displays the sourcecode for the * `show_imethod <methname>` Displays the sourcecode for the
instance method <methname>. E.g `show_instance_method goodbye` instance method <methname>. E.g `show_instance_method goodbye`
* `method_doc <methname>` Displays comments for `<methname>` * `show_doc <methname>` Displays comments for `<methname>`
* `instance_method_doc <methname>` Displays comments for instance * `show_idoc <methname>` Displays comments for instance
method `<methname>` method `<methname>`
* `exit_program` or `quit_program` will end the currently running * `exit_program` or `quit_program` will end the currently running
program. program.

View File

@ -165,12 +165,12 @@ class Pry
obj = $~.captures.first obj = $~.captures.first
target.eval("#{obj}.pry") target.eval("#{obj}.pry")
eval_string.clear eval_string.clear
when /^method_doc\s*(.+)/ when /^show_doc\s*(.+)/
meth_name = ($~.captures).first meth_name = ($~.captures).first
doc = target.eval("method(:#{meth_name})").comment doc = target.eval("method(:#{meth_name})").comment
output.show_doc doc output.show_doc doc
eval_string.clear eval_string.clear
when /^instance_method_doc\s*(.+)/ when /^show_idoc\s*(.+)/
meth_name = ($~.captures).first meth_name = ($~.captures).first
doc = target.eval("instance_method(:#{meth_name})").comment doc = target.eval("instance_method(:#{meth_name})").comment
output.show_doc doc output.show_doc doc
@ -180,7 +180,7 @@ class Pry
code = target.eval("method(:#{meth_name})").source code = target.eval("method(:#{meth_name})").source
output.show_method code output.show_method code
eval_string.clear eval_string.clear
when /^show_instance_method\s*(.+)/ when /^show_instance_method\s*(.+)/, /^show_imethod\s*(.+)/
meth_name = ($~.captures).first meth_name = ($~.captures).first
code = target.eval("instance_method(:#{meth_name})").source code = target.eval("instance_method(:#{meth_name})").source
output.show_method code output.show_method code

View File

@ -1,90 +1,96 @@
class Pry class Pry
class Output class Output
attr_reader :out
def initialize(out=STDOUT)
@out = out
end
def refresh def refresh
puts "Refreshed REPL" out.puts "Refreshed REPL"
end end
def session_start(obj) def session_start(obj)
puts "Beginning Pry session for #{Pry.view(obj)}" out.puts "Beginning Pry session for #{Pry.view(obj)}"
end end
def session_end(obj) def session_end(obj)
puts "Ending Pry session for #{Pry.view(obj)}" out.puts "Ending Pry session for #{Pry.view(obj)}"
end end
# the print component of READ-EVAL-PRINT-LOOP # the print component of READ-EVAL-PRINT-LOOP
def print(value) def print(value)
case value case value
when Exception when Exception
puts "#{value.class}: #{value.message}" out.puts "#{value.class}: #{value.message}"
else else
puts "=> #{Pry.view(value)}" out.puts "=> #{Pry.view(value)}"
end end
end end
def show_help def show_help
puts "Command list:" out.puts "Command list:"
puts "--" out.puts "--"
puts "help This menu" out.puts "help This menu"
puts "status Show status information" out.puts "status Show status information"
puts "! Refresh the REPL" out.puts "! Refresh the REPL"
puts "nesting Show nesting information" out.puts "nesting Show nesting information"
puts "ls Show the list of variables in the current scope" out.puts "ls Show the list of variables in the current scope"
puts "cat <var> Show output of <var>.inspect" out.puts "cat <var> Show output of <var>.inspect"
puts "cd <var> Start a Pry session on <var> (use `cd ..` to go back)" out.puts "cd <var> Start a Pry session on <var> (use `cd ..` to go back)"
puts "show_method <methname> Show the sourcecode for the method <methname>" out.puts "show_method <methname> Show the sourcecode for the method <methname>"
puts "show_instance_method <methname> Show the sourcecode for the instance method <method_name>" out.puts "show_imethod <methname> Show the sourcecode for the instance method <method_name>"
puts "method_doc <methname> Show the comments above <methname>" out.puts "show_doc <methname> Show the comments above <methname>"
puts "instance_method_doc <methname> Show the comments above instance method <methname>" out.puts "show_idoc <methname> Show the comments above instance method <methname>"
puts "exit/quit/back End the current Pry session" out.puts "exit/quit/back End the current Pry session"
puts "exit_all End all nested Pry sessions" out.puts "exit_all End all nested Pry sessions"
puts "exit_program/quit_program End the current program" out.puts "exit_program/quit_program End the current program"
puts "jump_to <level> Jump to a Pry session further up the stack, exiting all sessions below" out.puts "jump_to <level> Jump to a Pry session further up the stack, exiting all sessions below"
end end
def show_nesting(nesting) def show_nesting(nesting)
puts "Nesting status:" out.puts "Nesting status:"
puts "--" out.puts "--"
nesting.each do |level, obj| nesting.each do |level, obj|
if level == 0 if level == 0
puts "#{level}. #{Pry.view(obj)} (Pry top level)" out.puts "#{level}. #{Pry.view(obj)} (Pry top level)"
else else
puts "#{level}. #{Pry.view(obj)}" out.puts "#{level}. #{Pry.view(obj)}"
end end
end end
end end
def show_status(nesting, target) def show_status(nesting, target)
puts "Status:" out.puts "Status:"
puts "--" out.puts "--"
puts "Receiver: #{Pry.view(target.eval('self'))}" out.puts "Receiver: #{Pry.view(target.eval('self'))}"
puts "Nesting level: #{nesting.level}" out.puts "Nesting level: #{nesting.level}"
puts "Local variables: #{target.eval('Pry.view(local_variables)')}" out.puts "Local variables: #{target.eval('Pry.view(local_variables)')}"
puts "Last result: #{Pry.view(Pry.last_result)}" out.puts "Last result: #{Pry.view(Pry.last_result)}"
end end
def ls(target) def ls(target)
puts "#{target.eval('Pry.view(local_variables + instance_variables)')}" out.puts "#{target.eval('Pry.view(local_variables + instance_variables)')}"
end end
def cat(target, var) def cat(target, var)
puts target.eval("#{var}.inspect") out.puts target.eval("#{var}.inspect")
end end
def show_method(code) def show_method(code)
code.display out.puts code
end end
def show_doc(doc) def show_doc(doc)
doc.display out.puts doc
end end
def warn_already_at_level(nesting_level) def warn_already_at_level(nesting_level)
puts "Already at nesting level #{nesting_level}" out.puts "Already at nesting level #{nesting_level}"
end end
def err_invalid_nest_level(nest_level, max_nest_level) 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 end
def exit() end def exit() end