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'
* 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 <var>` starts a `Pry` session on the variable <var>. E.g `cd @x`
* `show_method <methname>` Displays the sourcecode for the method
<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`
* `method_doc <methname>` Displays comments for `<methname>`
* `instance_method_doc <methname>` Displays comments for instance
* `show_doc <methname>` Displays comments for `<methname>`
* `show_idoc <methname>` Displays comments for instance
method `<methname>`
* `exit_program` or `quit_program` will end the currently running
program.

View File

@ -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

View File

@ -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 <var> Show output of <var>.inspect"
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>"
puts "show_instance_method <methname> Show the sourcecode for the instance method <method_name>"
puts "method_doc <methname> Show the comments above <methname>"
puts "instance_method_doc <methname> Show the comments above instance method <methname>"
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 <level> 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 <var> Show output of <var>.inspect"
out.puts "cd <var> Start a Pry session on <var> (use `cd ..` to go back)"
out.puts "show_method <methname> Show the sourcecode for the method <methname>"
out.puts "show_imethod <methname> Show the sourcecode for the instance method <method_name>"
out.puts "show_doc <methname> Show the comments above <methname>"
out.puts "show_idoc <methname> Show the comments above instance method <methname>"
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 <level> 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