feat: simplify fzf scripts and extract FZF_DEFAULT_OPTS

This commit is contained in:
2026-03-11 15:01:25 +01:00
parent 574a190468
commit f0aea1112a
5 changed files with 51 additions and 127 deletions

View File

@@ -4,72 +4,50 @@ function _ssh_menu_preview {
file=$1; shift
host=$1; shift
j=$(cat $file \
| jc --ssh-conf \
| jq -r ".[] \
| select(.host == \"$host\") \
| .jumps = (\
if (.proxyjump | type == \"array\" and length > 0) then \
(.proxyjump | join(\",\")) \
else \
\"null\" \
end \
)" \
)
user=$(jq -r '.user' <<<$j)
port=$(jq -r '.port' <<<$j)
hostname=$(jq -r '.hostname' <<<$j)
jumps=$(jq -r '.jumps' <<<$j)
echo "$(cat <<EOF
User $user
HostName $hostname\
$([[ $port != "null" ]] && echo -e "\nPort $port")\
$([[ $jumps != "null" ]] && echo -e "\nProxyJump $jumps")
command:
ssh $user@$hostname$([[ $port != "null" ]] && echo -n " -p $port")$([[ $jumps != "null" ]] && echo -n " -J $jumps")
EOF
)"
# show host block from ssh config
awk -v h="$host" '
$0 == "Host " h { found=1 }
found && /^[^ \t]/ && $0 != "Host " h { exit }
found
' "$file"
echo ""
if command -v host 2>&1 >/dev/null; then
host $hostname
# resolve hostname for reachability check
hostname=$(awk -v h="$host" '
$0 == "Host " h { found=1; next }
found && /^[^ \t]/ { exit }
found && /HostName/ { print $2 }
' "$file")
if command -v host &>/dev/null; then
host "${hostname:-$host}"
echo ""
fi
ping -c 1 -W 1 $hostname &>/dev/null \
&& echo "Host is reachable!" \
|| echo "Host is not reachable!"
ping -c 1 -W 1 "${hostname:-$host}" &>/dev/null \
&& echo "reachable" \
|| echo "not reachable"
}
export -f _ssh_menu_preview
function ssh-menu {
selected="$(rg -N --no-heading '^Host .*' ~/.ssh \
function _ssh_menu_select {
rg -N --no-heading '^Host .*' ~/.ssh \
| sed 's/Host \(.*\)/\1/' | sort \
| SHELL=$(which bash) fzf --cycle \
--bind 'tab:toggle-up,btab:toggle-down' \
| SHELL=$(which bash) fzf \
--delimiter ':' \
--with-nth 2 \
--preview "_ssh_menu_preview {1} {2}" \
--preview-label "ssh config info" \
--header "Navigate with ARROW KEYS or TAB/S-TAB. Select with ENTER." \
--border "double" \
--border-label "ssh-menu" \
--ansi \
--highlight-line \
| cut -d ":" -f2
)"
if [[ -z $selected ]]; then
return
fi
ssh "$selected"
}
ssh-menu
case "${1:-}" in
-s) _ssh_menu_select ;;
*)
selected="$(_ssh_menu_select)"
[[ -n $selected ]] && ssh "$selected"
;;
esac