xtfc / mold

A fresh (ironic? maybe!) approach to project chores.
https://xtfc.org/mold
MIT License
7 stars 0 forks source link

Add shell autocomplete #134

Open scizzorz opened 4 years ago

scizzorz commented 4 years ago

It would be soooooo great if I could tab complete

philipdexter commented 4 years ago
function _mold_complete() {
  mold | grep -v "⮡" | sed -e 's/\s*\([^ ]*\)/\1/' | while IFS='' read -r LINE || [ -n "${LINE}" ]; do
    command=$(echo $LINE | sed -e 's/\([^ ]*\).*/\1/')
    description=$(echo $LINE | sed -e 's/[^ ]* \(.*\)/\1/')
    _describe 'command' "('$command:$description')"
  done
}
compdef _mold_complete mold

This works for zsh. Haven't tried for bash. Not sure how durable this solution is.

The question then is how to have users install this? One way is to have the tool itself inject this into the .zshrc. Another is to have the tool just spit this out and the user would do source <(mold --completions zsh) whenever they wanted to activate completion (this is how kubectl works). That could also go into their .zshrc.

scizzorz commented 4 years ago

I think for the actual completion function we'd be better off implementing some sort of querying flag that changes the output so we don't have to pipe things through sed. Something like:

$ mold
     c/build Build using cargo
     c/check Check for errors using cargo
             ⮡ c/clean-self
     c/clean Clean build artifacts
c/clean-self Only clean current package
       c/fmt Format using rustfmt
  c/fmtcheck Check formatting using rustfmt
   c/install Install the current project using cargo
      c/lint Lint using clippy
             ⮡ c/clean-self
      c/test Run tests using cargo
 staticbuild Build a static MUSL binary using Docker

$ mold --completion
c/build c/check c/clean c/clean-self c/fmt c/fmtcheck c/install c/lint c/test staticbuild

I think where it gets kinda fuzzy is when you're tab-completing on a partial command:

$ mold -f <tab>
# list files

$ mold -a <tab>
# list environments

$ mold -f foo.mold <tab>
# list recipes from `foo.mold`

I don't know how we'd implement something like that.

I have seen in some other tools you can run something like mold --completions zsh >> ~/.zshrc which appends a completion script. Which I guess is exactly the same functionality you said, just a different form... eh. Guess that means I'm down with it.