fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Johannes AltmanningerPrepare to not localize private function descriptionsd835c5252

master
2.3 KiB82 linesraw
1# localization: tier1
2function pushd --description 'Push directory to stack'
3    set -l rot_r
4    set -l rot_l
5
6    if count $argv >/dev/null
7        # check for --help
8        switch $argv[1]
9            case -h --h --he --hel --help
10                __fish_print_help pushd
11                return 0
12        end
13
14        # emulate bash by checking if argument of form +n or -n
15        if string match -qr '^-[0-9]+$' -- $argv[1]
16            set rot_r (string sub -s 2 -- $argv[1])
17        else if string match -qr '^\+[0-9]+$' -- $argv[1]
18            set rot_l (string sub -s 2 -- $argv[1])
19        end
20    end
21
22    set -q dirstack
23    or set -g dirstack
24
25    # emulate bash: an empty pushd should switch the top of dirs
26    if not set -q argv[1]
27        # check that the stack isn't empty
28        if not set -q dirstack[1]
29            echo "pushd: no other directory" >&2
30            return 1
31        end
32
33        # get the top two values of the dirs stack ... the first is pwd
34        set -l top_dir $PWD
35        set -l next_dir $dirstack[1]
36
37        # alter the top of dirstack and move to directory
38        set -g dirstack[1] $top_dir
39        cd $next_dir
40        return
41    end
42
43    # emulate bash: check for rotations
44    if test -n "$rot_l" -o -n "$rot_r"
45        # grab the current stack
46        set -l stack $PWD $dirstack
47
48        # translate a right rotation to a left rotation
49        if test -n "$rot_r"
50            # check the rotation in range
51            if test $rot_r -ge (count $stack)
52                echo "pushd: -$rot_r: directory stack index out of range" >&2
53                return 1
54            end
55
56            set rot_l (math (count $stack) - 1 - $rot_r)
57        end
58
59        # check the rotation in range
60        if test $rot_l -ge (count $stack)
61            echo "pushd: +$rot_l: directory stack index out of range" >&2
62            return 1
63        else
64            # rotate stack unless rot_l is 0
65            if test $rot_l -gt 0
66                set stack $stack[(math $rot_l + 1)..(count $stack)] $stack[1..$rot_l]
67            end
68
69            # now reconstruct dirstack and change directory
70            set -g dirstack $stack[2..(count $stack)]
71            cd $stack[1]
72        end
73
74        # print the new stack
75        dirs
76        return
77    end
78
79    # argv[1] is a directory
80    set -l old_pwd $PWD
81    cd $argv[1]; and set -g -p dirstack $old_pwd
82end