__fzf_get_dir.fish 586 B

1234567891011121314151617
  1. function __fzf_get_dir -d 'Find the longest existing filepath from input string'
  2. set dir $argv
  3. # Strip all trailing slashes. Ignore if $dir is root dir (/)
  4. if [ (string length $dir) -gt 1 ]
  5. set dir (string replace -r '/*$' '' $dir)
  6. end
  7. # Iteratively check if dir exists and strip tail end of path
  8. while [ ! -d "$dir" ]
  9. # If path is absolute, this can keep going until ends up at /
  10. # If path is relative, this can keep going until entire input is consumed, dirname returns "."
  11. set dir (dirname "$dir")
  12. end
  13. echo $dir
  14. end