pry--pry/lib/pry/default_commands/cd.rb

82 lines
2.6 KiB
Ruby
Raw Normal View History

class Pry
module DefaultCommands
Cd = Pry::CommandSet.new do
create_command "cd" do
group "Context"
description "Move into a new context (object or scope)."
banner <<-BANNER
Usage: cd [OPTIONS] [--help]
Move into new context (object or scope). As in unix shells use
`cd ..` to go back, `cd /` to return to Pry top-level and `cd -`
to toggle between last two scopes).
Complex syntax (e.g `cd ../@x/y`) also supported.
e.g: `cd @x`
e.g: `cd ..`
e.g: `cd /`
e.g: `cd -`
https://github.com/pry/pry/wiki/State-navigation#wiki-Changing_scope
BANNER
def process
# Extract command arguments. Delete blank arguments like " ", but
# don't delete empty strings like "".
Change behavior of `cd -` command Since banister begged me to do that... completely rewrite `cd -` command (implemetation is much simpler now). This commit brings such changes: * completely rewrite behavior of `cd -` command; * implement ScratchPad aka Pad for unit testing purposes (by banister); * use Pad riches in the unit tests for `cd -` command; * remove verbose and clunky unit tests; This commit brings new meaning to the `cd -` command. The main difference is that the new command saves entire binding stack, not just the last binding. Let me show you an example of the variance between these two implemetations: * Old `cd -` implementation saves *only* last binding. With our next `cd -` invocation our interjacent results are lost: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):1> nesting Nesting status: -- 0. main (Pry top level) 1. 4 Also, there are a few bugs in old `cd -` command: * you type `cd :foo`, `cd 1/2/3` and `cd -`. The last command relocates you to the scope of `3` (leaves you where you was), when `:foo` is expected; * you type `cd :foo`, `cd 1/2/3/../4`, `cd -`. The last command relocates you to the scope of `3`, when `:foo` is expected. * New and shiny `cd -` is devoid of those shortcomings: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):3> nesting Nesting status: -- 0. main (Pry top level) 1. 1 2. 2 3. 4 As I said before, this solution is *much* simpler and less error-prone. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
2012-06-27 11:54:07 +00:00
path = arg_string.split(/\//).delete_if { |a| a =~ /\A\s+\z/ }
stack = _pry_.binding_stack.dup
old_stack = state.old_stack || []
# Special case when we only get a single "/", return to root.
if path.empty?
Change behavior of `cd -` command Since banister begged me to do that... completely rewrite `cd -` command (implemetation is much simpler now). This commit brings such changes: * completely rewrite behavior of `cd -` command; * implement ScratchPad aka Pad for unit testing purposes (by banister); * use Pad riches in the unit tests for `cd -` command; * remove verbose and clunky unit tests; This commit brings new meaning to the `cd -` command. The main difference is that the new command saves entire binding stack, not just the last binding. Let me show you an example of the variance between these two implemetations: * Old `cd -` implementation saves *only* last binding. With our next `cd -` invocation our interjacent results are lost: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):1> nesting Nesting status: -- 0. main (Pry top level) 1. 4 Also, there are a few bugs in old `cd -` command: * you type `cd :foo`, `cd 1/2/3` and `cd -`. The last command relocates you to the scope of `3` (leaves you where you was), when `:foo` is expected; * you type `cd :foo`, `cd 1/2/3/../4`, `cd -`. The last command relocates you to the scope of `3`, when `:foo` is expected. * New and shiny `cd -` is devoid of those shortcomings: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):3> nesting Nesting status: -- 0. main (Pry top level) 1. 1 2. 2 3. 4 As I said before, this solution is *much* simpler and less error-prone. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
2012-06-27 11:54:07 +00:00
state.old_stack = stack.dup unless old_stack.empty?
stack = [stack.first]
end
Change behavior of `cd -` command Since banister begged me to do that... completely rewrite `cd -` command (implemetation is much simpler now). This commit brings such changes: * completely rewrite behavior of `cd -` command; * implement ScratchPad aka Pad for unit testing purposes (by banister); * use Pad riches in the unit tests for `cd -` command; * remove verbose and clunky unit tests; This commit brings new meaning to the `cd -` command. The main difference is that the new command saves entire binding stack, not just the last binding. Let me show you an example of the variance between these two implemetations: * Old `cd -` implementation saves *only* last binding. With our next `cd -` invocation our interjacent results are lost: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):1> nesting Nesting status: -- 0. main (Pry top level) 1. 4 Also, there are a few bugs in old `cd -` command: * you type `cd :foo`, `cd 1/2/3` and `cd -`. The last command relocates you to the scope of `3` (leaves you where you was), when `:foo` is expected; * you type `cd :foo`, `cd 1/2/3/../4`, `cd -`. The last command relocates you to the scope of `3`, when `:foo` is expected. * New and shiny `cd -` is devoid of those shortcomings: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):3> nesting Nesting status: -- 0. main (Pry top level) 1. 1 2. 2 3. 4 As I said before, this solution is *much* simpler and less error-prone. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
2012-06-27 11:54:07 +00:00
path.each_with_index do |context, i|
begin
case context.chomp
when ""
Change behavior of `cd -` command Since banister begged me to do that... completely rewrite `cd -` command (implemetation is much simpler now). This commit brings such changes: * completely rewrite behavior of `cd -` command; * implement ScratchPad aka Pad for unit testing purposes (by banister); * use Pad riches in the unit tests for `cd -` command; * remove verbose and clunky unit tests; This commit brings new meaning to the `cd -` command. The main difference is that the new command saves entire binding stack, not just the last binding. Let me show you an example of the variance between these two implemetations: * Old `cd -` implementation saves *only* last binding. With our next `cd -` invocation our interjacent results are lost: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):1> nesting Nesting status: -- 0. main (Pry top level) 1. 4 Also, there are a few bugs in old `cd -` command: * you type `cd :foo`, `cd 1/2/3` and `cd -`. The last command relocates you to the scope of `3` (leaves you where you was), when `:foo` is expected; * you type `cd :foo`, `cd 1/2/3/../4`, `cd -`. The last command relocates you to the scope of `3`, when `:foo` is expected. * New and shiny `cd -` is devoid of those shortcomings: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):3> nesting Nesting status: -- 0. main (Pry top level) 1. 1 2. 2 3. 4 As I said before, this solution is *much* simpler and less error-prone. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
2012-06-27 11:54:07 +00:00
state.old_stack = stack.dup
stack = [stack.first]
when "::"
Change behavior of `cd -` command Since banister begged me to do that... completely rewrite `cd -` command (implemetation is much simpler now). This commit brings such changes: * completely rewrite behavior of `cd -` command; * implement ScratchPad aka Pad for unit testing purposes (by banister); * use Pad riches in the unit tests for `cd -` command; * remove verbose and clunky unit tests; This commit brings new meaning to the `cd -` command. The main difference is that the new command saves entire binding stack, not just the last binding. Let me show you an example of the variance between these two implemetations: * Old `cd -` implementation saves *only* last binding. With our next `cd -` invocation our interjacent results are lost: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):1> nesting Nesting status: -- 0. main (Pry top level) 1. 4 Also, there are a few bugs in old `cd -` command: * you type `cd :foo`, `cd 1/2/3` and `cd -`. The last command relocates you to the scope of `3` (leaves you where you was), when `:foo` is expected; * you type `cd :foo`, `cd 1/2/3/../4`, `cd -`. The last command relocates you to the scope of `3`, when `:foo` is expected. * New and shiny `cd -` is devoid of those shortcomings: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):3> nesting Nesting status: -- 0. main (Pry top level) 1. 1 2. 2 3. 4 As I said before, this solution is *much* simpler and less error-prone. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
2012-06-27 11:54:07 +00:00
state.old_stack = stack.dup
stack.push(TOPLEVEL_BINDING)
when "."
next
when ".."
unless stack.size == 1
Change behavior of `cd -` command Since banister begged me to do that... completely rewrite `cd -` command (implemetation is much simpler now). This commit brings such changes: * completely rewrite behavior of `cd -` command; * implement ScratchPad aka Pad for unit testing purposes (by banister); * use Pad riches in the unit tests for `cd -` command; * remove verbose and clunky unit tests; This commit brings new meaning to the `cd -` command. The main difference is that the new command saves entire binding stack, not just the last binding. Let me show you an example of the variance between these two implemetations: * Old `cd -` implementation saves *only* last binding. With our next `cd -` invocation our interjacent results are lost: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):1> nesting Nesting status: -- 0. main (Pry top level) 1. 4 Also, there are a few bugs in old `cd -` command: * you type `cd :foo`, `cd 1/2/3` and `cd -`. The last command relocates you to the scope of `3` (leaves you where you was), when `:foo` is expected; * you type `cd :foo`, `cd 1/2/3/../4`, `cd -`. The last command relocates you to the scope of `3`, when `:foo` is expected. * New and shiny `cd -` is devoid of those shortcomings: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):3> nesting Nesting status: -- 0. main (Pry top level) 1. 1 2. 2 3. 4 As I said before, this solution is *much* simpler and less error-prone. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
2012-06-27 11:54:07 +00:00
# Don't rewrite old_stack if we're in complex expression
# (e.g.: `cd 1/2/3/../4).
state.old_stack = stack.dup if path.first == ".."
stack.pop
end
when "-"
Change behavior of `cd -` command Since banister begged me to do that... completely rewrite `cd -` command (implemetation is much simpler now). This commit brings such changes: * completely rewrite behavior of `cd -` command; * implement ScratchPad aka Pad for unit testing purposes (by banister); * use Pad riches in the unit tests for `cd -` command; * remove verbose and clunky unit tests; This commit brings new meaning to the `cd -` command. The main difference is that the new command saves entire binding stack, not just the last binding. Let me show you an example of the variance between these two implemetations: * Old `cd -` implementation saves *only* last binding. With our next `cd -` invocation our interjacent results are lost: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):1> nesting Nesting status: -- 0. main (Pry top level) 1. 4 Also, there are a few bugs in old `cd -` command: * you type `cd :foo`, `cd 1/2/3` and `cd -`. The last command relocates you to the scope of `3` (leaves you where you was), when `:foo` is expected; * you type `cd :foo`, `cd 1/2/3/../4`, `cd -`. The last command relocates you to the scope of `3`, when `:foo` is expected. * New and shiny `cd -` is devoid of those shortcomings: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):3> nesting Nesting status: -- 0. main (Pry top level) 1. 1 2. 2 3. 4 As I said before, this solution is *much* simpler and less error-prone. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
2012-06-27 11:54:07 +00:00
unless old_stack.empty?
# Interchange current stack and old stack with each other.
stack, state.old_stack = state.old_stack, stack
end
else
Change behavior of `cd -` command Since banister begged me to do that... completely rewrite `cd -` command (implemetation is much simpler now). This commit brings such changes: * completely rewrite behavior of `cd -` command; * implement ScratchPad aka Pad for unit testing purposes (by banister); * use Pad riches in the unit tests for `cd -` command; * remove verbose and clunky unit tests; This commit brings new meaning to the `cd -` command. The main difference is that the new command saves entire binding stack, not just the last binding. Let me show you an example of the variance between these two implemetations: * Old `cd -` implementation saves *only* last binding. With our next `cd -` invocation our interjacent results are lost: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):1> nesting Nesting status: -- 0. main (Pry top level) 1. 4 Also, there are a few bugs in old `cd -` command: * you type `cd :foo`, `cd 1/2/3` and `cd -`. The last command relocates you to the scope of `3` (leaves you where you was), when `:foo` is expected; * you type `cd :foo`, `cd 1/2/3/../4`, `cd -`. The last command relocates you to the scope of `3`, when `:foo` is expected. * New and shiny `cd -` is devoid of those shortcomings: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):3> nesting Nesting status: -- 0. main (Pry top level) 1. 1 2. 2 3. 4 As I said before, this solution is *much* simpler and less error-prone. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
2012-06-27 11:54:07 +00:00
state.old_stack = stack.dup if i == 0
stack.push(Pry.binding_for(stack.last.eval(context)))
end
rescue RescuableException => e
Change behavior of `cd -` command Since banister begged me to do that... completely rewrite `cd -` command (implemetation is much simpler now). This commit brings such changes: * completely rewrite behavior of `cd -` command; * implement ScratchPad aka Pad for unit testing purposes (by banister); * use Pad riches in the unit tests for `cd -` command; * remove verbose and clunky unit tests; This commit brings new meaning to the `cd -` command. The main difference is that the new command saves entire binding stack, not just the last binding. Let me show you an example of the variance between these two implemetations: * Old `cd -` implementation saves *only* last binding. With our next `cd -` invocation our interjacent results are lost: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):1> nesting Nesting status: -- 0. main (Pry top level) 1. 4 Also, there are a few bugs in old `cd -` command: * you type `cd :foo`, `cd 1/2/3` and `cd -`. The last command relocates you to the scope of `3` (leaves you where you was), when `:foo` is expected; * you type `cd :foo`, `cd 1/2/3/../4`, `cd -`. The last command relocates you to the scope of `3`, when `:foo` is expected. * New and shiny `cd -` is devoid of those shortcomings: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):3> nesting Nesting status: -- 0. main (Pry top level) 1. 1 2. 2 3. 4 As I said before, this solution is *much* simpler and less error-prone. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
2012-06-27 11:54:07 +00:00
# Restore old stack to its initial values.
state.old_stack = old_stack
output.puts "Bad object path: #{arg_string.chomp}. Failed trying to resolve: #{context}"
output.puts e.inspect
return
end
end
_pry_.binding_stack = stack
end
end
end
end
end