|
6 | 6 |
|
7 | 7 | (declare with-completions) |
8 | 8 |
|
9 | | -(defn zsh-completions [cmdspec {:lambdaisland.cli/keys [argv] :as opts}] |
| 9 | +(defn shell-completions [cmdspec {:lambdaisland.cli/keys [argv] :as opts}] |
10 | 10 | (let [cmdspec (reduce |
11 | 11 | (fn [cmdspec arg] |
12 | 12 | (let [commands (cmdspec/prepare-cmdpairs (:commands (with-completions cmdspec)))] |
|
31 | 31 | zshrc-path (io/file home ".zshrc") |
32 | 32 | zshrc (if (.exists zshrc-path) (slurp zshrc-path) "")] |
33 | 33 | (when-not script-name |
34 | | - (println "When running via clj/clojure (not babashka), you need to pass the fully qualified of the script as the first argument to __install_zsh_completions") |
35 | | - (println "e.g. bin/my_script __install_zsh_completions /full/path/to/bin/my_script") |
| 34 | + (println "When running via clj/clojure (not babashka), you need to pass the fully qualified of the script as the first argument to install-completions") |
| 35 | + (println "e.g. bin/my_script __licli install-completions /full/path/to/bin/my_script") |
36 | 36 | (System/exit -1)) |
37 | 37 | (when-not (.exists cdir) (.mkdirs cdir)) |
38 | 38 | (println "Creating" (str (io/file cdir "_licli"))) |
|
54 | 54 | (spit zshrc-path (str zshrc "\n" stanza))))) |
55 | 55 |
|
56 | 56 | (defn install-bash-completions [opts] |
57 | | - (throw (ex-info {} "Not implemented")) |
58 | | - ) |
| 57 | + (let [home (System/getProperty "user.home") |
| 58 | + script-name (or (System/getProperty "babashka.file") (first (:lambdaisland.cli/argv opts))) |
| 59 | + bashrc-path (io/file home ".bashrc") |
| 60 | + bashrc (if (.exists bashrc-path) (slurp bashrc-path) "")] |
| 61 | + (when-not script-name |
| 62 | + (println "When running via clj/clojure (not babashka), you need to pass the fully qualified of the script as the first argument to install-completions") |
| 63 | + (println "e.g. bin/my_script __licli install-completions /full/path/to/bin/my_script") |
| 64 | + (System/exit -1)) |
| 65 | + (let [base-name (last (str/split script-name #"/")) |
| 66 | + stanza (str |
| 67 | + (when-not (re-find #"(?m)^# lambdaisland.cli completions" bashrc) |
| 68 | + (str |
| 69 | + "\n# lambdaisland.cli completions\n" |
| 70 | + "if [ -f " (str (io/file home ".bash_completion.d/_licli.bash")) " ]; then\n" |
| 71 | + " source " (str (io/file home ".bash_completion.d/_licli.bash")) "\n" |
| 72 | + "fi\n")) |
| 73 | + (str "complete -F _licli " script-name " " "*/" base-name " " base-name "\n"))] |
| 74 | + (let [cdir (io/file home ".bash_completion.d")] |
| 75 | + (when-not (.exists cdir) (.mkdirs cdir)) |
| 76 | + (println "Creating" (str (io/file cdir "_licli.bash"))) |
| 77 | + (spit (io/file cdir "_licli.bash") |
| 78 | + (slurp (io/resource "lambdaisland/cli/_licli.bash")))) |
| 79 | + (println "Updating" (str bashrc-path) ", adding:") |
| 80 | + (println stanza) |
| 81 | + (spit bashrc-path (str bashrc "\n" stanza))))) |
59 | 82 |
|
60 | 83 | (defn install-completions |
61 | 84 | "Do the necessary setup to get shell completions. Shell will be guessed from |
|
81 | 104 | ["completions" {:doc "Generate completions based on partially completed arguments |
82 | 105 |
|
83 | 106 | called by shell functions to do the actual completing." |
84 | | - :command (partial zsh-completions cmdspec)} |
| 107 | + :command (partial shell-completions cmdspec)} |
85 | 108 | "install-completions" #'install-completions]}])) |
0 commit comments