Skip to content

Instantly share code, notes, and snippets.

@Artoria2e5
Last active May 12, 2016 00:14
Show Gist options
  • Save Artoria2e5/3a909d3d6b31b67f6947d5678d2d6ec5 to your computer and use it in GitHub Desktop.
Save Artoria2e5/3a909d3d6b31b67f6947d5678d2d6ec5 to your computer and use it in GitHub Desktop.
bcomp
#!/bin/bash
declare -A completion_cmd completion_pat completion_pre completion_suf completion_opt
# the good old (i) => `'${i.replace("'","'\\''")}'` rule
# good for Shlex
posix_quote(){
printf " '%s'" "${@//"'"/"'\\''"}"
# If we want a string ret, we will need IFS=' ' and the good old join..
}
# hijacks bash completion system, and gets the candidates.
# complete [-abcdefgjksuv] [-o comp-option] [-DE] [-A action] [-G globpat] \
# [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] \
# name [name …]
# complete -pr [-DE] [name …]
complete(){
local OPT OPTIND OPTARG
local define=0 print=0 remove=0
while getopt 'abcdefgjksuvo:DEA:G:W:F:C:X:P:S:pr' OPT; do
case $OPT in
p)
print=2 # force print; not expecting anything but r
;;
r)
remove=1
;;
D|E)
# noop, as designed (for elvish..)
;;
*)
echo TODO
return 2
;;
esac
done
if ((! (define || remove || print)); then
print=1
fi
if ((define && (remove || print) )); then
return 42 # unexpected!
fi
if ((define)); then
return
fi
if ((print)); then
fi &&
if ((remove)); then
fi
}
# do_complete WORDS
do_complete(){
local cmpkey cmpitem COMPREPLY COMPRESULT
local filterpat="${completion_pat[$cmpkey]}"
for cmpitem in "${COMPREPLY[@]}"; do
if [[ $cmpitem != $filterpat ]]; then
COMPRESULT+=("${completion_pre[$cmpkey]}${compitem}${completion_suf[$cmpkey]}")
fi
done
# don't want to try dirname...
posix_quote "${COMPRESULT[@]}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment