A non-recursive version of _hledger_compreply_optarg()

Feels more streamlined and readable
This commit is contained in:
Vladimir Zhelezov 2020-12-11 11:27:20 +01:00
parent 985f041d81
commit 8c14dfb3eb
2 changed files with 22 additions and 32 deletions

View File

@ -190,14 +190,17 @@ _hledger_compgen() {
# Try required option argument completion. Set COMPREPLY and return 0 on # Try required option argument completion. Set COMPREPLY and return 0 on
# success, 1 if option doesn't require an argument or out of context # success, 1 if option doesn't require an argument or out of context
_hledger_compreply_optarg() { _hledger_compreply_optarg() {
local optionIndex=${1:-$((cword - 1))} local optionIndex=$((cword - 1))
local recursionLevel=${2:-0} local match=$cur
local wordlist local wordlist
local error=0
local match
# Match the empty string on --file=<TAB> # Match the empty string on --file=<TAB>, not the equal sign itself
[[ $cur == = ]] || match=$cur if [[ $cur == = ]]; then
match=""
# Once input is present, cword is incremented so we compensate
elif [[ $prev == = ]]; then
optionIndex=$((cword - 2))
fi
case ${words[optionIndex]} in case ${words[optionIndex]} in
--alias) --alias)
@ -241,20 +244,12 @@ _hledger_compreply_optarg() {
-b|--begin|-e|--end|-p|--period|--depth|--drop) -b|--begin|-e|--end|-p|--period|--depth|--drop)
_hledger_compreply "" _hledger_compreply ""
;; ;;
=)
# Recurse only once!
((recursionLevel > 1)) && return 1
if [[ ${words[optionIndex - 1]} == -* ]]; then
_hledger_compreply_optarg $((optionIndex - 1)) $((recursionLevel + 1))
error=$?
fi
;;
*) *)
error=1 return 1
;; ;;
esac esac
return $error return 0
} }
# Query filter completion through introspection # Query filter completion through introspection

View File

@ -190,14 +190,17 @@ _hledger_compgen() {
# Try required option argument completion. Set COMPREPLY and return 0 on # Try required option argument completion. Set COMPREPLY and return 0 on
# success, 1 if option doesn't require an argument or out of context # success, 1 if option doesn't require an argument or out of context
_hledger_compreply_optarg() { _hledger_compreply_optarg() {
local optionIndex=${1:-$((cword - 1))} local optionIndex=$((cword - 1))
local recursionLevel=${2:-0} local match=$cur
local wordlist local wordlist
local error=0
local match
# Match the empty string on --file=<TAB> # Match the empty string on --file=<TAB>, not the equal sign itself
[[ $cur == = ]] || match=$cur if [[ $cur == = ]]; then
match=""
# Once input is present, cword is incremented so we compensate
elif [[ $prev == = ]]; then
optionIndex=$((cword - 2))
fi
case ${words[optionIndex]} in case ${words[optionIndex]} in
--alias) --alias)
@ -241,20 +244,12 @@ _hledger_compreply_optarg() {
-b|--begin|-e|--end|-p|--period|--depth|--drop) -b|--begin|-e|--end|-p|--period|--depth|--drop)
_hledger_compreply "" _hledger_compreply ""
;; ;;
=)
# Recurse only once!
((recursionLevel > 1)) && return 1
if [[ ${words[optionIndex - 1]} == -* ]]; then
_hledger_compreply_optarg $((optionIndex - 1)) $((recursionLevel + 1))
error=$?
fi
;;
*) *)
error=1 return 1
;; ;;
esac esac
return $error return 0
} }
# Query filter completion through introspection # Query filter completion through introspection