From 67e3bac583409dcc44f38c79ff951ee213bfe075 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Tue, 26 Sep 2017 09:21:16 -0500 Subject: [PATCH] Speed up __fish_print_hostnames for faster completions Thanks to @ThomasAH, as per #4378. Tested on many platforms (OS X, FreeBSD, Linux, and Solaris). Works with IPv4 and IPv6 as well as host names and loopback addresses. (cherry picked from commit 3b3bcc998e9c27b910f362eb9b6f24f752b4ca5c) --- share/functions/__fish_print_hostnames.fish | 26 ++++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/share/functions/__fish_print_hostnames.fish b/share/functions/__fish_print_hostnames.fish index c3f392a3b..a6d4ef991 100644 --- a/share/functions/__fish_print_hostnames.fish +++ b/share/functions/__fish_print_hostnames.fish @@ -94,16 +94,24 @@ function __fish_print_hostnames -d "Print a list of known hostnames" set known_hosts $known_hosts (string replace -rfi '.*KnownHostsFile\s*' '' <$file) end end - for file in $known_hosts + for file in $known_hosts if test -r $file - # Ignore hosts that are hashed, commented or @-marked and strip the key. - string replace -r '[#@| ].*' '' <$file \ - # Split on ",". - # Ignore negated/wildcarded hosts. - | string split "," | string match -rv '[!\*\?]' \ - # Extract hosts with custom port. - | string replace -r '\[([^]]+)\]:.*' '$1' - # string replace -ra '(\S+) .*' '$1' <$file | string match -r '^[^#|[=]+$' | string split "," + # Ignore hosts that are hashed, commented or @-marked and strip the key. + awk '$1 !~ /[|#@]/ { + n=split($1, entries, ",") + for (i=1; i<=n; i++) { + # Ignore negated/wildcarded hosts. + if (!match(entry=entries[i], "[!*?]")) { + # Extract hosts with custom port. + if (substr(entry, 1, 1) == "[") { + if (pos=match(entry, "]:.*$")) { + entry=substr(entry, 2, pos-2) + } + } + print entry + } + } + }' $file end end return 0