Add optional arguments $prefix and $match to _hledger_compgen()
This allows more flexibility when generating completion candidates and we don't need to resort to external help from `sed` or others. _hledger_compgen's arguments become: $1 -> $wordlist: a newline separated wordlist with completion cadidates $2 -> $prefix: (optional) a prefix string to add to generated completions $3 -> $match: (optional) a word to match instead of $cur, the default. If $match is null and $prefix is defined the match is done against $cur stripped of $prefix. If both $prefix and $match are null we match against $cur and no prefix is added to completions. Of course you can also pass an empty string as $prefix and set $match to whatever you wish.
This commit is contained in:
parent
10cc8b72b9
commit
fc89340c93
@ -158,8 +158,18 @@ _hledger_compreply_append() {
|
|||||||
# wordlist to compgen -- it will eat your quotes, drink your booze and...
|
# wordlist to compgen -- it will eat your quotes, drink your booze and...
|
||||||
# Completion candidates are quoted accordingly first and then we leave it to
|
# Completion candidates are quoted accordingly first and then we leave it to
|
||||||
# compgen to deal with readline.
|
# compgen to deal with readline.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# $1: a newline separated wordlist with completion cadidates
|
||||||
|
# $2: (optional) a prefix string to add to generated completions
|
||||||
|
# $3: (optional) a word to match instead of $cur, the default.
|
||||||
|
# If $match is null and $prefix is defined the match is done against $cur
|
||||||
|
# stripped of $prefix. If both $prefix and $match are null we match against
|
||||||
|
# $cur and no prefix is added to completions.
|
||||||
_hledger_compgen() {
|
_hledger_compgen() {
|
||||||
local wordlist=$1
|
local wordlist=$1
|
||||||
|
local prefix=$2
|
||||||
|
local match=$3
|
||||||
local quoted=()
|
local quoted=()
|
||||||
local word
|
local word
|
||||||
local i=0
|
local i=0
|
||||||
@ -170,7 +180,7 @@ _hledger_compgen() {
|
|||||||
done <<< "$wordlist"
|
done <<< "$wordlist"
|
||||||
|
|
||||||
local IFS=$'\n'
|
local IFS=$'\n'
|
||||||
compgen -W "${quoted[*]}" -- "$cur"
|
compgen -P "$prefix" -W "${quoted[*]}" -- "${match:-${cur:${#prefix}}}"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Try required option argument completion. Set COMPREPLY and return 0 on
|
# Try required option argument completion. Set COMPREPLY and return 0 on
|
||||||
@ -258,8 +268,8 @@ _hledger_compreply_query() {
|
|||||||
|
|
||||||
_hledger_compreply "$(
|
_hledger_compreply "$(
|
||||||
_hledger_compgen "$(
|
_hledger_compgen "$(
|
||||||
_hledger "${hledgerArgs[@]}" | sed "s/^/$query/g"
|
_hledger "${hledgerArgs[@]}"
|
||||||
)"
|
)" "$query"
|
||||||
)"
|
)"
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
@ -158,8 +158,18 @@ _hledger_compreply_append() {
|
|||||||
# wordlist to compgen -- it will eat your quotes, drink your booze and...
|
# wordlist to compgen -- it will eat your quotes, drink your booze and...
|
||||||
# Completion candidates are quoted accordingly first and then we leave it to
|
# Completion candidates are quoted accordingly first and then we leave it to
|
||||||
# compgen to deal with readline.
|
# compgen to deal with readline.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# $1: a newline separated wordlist with completion cadidates
|
||||||
|
# $2: (optional) a prefix string to add to generated completions
|
||||||
|
# $3: (optional) a word to match instead of $cur, the default.
|
||||||
|
# If $match is null and $prefix is defined the match is done against $cur
|
||||||
|
# stripped of $prefix. If both $prefix and $match are null we match against
|
||||||
|
# $cur and no prefix is added to completions.
|
||||||
_hledger_compgen() {
|
_hledger_compgen() {
|
||||||
local wordlist=$1
|
local wordlist=$1
|
||||||
|
local prefix=$2
|
||||||
|
local match=$3
|
||||||
local quoted=()
|
local quoted=()
|
||||||
local word
|
local word
|
||||||
local i=0
|
local i=0
|
||||||
@ -170,7 +180,7 @@ _hledger_compgen() {
|
|||||||
done <<< "$wordlist"
|
done <<< "$wordlist"
|
||||||
|
|
||||||
local IFS=$'\n'
|
local IFS=$'\n'
|
||||||
compgen -W "${quoted[*]}" -- "$cur"
|
compgen -P "$prefix" -W "${quoted[*]}" -- "${match:-${cur:${#prefix}}}"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Try required option argument completion. Set COMPREPLY and return 0 on
|
# Try required option argument completion. Set COMPREPLY and return 0 on
|
||||||
@ -258,8 +268,8 @@ _hledger_compreply_query() {
|
|||||||
|
|
||||||
_hledger_compreply "$(
|
_hledger_compreply "$(
|
||||||
_hledger_compgen "$(
|
_hledger_compgen "$(
|
||||||
_hledger "${hledgerArgs[@]}" | sed "s/^/$query/g"
|
_hledger "${hledgerArgs[@]}"
|
||||||
)"
|
)" "$query"
|
||||||
)"
|
)"
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user