refactor: apply maintainer feedback to SSH integration scripts across all shells

- Update Bash script (baseline): Simplify cache checking logic, clarify
"xterm-ghostty terminfo" message, remove unnecessary ssh_opts from
terminfo installation, remove extra success message
- Align ZSH/Fish/Elvish with updated Bash: Remove extra success
messages, adopt simplified cache checking, standardize setup messages
- Apply Elvish improvements: Remove unnecessary try/catch blocks, use
idiomatic error handling patterns
- Apply Fish improvements: Replace string pattern matching with
efficient `contains` checks on split features list
This commit is contained in:
Jason Rayne
2025-07-08 10:45:42 -07:00
parent 740c9c6644
commit f95476b181
4 changed files with 56 additions and 113 deletions

View File

@ -124,12 +124,7 @@ if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-* ]]; then
if [[ -n "$ssh_hostname" ]]; then if [[ -n "$ssh_hostname" ]]; then
# Check if terminfo is already cached # Check if terminfo is already cached
builtin local ssh_cache_check_success=false if ghostty +ssh-cache --host="$ssh_target" >/dev/null 2>&1; then
if builtin command -v ghostty >/dev/null 2>&1; then
ghostty +ssh-cache --host="$ssh_target" >/dev/null 2>&1 && ssh_cache_check_success=true
fi
if [[ "$ssh_cache_check_success" == "true" ]]; then
ssh_term="xterm-ghostty" ssh_term="xterm-ghostty"
elif builtin command -v infocmp >/dev/null 2>&1; then elif builtin command -v infocmp >/dev/null 2>&1; then
builtin local ssh_terminfo ssh_cpath_dir ssh_cpath builtin local ssh_terminfo ssh_cpath_dir ssh_cpath
@ -137,18 +132,17 @@ if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-* ]]; then
ssh_terminfo=$(infocmp -0 -x xterm-ghostty 2>/dev/null) ssh_terminfo=$(infocmp -0 -x xterm-ghostty 2>/dev/null)
if [[ -n "$ssh_terminfo" ]]; then if [[ -n "$ssh_terminfo" ]]; then
builtin echo "Setting up Ghostty terminfo on $ssh_hostname..." >&2 builtin echo "Setting up xterm-ghostty terminfo on $ssh_hostname..." >&2
ssh_cpath_dir=$(mktemp -d "/tmp/ghostty-ssh-$ssh_user.XXXXXX" 2>/dev/null) || ssh_cpath_dir="/tmp/ghostty-ssh-$ssh_user.$$" ssh_cpath_dir=$(mktemp -d "/tmp/ghostty-ssh-$ssh_user.XXXXXX" 2>/dev/null) || ssh_cpath_dir="/tmp/ghostty-ssh-$ssh_user.$$"
ssh_cpath="$ssh_cpath_dir/socket" ssh_cpath="$ssh_cpath_dir/socket"
if builtin echo "$ssh_terminfo" | builtin command ssh "${ssh_opts[@]}" -o ControlMaster=yes -o ControlPath="$ssh_cpath" -o ControlPersist=60s "$@" ' if builtin echo "$ssh_terminfo" | builtin command ssh -o ControlMaster=yes -o ControlPath="$ssh_cpath" -o ControlPersist=60s "$@" '
infocmp xterm-ghostty >/dev/null 2>&1 && exit 0 infocmp xterm-ghostty >/dev/null 2>&1 && exit 0
command -v tic >/dev/null 2>&1 || exit 1 command -v tic >/dev/null 2>&1 || exit 1
mkdir -p ~/.terminfo 2>/dev/null && tic -x - 2>/dev/null && exit 0 mkdir -p ~/.terminfo 2>/dev/null && tic -x - 2>/dev/null && exit 0
exit 1 exit 1
' 2>/dev/null; then ' 2>/dev/null; then
builtin echo "Terminfo setup complete on $ssh_hostname." >&2
ssh_term="xterm-ghostty" ssh_term="xterm-ghostty"
ssh_opts+=(-o "ControlPath=$ssh_cpath") ssh_opts+=(-o "ControlPath=$ssh_cpath")

View File

@ -108,8 +108,10 @@
# Configure environment variables for remote session # Configure environment variables for remote session
if (str:contains $E:GHOSTTY_SHELL_FEATURES ssh-env) { if (str:contains $E:GHOSTTY_SHELL_FEATURES ssh-env) {
set ssh-opts = [$@ssh-opts -o "SetEnv COLORTERM=truecolor"] set ssh-opts = (conj $ssh-opts
set ssh-opts = [$@ssh-opts -o "SendEnv TERM_PROGRAM TERM_PROGRAM_VERSION"] -o "SetEnv COLORTERM=truecolor"
-o "SendEnv TERM_PROGRAM TERM_PROGRAM_VERSION"
)
} }
# Install terminfo on remote host if needed # Install terminfo on remote host if needed
@ -117,112 +119,71 @@
var ssh-user = "" var ssh-user = ""
var ssh-hostname = "" var ssh-hostname = ""
try { # Parse ssh config
var ssh-config = (external ssh -G $@args 2>/dev/null | slurp) var ssh-config = (external ssh -G $@args 2>/dev/null | slurp)
for line (str:split "\n" $ssh-config) { for line (str:split "\n" $ssh-config) {
var parts = (str:split " " $line) var parts = (str:split " " $line)
if (> (count $parts) 1) { if (> (count $parts) 1) {
if (eq $parts[0] user) { var ssh-key = $parts[0]
set ssh-user = $parts[1] var ssh-value = $parts[1]
} elif (eq $parts[0] hostname) { if (eq $ssh-key user) {
set ssh-hostname = $parts[1] set ssh-user = $ssh-value
} } elif (eq $ssh-key hostname) {
if (and (not-eq $ssh-user "") (not-eq $ssh-hostname "")) { set ssh-hostname = $ssh-value
break }
} if (and (not-eq $ssh-user "") (not-eq $ssh-hostname "")) {
break
} }
} }
} catch {
# ssh config failed
} }
var ssh-target = $ssh-user"@"$ssh-hostname var ssh-target = $ssh-user"@"$ssh-hostname
if (not-eq $ssh-hostname "") { if (not-eq $ssh-hostname "") {
# Check if terminfo is already cached # Check if terminfo is already cached
var ssh-cache-check-success = $false if (and (has-external ghostty) (bool ?(external ghostty +ssh-cache --host=$ssh-target >/dev/null 2>&1))) {
try {
external ghostty +ssh-cache --host=$ssh-target >/dev/null 2>&1
set ssh-cache-check-success = $true
} catch {
# cache check failed
}
if $ssh-cache-check-success {
set ssh-term = "xterm-ghostty" set ssh-term = "xterm-ghostty"
} else { } elif (has-external infocmp) {
try { var ssh-terminfo = (external infocmp -0 -x xterm-ghostty 2>/dev/null | slurp)
external infocmp --help >/dev/null 2>&1
if (not-eq $ssh-terminfo "") {
echo "Setting up xterm-ghostty terminfo on "$ssh-hostname"..." >&2
var ssh-terminfo = ""
var ssh-cpath-dir = "" var ssh-cpath-dir = ""
var ssh-cpath = ""
try { try {
set ssh-terminfo = (external infocmp -0 -x xterm-ghostty 2>/dev/null | slurp) set ssh-cpath-dir = (external mktemp -d "/tmp/ghostty-ssh-"$ssh-user".XXXXXX" 2>/dev/null | slurp)
} catch { } catch {
set ssh-terminfo = "" set ssh-cpath-dir = "/tmp/ghostty-ssh-"$ssh-user"."(randint 10000 99999)
} }
var ssh-cpath = $ssh-cpath-dir"/socket"
if (not-eq $ssh-terminfo "") { if (bool ?(echo $ssh-terminfo | external ssh $@ssh-opts -o ControlMaster=yes -o ControlPath=$ssh-cpath -o ControlPersist=60s $@args '
echo "Setting up Ghostty terminfo on "$ssh-hostname"..." >&2 infocmp xterm-ghostty >/dev/null 2>&1 && exit 0
command -v tic >/dev/null 2>&1 || exit 1
mkdir -p ~/.terminfo 2>/dev/null && tic -x - 2>/dev/null && exit 0
exit 1
' 2>/dev/null)) {
set ssh-term = "xterm-ghostty"
set ssh-opts = (conj $ssh-opts -o ControlPath=$ssh-cpath)
try { # Cache successful installation
set ssh-cpath-dir = (external mktemp -d "/tmp/ghostty-ssh-"$ssh-user".XXXXXX" 2>/dev/null | slurp) if (and (not-eq $ssh-target "") (has-external ghostty)) {
} catch { external ghostty +ssh-cache --add=$ssh-target >/dev/null 2>&1
set ssh-cpath-dir = "/tmp/ghostty-ssh-"$ssh-user"."(randint 10000 99999)
}
set ssh-cpath = $ssh-cpath-dir"/socket"
var terminfo-install-success = $false
try {
echo $ssh-terminfo | external ssh $@ssh-opts -o ControlMaster=yes -o ControlPath=$ssh-cpath -o ControlPersist=60s $@args '
infocmp xterm-ghostty >/dev/null 2>&1 && exit 0
command -v tic >/dev/null 2>&1 || exit 1
mkdir -p ~/.terminfo 2>/dev/null && tic -x - 2>/dev/null && exit 0
exit 1
' >/dev/null 2>&1
set terminfo-install-success = $true
} catch {
set terminfo-install-success = $false
}
if $terminfo-install-success {
echo "Terminfo setup complete on "$ssh-hostname"." >&2
set ssh-term = "xterm-ghostty"
set ssh-opts = [$@ssh-opts -o ControlPath=$ssh-cpath]
# Cache successful installation
if (and (not-eq $ssh-target "") (has-external ghostty)) {
try {
external ghostty +ssh-cache --add=$ssh-target >/dev/null 2>&1
} catch {
# cache add failed
}
}
} else {
echo "Warning: Failed to install terminfo." >&2
} }
} else { } else {
echo "Warning: Could not generate terminfo data." >&2 echo "Warning: Failed to install terminfo." >&2
} }
} catch { } else {
echo "Warning: ghostty command not available for cache management." >&2 echo "Warning: Could not generate terminfo data." >&2
} }
} else {
echo "Warning: ghostty command not available for cache management." >&2
} }
} }
} }
# Execute SSH with TERM environment variable # Execute SSH with TERM environment variable
var old-term = $E:TERM external E:TERM=$ssh-term ssh $@ssh-opts $@args
set-env TERM $ssh-term
try {
external ssh $@ssh-opts $@args
} catch e {
set-env TERM $old-term
fail $e
}
set-env TERM $old-term
} }
} }

View File

@ -87,19 +87,21 @@ function __ghostty_setup --on-event fish_prompt -d "Setup ghostty integration"
end end
# SSH Integration # SSH Integration
if string match -q '*ssh-*' -- "$GHOSTTY_SHELL_FEATURES" set -l features (string split ',' -- "$GHOSTTY_SHELL_FEATURES")
if contains ssh-env $features; or contains ssh-terminfo $features
function ssh --wraps=ssh --description "SSH wrapper with Ghostty integration" function ssh --wraps=ssh --description "SSH wrapper with Ghostty integration"
set -l features (string split ',' -- "$GHOSTTY_SHELL_FEATURES")
set -l ssh_term "xterm-256color" set -l ssh_term "xterm-256color"
set -l ssh_opts set -l ssh_opts
# Configure environment variables for remote session # Configure environment variables for remote session
if string match -q '*ssh-env*' -- "$GHOSTTY_SHELL_FEATURES" if contains ssh-env $features
set -a ssh_opts -o "SetEnv COLORTERM=truecolor" set -a ssh_opts -o "SetEnv COLORTERM=truecolor"
set -a ssh_opts -o "SendEnv TERM_PROGRAM TERM_PROGRAM_VERSION" set -a ssh_opts -o "SendEnv TERM_PROGRAM TERM_PROGRAM_VERSION"
end end
# Install terminfo on remote host if needed # Install terminfo on remote host if needed
if string match -q '*ssh-terminfo*' -- "$GHOSTTY_SHELL_FEATURES" if contains ssh-terminfo $features
set -l ssh_user set -l ssh_user
set -l ssh_hostname set -l ssh_hostname
@ -122,14 +124,7 @@ function __ghostty_setup --on-event fish_prompt -d "Setup ghostty integration"
if test -n "$ssh_hostname" if test -n "$ssh_hostname"
# Check if terminfo is already cached # Check if terminfo is already cached
set -l ssh_cache_check_success false if command -v ghostty >/dev/null 2>&1; and ghostty +ssh-cache --host="$ssh_target" >/dev/null 2>&1
if command -v ghostty >/dev/null 2>&1
if ghostty +ssh-cache --host="$ssh_target" >/dev/null 2>&1
set ssh_cache_check_success true
end
end
if test "$ssh_cache_check_success" = "true"
set ssh_term "xterm-ghostty" set ssh_term "xterm-ghostty"
else if command -v infocmp >/dev/null 2>&1 else if command -v infocmp >/dev/null 2>&1
set -l ssh_terminfo set -l ssh_terminfo
@ -139,7 +134,7 @@ function __ghostty_setup --on-event fish_prompt -d "Setup ghostty integration"
set ssh_terminfo (infocmp -0 -x xterm-ghostty 2>/dev/null) set ssh_terminfo (infocmp -0 -x xterm-ghostty 2>/dev/null)
if test -n "$ssh_terminfo" if test -n "$ssh_terminfo"
echo "Setting up Ghostty terminfo on $ssh_hostname..." >&2 echo "Setting up xterm-ghostty terminfo on $ssh_hostname..." >&2
set ssh_cpath_dir (mktemp -d "/tmp/ghostty-ssh-$ssh_user.XXXXXX" 2>/dev/null; or echo "/tmp/ghostty-ssh-$ssh_user."(random)) set ssh_cpath_dir (mktemp -d "/tmp/ghostty-ssh-$ssh_user.XXXXXX" 2>/dev/null; or echo "/tmp/ghostty-ssh-$ssh_user."(random))
set ssh_cpath "$ssh_cpath_dir/socket" set ssh_cpath "$ssh_cpath_dir/socket"
@ -150,7 +145,6 @@ function __ghostty_setup --on-event fish_prompt -d "Setup ghostty integration"
mkdir -p ~/.terminfo 2>/dev/null && tic -x - 2>/dev/null && exit 0 mkdir -p ~/.terminfo 2>/dev/null && tic -x - 2>/dev/null && exit 0
exit 1 exit 1
' 2>/dev/null ' 2>/dev/null
echo "Terminfo setup complete on $ssh_hostname." >&2
set ssh_term "xterm-ghostty" set ssh_term "xterm-ghostty"
set -a ssh_opts -o "ControlPath=$ssh_cpath" set -a ssh_opts -o "ControlPath=$ssh_cpath"

View File

@ -276,12 +276,7 @@ _ghostty_deferred_init() {
if [[ -n "$ssh_hostname" ]]; then if [[ -n "$ssh_hostname" ]]; then
# Check if terminfo is already cached # Check if terminfo is already cached
local ssh_cache_check_success=false if (( $+commands[ghostty] )) && ghostty +ssh-cache --host="$ssh_target" >/dev/null 2>&1; then
if (( $+commands[ghostty] )); then
ghostty +ssh-cache --host="$ssh_target" >/dev/null 2>&1 && ssh_cache_check_success=true
fi
if [[ "$ssh_cache_check_success" == "true" ]]; then
ssh_term="xterm-ghostty" ssh_term="xterm-ghostty"
elif (( $+commands[infocmp] )); then elif (( $+commands[infocmp] )); then
local ssh_terminfo ssh_cpath_dir ssh_cpath local ssh_terminfo ssh_cpath_dir ssh_cpath
@ -289,7 +284,7 @@ _ghostty_deferred_init() {
ssh_terminfo=$(infocmp -0 -x xterm-ghostty 2>/dev/null) ssh_terminfo=$(infocmp -0 -x xterm-ghostty 2>/dev/null)
if [[ -n "$ssh_terminfo" ]]; then if [[ -n "$ssh_terminfo" ]]; then
print "Setting up Ghostty terminfo on $ssh_hostname..." >&2 print "Setting up xterm-ghostty terminfo on $ssh_hostname..." >&2
ssh_cpath_dir=$(mktemp -d "/tmp/ghostty-ssh-$ssh_user.XXXXXX" 2>/dev/null) || ssh_cpath_dir="/tmp/ghostty-ssh-$ssh_user.$$" ssh_cpath_dir=$(mktemp -d "/tmp/ghostty-ssh-$ssh_user.XXXXXX" 2>/dev/null) || ssh_cpath_dir="/tmp/ghostty-ssh-$ssh_user.$$"
ssh_cpath="$ssh_cpath_dir/socket" ssh_cpath="$ssh_cpath_dir/socket"
@ -300,7 +295,6 @@ _ghostty_deferred_init() {
mkdir -p ~/.terminfo 2>/dev/null && tic -x - 2>/dev/null && exit 0 mkdir -p ~/.terminfo 2>/dev/null && tic -x - 2>/dev/null && exit 0
exit 1 exit 1
' 2>/dev/null; then ' 2>/dev/null; then
print "Terminfo setup complete on $ssh_hostname." >&2
ssh_term="xterm-ghostty" ssh_term="xterm-ghostty"
ssh_opts+=(-o "ControlPath=$ssh_cpath") ssh_opts+=(-o "ControlPath=$ssh_cpath")