Add public function fish_nth_token to mirror fish_is_nth_token

Completions may benefit from using these in tandem to dynamically generate
completions predicated on the value of an earlier token in a cleaner fashion.
(Currently, most of called completion helper functions introspect the command
line to get the value of an earlier argument, making them less reusable for
different expressions that need completions of the same type. This way, the
completion can provide the function with the argument value explicitly.)
This commit is contained in:
Mahmoud Al-Qudsi 2022-02-07 13:16:02 -06:00
parent 99e0aa3c64
commit b06a8a2e0c

View File

@ -0,0 +1,9 @@
function fish_nth_token --description 'Prints the Nth token (ignoring command and switches/flags)' --argument-names n
set -l tokens (commandline -po | string replace -r --filter '^([^-].*)' '$1')
# Increment $n by one to account for ignoring the command
if test (count $tokens) -ge (math "$n" + 1)
echo $tokens[(math $n + 1)]
else
return 1
end
end