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
|
2011-02-14 09:48:19 +00:00
|
|
|
|
|
|
|
# We make this a lambda to avoid documenting it
|
|
|
|
meth_name_from_binding = lambda do |b|
|
|
|
|
meth_name = b.eval('__method__')
|
|
|
|
if [nil, :__binding__, :__binding_impl__].include?(meth_name)
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
meth_name
|
|
|
|
end
|
|
|
|
end
|
2010-12-26 13:39:46 +00:00
|
|
|
|
2011-02-13 15:49:53 +00:00
|
|
|
command "!", "Clear the input buffer. Useful if the parsing process goes wrong." do
|
|
|
|
output.puts "Input buffer cleared!"
|
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
|
2011-02-13 15:49:53 +00:00
|
|
|
out.puts "#{level}. #{Pry.view_clip(obj)} (Pry top level)"
|
2011-01-17 14:38:09 +00:00
|
|
|
else
|
2011-02-13 15:49:53 +00:00
|
|
|
out.puts "#{level}. #{Pry.view_clip(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 "--"
|
2011-02-13 15:49:53 +00:00
|
|
|
out.puts "Receiver: #{Pry.view_clip(target.eval('self'))}"
|
2011-01-17 14:38:09 +00:00
|
|
|
out.puts "Nesting level: #{nesting.level}"
|
2011-02-14 09:48:19 +00:00
|
|
|
out.puts "Pry version: #{Pry::VERSION}"
|
|
|
|
out.puts "Ruby version: #{RUBY_VERSION}"
|
|
|
|
|
|
|
|
mn = meth_name_from_binding.call(target)
|
|
|
|
out.puts "Current method: #{mn ? mn : "N/A"}"
|
2011-01-17 14:38:09 +00:00
|
|
|
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-02-14 09:48:19 +00:00
|
|
|
command "version", "Show Pry version." do
|
|
|
|
output.puts "Pry version: #{Pry::VERSION}"
|
|
|
|
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-02-13 15:49:53 +00:00
|
|
|
command "ls", "Show the list of vars in the current scope. Use -c to include constants and -g to include globals." do |*args|
|
|
|
|
params = []
|
|
|
|
args.each do |v|
|
|
|
|
if v[0].chr == "-"
|
|
|
|
params += v[1..-1].split("")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-02-02 10:43:21 +00:00
|
|
|
target_self = target.eval('self')
|
|
|
|
|
2011-02-13 15:49:53 +00:00
|
|
|
extras = []
|
|
|
|
extras += target.eval("global_variables") if params.include?("g")
|
|
|
|
|
2011-02-02 10:43:21 +00:00
|
|
|
case target_self
|
|
|
|
when Module
|
2011-02-13 15:49:53 +00:00
|
|
|
extras += target.eval("constants") if params.include?("c")
|
|
|
|
output.puts "#{Pry.view(target.eval("local_variables + instance_variables + #{extras.inspect}"))}"
|
2011-02-02 10:43:21 +00:00
|
|
|
else
|
2011-02-13 15:49:53 +00:00
|
|
|
extras += target.eval("self.class.constants") if params.include?("c")
|
|
|
|
output.puts "#{Pry.view(target.eval("local_variables + instance_variables + #{extras.inspect}"))}"
|
2011-02-02 10:43:21 +00:00
|
|
|
end
|
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-02-14 09:48:19 +00:00
|
|
|
|
|
|
|
alias_command "inspect", "cat", ""
|
2011-01-12 01:16:04 +00:00
|
|
|
|
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-02-14 09:48:19 +00:00
|
|
|
meth_name = meth_name_from_binding.call(target) if !meth_name
|
|
|
|
if meth_name
|
2011-01-27 09:37:43 +00:00
|
|
|
code = target.eval("method(\"#{meth_name.to_s}\")").source
|
|
|
|
output.puts code
|
2011-02-14 09:48:19 +00:00
|
|
|
else
|
|
|
|
output.puts "Error: Not in a method."
|
2011-01-26 18:05:40 +00:00
|
|
|
end
|
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-02-13 15:49:53 +00:00
|
|
|
command "show_command", "Show sourcecode for a Pry command, e.g: show_command ls" do |command_name|
|
|
|
|
cmds = Pry.active_instance.commands.commands
|
|
|
|
|
|
|
|
if cmds[command_name]
|
|
|
|
code = cmds[command_name][:action].source
|
|
|
|
output.puts code
|
|
|
|
else
|
|
|
|
output.puts "No such command: #{command_name}."
|
|
|
|
end
|
|
|
|
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
|