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:
Vladimir Zhelezov 2020-12-09 12:23:09 +01:00
parent 10cc8b72b9
commit fc89340c93
2 changed files with 26 additions and 6 deletions

View File

@ -158,8 +158,18 @@ _hledger_compreply_append() {
# wordlist to compgen -- it will eat your quotes, drink your booze and...
# Completion candidates are quoted accordingly first and then we leave it to
# 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() {
local wordlist=$1
local prefix=$2
local match=$3
local quoted=()
local word
local i=0
@ -170,7 +180,7 @@ _hledger_compgen() {
done <<< "$wordlist"
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
@ -258,8 +268,8 @@ _hledger_compreply_query() {
_hledger_compreply "$(
_hledger_compgen "$(
_hledger "${hledgerArgs[@]}" | sed "s/^/$query/g"
)"
_hledger "${hledgerArgs[@]}"
)" "$query"
)"
return 0

View File

@ -158,8 +158,18 @@ _hledger_compreply_append() {
# wordlist to compgen -- it will eat your quotes, drink your booze and...
# Completion candidates are quoted accordingly first and then we leave it to
# 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() {
local wordlist=$1
local prefix=$2
local match=$3
local quoted=()
local word
local i=0
@ -170,7 +180,7 @@ _hledger_compgen() {
done <<< "$wordlist"
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
@ -258,8 +268,8 @@ _hledger_compreply_query() {
_hledger_compreply "$(
_hledger_compgen "$(
_hledger "${hledgerArgs[@]}" | sed "s/^/$query/g"
)"
_hledger "${hledgerArgs[@]}"
)" "$query"
)"
return 0