Skip to content

Commit 3822b6c

Browse files
committed
[shell] make __fzf_list_hosts() definable by the user
Just like it’s already done for `_fzf_compgen_path()` and `_fzf_compgen_dir()` allow a user to easily define his own version of `__fzf_list_hosts()`. Also add some documentation on the expected “interface” of such custom function. Signed-off-by: Christoph Anton Mitterer <[email protected]>
1 parent c64fe9d commit 3822b6c

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

Diff for: shell/completion.bash

+10-6
Original file line numberDiff line numberDiff line change
@@ -280,12 +280,16 @@ _fzf_proc_completion_post() {
280280
awk '{print $2}'
281281
}
282282
283-
__fzf_list_hosts() {
284-
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 '[*?%]') \
285-
<(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts 2> /dev/null | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \
286-
<(command grep -v '^\s*\(#\|$\)' /etc/hosts 2> /dev/null | command grep -Fv '0.0.0.0') |
287-
awk '{if (length($2) > 0) {print $2}}' | sort -u
288-
}
283+
# To use custom hostname lising, override __fzf_list_hosts.
284+
# The function is expected to print hostnames, one per line, to standard output.
285+
if ! declare -F __fzf_list_hosts > /dev/null; then
286+
__fzf_list_hosts() {
287+
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 '[*?%]') \
288+
<(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts 2> /dev/null | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \
289+
<(command grep -v '^\s*\(#\|$\)' /etc/hosts 2> /dev/null | command grep -Fv '0.0.0.0') |
290+
awk '{if (length($2) > 0) {print $2}}' | sort -u
291+
}
292+
fi
289293
290294
_fzf_host_completion() {
291295
_fzf_complete +m -- "$@" < <(__fzf_list_hosts)

Diff for: shell/completion.zsh

+11-7
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,17 @@ _fzf_complete() {
215215
command rm -f "$fifo"
216216
}
217217
218-
__fzf_list_hosts() {
219-
setopt localoptions nonomatch
220-
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 '[*?%]') \
221-
<(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts 2> /dev/null | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \
222-
<(command grep -v '^\s*\(#\|$\)' /etc/hosts 2> /dev/null | command grep -Fv '0.0.0.0') |
223-
awk '{if (length($2) > 0) {print $2}}' | sort -u
224-
}
218+
# To use custom hostname lising, override __fzf_list_hosts.
219+
# The function is expected to print hostnames, one per line, to standard output.
220+
if ! declare -F __fzf_list_hosts > /dev/null; then
221+
__fzf_list_hosts() {
222+
setopt localoptions nonomatch
223+
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 '[*?%]') \
224+
<(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts 2> /dev/null | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \
225+
<(command grep -v '^\s*\(#\|$\)' /etc/hosts 2> /dev/null | command grep -Fv '0.0.0.0') |
226+
awk '{if (length($2) > 0) {print $2}}' | sort -u
227+
}
228+
fi
225229
226230
_fzf_complete_telnet() {
227231
_fzf_complete +m -- "$@" < <(__fzf_list_hosts)

0 commit comments

Comments
 (0)