1
0

project_cd.fish 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. set -g project_roots \
  2. pu "$HOME/Projects/untracked" \
  3. pj "$HOME/Projects/johron" \
  4. pd "$HOME/Projects/dotkom" \
  5. pz "$HOME/Projects/zelkel-lang"
  6. set -g project_max_depth 2
  7. function project_cd --description 'cd to a project root'
  8. set -l root $argv[1]
  9. set -l target $root
  10. if test (count $argv) -gt 1
  11. set target "$root/"(string join / $argv[2..-1])
  12. end
  13. cd "$target"
  14. end
  15. function __project_find_dirs
  16. set -l base $argv[1]
  17. set -l depth $argv[2]
  18. set -l extra $argv[3..-1]
  19. command find "$base" -mindepth 1 -maxdepth $depth \
  20. \( -name '.*' -o -name node_modules -o -name target -o -name venv \
  21. -o -name __pycache__ -o -name dist -o -name build \) -prune -o \
  22. -type d $extra -printf '%P\n' 2>/dev/null
  23. end
  24. function __project_complete_directories --argument-names base
  25. __project_find_dirs "$base" 1
  26. end
  27. function __project_complete_subdirectories --argument-names base
  28. __project_find_dirs "$base" $project_max_depth
  29. end
  30. function p --description 'cd to a project directory anywhere under ~/Projects'
  31. set -l root "$HOME/Projects"
  32. if test (count $argv) -eq 0
  33. cd "$root"
  34. return
  35. end
  36. set -l target (string join / $argv)
  37. if test -d "$root/$target"
  38. cd "$root/$target"
  39. return
  40. end
  41. set -l matches (__project_find_dirs "$root" $project_max_depth -name "$target")
  42. switch (count $matches)
  43. case 1
  44. cd "$root/$matches[1]"
  45. case 0
  46. cd "$root/$target"
  47. case '*'
  48. printf '%s\n' $matches
  49. return 1
  50. end
  51. end
  52. for i in (seq 1 2 (count $project_roots))
  53. set -l name $project_roots[$i]
  54. set -l root $project_roots[(math $i + 1)]
  55. function $name --inherit-variable root --description "cd to $root"
  56. project_cd "$root" $argv
  57. end
  58. complete -c $name -f -a "(__project_complete_directories \"$root\")"
  59. end
  60. complete -c p -f -a "(__project_complete_subdirectories \"$HOME/Projects\")"