feat: improved bash autocompletions

- Dramatically improves quality of bash autocompletions

- Provides optional integration with `fzf`
This commit is contained in:
Chris Lane 2020-02-02 14:23:03 -05:00
parent 825bd0139d
commit c8747bd91d
1 changed files with 41 additions and 31 deletions

View File

@ -1,63 +1,73 @@
_cheat_cheatpaths() # cheat(1) completion -*- shell-script -*-
{
if [[ -z "${cur// }" ]]; then
compgen -W "$(cheat -d | cut -d':' -f1)"
else
compgen -W "$(cheat -d | cut -d':' -f1 | grep $cur)"
fi
}
_cheat_cheatsheets() # generate cheatsheet completions, optionally using `fzf`
_cheat_complete_cheatsheets()
{ {
if [[ "${CHEAT_USE_FZF:-0}" != 0 ]]; then if [[ "${CHEAT_USE_FZF:-0}" != 0 ]]; then
FZF_COMPLETION_TRIGGER='' _fzf_complete "--no-multi" "$@" < <(cheat -l | tail -n +2 | cut -d' ' -f1) FZF_COMPLETION_TRIGGER='' _fzf_complete "--no-multi" "$@" < <(
cheat -l | tail -n +2 | cut -d' ' -f1
)
else else
COMPREPLY=( $(compgen -W "$(cheat -l $cur | tail -n +2 | cut -d' ' -f1)") ) COMPREPLY=( $(compgen -W "$(cheat -l | tail -n +2 | cut -d' ' -f1)" -- "$cur") )
fi fi
} }
_cheat_tags() # generate tag completions, optionally using `fzf`
_cheat_complete_tags()
{ {
if [[ -z "${cur// }" ]]; then if [[ "${CHEAT_USE_FZF:-0}" != 0 ]]; then
compgen -W "$(cheat -T)" FZF_COMPLETION_TRIGGER='' _fzf_complete "--no-multi" "$@" < <(cheat -T)
else else
compgen -W "$(cheat -T | grep $cur)" COMPREPLY=( $(compgen -W "$(cheat -T)" -- "$cur") )
fi fi
} }
# implement the `cheat` autocompletions
_cheat() _cheat()
{ {
local cur prev words cword split local cur prev words cword split
_init_completion -s || return _init_completion -s || return
# complete options that are currently being typed: `--col` => `--colorize`
if [[ $cur == -* ]]; then
COMPREPLY=( $(compgen -W '$(_parse_help "$1" | sed "s/=//g")' -- "$cur") )
[[ $COMPREPLY == *= ]] && compopt -o nospace
return
fi
# implement completions
case $prev in case $prev in
--colorize|-c|\
--directories|-d|\
--init|\
--regex|-r|\
--search|-s|\
--tags|-T|\
--version|-v)
# noop the above, which should implement no completions
;;
--edit|-e) --edit|-e)
_cheat_cheatsheets _cheat_complete_cheatsheets
;;
--list|-l)
_cheat_complete_cheatsheets
;; ;;
--path|-p) --path|-p)
COMPREPLY=( $(_cheat_cheatpaths) ) COMPREPLY=( $(compgen -W "$(cheat -d | cut -d':' -f1)" -- "$cur") )
;; ;;
--rm) --rm)
_cheat_cheatsheets _cheat_complete_cheatsheets
;; ;;
--tags|-t) --tag|-t)
COMPREPLY=( $(_cheat_tags) ) _cheat_complete_tags
;;
*)
_cheat_complete_cheatsheets
;; ;;
esac esac
$split && return $split && return
if [[ ! "$cur" =~ ^-.* ]]; then
_cheat_cheatsheets
return
fi
if [[ $cur == -* ]]; then
COMPREPLY=( $(compgen -W '$(_parse_help "$1")' -- "$cur") )
[[ $COMPREPLY == *= ]] && compopt -o nospace
return
fi
} && } &&
complete -F _cheat cheat complete -F _cheat cheat