mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-25 20:33:08 +00:00
convert nextd
to use argparse
Also fix a bug I introduce in `prevd` when I converted it to use `argparse`.
This commit is contained in:
parent
c149f4f301
commit
5ac8c42fad
2 changed files with 25 additions and 50 deletions
|
@ -1,6 +1,6 @@
|
||||||
function nextd --description "Move forward in the directory history"
|
function nextd --description "Move forward in the directory history"
|
||||||
set -l options 'h/help' 'l/list'
|
set -l options 'h/help' 'l/list'
|
||||||
argparse -n nextd $options -- $argv
|
argparse -n nextd --max-args=1 $options -- $argv
|
||||||
or return
|
or return
|
||||||
|
|
||||||
if set -q _flag_help
|
if set -q _flag_help
|
||||||
|
@ -8,13 +8,8 @@ function nextd --description "Move forward in the directory history"
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
|
|
||||||
if set -q argv[2]
|
|
||||||
printf (_ "%s: Too many arguments") nextd >&2
|
|
||||||
return 1
|
|
||||||
end
|
|
||||||
|
|
||||||
set -l times 1
|
set -l times 1
|
||||||
if set -q $argv[1]
|
if set -q argv[1]
|
||||||
if test $argv[1] -ge 0 ^/dev/null
|
if test $argv[1] -ge 0 ^/dev/null
|
||||||
set times $argv[1]
|
set times $argv[1]
|
||||||
else
|
else
|
||||||
|
|
|
@ -1,64 +1,44 @@
|
||||||
|
|
||||||
function prevd --description "Move back in the directory history"
|
function prevd --description "Move back in the directory history"
|
||||||
|
set -l options 'h/help' 'l/list'
|
||||||
|
argparse -n prevd --max-args=1 $options -- $argv
|
||||||
|
or return
|
||||||
|
|
||||||
if count $argv >/dev/null
|
if set -q _flag_help
|
||||||
switch $argv[1]
|
__fish_print_help prevd
|
||||||
case -h --h --he --hel --help
|
return 0
|
||||||
__fish_print_help prevd
|
|
||||||
return 0
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Parse arguments
|
|
||||||
set -l show_hist 0
|
|
||||||
set -l times 1
|
set -l times 1
|
||||||
if count $argv >/dev/null
|
if set -q argv[1]
|
||||||
for i in (seq (count $argv))
|
if test $argv[1] -ge 0 ^/dev/null
|
||||||
switch $argv[$i]
|
set times $argv[1]
|
||||||
case '-l' --l --li --lis --list
|
else
|
||||||
set show_hist 1
|
printf (_ "%s: The number of positions to skip must be a non-negative integer\n") nextd
|
||||||
continue
|
return 1
|
||||||
case '-*'
|
|
||||||
printf (_ "%s: Unknown option %s\n" ) prevd $argv[$i]
|
|
||||||
return 1
|
|
||||||
case '*'
|
|
||||||
if test $argv[$i] -ge 0 ^/dev/null
|
|
||||||
set times $argv[$i]
|
|
||||||
else
|
|
||||||
printf (_ "The number of positions to skip must be a non-negative integer\n")
|
|
||||||
return 1
|
|
||||||
end
|
|
||||||
continue
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Traverse history
|
# Traverse history
|
||||||
set -l code 1
|
set -l code 1
|
||||||
if count $times >/dev/null
|
for i in (seq $times)
|
||||||
for i in (seq $times)
|
# Try one step forward
|
||||||
# Try one step backward
|
if __fish_move_last dirprev dirnext
|
||||||
if __fish_move_last dirprev dirnext
|
# We consider it a success if we were able to do at least 1 step
|
||||||
|
# (low expectations are the key to happiness ;)
|
||||||
# We consider it a success if we were able to do at least 1 step
|
set code 0
|
||||||
# (low expectations are the key to happiness ;)
|
else
|
||||||
set code 0
|
break
|
||||||
else
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Show history if needed
|
# Show history if needed
|
||||||
if test $show_hist = 1
|
if set -q _flag_list
|
||||||
dirh
|
dirh
|
||||||
end
|
end
|
||||||
|
|
||||||
# Set direction for 'cd -'
|
# Set direction for 'cd -'
|
||||||
if test $code = 0 ^/dev/null
|
test $code = 0
|
||||||
set -g __fish_cd_direction next
|
and set -g __fish_cd_direction next
|
||||||
end
|
|
||||||
|
|
||||||
# All done
|
|
||||||
return $code
|
return $code
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue