cli icon indicating copy to clipboard operation
cli copied to clipboard

auto-completions for bb.cli/dispatch

Open Sohalt opened this issue 1 year ago • 13 comments

You can test it with

# install test script
bbin install io.github.sohalt/bbct

# install completions

# bash
bbct --org.babashka.cli/completion-snippet bash >> ~/.bashrc

# zsh
# ~/.zsh needs to be on the $fpath variable for zsh
# if it's not, add this to your ~/.zshrc before a line with "compinit":
# fpath+=(~/.zsh)
bbct --org.babashka.cli/completion-snippet zsh > ~/.zsh/_bbct

# fish
bbct --org.babashka.cli/completion-snippet fish > ~/.config/fish/completions/bbct.fish

Sohalt avatar Mar 21 '24 10:03 Sohalt

@Sohalt Trying to test:

bbct --babashka.cli/completion-snippet zsh > /usr/local/share/zsh/site-functions/_bbct

I'm getting:

zsh: no such file or directory: /usr/local/share/zsh/site-functions/_bbct

borkdude avatar Mar 21 '24 10:03 borkdude

The installation for zsh can vary a bit, depending on how your distro packages zsh. Try echo $fpath from within zsh and put the _bbct file somewhere in a directory on that path.

Sohalt avatar Mar 21 '24 10:03 Sohalt

Also I just rebased on main and force pushed a new version of bbct with the latest sha

Sohalt avatar Mar 21 '24 10:03 Sohalt

What is the recommended approach for this Rust-based CLI package? I suppose they must have some user-friendly docs for this?

borkdude avatar Mar 21 '24 10:03 borkdude

They use $ your_program --bpaf-complete-style-zsh > ~/.zsh/_your_program (https://docs.rs/bpaf/latest/bpaf/_documentation/_2_howto/_1_completion/index.html) Though that depends on ~/.zsh/ being on your $fpath (e.g. on my system ~/.zsh/ doesn't exist).

Sohalt avatar Mar 21 '24 10:03 Sohalt

What definitely should work is this:

  1. mkdir ~/.zsh
  2. Add fpath+=($HOME/.zsh)" in your .zshrc before a line that says compinit or similar.
  3. Then bbct --babashka.cli/completion-snippet zsh > ~/.zsh/_bbct.

Sohalt avatar Mar 21 '24 10:03 Sohalt

Nice! First working result after doing that:

Screenshot 2024-03-21 at 12 00 49

borkdude avatar Mar 21 '24 11:03 borkdude

Tests are failing due to line endings on Windows. Does any of bash, zsh or fish even work on Windows? What's the situation there when using something like Cygwin?

Sohalt avatar Mar 21 '24 11:03 Sohalt

Current limitations:

  • only works with cli/dispatch
  • only works with gnu-style options (starting with -or --)
  • doesn't work well with options that take lists (e.g. :coerce [:string])

Sohalt avatar Mar 21 '24 12:03 Sohalt

We might want to add Powershell completions or so in the future, but let's not worry about that for now. Does the example Rust library support this? You can disable those tests for Windows

borkdude avatar Mar 21 '24 12:03 borkdude

Does the example Rust library support this?

no afaics

You can disable those tests for Windows

how?

Sohalt avatar Mar 21 '24 12:03 Sohalt

By wrapping the whole test body in:

(when-not (fs/windows?) ...)

borkdude avatar Mar 21 '24 12:03 borkdude

By wrapping the whole test body in:

(when-not (fs/windows?) ...)

Sometimes things can be so simple :) I thought there was some yaml programming involved to configure the test runner or similar :sweat_smile:

Sohalt avatar Mar 21 '24 12:03 Sohalt