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