From b54303b509d30e21679e0032e0875300c7b8b7dc Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 7 Dec 2018 01:01:49 +1300 Subject: Refactor some completions to avoid loops --- bash/bash_completion.d/_ssh_config_hosts.bash | 43 ++++++++++++--------------- bash/bash_completion.d/gpg.bash | 13 +++----- bash/bash_completion.d/openssl.bash | 27 +++++++---------- 3 files changed, 33 insertions(+), 50 deletions(-) (limited to 'bash') diff --git a/bash/bash_completion.d/_ssh_config_hosts.bash b/bash/bash_completion.d/_ssh_config_hosts.bash index 3f937a2a..0959f52b 100644 --- a/bash/bash_completion.d/_ssh_config_hosts.bash +++ b/bash/bash_completion.d/_ssh_config_hosts.bash @@ -1,31 +1,26 @@ # Complete ssh_config(5) hostnames _ssh_config_hosts() { - # Iterate through words from a subshell - local ci comp - while read -r comp ; do - COMPREPLY[ci++]=$comp - done < <( + # Iterate through SSH client config paths + local config + for config in "$HOME"/.ssh/config /etc/ssh/ssh_config ; do + [[ -e $config ]] || continue - # Iterate through SSH client config paths - for config in "$HOME"/.ssh/config /etc/ssh/ssh_config ; do - [[ -e $config ]] || continue + # Read 'Host' options and their first value from file + local option value ci + while read -r option value _ ; do + [[ $option == Host ]] || continue - # Read 'Host' options and their first value from file - while read -r option value _ ; do - [[ $option == Host ]] || continue + # Check host value + case $value in + # No empties + '') ;; + # No wildcards + *'*'*) ;; + # Found a match; print it + "$2"*) COMPREPLY[ci++]=$value ;; + esac - # Check host value - case $value in - # No empties - ('') ;; - # No wildcards - (*'*'*) ;; - # Found a match; print it - ("$2"*) printf '%s\n' "$value" ;; - esac - - done < "$config" - done - ) + done < "$config" + done } diff --git a/bash/bash_completion.d/gpg.bash b/bash/bash_completion.d/gpg.bash index c6f92676..5a055352 100644 --- a/bash/bash_completion.d/gpg.bash +++ b/bash/bash_completion.d/gpg.bash @@ -13,14 +13,9 @@ _gpg() { # Generate completion reply from gpg(1) options local ci comp while read -r comp ; do - COMPREPLY[ci++]=$comp - done < <( - gpg --dump-options 2>/dev/null | - while read -r option ; do - case $option in - ("$2"*) printf '%s\n' "$option" ;; - esac - done - ) + case $comp in + "$2"*) COMPREPLY[ci++]=$comp ;; + esac + done < <(gpg --dump-options 2>/dev/null) } complete -F _gpg -o bashdefault -o default gpg diff --git a/bash/bash_completion.d/openssl.bash b/bash/bash_completion.d/openssl.bash index 1cb4bd07..b1f13103 100644 --- a/bash/bash_completion.d/openssl.bash +++ b/bash/bash_completion.d/openssl.bash @@ -8,25 +8,18 @@ _openssl() { ((COMP_CWORD == 1)) || return # Iterate through completions produced by subshell + local -a subcmds local ci comp - while read -r comp ; do - COMPREPLY[ci++]=$comp + while read -a subcmds -r ; do + for subcmd in "${subcmds[@]}" ; do + case $subcmd in + "$2"*) COMPREPLY[ci++]=$comp ;; + esac + done done < <( - - # Run each of the command-listing commands; read each line into an - # array of subcommands (they are printed as a table) - for list in commands digest-commands cipher-commands ; do - openssl list -"$list" - done | { - declare -a subcmds - while read -a subcmds -r ; do - for subcmd in "${subcmds[@]}" ; do - case $subcmd in - ("$2"*) printf '%s\n' "$subcmd" ;; - esac - done - done - } + openssl list -commands \ + -cipher-commands \ + -digest-commands ) } complete -F _openssl -o bashdefault -o default openssl -- cgit v1.2.3