2011-05-01 10:25:37 +00:00
|
|
|
require "pry/default_commands/ls"
|
|
|
|
|
|
|
|
class Pry
|
|
|
|
module DefaultCommands
|
|
|
|
|
2011-05-07 05:32:05 +00:00
|
|
|
Context = Pry::CommandSet.new do
|
2011-05-01 10:25:37 +00:00
|
|
|
import Ls
|
|
|
|
|
|
|
|
command "cd", "Start a Pry session on VAR (use `cd ..` to go back and `cd /` to return to Pry top-level)", :keep_retval => true do |obj|
|
2011-05-08 06:21:15 +00:00
|
|
|
case obj
|
|
|
|
when nil
|
2011-05-01 10:25:37 +00:00
|
|
|
output.puts "Must provide an object."
|
|
|
|
next
|
2011-05-08 06:21:15 +00:00
|
|
|
when ".."
|
2011-08-22 02:48:50 +00:00
|
|
|
|
|
|
|
if _pry_.binding_stack.size == 1
|
|
|
|
|
|
|
|
# when breaking out of top-level then behave like `quit` command
|
|
|
|
_pry_.binding_stack.pop
|
|
|
|
throw(:breakout)
|
|
|
|
else
|
|
|
|
|
|
|
|
# otherwise just pop a binding
|
|
|
|
_pry_.binding_stack.pop.eval('self')
|
|
|
|
end
|
2011-05-08 06:21:15 +00:00
|
|
|
when "/"
|
2011-08-21 06:22:45 +00:00
|
|
|
_pry_.binding_stack = [_pry_.binding_stack.first]
|
|
|
|
nil
|
2011-05-08 06:21:15 +00:00
|
|
|
when "::"
|
2011-08-22 02:48:50 +00:00
|
|
|
_pry_.binding_stack.push TOPLEVEL_BINDING
|
|
|
|
nil
|
2011-05-08 06:21:15 +00:00
|
|
|
else
|
2011-08-21 06:22:45 +00:00
|
|
|
_pry_.binding_stack.push Pry.binding_for(target.eval(arg_string))
|
|
|
|
nil
|
2011-05-01 10:25:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-22 02:48:50 +00:00
|
|
|
command "switch-to", "Start a new sub-session on a binding in the current stack (numbered by nesting)." do |selection|
|
|
|
|
selection = selection.to_i
|
|
|
|
Pry.start(_pry_.binding_stack[selection])
|
|
|
|
end
|
|
|
|
|
2011-05-01 10:25:37 +00:00
|
|
|
command "nesting", "Show nesting information." do
|
|
|
|
nesting = opts[:nesting]
|
|
|
|
|
|
|
|
output.puts "Nesting status:"
|
|
|
|
output.puts "--"
|
2011-08-21 06:22:45 +00:00
|
|
|
_pry_.binding_stack.each_with_index do |obj, level|
|
2011-05-01 10:25:37 +00:00
|
|
|
if level == 0
|
2011-08-21 06:22:45 +00:00
|
|
|
output.puts "#{level}. #{Pry.view_clip(obj.eval('self'))} (Pry top level)"
|
2011-05-01 10:25:37 +00:00
|
|
|
else
|
2011-08-21 06:22:45 +00:00
|
|
|
output.puts "#{level}. #{Pry.view_clip(obj.eval('self'))}"
|
2011-05-01 10:25:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2011-08-22 02:48:50 +00:00
|
|
|
nesting_level = _pry_.binding_stack.size - 1
|
2011-05-01 10:25:37 +00:00
|
|
|
|
|
|
|
case break_level
|
2011-08-22 02:48:50 +00:00
|
|
|
when nesting_level
|
|
|
|
output.puts "Already at nesting level #{nesting_level}"
|
|
|
|
when (0...nesting_level)
|
|
|
|
_pry_.binding_stack.slice!(break_level + 1, _pry_.binding_stack.size)
|
|
|
|
|
2011-05-01 10:25:37 +00:00
|
|
|
else
|
2011-08-22 02:48:50 +00:00
|
|
|
max_nest_level = nesting_level - 1
|
2011-05-01 10:25:37 +00:00
|
|
|
output.puts "Invalid nest level. Must be between 0 and #{max_nest_level}. Got #{break_level}."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-21 06:22:45 +00:00
|
|
|
command "quit", "End the current Pry session. Accepts optional return value. Aliases: exit-all, !!@" do
|
2011-08-22 02:48:50 +00:00
|
|
|
|
|
|
|
# clear the binding stack
|
|
|
|
_pry_.binding_stack.replace([])
|
|
|
|
|
|
|
|
# break out of the repl loop
|
2011-08-21 06:22:45 +00:00
|
|
|
throw(:breakout, target.eval(arg_string))
|
2011-05-01 10:25:37 +00:00
|
|
|
end
|
|
|
|
|
2011-08-21 06:22:45 +00:00
|
|
|
alias_command "!!@", "quit", ""
|
|
|
|
alias_command "exit-all", "quit", ""
|
2011-05-01 10:25:37 +00:00
|
|
|
|
|
|
|
alias_command "!!@", "exit-all", ""
|
|
|
|
|
2011-08-22 02:48:50 +00:00
|
|
|
command "exit", "End the current program. Aliases: exit-program, quit-program, !!!" do
|
2011-08-07 09:30:08 +00:00
|
|
|
Pry.save_history if Pry.config.history.should_save
|
2011-05-01 10:25:37 +00:00
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
2011-08-22 02:48:50 +00:00
|
|
|
alias_command "exit-program", "exit", ""
|
|
|
|
alias_command "quit-program", "exit", ""
|
|
|
|
alias_command "!!!", "exit", ""
|
2011-05-01 10:25:37 +00:00
|
|
|
|
|
|
|
command "!pry", "Start a Pry session on current self; this even works mid-expression." do
|
2011-05-29 15:41:40 +00:00
|
|
|
target.pry
|
2011-05-01 10:25:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
command "whereami", "Show the code context for the session. (whereami <n> shows <n> extra lines of code around the invocation line. Default: 5)" do |num|
|
|
|
|
file = target.eval('__FILE__')
|
|
|
|
line_num = target.eval('__LINE__')
|
|
|
|
klass = target.eval('self.class')
|
|
|
|
|
|
|
|
if num
|
|
|
|
i_num = num.to_i
|
|
|
|
else
|
|
|
|
i_num = 5
|
|
|
|
end
|
|
|
|
|
|
|
|
meth_name = meth_name_from_binding(target)
|
|
|
|
meth_name = "N/A" if !meth_name
|
|
|
|
|
|
|
|
if file =~ /(\(.*\))|<.*>/ || file == "" || file == "-e"
|
|
|
|
output.puts "Cannot find local context. Did you use `binding.pry` ?"
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
set_file_and_dir_locals(file)
|
2011-05-07 14:26:53 +00:00
|
|
|
output.puts "\n#{text.bold('From:')} #{file} @ line #{line_num} in #{klass}##{meth_name}:\n\n"
|
2011-05-01 10:25:37 +00:00
|
|
|
|
|
|
|
# This method inspired by http://rubygems.org/gems/ir_b
|
|
|
|
File.open(file).each_with_index do |line, index|
|
|
|
|
line_n = index + 1
|
|
|
|
next unless line_n > (line_num - i_num - 1)
|
|
|
|
break if line_n > (line_num + i_num)
|
|
|
|
if line_n == line_num
|
|
|
|
code =" =>#{line_n.to_s.rjust(3)}: #{line.chomp}"
|
|
|
|
if Pry.color
|
|
|
|
code = CodeRay.scan(code, :ruby).term
|
|
|
|
end
|
|
|
|
output.puts code
|
|
|
|
code
|
|
|
|
else
|
|
|
|
code = "#{line_n.to_s.rjust(6)}: #{line.chomp}"
|
|
|
|
if Pry.color
|
|
|
|
code = CodeRay.scan(code, :ruby).term
|
|
|
|
end
|
|
|
|
output.puts code
|
|
|
|
code
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|