refactor: simplify SSH terminfo and environment handling

- Simplify feature detection to use single wildcard check
- Replace ssh_env array with simple ssh_term string variable
- Use TERM environment prefix instead of save/restore pattern
- Remove unnecessary backgrounded subshell for cache operations
This commit is contained in:
Jason Rayne
2025-07-07 10:00:56 -07:00
parent e0035e153d
commit c3b14dff71
4 changed files with 68 additions and 166 deletions

View File

@ -96,24 +96,16 @@ if [[ "$GHOSTTY_SHELL_FEATURES" == *"sudo"* && -n "$TERMINFO" ]]; then
fi fi
# SSH Integration # SSH Integration
if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* ]] || [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-terminfo* ]]; then if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-* ]]; then
ssh() { ssh() {
builtin local ssh_env ssh_opts builtin local ssh_term ssh_opts
ssh_env=() ssh_term=""
ssh_opts=() ssh_opts=()
# Configure environment variables for remote session # Configure environment variables for remote session
if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* ]]; then if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* ]]; then
ssh_opts+=(-o "SetEnv COLORTERM=truecolor") ssh_opts+=(-o "SetEnv COLORTERM=truecolor")
ssh_opts+=(-o "SendEnv TERM_PROGRAM TERM_PROGRAM_VERSION") ssh_opts+=(-o "SendEnv TERM_PROGRAM TERM_PROGRAM_VERSION")
ssh_env+=(
"COLORTERM=truecolor"
"TERM_PROGRAM=ghostty"
)
if [[ -n "$TERM_PROGRAM_VERSION" ]]; then
ssh_env+=("TERM_PROGRAM_VERSION=$TERM_PROGRAM_VERSION")
fi
fi fi
# Install terminfo on remote host if needed # Install terminfo on remote host if needed
@ -139,11 +131,11 @@ if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* ]] || [[ "$GHOSTTY_SHELL_FEATURES"
fi fi
if [[ "$ssh_cache_check_success" == "true" ]]; then if [[ "$ssh_cache_check_success" == "true" ]]; then
ssh_env+=(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
if ! builtin command -v base64 >/dev/null 2>&1; then if ! builtin command -v base64 >/dev/null 2>&1; then
builtin echo "Warning: base64 command not available for terminfo installation." >&2 builtin echo "Warning: base64 command not available for terminfo installation." >&2
ssh_env+=(TERM=xterm-256color) ssh_term="xterm-256color"
else else
builtin local ssh_terminfo ssh_base64_decode_cmd builtin local ssh_terminfo ssh_base64_decode_cmd
@ -170,64 +162,43 @@ if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* ]] || [[ "$GHOSTTY_SHELL_FEATURES"
exit 1 exit 1
' 2>/dev/null; then ' 2>/dev/null; then
builtin echo "Terminfo setup complete on $ssh_hostname." >&2 builtin echo "Terminfo setup complete on $ssh_hostname." >&2
ssh_env+=(TERM=xterm-ghostty) ssh_term="xterm-ghostty"
ssh_opts+=(-o "ControlPath=$ssh_cpath") ssh_opts+=(-o "ControlPath=$ssh_cpath")
# Cache successful installation # Cache successful installation
if [[ -n "$ssh_target" ]] && builtin command -v ghostty >/dev/null 2>&1; then if [[ -n "$ssh_target" ]] && builtin command -v ghostty >/dev/null 2>&1; then
(
set +m
{
ghostty +ssh-cache --add="$ssh_target" >/dev/null 2>&1 || true ghostty +ssh-cache --add="$ssh_target" >/dev/null 2>&1 || true
} &
)
fi fi
else else
builtin echo "Warning: Failed to install terminfo." >&2 builtin echo "Warning: Failed to install terminfo." >&2
ssh_env+=(TERM=xterm-256color) ssh_term="xterm-256color"
fi fi
else else
builtin echo "Warning: Could not generate terminfo data." >&2 builtin echo "Warning: Could not generate terminfo data." >&2
ssh_env+=(TERM=xterm-256color) ssh_term="xterm-256color"
fi fi
fi fi
else else
builtin echo "Warning: ghostty command not available for cache management." >&2 builtin echo "Warning: ghostty command not available for cache management." >&2
ssh_env+=(TERM=xterm-256color) ssh_term="xterm-256color"
fi fi
else else
if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* ]]; then if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* ]]; then
ssh_env+=(TERM=xterm-256color) ssh_term="xterm-256color"
fi fi
fi fi
fi fi
# Execute SSH with environment handling # Execute SSH with environment handling
builtin local ssh_term_override="" if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* && -z "$ssh_term" ]]; then
for ssh_v in "${ssh_env[@]}"; do ssh_term="xterm-256color"
if [[ "$ssh_v" =~ ^TERM=(.*)$ ]]; then
ssh_term_override="${BASH_REMATCH[1]}"
break
fi
done
if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* && -z "$ssh_term_override" ]]; then
ssh_env+=(TERM=xterm-256color)
ssh_term_override="xterm-256color"
fi fi
if [[ -n "$ssh_term_override" ]]; then if [[ -n "$ssh_term" ]]; then
builtin local ssh_original_term="$TERM" TERM="$ssh_term" builtin command ssh "${ssh_opts[@]}" "$@"
builtin export TERM="$ssh_term_override"
builtin command ssh "${ssh_opts[@]}" "$@"
local ssh_ret=$?
builtin export TERM="$ssh_original_term"
else else
builtin command ssh "${ssh_opts[@]}" "$@" builtin command ssh "${ssh_opts[@]}" "$@"
local ssh_ret=$?
fi fi
return $ssh_ret
} }
fi fi

View File

@ -101,23 +101,16 @@
# SSH Integration # SSH Integration
use str use str
if (or (str:contains $E:GHOSTTY_SHELL_FEATURES ssh-env) (str:contains $E:GHOSTTY_SHELL_FEATURES ssh-terminfo)) { if (str:contains $E:GHOSTTY_SHELL_FEATURES ssh-) {
# SSH wrapper that preserves Ghostty features across remote connections
fn ssh {|@args| fn ssh {|@args|
var ssh-env = [] var ssh-term = ""
var ssh-opts = [] var ssh-opts = []
# 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 = [$@ssh-opts -o "SetEnv COLORTERM=truecolor"]
set ssh-opts = [$@ssh-opts -o "SendEnv TERM_PROGRAM TERM_PROGRAM_VERSION"] set ssh-opts = [$@ssh-opts -o "SendEnv TERM_PROGRAM TERM_PROGRAM_VERSION"]
set ssh-env = [
"COLORTERM=truecolor"
"TERM_PROGRAM=ghostty"
]
if (has-env TERM_PROGRAM_VERSION) {
set ssh-env = [$@ssh-env "TERM_PROGRAM_VERSION="$E:TERM_PROGRAM_VERSION]
}
} }
# Install terminfo on remote host if needed # Install terminfo on remote host if needed
@ -159,7 +152,7 @@
} }
if $ssh-cache-check-success { if $ssh-cache-check-success {
set ssh-env = [$@ssh-env TERM=xterm-ghostty] set ssh-term = "xterm-ghostty"
} else { } else {
try { try {
external infocmp --help >/dev/null 2>&1 external infocmp --help >/dev/null 2>&1
@ -208,71 +201,58 @@
if $terminfo-install-success { if $terminfo-install-success {
echo "Terminfo setup complete on "$ssh-hostname"." >&2 echo "Terminfo setup complete on "$ssh-hostname"." >&2
set ssh-env = [$@ssh-env TERM=xterm-ghostty] set ssh-term = "xterm-ghostty"
set ssh-opts = [$@ssh-opts -o ControlPath=$ssh-cpath] set ssh-opts = [$@ssh-opts -o ControlPath=$ssh-cpath]
# Cache successful installation # Cache successful installation
if (and (not-eq $ssh-target "") (has-external ghostty)) { if (and (not-eq $ssh-target "") (has-external ghostty)) {
external ghostty +ssh-cache --add=$ssh-target >/dev/null 2>&1 & try {
external ghostty +ssh-cache --add=$ssh-target >/dev/null 2>&1
} catch {
# cache add failed
}
} }
} else { } else {
echo "Warning: Failed to install terminfo." >&2 echo "Warning: Failed to install terminfo." >&2
set ssh-env = [$@ssh-env TERM=xterm-256color] set ssh-term = "xterm-256color"
} }
} else { } else {
echo "Warning: Could not generate terminfo data." >&2 echo "Warning: Could not generate terminfo data." >&2
set ssh-env = [$@ssh-env TERM=xterm-256color] set ssh-term = "xterm-256color"
} }
} catch { } catch {
echo "Warning: base64 command not available for terminfo installation." >&2 echo "Warning: base64 command not available for terminfo installation." >&2
set ssh-env = [$@ssh-env TERM=xterm-256color] set ssh-term = "xterm-256color"
} }
} catch { } catch {
echo "Warning: ghostty command not available for cache management." >&2 echo "Warning: ghostty command not available for cache management." >&2
set ssh-env = [$@ssh-env TERM=xterm-256color] set ssh-term = "xterm-256color"
} }
} }
} else { } else {
if (str:contains $E:GHOSTTY_SHELL_FEATURES ssh-env) { if (str:contains $E:GHOSTTY_SHELL_FEATURES ssh-env) {
set ssh-env = [$@ssh-env TERM=xterm-256color] set ssh-term = "xterm-256color"
} }
} }
} }
# Execute SSH with environment handling # Execute SSH with environment handling
var ssh-term-override = "" if (and (str:contains $E:GHOSTTY_SHELL_FEATURES ssh-env) (eq $ssh-term "")) {
for ssh-v $ssh-env { set ssh-term = "xterm-256color"
if (str:has-prefix $ssh-v TERM=) {
set ssh-term-override = (str:trim-prefix $ssh-v TERM=)
break
}
} }
if (and (str:contains $E:GHOSTTY_SHELL_FEATURES ssh-env) (eq $ssh-term-override "")) { if (not-eq $ssh-term "") {
set ssh-env = [$@ssh-env TERM=xterm-256color] var old-term = $E:TERM
set ssh-term-override = xterm-256color set-env TERM $ssh-term
}
var ssh-ret = 0
if (not-eq $ssh-term-override "") {
var ssh-original-term = $E:TERM
set-env TERM $ssh-term-override
try { try {
external ssh $@ssh-opts $@args external ssh $@ssh-opts $@args
} catch e { } catch e {
set ssh-ret = $e[reason][exit-status] set-env TERM $old-term
fail $e
} }
set-env TERM $ssh-original-term set-env TERM $old-term
} else { } else {
try {
external ssh $@ssh-opts $@args external ssh $@ssh-opts $@args
} catch e {
set ssh-ret = $e[reason][exit-status]
}
}
if (not-eq $ssh-ret 0) {
fail ssh-failed
} }
} }
} }

View File

@ -87,21 +87,15 @@ function __ghostty_setup --on-event fish_prompt -d "Setup ghostty integration"
end end
# SSH Integration # SSH Integration
if string match -q '*ssh-env*' -- "$GHOSTTY_SHELL_FEATURES"; or string match -q '*ssh-terminfo*' -- "$GHOSTTY_SHELL_FEATURES" if string match -q '*ssh-*' -- "$GHOSTTY_SHELL_FEATURES"
function ssh --wraps=ssh --description "SSH wrapper with Ghostty integration" function ssh --wraps=ssh --description "SSH wrapper with Ghostty integration"
set -l ssh_env set -l ssh_term ""
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 string match -q '*ssh-env*' -- "$GHOSTTY_SHELL_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"
set -a ssh_env "COLORTERM=truecolor"
set -a ssh_env "TERM_PROGRAM=ghostty"
if test -n "$TERM_PROGRAM_VERSION"
set -a ssh_env "TERM_PROGRAM_VERSION=$TERM_PROGRAM_VERSION"
end
end end
# Install terminfo on remote host if needed # Install terminfo on remote host if needed
@ -137,11 +131,11 @@ function __ghostty_setup --on-event fish_prompt -d "Setup ghostty integration"
end end
if test "$ssh_cache_check_success" = "true" if test "$ssh_cache_check_success" = "true"
set -a ssh_env 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
if not command -v base64 >/dev/null 2>&1 if not command -v base64 >/dev/null 2>&1
echo "Warning: base64 command not available for terminfo installation." >&2 echo "Warning: base64 command not available for terminfo installation." >&2
set -a ssh_env TERM=xterm-256color set ssh_term "xterm-256color"
else else
set -l ssh_terminfo set -l ssh_terminfo
set -l ssh_base64_decode_cmd set -l ssh_base64_decode_cmd
@ -167,60 +161,43 @@ function __ghostty_setup --on-event fish_prompt -d "Setup ghostty integration"
exit 1 exit 1
' 2>/dev/null ' 2>/dev/null
echo "Terminfo setup complete on $ssh_hostname." >&2 echo "Terminfo setup complete on $ssh_hostname." >&2
set -a ssh_env TERM=xterm-ghostty set ssh_term "xterm-ghostty"
set -a ssh_opts -o "ControlPath=$ssh_cpath" set -a ssh_opts -o "ControlPath=$ssh_cpath"
# Cache successful installation # Cache successful installation
if test -n "$ssh_target"; and command -v ghostty >/dev/null 2>&1 if test -n "$ssh_target"; and command -v ghostty >/dev/null 2>&1
fish -c "ghostty +ssh-cache --add='$ssh_target' >/dev/null 2>&1; or true" & ghostty +ssh-cache --add="$ssh_target" >/dev/null 2>&1; or true
end end
else else
echo "Warning: Failed to install terminfo." >&2 echo "Warning: Failed to install terminfo." >&2
set -a ssh_env TERM=xterm-256color set ssh_term "xterm-256color"
end end
else else
echo "Warning: Could not generate terminfo data." >&2 echo "Warning: Could not generate terminfo data." >&2
set -a ssh_env TERM=xterm-256color set ssh_term "xterm-256color"
end end
end end
else else
echo "Warning: ghostty command not available for cache management." >&2 echo "Warning: ghostty command not available for cache management." >&2
set -a ssh_env TERM=xterm-256color set ssh_term "xterm-256color"
end end
else else
if string match -q '*ssh-env*' -- "$GHOSTTY_SHELL_FEATURES" if string match -q '*ssh-env*' -- "$GHOSTTY_SHELL_FEATURES"
set -a ssh_env TERM=xterm-256color set ssh_term "xterm-256color"
end end
end end
end end
# Execute SSH with environment handling # Execute SSH with environment handling
set -l ssh_term_override if string match -q '*ssh-env*' -- "$GHOSTTY_SHELL_FEATURES"; and test -z "$ssh_term"
for ssh_v in $ssh_env set ssh_term "xterm-256color"
if string match -q 'TERM=*' -- $ssh_v
set ssh_term_override (string replace 'TERM=' '' -- $ssh_v)
break
end
end end
if string match -q '*ssh-env*' -- "$GHOSTTY_SHELL_FEATURES"; and test -z "$ssh_term_override" if test -n "$ssh_term"
set -a ssh_env TERM=xterm-256color env TERM="$ssh_term" command ssh $ssh_opts $argv
set ssh_term_override xterm-256color
end
set -l ssh_ret
if test -n "$ssh_term_override"
set -l ssh_original_term "$TERM"
set -gx TERM "$ssh_term_override"
command ssh $ssh_opts $argv
set ssh_ret $status
set -gx TERM "$ssh_original_term"
else else
command ssh $ssh_opts $argv command ssh $ssh_opts $argv
set ssh_ret $status
end end
return $ssh_ret
end end
end end

View File

@ -245,25 +245,19 @@ _ghostty_deferred_init() {
fi fi
# SSH Integration # SSH Integration
if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* ]] || [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-terminfo* ]]; then if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-* ]]; then
ssh() { ssh() {
emulate -L zsh emulate -L zsh
setopt local_options no_glob_subst setopt local_options no_glob_subst
local ssh_env ssh_opts local ssh_term ssh_opts
ssh_env=() ssh_term=""
ssh_opts=() ssh_opts=()
# Configure environment variables for remote session # Configure environment variables for remote session
if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* ]]; then if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* ]]; then
ssh_opts+=(-o "SetEnv COLORTERM=truecolor") ssh_opts+=(-o "SetEnv COLORTERM=truecolor")
ssh_opts+=(-o "SendEnv TERM_PROGRAM TERM_PROGRAM_VERSION") ssh_opts+=(-o "SendEnv TERM_PROGRAM TERM_PROGRAM_VERSION")
ssh_env+=(
"COLORTERM=truecolor"
"TERM_PROGRAM=ghostty"
)
[[ -n "$TERM_PROGRAM_VERSION" ]] && ssh_env+=("TERM_PROGRAM_VERSION=$TERM_PROGRAM_VERSION")
fi fi
# Install terminfo on remote host if needed # Install terminfo on remote host if needed
@ -289,11 +283,11 @@ _ghostty_deferred_init() {
fi fi
if [[ "$ssh_cache_check_success" == "true" ]]; then if [[ "$ssh_cache_check_success" == "true" ]]; then
ssh_env+=(TERM=xterm-ghostty) ssh_term="xterm-ghostty"
elif (( $+commands[infocmp] )); then elif (( $+commands[infocmp] )); then
if ! (( $+commands[base64] )); then if ! (( $+commands[base64] )); then
print "Warning: base64 command not available for terminfo installation." >&2 print "Warning: base64 command not available for terminfo installation." >&2
ssh_env+=(TERM=xterm-256color) ssh_term="xterm-256color"
else else
local ssh_terminfo ssh_base64_decode_cmd local ssh_terminfo ssh_base64_decode_cmd
@ -320,61 +314,41 @@ _ghostty_deferred_init() {
exit 1 exit 1
' 2>/dev/null; then ' 2>/dev/null; then
print "Terminfo setup complete on $ssh_hostname." >&2 print "Terminfo setup complete on $ssh_hostname." >&2
ssh_env+=(TERM=xterm-ghostty) ssh_term="xterm-ghostty"
ssh_opts+=(-o "ControlPath=$ssh_cpath") ssh_opts+=(-o "ControlPath=$ssh_cpath")
# Cache successful installation # Cache successful installation
if [[ -n "$ssh_target" ]] && (( $+commands[ghostty] )); then if [[ -n "$ssh_target" ]] && (( $+commands[ghostty] )); then
{
ghostty +ssh-cache --add="$ssh_target" >/dev/null 2>&1 || true ghostty +ssh-cache --add="$ssh_target" >/dev/null 2>&1 || true
} &!
fi fi
else else
print "Warning: Failed to install terminfo." >&2 print "Warning: Failed to install terminfo." >&2
ssh_env+=(TERM=xterm-256color) ssh_term="xterm-256color"
fi fi
else else
print "Warning: Could not generate terminfo data." >&2 print "Warning: Could not generate terminfo data." >&2
ssh_env+=(TERM=xterm-256color) ssh_term="xterm-256color"
fi fi
fi fi
else else
print "Warning: ghostty command not available for cache management." >&2 print "Warning: ghostty command not available for cache management." >&2
ssh_env+=(TERM=xterm-256color) ssh_term="xterm-256color"
fi fi
else else
[[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* ]] && ssh_env+=(TERM=xterm-256color) [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* ]] && ssh_term="xterm-256color"
fi fi
fi fi
# Execute SSH with environment handling # Execute SSH with environment handling
local ssh_term_override="" if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* && -z "$ssh_term" ]]; then
local ssh_v ssh_term="xterm-256color"
for ssh_v in "${ssh_env[@]}"; do
if [[ "$ssh_v" =~ ^TERM=(.*)$ ]]; then
ssh_term_override="${match[1]}"
break
fi
done
if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* && -z "$ssh_term_override" ]]; then
ssh_env+=(TERM=xterm-256color)
ssh_term_override="xterm-256color"
fi fi
local ssh_ret if [[ -n "$ssh_term" ]]; then
if [[ -n "$ssh_term_override" ]]; then TERM="$ssh_term" command ssh "${ssh_opts[@]}" "$@"
local ssh_original_term="$TERM"
export TERM="$ssh_term_override"
command ssh "${ssh_opts[@]}" "$@"
ssh_ret=$?
export TERM="$ssh_original_term"
else else
command ssh "${ssh_opts[@]}" "$@" command ssh "${ssh_opts[@]}" "$@"
ssh_ret=$?
fi fi
return $ssh_ret
} }
fi fi