Skip to content

Instantly share code, notes, and snippets.

@xPMo
Last active February 3, 2021 16:45
Show Gist options
  • Save xPMo/15897f18bd385cff828b5263ea259ca1 to your computer and use it in GitHub Desktop.
Save xPMo/15897f18bd385cff828b5263ea259ca1 to your computer and use it in GitHub Desktop.
Zsh completion for doy/rbw
_rbw.zsh
#compdef rbw
emulate -L zsh
setopt extendedglob
# Rust's term_size looks at stdin, stdout, and stderr
local -a clist=(${(f)"$(command rbw --help </dev/null 2>/dev/null)"})
# TODO: automatically parse flags
local -a flist=("${(@)clist[$clist[(i)FLAGS*]+1,$clist[(i)SUBCOMMANDS:*]-1]}")
local -a match mbegin mend
typeset -ga _rbw_{aliases,commands}
typeset -gA _rbw_aliasmap
for cmd_desc in "${(@)clist[$clist[(i)SUBCOMMANDS*]+1,-1]}"; do
match=()
[[ $cmd_desc = (#b)*'[aliases: '([^, \]]#)* ]] &&
_rbw_aliasmap[$match[1]]=${cmd_desc[(w)1]}
cmd_desc=${cmd_desc%\[aliases:*}
_rbw_aliases+=("${match[1]}:${cmd_desc[(w)2,-1]//:/\\:}")
_rbw_commands+=("${cmd_desc[(w)1]}:${cmd_desc[(w)2,-1]//:/\\:}")
done
_rbw() {
local curcontext="${curcontext}" state line ret=1
local mode rbwcommand
_arguments -C -s -w \
{-h,--help}'[Prints help information]' \
{-V,--version}'[Prints version information]' \
'*:: :->subcommand_or_options' && ret=0
[[ -z $state ]] && return ret
if (( CURRENT == 1 )); then
zstyle -s ":completion:${curcontext}:subcommands" mode mode || mode='both'
if [[ ${mode} == 'commands' ]]; then
_describe -t subcommands 'rbw commands' _rbw_commands && ret=0
elif [[ ${mode} == 'aliases' ]]; then
_describe -t subcommands 'rbw aliases' _rbw_aliases && ret=0
else
_describe -t subcommands 'rbw commands and aliases' _rbw_commands -- _rbw_aliases && ret=0
fi
else
rbwcommand="${words[1]}"
if [[ -n ${_rbw_aliasmap[$rbwcommand]} ]] ; then
rbwcommand=${_rbw_aliasmap[$rbwcommand]}
fi
curcontext="${curcontext%:*}-${rbwcommand}:"
_call_function ret _rbw-subcommand $rbwcommand
fi
return ret
}
_rbw-subcommand(){
local -a clist=(${(f)"$(command rbw "$1" --help </dev/null 2>/dev/null)"})
# TODO
return
}
_rbw "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment