1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
function completion/typst {
typeset OPTIONS ARGOPT PREFIX
OPTIONS=( #>#
"h --help; show help"
"V --version; show version"
"--color:; colorize output (auto, always, never)"
"--cert:; TLS certificate chain file"
) #<#
command -f completion//parseoptions -n
case $ARGOPT in
(-)
command -f completion//completeoptions
;;
(--color)
complete -P "$PREFIX" auto always never
;;
(--cert)
complete -P "$PREFIX" -f
;;
('')
# find first non-option argument, which is subcommand
typeset OPTIND=2 typcmd=
while [ $OPTIND -le ${WORDS[#]} ]; do
case ${WORDS[OPTIND]} in
(--color)
OPTIND=$((OPTIND+1))
;;
(--color=*)
;;
(--cert)
OPTIND=$((OPTIND+1))
;;
(--cert=*)
;;
(-?*)
;;
(*)
typcmd=${WORDS[OPTIND]}
break
;;
esac
OPTIND=$((OPTIND+1))
done
if [ $OPTIND -le ${WORDS[#]} ]; then
# some short aliases
# TODO: lookup if others exist
case $typcmd in
(c) typcmd=compile;;
(w) typcmd=watch;;
esac
OPTIND=$((OPTIND+1))
# complete args of subcommand
typeset OLDWORDS
OLDWORDS=("$WORDS")
WORDS=("${WORDS[1]}" "${WORDS[OPTIND,-1]}")
# since all commands have -h/--help option
if [ ${WORDS[#]} -le 1 ]; then
case $TARGETWORD in (-*)
complete -- --help -h
esac
fi
if { command -vf "completion/typst::$typcmd:arg" ||
. -AL "completion/typst-$typcmd"; } >/dev/null 2>&1
then
command -f "completion/typst::$typcmd:arg"
else
complete -P "$PREFIX" -f
fi
else
# if no subcommand yet, then: complete subcommand names
command -f completion/typst::completecmd
fi
;;
esac
}
function completion/typst::completecmd {
complete -P "$PREFIX" -D "compile a document" compile c
complete -P "$PREFIX" -D "watch and recompile on changes" watch w
complete -P "$PREFIX" -D "initialize a new project" init
complete -P "$PREFIX" -D "query elements from a document" query
complete -P "$PREFIX" -D "list available fonts" fonts
complete -P "$PREFIX" -D "update packages" update
complete -P "$PREFIX" -D "show help for subcommands" help
}
# vim: set ft=sh ts=8 sts=8 sw=8 et:
|