jacobdeichert/mask

Bash completion

jgroom33 opened this issue · 5 comments

Add bash auto completion that parses the markdown file and provides a list of commands (headings) that can be executed
Related to #17

I think this is definitely helpful.. i would also use this. I wonder if zsh and fish shell completions can also be added.

This is not very high on my todo list, but if someone wants to tackle this I would love that.

Ideally it would be as simple as possible and require no changes to mask itself. I imagine it's not too hard for bash/zsh/fish to grep a maskfile.md's headings for commands.

create a file with contents below and then source it

#/usr/bin/env bash
_mask_completions()
{
  if [ "${#COMP_WORDS[@]}" != "2" ]; then
    return
  fi
  local suggestions=($(compgen -W "$(sed -n '/^```/,/^```/ !p' < maskfile.md | grep \#\# | sed 's/## //')" -- "${COMP_WORDS[1]}"))

  if [ "${#suggestions[@]}" == "1" ]; then
    local number=$(echo ${suggestions[0]/%\ */})
    COMPREPLY=("$number")
  else
    COMPREPLY=("${suggestions[@]}")
  fi
}

complete -F _mask_completions mask

or, if you are trusting:

source /dev/stdin <<< "$(curl https://gist.githubusercontent.com/jgroom33/516283800c4679b6771bc6303ba9610d/raw/fd68678fb122142a4fb4dafe77701ece6a5d4c3c/mask-completion)"

Looks interesting 👍

I'll try this out in the next few days or on the weekend!

Decided to postpone the decision on incorporating bash/zsh/fish/etc shell completion into mask until at least clap v3 is released. At that point, we can reconsider if adding completion script generation into mask makes sense or not.

In the mean time, I'm happy to link to community-supplied completion scripts from the README. If anyone has completion scripts they feel like sharing, please make a PR that links to your completion script gist or repo! :)

Going to close this issue as jgroom33 has supplied a script above that people may be using.

Just a heads up, I'll be publishing a new mask update soon with #85 merged. I hope that exposing the maskfile structure as json can be helpful for generating autocomplete commands in other environments.

On top of this, mask-parser is now a thing. It's a helper lib to parse the maskfile.md format.