2011-01-12 01:16:04 +00:00
|
|
|
direc = File.dirname(__FILE__)
|
|
|
|
require "#{direc}/command_base"
|
2011-01-11 00:01:48 +00:00
|
|
|
|
2011-01-12 01:16:04 +00:00
|
|
|
class Pry
|
2011-01-18 03:40:28 +00:00
|
|
|
|
2010-12-30 15:01:11 +00:00
|
|
|
# Default commands used by Pry.
|
2011-01-12 01:16:04 +00:00
|
|
|
class Commands < CommandBase
|
2010-12-26 13:39:46 +00:00
|
|
|
|
2011-01-17 14:38:09 +00:00
|
|
|
command "!", "Refresh the REPL" do
|
2011-01-20 14:41:41 +00:00
|
|
|
output.puts "Refreshed REPL"
|
2011-01-17 14:38:09 +00:00
|
|
|
opts[:eval_string].clear
|
2011-01-12 01:16:04 +00:00
|
|
|
end
|
|
|
|
|
2011-01-17 14:38:09 +00:00
|
|
|
command "!pry", "Start a Pry session on current self; this even works mid-expression." do
|
2011-01-20 14:41:41 +00:00
|
|
|
Pry.start(target)
|
2011-01-12 01:16:04 +00:00
|
|
|
end
|
2011-01-10 13:54:17 +00:00
|
|
|
|
2011-01-17 14:38:09 +00:00
|
|
|
command ["exit_program", "quit_program"], "End the current program." do
|
|
|
|
exit
|
2011-01-12 01:16:04 +00:00
|
|
|
end
|
|
|
|
|
2011-01-17 14:38:09 +00:00
|
|
|
command "nesting", "Show nesting information." do
|
2011-01-20 14:41:41 +00:00
|
|
|
out = output
|
2011-01-17 14:38:09 +00:00
|
|
|
nesting = opts[:nesting]
|
|
|
|
|
|
|
|
out.puts "Nesting status:"
|
|
|
|
out.puts "--"
|
|
|
|
nesting.each do |level, obj|
|
|
|
|
if level == 0
|
|
|
|
out.puts "#{level}. #{Pry.view(obj)} (Pry top level)"
|
|
|
|
else
|
|
|
|
out.puts "#{level}. #{Pry.view(obj)}"
|
2010-12-26 13:39:46 +00:00
|
|
|
end
|
2011-01-12 01:16:04 +00:00
|
|
|
end
|
2010-12-26 13:39:46 +00:00
|
|
|
end
|
|
|
|
|
2011-01-17 14:38:09 +00:00
|
|
|
command "status", "Show status information." do
|
2011-01-20 14:41:41 +00:00
|
|
|
out = output
|
2011-01-17 14:38:09 +00:00
|
|
|
nesting = opts[:nesting]
|
|
|
|
|
|
|
|
out.puts "Status:"
|
|
|
|
out.puts "--"
|
|
|
|
out.puts "Receiver: #{Pry.view(target.eval('self'))}"
|
|
|
|
out.puts "Nesting level: #{nesting.level}"
|
|
|
|
out.puts "Local variables: #{Pry.view(target.eval('local_variables'))}"
|
|
|
|
out.puts "Pry instance: #{Pry.active_instance}"
|
|
|
|
out.puts "Last result: #{Pry.view(Pry.last_result)}"
|
2011-01-12 01:16:04 +00:00
|
|
|
end
|
|
|
|
|
2011-01-17 14:38:09 +00:00
|
|
|
command "exit_all", "End all nested Pry sessions." do
|
|
|
|
throw(:breakout, 0)
|
2011-01-12 01:16:04 +00:00
|
|
|
end
|
|
|
|
|
2011-01-17 14:38:09 +00:00
|
|
|
command "ls", "Show the list of vars in the current scope." do
|
2011-01-20 14:41:41 +00:00
|
|
|
output.puts "#{Pry.view(target.eval('local_variables + instance_variables'))}"
|
2011-01-12 01:16:04 +00:00
|
|
|
end
|
|
|
|
|
2011-01-17 14:38:09 +00:00
|
|
|
command "cat", "Show output of <var>.inspect." do |obj|
|
2011-01-20 14:41:41 +00:00
|
|
|
out = output
|
|
|
|
out.puts target.eval("#{obj}.inspect")
|
2011-01-12 01:16:04 +00:00
|
|
|
end
|
|
|
|
|
2011-01-17 14:38:09 +00:00
|
|
|
command "cd", "Start a Pry session on <var> (use `cd ..` to go back)" do |obj|
|
|
|
|
throw(:breakout, opts[:nesting].level) if obj == ".."
|
2011-01-20 14:41:41 +00:00
|
|
|
target.eval("#{obj}.pry")
|
2011-01-12 01:16:04 +00:00
|
|
|
end
|
|
|
|
|
2011-01-17 14:38:09 +00:00
|
|
|
command "show_doc", "Show the comments above <methname>" do |meth_name|
|
2011-01-20 14:41:41 +00:00
|
|
|
doc = target.eval("method(:#{meth_name})").comment
|
|
|
|
output.puts doc
|
2011-01-12 01:16:04 +00:00
|
|
|
end
|
|
|
|
|
2011-01-17 14:38:09 +00:00
|
|
|
command "show_idoc", "Show the comments above instance method <methname>" do |meth_name|
|
2011-01-20 14:41:41 +00:00
|
|
|
doc = target.eval("instance_method(:#{meth_name})").comment
|
|
|
|
output.puts doc
|
2010-12-26 13:39:46 +00:00
|
|
|
end
|
2010-12-24 08:30:51 +00:00
|
|
|
|
2011-01-17 14:38:09 +00:00
|
|
|
command "show_method", "Show sourcecode for method <methname>." do |meth_name|
|
2011-01-27 09:37:43 +00:00
|
|
|
context_meth_name = target.eval("__method__")
|
|
|
|
meth_name = context_meth_name if !meth_name
|
|
|
|
|
|
|
|
# fragile as it hard-codes in the __binding_impl__ method name
|
|
|
|
# from core_extensions.rb
|
|
|
|
if meth_name && meth_name != :__binding_impl__
|
|
|
|
code = target.eval("method(\"#{meth_name.to_s}\")").source
|
|
|
|
output.puts code
|
|
|
|
next
|
2011-01-26 18:05:40 +00:00
|
|
|
end
|
2011-01-27 09:37:43 +00:00
|
|
|
output.puts "Error: Not in a method."
|
2011-01-12 01:16:04 +00:00
|
|
|
end
|
|
|
|
|
2011-01-17 14:38:09 +00:00
|
|
|
command "show_imethod", "Show sourcecode for instance method <methname>." do |meth_name|
|
2011-01-27 09:37:43 +00:00
|
|
|
code = target.eval("instance_method(\"#{meth_name}\")").source
|
|
|
|
output.puts code
|
2011-01-12 01:16:04 +00:00
|
|
|
end
|
|
|
|
|
2011-01-17 14:38:09 +00:00
|
|
|
command "jump_to", "Jump to a Pry session further up the stack, exiting all sessions below." do |break_level|
|
|
|
|
break_level = break_level.to_i
|
|
|
|
nesting = opts[:nesting]
|
2011-01-12 01:16:04 +00:00
|
|
|
|
2011-01-17 14:38:09 +00:00
|
|
|
case break_level
|
|
|
|
when nesting.level
|
2011-01-20 14:41:41 +00:00
|
|
|
output.puts "Already at nesting level #{nesting.level}"
|
2011-01-17 14:38:09 +00:00
|
|
|
when (0...nesting.level)
|
|
|
|
throw(:breakout, break_level + 1)
|
|
|
|
else
|
|
|
|
max_nest_level = nesting.level - 1
|
2011-01-20 14:41:41 +00:00
|
|
|
output.puts "Invalid nest level. Must be between 0 and #{max_nest_level}. Got #{break_level}."
|
2010-12-25 13:51:34 +00:00
|
|
|
end
|
2010-12-24 08:30:51 +00:00
|
|
|
end
|
2010-12-25 13:51:34 +00:00
|
|
|
|
2011-01-19 02:23:20 +00:00
|
|
|
command "ls_methods", "List all methods defined on class of receiver." do
|
2011-01-26 18:05:40 +00:00
|
|
|
output.puts "#{Pry.view(target.eval('(public_methods(false) + private_methods(false) + protected_methods(false)).sort'))}"
|
2011-01-12 01:16:04 +00:00
|
|
|
end
|
|
|
|
|
2011-01-19 02:23:20 +00:00
|
|
|
command "ls_imethods", "List all instance methods defined on class of receiver." do
|
2011-01-26 18:05:40 +00:00
|
|
|
output.puts "#{Pry.view(target.eval('(public_instance_methods(false) + private_instance_methods(false) + protected_instance_methods(false)).sort'))}"
|
2011-01-12 01:16:04 +00:00
|
|
|
end
|
|
|
|
|
2011-01-17 14:38:09 +00:00
|
|
|
command ["exit", "quit", "back"], "End the current Pry session." do
|
|
|
|
throw(:breakout, opts[:nesting].level)
|
2010-12-26 13:39:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|