|
1 |
| -cite about-plugin |
2 |
| -about-plugin 'quickly navigate configured paths with `pj` and `pjo`. example: "export PROJECT_PATHS=~/projects:~/work/projects"' |
3 |
| - |
4 |
| -function pj { |
5 |
| -about 'navigate quickly to your various project directories' |
6 |
| -group 'projects' |
7 |
| - |
8 |
| - |
9 |
| -if [ -z "$PROJECT_PATHS" ]; then |
10 |
| - echo "error: PROJECT_PATHS not set" |
11 |
| - return 1 |
12 |
| -fi |
13 |
| - |
14 |
| - |
15 |
| -local cmd |
16 |
| -local dest |
17 |
| -local -a dests |
18 |
| - |
19 |
| - |
20 |
| -if [ "$1" == "open" ]; then |
21 |
| - shift |
22 |
| - cmd="$EDITOR" |
23 |
| -fi |
24 |
| -cmd="${cmd:-cd}" |
25 |
| - |
26 |
| - |
27 |
| -if [ -z "$1" ]; then |
28 |
| - echo "error: no project provided" |
29 |
| - return 1 |
30 |
| -fi |
31 |
| - |
32 |
| - |
33 |
| -# collect possible destinations to account for directories |
34 |
| -# with the same name in project directories |
35 |
| -for i in ${PROJECT_PATHS//:/$'\n'}; do |
36 |
| - if [ -d "$i"/"$1" ]; then |
37 |
| - dests+=("$i/$1") |
38 |
| - fi |
39 |
| -done |
40 |
| - |
41 |
| - |
42 |
| -# when multiple destinations are found, present a menu |
43 |
| -if [ ${#dests[@]} -eq 0 ]; then |
44 |
| - echo "error: no such project '$1'" |
45 |
| - return 1 |
46 |
| - |
47 |
| -elif [ ${#dests[@]} -eq 1 ]; then |
48 |
| - dest="${dests[0]}" |
49 |
| - |
50 |
| -elif [ ${#dests[@]} -gt 1 ]; then |
51 |
| - PS3="Multiple project directories found. Please select one: " |
52 |
| - dests+=("cancel") |
53 |
| - select d in "${dests[@]}"; do |
54 |
| - case $d in |
55 |
| - "cancel") |
56 |
| - return |
57 |
| - ;; |
58 |
| - *) |
59 |
| - dest=$d |
60 |
| - break |
61 |
| - ;; |
62 |
| - esac |
63 |
| - done |
64 |
| - |
65 |
| -else |
66 |
| - echo "error: please report this error" |
67 |
| - return 1 # should never reach this |
68 |
| - |
69 |
| -fi |
70 |
| - |
71 |
| - |
72 |
| -$cmd "$dest" |
| 1 | +# shellcheck shell=bash |
| 2 | +about-plugin 'quickly navigate configured project paths' |
| 3 | + |
| 4 | +: "${BASH_IT_PROJECT_PATHS:=$HOME/Projects:$HOME/src:$HOME/work}" |
| 5 | + |
| 6 | +function pj() { |
| 7 | + about 'navigate quickly to your various project directories' |
| 8 | + group 'projects' |
| 9 | + |
| 10 | + local proj="${1?${FUNCNAME[0]}: project name required}" |
| 11 | + local cmd PS3 dest |
| 12 | + local -a dests=() |
| 13 | + |
| 14 | + if [[ "$proj" == "open" ]]; then |
| 15 | + shift |
| 16 | + cmd="${EDITOR?}" |
| 17 | + fi |
| 18 | + |
| 19 | + # collect possible destinations to account for directories |
| 20 | + # with the same name in project directories |
| 21 | + IFS=':' read -ra dests <<< "${BASH_IT_PROJECT_PATHS}" |
| 22 | + |
| 23 | + # when multiple destinations are found, present a menu |
| 24 | + if [[ ${#dests[@]} -eq 0 ]]; then |
| 25 | + _log_error "no such project '${1:-}'" |
| 26 | + return 1 |
| 27 | + elif [[ ${#dests[@]} -eq 1 ]]; then |
| 28 | + dest="${dests[0]}" |
| 29 | + elif [[ ${#dests[@]} -gt 1 ]]; then |
| 30 | + PS3="Multiple project directories found. Please select one: " |
| 31 | + dests+=("cancel") |
| 32 | + select d in "${dests[@]}"; do |
| 33 | + case $d in |
| 34 | + "cancel") |
| 35 | + return |
| 36 | + ;; |
| 37 | + *) |
| 38 | + dest=$d |
| 39 | + break |
| 40 | + ;; |
| 41 | + esac |
| 42 | + done |
| 43 | + else |
| 44 | + _log_error "please report this error" |
| 45 | + return 2 # should never reach this |
| 46 | + fi |
| 47 | + |
| 48 | + "${cmd:-cd}" "${dest}" |
73 | 49 | }
|
74 | 50 |
|
75 | 51 | alias pjo="pj open"
|
0 commit comments