pushd: Remove unnecessary forks

Convert sed to string and remove unnecessary math calls.
This commit is contained in:
Fabian Homborg 2016-08-28 23:59:17 +02:00
parent 3e30857e99
commit 3702616b60

View File

@ -1,4 +1,3 @@
function pushd --description 'Push directory to stack'
if count $argv >/dev/null
# check for --help
@ -9,8 +8,13 @@ function pushd --description 'Push directory to stack'
end
# emulate bash by checking if argument of form +n or -n
set rot_l (echo $argv[1] | sed -n '/^+\([0-9]\)$/ s//\1/g p')
set rot_r (echo $argv[1] | sed -n '/^-\([0-9]\)$/ s//\1/g p')
set -l rot_r
set -l rot_l
if string match -qr '^-[0-9]+$' -- $argv[1]
set rot_r (string sub -s 2 -- $argv[1])
else if string match -qr '^\+[0-9]+$' -- $argv[1]
set rot_l (string sub -s 2 -- $argv[1])
end
end
# emulate bash: an empty pushd should switch the top of dirs
@ -39,7 +43,7 @@ function pushd --description 'Push directory to stack'
# translate a right rotation to a left rotation
if test -n "$rot_r"
# check the rotation in range
if test $rot_r -gt (math (count $stack) - 1)
if test $rot_r -ge (count $stack)
echo "pushd: -$rot_r: directory stack index out of range"
return 1
end
@ -48,7 +52,7 @@ function pushd --description 'Push directory to stack'
end
# check the rotation in range
if test $rot_l -gt (math (count $stack) - 1)
if test $rot_l -ge (count $stack)
echo "pushd: +$rot_l: directory stack index out of range"
return 1
else
@ -67,8 +71,7 @@ function pushd --description 'Push directory to stack'
return
end
# Comment to avoid set completions
# argv[1] is a directory
set -g dirstack (command pwd) $dirstack
cd $argv[1]
end