refactor: simplify terminfo handling and remove base64 dependency

- Default ssh_term to xterm-256color to eliminate fallback assignments
- Remove base64 and replace infocmp -Q2 with standard -0 -x options for
compatibility
- Use process substitution instead of intermediate ssh_config variable
- Always set TERM explicitly since ssh_term is always defined
This commit is contained in:
Jason Rayne
2025-07-07 11:33:26 -07:00
parent 22edfd2c5d
commit f279937377
4 changed files with 145 additions and 258 deletions

View File

@ -99,7 +99,7 @@ fi
if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-* ]]; then if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-* ]]; then
ssh() { ssh() {
builtin local ssh_term ssh_opts builtin local ssh_term ssh_opts
ssh_term="" ssh_term="xterm-256color"
ssh_opts=() ssh_opts=()
# Configure environment variables for remote session # Configure environment variables for remote session
@ -110,8 +110,7 @@ if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-* ]]; then
# Install terminfo on remote host if needed # Install terminfo on remote host if needed
if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-terminfo* ]]; then if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-terminfo* ]]; then
builtin local ssh_config ssh_user ssh_hostname builtin local ssh_user ssh_hostname
ssh_config=$(builtin command ssh -G "$@" 2>/dev/null)
while IFS=' ' read -r ssh_key ssh_value; do while IFS=' ' read -r ssh_key ssh_value; do
case "$ssh_key" in case "$ssh_key" in
@ -119,7 +118,7 @@ if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-* ]]; then
hostname) ssh_hostname="$ssh_value" ;; hostname) ssh_hostname="$ssh_value" ;;
esac esac
[[ -n "$ssh_user" && -n "$ssh_hostname" ]] && break [[ -n "$ssh_user" && -n "$ssh_hostname" ]] && break
done <<< "$ssh_config" done < <(builtin command ssh -G "$@" 2>/dev/null)
builtin local ssh_target="${ssh_user}@${ssh_hostname}" builtin local ssh_target="${ssh_user}@${ssh_hostname}"
@ -133,29 +132,17 @@ if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-* ]]; then
if [[ "$ssh_cache_check_success" == "true" ]]; then 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
if ! builtin command -v base64 >/dev/null 2>&1; then builtin local ssh_terminfo ssh_cpath_dir ssh_cpath
builtin echo "Warning: base64 command not available for terminfo installation." >&2
ssh_term="xterm-256color"
else
builtin local ssh_terminfo ssh_base64_decode_cmd
# BSD vs GNU base64 compatibility ssh_terminfo=$(infocmp -0 -x xterm-ghostty 2>/dev/null)
if base64 --help 2>&1 | grep -q GNU; then
ssh_base64_decode_cmd="base64 -d"
ssh_terminfo=$(infocmp -0 -Q2 -q xterm-ghostty 2>/dev/null | base64 -w0 2>/dev/null)
else
ssh_base64_decode_cmd="base64 -D"
ssh_terminfo=$(infocmp -0 -Q2 -q xterm-ghostty 2>/dev/null | base64 2>/dev/null | tr -d '\n')
fi
if [[ -n "$ssh_terminfo" ]]; then if [[ -n "$ssh_terminfo" ]]; then
builtin echo "Setting up Ghostty terminfo on $ssh_hostname..." >&2 builtin echo "Setting up Ghostty terminfo on $ssh_hostname..." >&2
builtin local ssh_cpath_dir ssh_cpath
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" | $ssh_base64_decode_cmd | builtin command ssh "${ssh_opts[@]}" -o ControlMaster=yes -o ControlPath="$ssh_cpath" -o ControlPersist=60s "$@" ' if builtin echo "$ssh_terminfo" | builtin command ssh "${ssh_opts[@]}" -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
@ -171,34 +158,18 @@ if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-* ]]; then
fi fi
else else
builtin echo "Warning: Failed to install terminfo." >&2 builtin echo "Warning: Failed to install terminfo." >&2
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_term="xterm-256color"
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_term="xterm-256color"
fi
else
if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* ]]; then
ssh_term="xterm-256color"
fi fi
fi fi
fi fi
# Execute SSH with environment handling # Execute SSH with TERM environment variable
if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* && -z "$ssh_term" ]]; then
ssh_term="xterm-256color"
fi
if [[ -n "$ssh_term" ]]; then
TERM="$ssh_term" builtin command ssh "${ssh_opts[@]}" "$@" TERM="$ssh_term" builtin command ssh "${ssh_opts[@]}" "$@"
else
builtin command ssh "${ssh_opts[@]}" "$@"
fi
} }
fi fi

View File

@ -102,9 +102,8 @@
use str use str
if (str:contains $E:GHOSTTY_SHELL_FEATURES ssh-) { 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-term = "" var ssh-term = "xterm-256color"
var ssh-opts = [] var ssh-opts = []
# Configure environment variables for remote session # Configure environment variables for remote session
@ -115,16 +114,11 @@
# Install terminfo on remote host if needed # Install terminfo on remote host if needed
if (str:contains $E:GHOSTTY_SHELL_FEATURES ssh-terminfo) { if (str:contains $E:GHOSTTY_SHELL_FEATURES ssh-terminfo) {
var ssh-config = ""
try {
set ssh-config = (external ssh -G $@args 2>/dev/null | slurp)
} catch {
set ssh-config = ""
}
var ssh-user = "" var ssh-user = ""
var ssh-hostname = "" var ssh-hostname = ""
try {
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) {
@ -138,6 +132,9 @@
} }
} }
} }
} catch {
# ssh config failed
}
var ssh-target = $ssh-user"@"$ssh-hostname var ssh-target = $ssh-user"@"$ssh-hostname
@ -157,38 +154,29 @@
try { try {
external infocmp --help >/dev/null 2>&1 external infocmp --help >/dev/null 2>&1
try {
external base64 --help >/dev/null 2>&1
# Generate terminfo data (BSD base64 compatibility)
var ssh-terminfo = "" var ssh-terminfo = ""
var ssh-base64-decode-cmd = "" var ssh-cpath-dir = ""
var ssh-cpath = ""
try { try {
var base64-help = (external base64 --help 2>&1 | slurp) set ssh-terminfo = (external infocmp -0 -x xterm-ghostty 2>/dev/null | slurp)
if (str:contains $base64-help GNU) {
set ssh-base64-decode-cmd = "base64 -d"
set ssh-terminfo = (external infocmp -0 -Q2 -q xterm-ghostty 2>/dev/null | external base64 -w0 2>/dev/null | slurp)
} else {
set ssh-base64-decode-cmd = "base64 -D"
set ssh-terminfo = (external infocmp -0 -Q2 -q xterm-ghostty 2>/dev/null | external base64 2>/dev/null | external tr -d '\n' | slurp)
}
} catch { } catch {
set ssh-terminfo = "" set ssh-terminfo = ""
} }
if (not-eq $ssh-terminfo "") { if (not-eq $ssh-terminfo "") {
echo "Setting up Ghostty terminfo on "$ssh-hostname"..." >&2 echo "Setting up Ghostty terminfo on "$ssh-hostname"..." >&2
var ssh-cpath-dir = ""
try { try {
set ssh-cpath-dir = (external mktemp -d "/tmp/ghostty-ssh-"$ssh-user".XXXXXX" 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-cpath-dir = "/tmp/ghostty-ssh-"$ssh-user"."(randint 10000 99999) set ssh-cpath-dir = "/tmp/ghostty-ssh-"$ssh-user"."(randint 10000 99999)
} }
var ssh-cpath = $ssh-cpath-dir"/socket" set ssh-cpath = $ssh-cpath-dir"/socket"
var terminfo-install-success = $false var terminfo-install-success = $false
try { try {
echo $ssh-terminfo | external sh -c $ssh-base64-decode-cmd | external ssh $@ssh-opts -o ControlMaster=yes -o ControlPath=$ssh-cpath -o ControlPersist=60s $@args ' 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 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
@ -214,34 +202,18 @@
} }
} else { } else {
echo "Warning: Failed to install terminfo." >&2 echo "Warning: Failed to install terminfo." >&2
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-term = "xterm-256color"
}
} catch {
echo "Warning: base64 command not available for terminfo installation." >&2
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-term = "xterm-256color"
} }
} }
} else {
if (str:contains $E:GHOSTTY_SHELL_FEATURES ssh-env) {
set ssh-term = "xterm-256color"
}
} }
} }
# Execute SSH with environment handling # Execute SSH with TERM environment variable
if (and (str:contains $E:GHOSTTY_SHELL_FEATURES ssh-env) (eq $ssh-term "")) {
set ssh-term = "xterm-256color"
}
if (not-eq $ssh-term "") {
var old-term = $E:TERM var old-term = $E:TERM
set-env TERM $ssh-term set-env TERM $ssh-term
try { try {
@ -251,9 +223,6 @@
fail $e fail $e
} }
set-env TERM $old-term set-env TERM $old-term
} else {
external ssh $@ssh-opts $@args
}
} }
} }

View File

@ -89,7 +89,7 @@ function __ghostty_setup --on-event fish_prompt -d "Setup ghostty integration"
# SSH Integration # SSH Integration
if string match -q '*ssh-*' -- "$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_term "" 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
@ -100,11 +100,10 @@ function __ghostty_setup --on-event fish_prompt -d "Setup ghostty integration"
# Install terminfo on remote host if needed # Install terminfo on remote host if needed
if string match -q '*ssh-terminfo*' -- "$GHOSTTY_SHELL_FEATURES" if string match -q '*ssh-terminfo*' -- "$GHOSTTY_SHELL_FEATURES"
set -l ssh_config (command ssh -G $argv 2>/dev/null)
set -l ssh_user set -l ssh_user
set -l ssh_hostname set -l ssh_hostname
for line in $ssh_config for line in (command ssh -G $argv 2>/dev/null)
set -l parts (string split ' ' -- $line) set -l parts (string split ' ' -- $line)
if test (count $parts) -ge 2 if test (count $parts) -ge 2
switch $parts[1] switch $parts[1]
@ -133,28 +132,19 @@ function __ghostty_setup --on-event fish_prompt -d "Setup ghostty integration"
if test "$ssh_cache_check_success" = "true" 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
if not command -v base64 >/dev/null 2>&1
echo "Warning: base64 command not available for terminfo installation." >&2
set ssh_term "xterm-256color"
else
set -l ssh_terminfo set -l ssh_terminfo
set -l ssh_base64_decode_cmd set -l ssh_cpath_dir
set -l ssh_cpath
# BSD vs GNU base64 compatibility set ssh_terminfo (infocmp -0 -x xterm-ghostty 2>/dev/null)
if base64 --help 2>&1 | grep -q GNU
set ssh_base64_decode_cmd "base64 -d"
set ssh_terminfo (infocmp -0 -Q2 -q xterm-ghostty 2>/dev/null | base64 -w0 2>/dev/null)
else
set ssh_base64_decode_cmd "base64 -D"
set ssh_terminfo (infocmp -0 -Q2 -q xterm-ghostty 2>/dev/null | base64 2>/dev/null | tr -d '\n')
end
if test -n "$ssh_terminfo" if test -n "$ssh_terminfo"
echo "Setting up Ghostty terminfo on $ssh_hostname..." >&2 echo "Setting up Ghostty terminfo on $ssh_hostname..." >&2
set -l ssh_cpath_dir (mktemp -d "/tmp/ghostty-ssh-$ssh_user.XXXXXX" 2>/dev/null; or echo "/tmp/ghostty-ssh-$ssh_user."(random))
set -l ssh_cpath "$ssh_cpath_dir/socket"
if echo "$ssh_terminfo" | eval $ssh_base64_decode_cmd | command ssh $ssh_opts -o ControlMaster=yes -o ControlPath="$ssh_cpath" -o ControlPersist=60s $argv ' 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"
if echo "$ssh_terminfo" | command ssh $ssh_opts -o ControlMaster=yes -o ControlPath="$ssh_cpath" -o ControlPersist=60s $argv '
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
@ -170,34 +160,18 @@ function __ghostty_setup --on-event fish_prompt -d "Setup ghostty integration"
end end
else else
echo "Warning: Failed to install terminfo." >&2 echo "Warning: Failed to install terminfo." >&2
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 ssh_term "xterm-256color"
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 ssh_term "xterm-256color"
end
else
if string match -q '*ssh-env*' -- "$GHOSTTY_SHELL_FEATURES"
set ssh_term "xterm-256color"
end end
end end
end end
# Execute SSH with environment handling # Execute SSH with TERM environment variable
if string match -q '*ssh-env*' -- "$GHOSTTY_SHELL_FEATURES"; and test -z "$ssh_term"
set ssh_term "xterm-256color"
end
if test -n "$ssh_term"
env TERM="$ssh_term" command ssh $ssh_opts $argv env TERM="$ssh_term" command ssh $ssh_opts $argv
else
command ssh $ssh_opts $argv
end
end end
end end

View File

@ -251,7 +251,7 @@ _ghostty_deferred_init() {
setopt local_options no_glob_subst setopt local_options no_glob_subst
local ssh_term ssh_opts local ssh_term ssh_opts
ssh_term="" ssh_term="xterm-256color"
ssh_opts=() ssh_opts=()
# Configure environment variables for remote session # Configure environment variables for remote session
@ -262,8 +262,7 @@ _ghostty_deferred_init() {
# Install terminfo on remote host if needed # Install terminfo on remote host if needed
if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-terminfo* ]]; then if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-terminfo* ]]; then
local ssh_config ssh_user ssh_hostname local ssh_user ssh_hostname
ssh_config=$(command ssh -G "$@" 2>/dev/null)
while IFS=' ' read -r ssh_key ssh_value; do while IFS=' ' read -r ssh_key ssh_value; do
case "$ssh_key" in case "$ssh_key" in
@ -271,7 +270,7 @@ _ghostty_deferred_init() {
hostname) ssh_hostname="$ssh_value" ;; hostname) ssh_hostname="$ssh_value" ;;
esac esac
[[ -n "$ssh_user" && -n "$ssh_hostname" ]] && break [[ -n "$ssh_user" && -n "$ssh_hostname" ]] && break
done <<< "$ssh_config" done < <(command ssh -G "$@" 2>/dev/null)
local ssh_target="${ssh_user}@${ssh_hostname}" local ssh_target="${ssh_user}@${ssh_hostname}"
@ -285,29 +284,17 @@ _ghostty_deferred_init() {
if [[ "$ssh_cache_check_success" == "true" ]]; then if [[ "$ssh_cache_check_success" == "true" ]]; then
ssh_term="xterm-ghostty" ssh_term="xterm-ghostty"
elif (( $+commands[infocmp] )); then elif (( $+commands[infocmp] )); then
if ! (( $+commands[base64] )); then local ssh_terminfo ssh_cpath_dir ssh_cpath
print "Warning: base64 command not available for terminfo installation." >&2
ssh_term="xterm-256color"
else
local ssh_terminfo ssh_base64_decode_cmd
# BSD vs GNU base64 compatibility ssh_terminfo=$(infocmp -0 -x xterm-ghostty 2>/dev/null)
if base64 --help 2>&1 | grep -q GNU; then
ssh_base64_decode_cmd="base64 -d"
ssh_terminfo=$(infocmp -0 -Q2 -q xterm-ghostty 2>/dev/null | base64 -w0 2>/dev/null)
else
ssh_base64_decode_cmd="base64 -D"
ssh_terminfo=$(infocmp -0 -Q2 -q xterm-ghostty 2>/dev/null | base64 2>/dev/null | tr -d '\n')
fi
if [[ -n "$ssh_terminfo" ]]; then if [[ -n "$ssh_terminfo" ]]; then
print "Setting up Ghostty terminfo on $ssh_hostname..." >&2 print "Setting up Ghostty terminfo on $ssh_hostname..." >&2
local ssh_cpath_dir ssh_cpath
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 print "$ssh_terminfo" | $ssh_base64_decode_cmd | command ssh "${ssh_opts[@]}" -o ControlMaster=yes -o ControlPath="$ssh_cpath" -o ControlPersist=60s "$@" ' if print "$ssh_terminfo" | command ssh "${ssh_opts[@]}" -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
@ -323,32 +310,18 @@ _ghostty_deferred_init() {
fi fi
else else
print "Warning: Failed to install terminfo." >&2 print "Warning: Failed to install terminfo." >&2
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_term="xterm-256color"
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_term="xterm-256color"
fi fi
else
[[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* ]] && ssh_term="xterm-256color"
fi fi
fi fi
# Execute SSH with environment handling # Execute SSH with TERM environment variable
if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* && -z "$ssh_term" ]]; then
ssh_term="xterm-256color"
fi
if [[ -n "$ssh_term" ]]; then
TERM="$ssh_term" command ssh "${ssh_opts[@]}" "$@" TERM="$ssh_term" command ssh "${ssh_opts[@]}" "$@"
else
command ssh "${ssh_opts[@]}" "$@"
fi
} }
fi fi