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

View File

@ -101,23 +101,16 @@
# SSH Integration
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|
var ssh-env = []
var ssh-term = ""
var ssh-opts = []
# Configure environment variables for remote session
if (str:contains $E:GHOSTTY_SHELL_FEATURES ssh-env) {
set ssh-opts = [$@ssh-opts -o "SetEnv COLORTERM=truecolor"]
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
@ -159,7 +152,7 @@
}
if $ssh-cache-check-success {
set ssh-env = [$@ssh-env TERM=xterm-ghostty]
set ssh-term = "xterm-ghostty"
} else {
try {
external infocmp --help >/dev/null 2>&1
@ -208,71 +201,58 @@
if $terminfo-install-success {
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]
# Cache successful installation
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 {
echo "Warning: Failed to install terminfo." >&2
set ssh-env = [$@ssh-env TERM=xterm-256color]
set ssh-term = "xterm-256color"
}
} else {
echo "Warning: Could not generate terminfo data." >&2
set ssh-env = [$@ssh-env TERM=xterm-256color]
set ssh-term = "xterm-256color"
}
} catch {
echo "Warning: base64 command not available for terminfo installation." >&2
set ssh-env = [$@ssh-env TERM=xterm-256color]
set ssh-term = "xterm-256color"
}
} catch {
echo "Warning: ghostty command not available for cache management." >&2
set ssh-env = [$@ssh-env TERM=xterm-256color]
set ssh-term = "xterm-256color"
}
}
} else {
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
var ssh-term-override = ""
for ssh-v $ssh-env {
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 "")) {
set ssh-term = "xterm-256color"
}
if (and (str:contains $E:GHOSTTY_SHELL_FEATURES ssh-env) (eq $ssh-term-override "")) {
set ssh-env = [$@ssh-env TERM=xterm-256color]
set ssh-term-override = xterm-256color
}
var ssh-ret = 0
if (not-eq $ssh-term-override "") {
var ssh-original-term = $E:TERM
set-env TERM $ssh-term-override
if (not-eq $ssh-term "") {
var old-term = $E:TERM
set-env TERM $ssh-term
try {
external ssh $@ssh-opts $@args
} 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 {
try {
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
# 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"
set -l ssh_env
set -l ssh_term ""
set -l ssh_opts
# Configure environment variables for remote session
if string match -q '*ssh-env*' -- "$GHOSTTY_SHELL_FEATURES"
set -a ssh_opts -o "SetEnv COLORTERM=truecolor"
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
# Install terminfo on remote host if needed
@ -137,11 +131,11 @@ function __ghostty_setup --on-event fish_prompt -d "Setup ghostty integration"
end
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
if not command -v base64 >/dev/null 2>&1
echo "Warning: base64 command not available for terminfo installation." >&2
set -a ssh_env TERM=xterm-256color
set ssh_term "xterm-256color"
else
set -l ssh_terminfo
set -l ssh_base64_decode_cmd
@ -167,60 +161,43 @@ function __ghostty_setup --on-event fish_prompt -d "Setup ghostty integration"
exit 1
' 2>/dev/null
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"
# Cache successful installation
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
else
echo "Warning: Failed to install terminfo." >&2
set -a ssh_env TERM=xterm-256color
set ssh_term "xterm-256color"
end
else
echo "Warning: Could not generate terminfo data." >&2
set -a ssh_env TERM=xterm-256color
set ssh_term "xterm-256color"
end
end
else
echo "Warning: ghostty command not available for cache management." >&2
set -a ssh_env TERM=xterm-256color
set ssh_term "xterm-256color"
end
else
if string match -q '*ssh-env*' -- "$GHOSTTY_SHELL_FEATURES"
set -a ssh_env TERM=xterm-256color
set ssh_term "xterm-256color"
end
end
end
# Execute SSH with environment handling
set -l ssh_term_override
for ssh_v in $ssh_env
if string match -q 'TERM=*' -- $ssh_v
set ssh_term_override (string replace 'TERM=' '' -- $ssh_v)
break
end
if string match -q '*ssh-env*' -- "$GHOSTTY_SHELL_FEATURES"; and test -z "$ssh_term"
set ssh_term "xterm-256color"
end
if string match -q '*ssh-env*' -- "$GHOSTTY_SHELL_FEATURES"; and test -z "$ssh_term_override"
set -a ssh_env TERM=xterm-256color
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"
if test -n "$ssh_term"
env TERM="$ssh_term" command ssh $ssh_opts $argv
else
command ssh $ssh_opts $argv
set ssh_ret $status
end
return $ssh_ret
end
end

View File

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