2010-12-24 03:30:51 -05:00
|
|
|
class Pry
|
2010-12-26 08:39:46 -05:00
|
|
|
class Commands
|
|
|
|
attr_accessor :out
|
|
|
|
|
|
|
|
def initialize(out)
|
|
|
|
@out = out
|
|
|
|
end
|
|
|
|
|
|
|
|
def commands
|
|
|
|
@commands ||= {
|
|
|
|
"!" => proc do |opts|
|
|
|
|
out.puts "Refreshed REPL"
|
|
|
|
opts[:eval_string].clear
|
|
|
|
end,
|
|
|
|
["exit_program", "quit_program"] => proc do
|
|
|
|
exit
|
|
|
|
end,
|
2010-12-27 05:56:55 -05:00
|
|
|
/^help\s*(.+)?/ => proc do |opts|
|
|
|
|
param = opts[:captures].first
|
|
|
|
self.show_help(param)
|
2010-12-26 08:39:46 -05:00
|
|
|
opts[:eval_string].clear
|
|
|
|
end,
|
|
|
|
"nesting" => proc do |opts|
|
|
|
|
self.show_nesting(opts[:nesting])
|
|
|
|
opts[:eval_string].clear
|
|
|
|
end,
|
|
|
|
"status" => proc do |opts|
|
|
|
|
self.show_status(opts[:nesting], opts[:target])
|
|
|
|
opts[:eval_string].clear
|
|
|
|
end,
|
|
|
|
"exit_all" => proc do
|
|
|
|
throw(:breakout, 0)
|
|
|
|
end,
|
2010-12-27 06:45:47 -05:00
|
|
|
["exit", "quit", "back", /cd\s*\.\./] => proc do |opts|
|
2010-12-26 08:39:46 -05:00
|
|
|
throw(:breakout, opts[:nesting].level)
|
|
|
|
end,
|
|
|
|
"ls" => proc do |opts|
|
|
|
|
out.puts "#{opts[:target].eval('Pry.view(local_variables + instance_variables)')}"
|
|
|
|
opts[:eval_string].clear
|
|
|
|
end,
|
|
|
|
/^cat\s+(.+)/ => proc do |opts|
|
|
|
|
obj = opts[:captures].first
|
|
|
|
out.puts opts[:target].eval("#{obj}.inspect")
|
|
|
|
opts[:eval_string].clear
|
|
|
|
end,
|
|
|
|
/^cd\s+(.+)/ => proc do |opts|
|
|
|
|
obj = opts[:captures].first
|
|
|
|
opts[:target].eval("#{obj}.pry")
|
|
|
|
opts[:eval_string].clear
|
|
|
|
end,
|
|
|
|
/^show_doc\s*(.+)/ => proc do |opts|
|
|
|
|
meth_name = opts[:captures].first
|
|
|
|
doc = opts[:target].eval("method(:#{meth_name})").comment
|
|
|
|
out.puts doc
|
|
|
|
opts[:eval_string].clear
|
|
|
|
end,
|
|
|
|
/^show_idoc\s*(.+)/ => proc do |opts|
|
|
|
|
meth_name = opts[:captures].first
|
|
|
|
doc = opts[:target].eval("instance_method(:#{meth_name})").comment
|
|
|
|
opts[:eval_string].clear
|
|
|
|
end,
|
|
|
|
/^show_method\s*(.+)/ => proc do |opts|
|
|
|
|
meth_name = opts[:captures].first
|
|
|
|
code = opts[:target].eval("method(:#{meth_name})").source
|
|
|
|
out.puts code
|
|
|
|
opts[:eval_string].clear
|
|
|
|
end,
|
|
|
|
/^show_imethod\s*(.+)/ => proc do |opts|
|
|
|
|
meth_name = opts[:captures].first
|
|
|
|
code = opts[:target].eval("instance_method(:#{meth_name})").source
|
|
|
|
opts[:eval_string].clear
|
|
|
|
end,
|
|
|
|
/^jump_to\s*(\d*)/ => proc do |opts|
|
|
|
|
break_level = opts[:captures].first.to_i
|
|
|
|
nesting = opts[:nesting]
|
|
|
|
|
|
|
|
case break_level
|
|
|
|
when nesting.level
|
|
|
|
out.puts "Already at nesting level #{nesting.level}"
|
|
|
|
opts[:eval_string].clear
|
|
|
|
when (0...nesting.level)
|
|
|
|
throw(:breakout, break_level + 1)
|
|
|
|
else
|
|
|
|
max_nest_level = nesting.level - 1
|
|
|
|
out.puts "Invalid nest level. Must be between 0 and #{max_nest_level}. Got #{break_level}."
|
|
|
|
opts[:eval_string].clear
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def command_info
|
|
|
|
@command_info ||= {
|
|
|
|
"!" => "Refresh the REPL.",
|
2010-12-27 05:56:55 -05:00
|
|
|
["exit_program", "quit_program"] => "end the current program.",
|
2010-12-26 08:39:46 -05:00
|
|
|
"help" => "This menu.",
|
|
|
|
"nesting" => "Show nesting information.",
|
|
|
|
"status" => "Show status information.",
|
|
|
|
"exit_all" => "End all nested Pry sessions",
|
2010-12-27 06:45:47 -05:00
|
|
|
["exit", "quit", "back", /cd\s*\.\./] => "End the current Pry session.",
|
2010-12-26 08:39:46 -05:00
|
|
|
"ls" => "Show the list of vars in the current scope.",
|
|
|
|
"cat" => "Show output of <var>.inspect",
|
|
|
|
"cd" => "Start a Pry session on <var> (use `cd ..` to go back)",
|
|
|
|
"show_doc" => "Show the comments above <methname>",
|
|
|
|
"show_idoc" => "Show the comments above instance method <methname>",
|
|
|
|
"show_method" => "Show sourcecode for method <methname>",
|
|
|
|
"show_imethod" => "Show sourcecode for instance method <methname>",
|
|
|
|
"jump_to" => "Jump to a Pry session further up the stack, exiting all sessions below."
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-12-27 05:56:55 -05:00
|
|
|
def show_help(param)
|
|
|
|
if !param
|
|
|
|
out.puts "Command list:"
|
|
|
|
out.puts "--"
|
|
|
|
command_info.each do |k, v|
|
2010-12-27 06:45:47 -05:00
|
|
|
puts "#{Array(k).first}".ljust(18) + v
|
2010-12-27 05:56:55 -05:00
|
|
|
end
|
|
|
|
else
|
2010-12-27 06:45:47 -05:00
|
|
|
key = command_info.keys.find { |v| Array(v).any? { |k| k === param } }
|
2010-12-27 05:56:55 -05:00
|
|
|
if key
|
|
|
|
out.puts command_info[key]
|
|
|
|
else
|
|
|
|
out.puts "No info for command: #{param}"
|
|
|
|
end
|
|
|
|
end
|
2010-12-26 08:39:46 -05:00
|
|
|
end
|
2010-12-24 03:30:51 -05:00
|
|
|
|
2010-12-26 08:39:46 -05:00
|
|
|
def show_nesting(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)}"
|
|
|
|
end
|
2010-12-25 08:51:34 -05:00
|
|
|
end
|
2010-12-24 03:30:51 -05:00
|
|
|
end
|
2010-12-25 08:51:34 -05:00
|
|
|
|
2010-12-26 08:39:46 -05:00
|
|
|
def show_status(nesting, target)
|
|
|
|
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)')}"
|
2010-12-27 06:45:47 -05:00
|
|
|
out.puts "Pry instance: #{Pry.active_instance}"
|
2010-12-26 08:39:46 -05:00
|
|
|
out.puts "Last result: #{Pry.view(Pry.last_result)}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|