From 791f56c62656ba19c4db804b7ccf8c2e8ba1d55a Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Tue, 30 May 2023 20:47:39 +0900 Subject: [PATCH] fix(portinstall): use awk and _comp_compgen to filter completions --- completions/portinstall | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/completions/portinstall b/completions/portinstall index 8799e6072fc..fa658e74ec7 100644 --- a/completions/portinstall +++ b/completions/portinstall @@ -7,24 +7,20 @@ _comp_cmd_portinstall() [[ $prev == -l || $prev == -L || $prev == -o ]] && return - local portsdir indexfile - local -a COMPREPLY2 - - portsdir=${PORTSDIR:-/usr/ports}/ + local -x portsdir=${PORTSDIR:-/usr/ports}/ # First try INDEX-5 - indexfile=$portsdir/INDEX-5 + local indexfile=$portsdir/INDEX-5 # Then INDEX if INDEX-5 does not exist or system is not FreeBSD 5.x [[ ${OSTYPE%.*} == freebsd5 && -f $indexfile ]] || indexfile=$portsdir/INDEX [[ -f $indexfile && -r $indexfile ]] || return - COMPREPLY=($(command grep -E "^$cur" 2>/dev/null <"$indexfile" | - cut -d'|' -f1)) - COMPREPLY2=($(command grep -E "^[^\|]+\|$portsdir$cur" 2>/dev/null \ - <"$indexfile" | cut -d'|' -f2)) - COMPREPLY2=(${COMPREPLY2[@]#$portsdir}) - COMPREPLY+=("${COMPREPLY2[@]}") + _comp_compgen_split -l -- "$(awk -F '|' ' + BEGIN { portsdir = ENVIRON["portsdir"]; len = length(portsdir) } + { print $1 } + substr($2, 1, len) == portsdir { print substr($2, len + 1) } + ' "$indexfile")" } && complete -F _comp_cmd_portinstall -o dirnames portinstall