function zs --description "Fuzzy-find or create a local or remote zmx session" argparse 'h/help' 'install' -- $argv or return 1 if set -q _flag_help; or test (count $argv) -gt 1 echo "Usage: zs [--install] [[user@]host]" echo "Examples:" echo " zs" echo " zs hel1" echo " zs --install finder-oscar.exe.xyz" return 0 end if test (count $argv) -eq 0 if set -q _flag_install echo "zs: --install only applies when selecting sessions on a remote host" >&2 return 2 end if not type -q zmx echo "zs: zmx is not installed or not on PATH" >&2 return 127 end set -l session_name (zmx list 2>/dev/null | __zmx_format_sessions | __zmx_choose_session --preview 'zmx history {1}') or return $status zmx attach $session_name return $status end set -l destination $argv[1] if not string match -qr '^[A-Za-z0-9._@:%+-]+$' -- $destination echo "zs: 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 \"zs: zmx is not installed on the remote host or is not on PATH; retry with zs --install HOST\" >&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