From 225f748f790c1071218fdb61be154026590dda65 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Mon, 5 Mar 2018 08:25:25 -0600 Subject: [PATCH] Add __fish_parent_directories helper function for completions Can be used to retrieve a list of parent paths, useful for searching ancestors recursively via their absolute paths. Paths are returned from deepest to shallowest, starting from the path passed in. Paths are not validated for performance reasons. (Usually the input to __fish_parent_directories would be (pwd) or (dir $file).) --- .../functions/__fish_parent_directories.fish | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 share/functions/__fish_parent_directories.fish diff --git a/share/functions/__fish_parent_directories.fish b/share/functions/__fish_parent_directories.fish new file mode 100644 index 000000000..9cdacdcc7 --- /dev/null +++ b/share/functions/__fish_parent_directories.fish @@ -0,0 +1,25 @@ +# Generates a list of parent directories for a given path +# i.e. /a/b/c/d -> [/a/b/c/, /a/b/, /a/, and /a] +function __fish_parent_directories + if test (count $argv) -ne 1 + # for use in completions, so don't spew error messages + return 1 + end + + set -l splits (string split '/' $argv[1]) + set -l parents + + for split in $splits + if test (string length "$split") -eq 0 + continue + end + + set parents "$parents[1]/$split" $parents + end + + for parent in $parents + echo $parent + end + + return 0 +end