From b06a8a2e0c5b7caf20d0eeaad6e460282c618510 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Mon, 7 Feb 2022 13:16:02 -0600 Subject: [PATCH] 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.) --- share/functions/fish_nth_token.fish | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 share/functions/fish_nth_token.fish diff --git a/share/functions/fish_nth_token.fish b/share/functions/fish_nth_token.fish new file mode 100644 index 000000000..7df863ae6 --- /dev/null +++ b/share/functions/fish_nth_token.fish @@ -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