From 1c3e81afe9f63fb47303384a82c7239458166f52 Mon Sep 17 00:00:00 2001 From: sigoden Date: Sat, 13 Apr 2024 19:50:10 +0800 Subject: feat: add completion scripts (#411) --- scripts/completions/aichat.bash | 93 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 scripts/completions/aichat.bash (limited to 'scripts/completions/aichat.bash') diff --git a/scripts/completions/aichat.bash b/scripts/completions/aichat.bash new file mode 100644 index 0000000..0d72747 --- /dev/null +++ b/scripts/completions/aichat.bash @@ -0,0 +1,93 @@ +_aichat() { + local i cur prev opts cmd + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + cmd="" + opts="" + + for i in ${COMP_WORDS[@]} + do + case "${cmd},${i}" in + ",$1") + cmd="aichat" + ;; + *) + ;; + esac + done + + case "${cmd}" in + aichat) + opts="-m -r -s -e -c -f -H -S -w -h -V --model --role --session --save-session --execute --code --file --no-highlight --no-stream --wrap --light-theme --dry-run --info --list-models --list-roles --list-sessions --help --version" + if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then + COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) + return 0 + fi + + # hacking -m or --model completion value that contains colon + if [ "$cur" == ":" ] || [ "$prev" == ":" ]; then + local option client + if [ "$cur" = ":" ]; then + option="${COMP_WORDS[COMP_CWORD-2]}" + client="$prev" + else + option="${COMP_WORDS[COMP_CWORD-3]}" + client="${COMP_WORDS[COMP_CWORD-2]}" + fi + if [ "$option" == "-m" ] || [ "$flag" == "--model" ]; then + if [ "$cur" == ":" ]; then + cur="" + fi + COMPREPLY=($(compgen -W "$("$1" --list-models | sed -n '/'"${client}"'/ s/'"${client%:*}"'://p')" -- "${cur}")) + return 0 + fi + fi + + case "${prev}" in + -m|--model) + COMPREPLY=($(compgen -W "$("$1" --list-models)" -- "${cur}")) + return 0 + ;; + -r|--role) + COMPREPLY=($(compgen -W "$("$1" --list-roles)" -- "${cur}")) + return 0 + ;; + -s|--session) + COMPREPLY=($(compgen -W "$("$1" --list-sessions)" -- "${cur}")) + return 0 + ;; + -f|--file) + local oldifs + if [[ -v IFS ]]; then + oldifs="$IFS" + fi + IFS=$'\n' + COMPREPLY=($(compgen -f "${cur}")) + if [[ -v oldifs ]]; then + IFS="$oldifs" + fi + if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then + compopt -o filenames + fi + return 0 + ;; + -w|--wrap) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + *) + COMPREPLY=() + ;; + esac + COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) + return 0 + ;; + esac +} + +if [[ "${BASH_VERSINFO[0]}" -eq 4 && "${BASH_VERSINFO[1]}" -ge 4 || "${BASH_VERSINFO[0]}" -gt 4 ]]; then + complete -F _aichat -o nosort -o bashdefault -o default aichat +else + complete -F _aichat -o bashdefault -o default aichat +fi -- cgit v1.2.3