summaryrefslogtreecommitdiff
path: root/lib/pry/commands/cd.rb
blob: 667453a0863411ed8ea0998b8dd7be849616a9df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Pry
  class Command::Cd < Pry::ClassCommand
    match 'cd'
    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.

      cd @x
      cd ..
      cd /
      cd -

      https://github.com/pry/pry/wiki/State-navigation#wiki-Changing_scope
    BANNER

    def process
      state.old_stack ||= []

      if arg_string.strip == "-"
        unless state.old_stack.empty?
          _pry_.binding_stack, state.old_stack = state.old_stack, _pry_.binding_stack
        end
      else
        stack = ObjectPath.new(arg_string, _pry_.binding_stack).resolve

        if stack && stack != _pry_.binding_stack
          state.old_stack = _pry_.binding_stack
          _pry_.binding_stack = stack
        end
      end
    end
  end

  Pry::Commands.add_command(Pry::Command::Cd)
end