From f1d306feab0d6ec91d440c61e54f2280f140de68 Mon Sep 17 00:00:00 2001 From: Christoph Anton Mitterer Date: Tue, 26 Sep 2023 20:21:09 +0200 Subject: [PATCH] [shell] move username prefixing code where needed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `__fzf_list_hosts()` seems like a function a user may want to override with some custom code. For that reason it should be kept as simple as possible, that is printing only hostnames, one per line, optionally in some sorting. The handling of adding a `username@` (which is then the same for each line), if any, would unnecessarily complicate that for people who want to override the function. Therefore this commit moves that to the places where it's actually used (as of now only `_fzf_complete_ssh()`). This also saves any such handling for `_fzf_host_completion()`, where this isn’t needed at all. Right now it comes at a cost, namely an extra invocation of `awk` in the `_fzf_complete_ssh()`-case. However, it should be easily possible to improve `__fzf_list_hosts()` to no longer need the final `awk` in the pipeline there. Signed-off-by: Christoph Anton Mitterer --- shell/completion.bash | 6 +++--- shell/completion.zsh | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/shell/completion.bash b/shell/completion.bash index f3dd19ef..e5f2927e 100644 --- a/shell/completion.bash +++ b/shell/completion.bash @@ -411,11 +411,11 @@ __fzf_list_hosts() { command cat <(command tail -n +1 ~/.ssh/config ~/.ssh/config.d/* /etc/ssh/ssh_config 2> /dev/null | command grep -i '^\s*host\(name\)\? ' | command awk '{for (i = 2; i <= NF; i++) print $1 " " $i}' | command grep -v '[*?%]') \ <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts 2> /dev/null | command tr ',' '\n' | command tr -d '[' | command awk '{ print $1 " " $1 }') \ <(command grep -v '^\s*\(#\|$\)' /etc/hosts 2> /dev/null | command grep -Fv '0.0.0.0') | - command awk -v "user=$1" '{if (length($2) > 0) {print user $2}}' | command sort -u + command awk '{if (length($2) > 0) {print $2}}' | command sort -u } _fzf_host_completion() { - _fzf_complete +m -- "$@" < <(__fzf_list_hosts "") + _fzf_complete +m -- "$@" < <(__fzf_list_hosts) } # Values for $1 $2 $3 are described here @@ -431,7 +431,7 @@ _fzf_complete_ssh() { *) local user= [[ "$2" =~ '@' ]] && user="${2%%@*}@" - _fzf_complete +m -- "$@" < <(__fzf_list_hosts "$user") + _fzf_complete +m -- "$@" < <(__fzf_list_hosts | command awk -v user="$user" '{print user $0}') ;; esac } diff --git a/shell/completion.zsh b/shell/completion.zsh index f76d8af3..c72d61cb 100644 --- a/shell/completion.zsh +++ b/shell/completion.zsh @@ -223,11 +223,11 @@ __fzf_list_hosts() { command cat <(command tail -n +1 ~/.ssh/config ~/.ssh/config.d/* /etc/ssh/ssh_config 2> /dev/null | command grep -i '^\s*host\(name\)\? ' | awk '{for (i = 2; i <= NF; i++) print $1 " " $i}' | command grep -v '[*?%]') \ <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts 2> /dev/null | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \ <(command grep -v '^\s*\(#\|$\)' /etc/hosts 2> /dev/null | command grep -Fv '0.0.0.0') | - awk -v "user=$1" '{if (length($2) > 0) {print user $2}}' | sort -u + awk '{if (length($2) > 0) {print $2}}' | sort -u } _fzf_complete_telnet() { - _fzf_complete +m -- "$@" < <(__fzf_list_hosts "") + _fzf_complete +m -- "$@" < <(__fzf_list_hosts) } # The first and the only argument is the LBUFFER without the current word that contains the trigger. @@ -241,7 +241,7 @@ _fzf_complete_ssh() { *) local user= [[ $prefix =~ @ ]] && user="${prefix%%@*}@" - _fzf_complete +m -- "$@" < <(__fzf_list_hosts "$user") + _fzf_complete +m -- "$@" < <(__fzf_list_hosts | awk -v user="$user" '{print user $0}') ;; esac }