Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ For breaking changes, check [here](#breaking-changes).

[Babashka CLI](https://github.com/babashka/cli): turn Clojure functions into CLIs!

## Unreleased

- zsh completion: disable sorting for registered program names to preserve candidate order

## 0.12.90

- `dispatch`: add `:supplied` to the opts map's `:org.babashka/cli` metadata with the set of option keys given on the command line
Expand Down
5 changes: 5 additions & 0 deletions src/babashka/cli.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1754,6 +1754,9 @@
names-zstyle (str/join "\n"
(map #(str "zstyle ':completion:*:*:" % ":*:options' prefix-needed false")
names))
names-zsort (str/join "\n"
(map #(str "zstyle ':completion:*:*:" % ":*' sort false")
names))
names-nu (str "[" (str/join " " (map #(str "\"" % "\"") names)) "]")]
(case shell
:bash (str fn "()
Expand Down Expand Up @@ -1841,6 +1844,8 @@ complete -F " fn " " names-sp "
# zsh hides options until a dash is typed. After a command there is usually
# nothing else to complete, so opt out for these programs only
" names-zstyle "
# show candidates in the order the program emits them
" names-zsort "
# register the bare name(s); zsh's _normal completes ./name and /abs/name via the basename
compdef " fn " " names-sp "
")
Expand Down
6 changes: 6 additions & 0 deletions test/babashka/cli/completion_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@
(is (str/includes? zsh "zstyle ':completion:*:*:sq:*:options' prefix-needed false"))
(is (str/includes? zsh "zstyle ':completion:*:*:squint:*:options' prefix-needed false")))))

(deftest zsh-snippet-keeps-order-test
(testing "every registered name turns off zsh sorting"
(let [zsh (snippet-via-cmd cmd-table {:prog "x"} "zsh" "--prog" "sq" "--prog" "squint")]
(is (str/includes? zsh "zstyle ':completion:*:*:sq:*' sort false"))
(is (str/includes? zsh "zstyle ':completion:*:*:squint:*' sort false")))))

(defn- complete-out
"Run the completion handler for `cmdline` and return its emitted
`value\\tdescription` lines as a set of strings."
Expand Down
2 changes: 2 additions & 0 deletions test/resources/completion/completion.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@ _babashka_cli_complete_myprogram() {
# zsh hides options until a dash is typed. After a command there is usually
# nothing else to complete, so opt out for these programs only
zstyle ':completion:*:*:myprogram:*:options' prefix-needed false
# show candidates in the order the program emits them
zstyle ':completion:*:*:myprogram:*' sort false
# register the bare name(s); zsh's _normal completes ./name and /abs/name via the basename
compdef _babashka_cli_complete_myprogram myprogram