瀏覽代碼

new very nice shell script

Johan Rong 1 周之前
父節點
當前提交
a60a1f5043
共有 2 個文件被更改,包括 79 次插入 和 0 次删除
  1. 1 0
      home/modules/apps/fish/default.nix
  2. 78 0
      home/modules/apps/fish/project_cd.fish

+ 1 - 0
home/modules/apps/fish/default.nix

@@ -5,6 +5,7 @@
 
     interactiveShellInit = ''
       set -g fish_greeting ""
+      source ${./project_cd.fish}
       direnv hook fish | source
     '';
 

+ 78 - 0
home/modules/apps/fish/project_cd.fish

@@ -0,0 +1,78 @@
+set -g project_roots \
+    pu "$HOME/Projects/untracked" \
+    pj "$HOME/Projects/johron" \
+    pd "$HOME/Projects/dotkom" \
+    pz "$HOME/Projects/zelkel-lang"
+
+set -g project_max_depth 2
+
+function project_cd --description 'cd to a project root'
+    set -l root $argv[1]
+    set -l target $root
+
+    if test (count $argv) -gt 1
+        set target "$root/"(string join / $argv[2..-1])
+    end
+
+    cd "$target"
+end
+
+function __project_find_dirs
+    set -l base $argv[1]
+    set -l depth $argv[2]
+    set -l extra $argv[3..-1]
+
+    command find "$base" -mindepth 1 -maxdepth $depth \
+        \( -name '.*' -o -name node_modules -o -name target -o -name venv \
+           -o -name __pycache__ -o -name dist -o -name build \) -prune -o \
+        -type d $extra -printf '%P\n' 2>/dev/null
+end
+
+function __project_complete_directories --argument-names base
+    __project_find_dirs "$base" 1
+end
+
+function __project_complete_subdirectories --argument-names base
+    __project_find_dirs "$base" $project_max_depth
+end
+
+function p --description 'cd to a project directory anywhere under ~/Projects'
+    set -l root "$HOME/Projects"
+
+    if test (count $argv) -eq 0
+        cd "$root"
+        return
+    end
+
+    set -l target (string join / $argv)
+
+    if test -d "$root/$target"
+        cd "$root/$target"
+        return
+    end
+
+    set -l matches (__project_find_dirs "$root" $project_max_depth -name "$target")
+
+    switch (count $matches)
+        case 1
+            cd "$root/$matches[1]"
+        case 0
+            cd "$root/$target"
+        case '*'
+            printf '%s\n' $matches
+            return 1
+    end
+end
+
+for i in (seq 1 2 (count $project_roots))
+    set -l name $project_roots[$i]
+    set -l root $project_roots[(math $i + 1)]
+
+    function $name --inherit-variable root --description "cd to $root"
+        project_cd "$root" $argv
+    end
+
+    complete -c $name -f -a "(__project_complete_directories \"$root\")"
+end
+
+complete -c p -f -a "(__project_complete_subdirectories \"$HOME/Projects\")"