mirror of
https://codeberg.org/dnkl/foot.git
synced 2026-02-20 01:40:14 -05:00
completions: bash: use "case" instead of long if/elif/else chain
This commit is contained in:
parent
7d28da5006
commit
8acc10b9d4
2 changed files with 53 additions and 44 deletions
|
|
@ -31,7 +31,7 @@ _foot()
|
|||
cur=${COMP_WORDS[COMP_CWORD]}
|
||||
prev=${COMP_WORDS[COMP_CWORD-1]}
|
||||
|
||||
# check if positional argument is completed
|
||||
# Check if positional argument is completed
|
||||
previous_words=( "${COMP_WORDS[@]}" )
|
||||
unset previous_words[-1]
|
||||
commands=$(compgen -c | grep -vFx "$(compgen -k)" | grep -vE '^([.:[]|foot)$' | sort -u)
|
||||
|
|
@ -43,41 +43,45 @@ _foot()
|
|||
(( i++ ))
|
||||
continue
|
||||
fi
|
||||
# positional argument found
|
||||
# Positional argument found
|
||||
offset=$i
|
||||
fi
|
||||
(( i++ ))
|
||||
done
|
||||
|
||||
if [[ ! -z "$offset" ]] ; then
|
||||
# depends on bash_completion being available
|
||||
# Depends on bash_completion being available
|
||||
declare -F _command_offset >/dev/null || return 1
|
||||
_command_offset $offset
|
||||
return 0
|
||||
elif [[ ${cur} == --* ]] ; then
|
||||
COMPREPLY=( $(compgen -W "${flags}" -- ${cur}) )
|
||||
elif [[ ${prev} =~ ^(--config|--print-pid|--server)$ ]] ; then
|
||||
compopt -o default
|
||||
elif [[ ${prev} == '--working-directory' ]] ; then
|
||||
compopt -o dirnames
|
||||
elif [[ ${prev} == '--term' ]] ; then
|
||||
# check if toe is available
|
||||
command -v toe > /dev/null || return 1
|
||||
COMPREPLY=( $(compgen -W "$(toe -a | awk '$1 ~ /[+]/ {next}; {print $1}')" -- ${cur}) )
|
||||
elif [[ ${prev} == '--font' ]] ; then
|
||||
# check if fc-list is available
|
||||
command -v fc-list > /dev/null || return 1
|
||||
COMPREPLY=( $(compgen -W "$(fc-list : family | sed 's/,/\n/g' | uniq | tr -d ' ')" -- ${cur}) )
|
||||
elif [[ ${prev} == '--log-level' ]] ; then
|
||||
COMPREPLY=( $(compgen -W "none error warning info" -- ${cur}) )
|
||||
elif [[ ${prev} == '--log-colorize' ]] ; then
|
||||
COMPREPLY=( $(compgen -W "never always auto" -- ${cur}) )
|
||||
elif [[ ${prev} =~ ^(--app-id|--help|--override|--title|--version|--window-size-chars|--window-size-pixels|--check-config)$ ]] ; then
|
||||
: # don't autocomplete for these flags
|
||||
else
|
||||
# complete commands from $PATH
|
||||
COMPREPLY=( $(compgen -c -- ${cur}) )
|
||||
return 0
|
||||
fi
|
||||
|
||||
case "$prev" in
|
||||
--config|--print-pid|--server)
|
||||
compopt -o default ;;
|
||||
--working-directory)
|
||||
compopt -o dirnames ;;
|
||||
--term)
|
||||
command -v toe > /dev/null || return 1
|
||||
COMPREPLY=( $(compgen -W "$(toe -a | awk '$1 ~ /[+]/ {next}; {print $1}')" -- ${cur}) ) ;;
|
||||
--font)
|
||||
command -v fc-list > /dev/null || return 1
|
||||
COMPREPLY=( $(compgen -W "$(fc-list : family | sed 's/,/\n/g' | uniq | tr -d ' ')" -- ${cur}) ) ;;
|
||||
--log-level)
|
||||
COMPREPLY=( $(compgen -W "none error warning info" -- ${cur}) ) ;;
|
||||
--log-colorize)
|
||||
COMPREPLY=( $(compgen -W "never always auto" -- ${cur}) ) ;;
|
||||
--app-id|--help|--override|--title|--version|--window-size-chars|--window-size-pixels|--check-config)
|
||||
# Don't autocomplete for these flags
|
||||
: ;;
|
||||
*)
|
||||
# Complete commands from $PATH
|
||||
COMPREPLY=( $(compgen -c -- ${cur}) ) ;;
|
||||
esac
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ _footclient()
|
|||
cur=${COMP_WORDS[COMP_CWORD]}
|
||||
prev=${COMP_WORDS[COMP_CWORD-1]}
|
||||
|
||||
# check if positional argument is completed
|
||||
# Check if positional argument is completed
|
||||
previous_words=( "${COMP_WORDS[@]}" )
|
||||
unset previous_words[-1]
|
||||
commands=$(compgen -c | grep -vFx "$(compgen -k)" | grep -vE '^([.:[]|footclient)$' | sort -u)
|
||||
|
|
@ -39,37 +39,42 @@ _footclient()
|
|||
(( i++ ))
|
||||
continue
|
||||
fi
|
||||
# positional argument found
|
||||
# Positional argument found
|
||||
offset=$i
|
||||
fi
|
||||
(( i++ ))
|
||||
done
|
||||
|
||||
if [[ ! -z "$offset" ]] ; then
|
||||
# depends on bash_completion being available
|
||||
# Depends on bash_completion being available
|
||||
declare -F _command_offset >/dev/null || return 1
|
||||
_command_offset $offset
|
||||
return 0
|
||||
elif [[ ${cur} == --* ]] ; then
|
||||
COMPREPLY=( $(compgen -W "${flags}" -- ${cur}) )
|
||||
elif [[ ${prev} == '--server-socket' ]] ; then
|
||||
compopt -o default
|
||||
elif [[ ${prev} == '--working-directory' ]] ; then
|
||||
compopt -o dirnames
|
||||
elif [[ ${prev} == '--term' ]] ; then
|
||||
# check if toe is available
|
||||
command -v toe > /dev/null || return 1
|
||||
COMPREPLY=( $(compgen -W "$(toe -a | awk '$1 ~ /[+]/ {next}; {print $1}')" -- ${cur}) )
|
||||
elif [[ ${prev} == '--log-level' ]] ; then
|
||||
COMPREPLY=( $(compgen -W "none error warning info" -- ${cur}) )
|
||||
elif [[ ${prev} == '--log-colorize' ]] ; then
|
||||
COMPREPLY=( $(compgen -W "never always auto" -- ${cur}) )
|
||||
elif [[ ${prev} =~ ^(--app-id|--help|--override|--title|--version|--window-size-chars|--window-size-pixels|)$ ]] ; then
|
||||
: # don't autocomplete for these flags
|
||||
else
|
||||
# complete commands from $PATH
|
||||
COMPREPLY=( $(compgen -c -- ${cur}) )
|
||||
return 0
|
||||
fi
|
||||
|
||||
case "$prev" in
|
||||
--server-socket)
|
||||
compopt -o default ;;
|
||||
--working-directory)
|
||||
compopt -o dirnames ;;
|
||||
--term)
|
||||
command -v toe > /dev/null || return 1
|
||||
COMPREPLY=( $(compgen -W "$(toe -a | awk '$1 ~ /[+]/ {next}; {print $1}')" -- ${cur}) ) ;;
|
||||
--log-level)
|
||||
COMPREPLY=( $(compgen -W "none error warning info" -- ${cur}) ) ;;
|
||||
--log-colorize)
|
||||
COMPREPLY=( $(compgen -W "never always auto" -- ${cur}) ) ;;
|
||||
--app-id|--help|--override|--title|--version|--window-size-chars|--window-size-pixels)
|
||||
# Don't autocomplete for these flags
|
||||
: ;;
|
||||
*)
|
||||
# Complete commands from $PATH
|
||||
COMPREPLY=( $(compgen -c -- ${cur}) ) ;;
|
||||
esac
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue