Skip to content

Commit

Permalink
fix(portinstall): use awk and _comp_compgen to filter completions
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Oct 3, 2023
1 parent 434eb08 commit 791f56c
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions completions/portinstall
Expand Up @@ -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
Expand Down

0 comments on commit 791f56c

Please sign in to comment.