function zssh-select --description "Fuzzy-find or create a zmx session on a remote host" argparse 'h/help' 'install' -- $argv or return 1 if set -q _flag_help; or test (count $argv) -ne 1 echo "Usage: zssh-select [--install] [user@]host" echo "Example: zssh-select hel1" return 0 end set -l destination $argv[1] if not string match -qr '^[A-Za-z0-9._@:%+-]+$' -- $destination echo "zssh-select: destination contains characters that cannot be safely used in the preview command" >&2 return 2 end mkdir -p "$HOME/.ssh/controlmasters" if set -q _flag_install zmx-remote-install $destination or return $status else zmx-remote-install --upgrade-only $destination or return $status end set -l remote_list (command ssh \ -o ControlMaster=auto \ -o ControlPersist=10m \ -o ControlPath="$HOME/.ssh/controlmasters/%C" \ $destination \ "sh -c 'PATH=\"\$HOME/.local/bin:\$PATH\"; command -v zmx >/dev/null 2>&1 || { echo \"zssh-select: zmx is not installed on the remote host or is not on PATH; retry with zssh-select --install\" >&2; exit 127; }; zmx list 2>/dev/null'") or return $status set -l preview_command "ssh -o ControlMaster=auto -o ControlPersist=10m -o ControlPath=$HOME/.ssh/controlmasters/%C $destination 'sh -c '\''PATH=\"\$HOME/.local/bin:\$PATH\"; zmx history {1}'\'' '" set -l session_name (printf '%s\n' $remote_list | __zmx_format_sessions | __zmx_choose_session --preview $preview_command) or return $status zssh $destination $session_name end