refactor: simplify SSH environment variable handling

- Remove complex ssh_exported_vars tracking and local environment
modification in favor of trusting Ghostty's local environment
- Replace regex patterns with glob-based feature detection for better
performance
- Fix local variable declaration consistency throughout
- Streamline logic while maintaining all functionality
This commit is contained in:
Jason Rayne
2025-07-07 08:58:44 -07:00
parent f242c6b5c4
commit 08db61e27e
4 changed files with 21 additions and 134 deletions

View File

@ -96,33 +96,16 @@ if [[ "$GHOSTTY_SHELL_FEATURES" == *"sudo"* && -n "$TERMINFO" ]]; then
fi fi
# SSH Integration # SSH Integration
if [[ "$GHOSTTY_SHELL_FEATURES" =~ ssh-(env|terminfo) ]]; then if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* ]] || [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-terminfo* ]]; then
ssh() { ssh() {
builtin local ssh_env ssh_opts ssh_exported_vars builtin local ssh_env ssh_opts
ssh_env=() ssh_env=()
ssh_opts=() ssh_opts=()
ssh_exported_vars=()
# 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")
if [[ -n "${TERM_PROGRAM+x}" ]]; then
ssh_exported_vars+=("TERM_PROGRAM=${TERM_PROGRAM}")
else
ssh_exported_vars+=("TERM_PROGRAM")
fi
builtin export "TERM_PROGRAM=ghostty"
ssh_opts+=(-o "SendEnv TERM_PROGRAM")
if [[ -n "$TERM_PROGRAM_VERSION" ]]; then
if [[ -n "${TERM_PROGRAM_VERSION+x}" ]]; then
ssh_exported_vars+=("TERM_PROGRAM_VERSION=${TERM_PROGRAM_VERSION}")
else
ssh_exported_vars+=("TERM_PROGRAM_VERSION")
fi
ssh_opts+=(-o "SendEnv TERM_PROGRAM_VERSION")
fi
ssh_env+=( ssh_env+=(
"COLORTERM=truecolor" "COLORTERM=truecolor"
@ -134,7 +117,7 @@ if [[ "$GHOSTTY_SHELL_FEATURES" =~ ssh-(env|terminfo) ]]; then
fi fi
# 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_config ssh_user ssh_hostname
ssh_config=$(builtin command ssh -G "$@" 2>/dev/null) ssh_config=$(builtin command ssh -G "$@" 2>/dev/null)
@ -146,7 +129,7 @@ if [[ "$GHOSTTY_SHELL_FEATURES" =~ ssh-(env|terminfo) ]]; then
[[ -n "$ssh_user" && -n "$ssh_hostname" ]] && break [[ -n "$ssh_user" && -n "$ssh_hostname" ]] && break
done <<< "$ssh_config" done <<< "$ssh_config"
ssh_target="${ssh_user}@${ssh_hostname}" builtin local ssh_target="${ssh_user}@${ssh_hostname}"
if [[ -n "$ssh_hostname" ]]; then if [[ -n "$ssh_hostname" ]]; then
# Check if terminfo is already cached # Check if terminfo is already cached
@ -213,7 +196,7 @@ if [[ "$GHOSTTY_SHELL_FEATURES" =~ ssh-(env|terminfo) ]]; then
ssh_env+=(TERM=xterm-256color) ssh_env+=(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_env+=(TERM=xterm-256color)
fi fi
fi fi
@ -228,7 +211,7 @@ if [[ "$GHOSTTY_SHELL_FEATURES" =~ ssh-(env|terminfo) ]]; then
fi fi
done done
if [[ "$GHOSTTY_SHELL_FEATURES" =~ ssh-env && -z "$ssh_term_override" ]]; then if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* && -z "$ssh_term_override" ]]; then
ssh_env+=(TERM=xterm-256color) ssh_env+=(TERM=xterm-256color)
ssh_term_override="xterm-256color" ssh_term_override="xterm-256color"
fi fi
@ -244,17 +227,6 @@ if [[ "$GHOSTTY_SHELL_FEATURES" =~ ssh-(env|terminfo) ]]; then
local ssh_ret=$? local ssh_ret=$?
fi fi
# Restore original environment variables
if [[ "$GHOSTTY_SHELL_FEATURES" =~ ssh-env ]]; then
for ssh_v in "${ssh_exported_vars[@]}"; do
if [[ "$ssh_v" == *=* ]]; then
builtin export "${ssh_v?}"
else
builtin unset "${ssh_v}"
fi
done
fi
return $ssh_ret return $ssh_ret
} }
fi fi
@ -286,8 +258,8 @@ function __ghostty_precmd() {
# Cursor # Cursor
if [[ "$GHOSTTY_SHELL_FEATURES" == *"cursor"* ]]; then if [[ "$GHOSTTY_SHELL_FEATURES" == *"cursor"* ]]; then
PS1=$PS1'\[\e[5 q\]' # blinking bar for input [[ "$PS1" != *'\[\e[5 q\]'* ]] && PS1=$PS1'\[\e[5 q\]' # input
builtin printf "\e[0 q" # reset to default cursor [[ "$PS0" != *'\[\e[0 q\]'* ]] && PS0=$PS0'\[\e[0 q\]' # reset
fi fi
# Title (working directory) # Title (working directory)

View File

@ -105,28 +105,11 @@
fn ssh {|@args| fn ssh {|@args|
var ssh-env = [] var ssh-env = []
var ssh-opts = [] var ssh-opts = []
var ssh-exported-vars = []
# 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"]
if (has-env TERM_PROGRAM) {
set ssh-exported-vars = [$@ssh-exported-vars "TERM_PROGRAM="$E:TERM_PROGRAM]
} else {
set ssh-exported-vars = [$@ssh-exported-vars "TERM_PROGRAM"]
}
set-env TERM_PROGRAM ghostty
set ssh-opts = [$@ssh-opts -o "SendEnv TERM_PROGRAM"]
if (has-env TERM_PROGRAM_VERSION) {
if (has-env TERM_PROGRAM_VERSION) {
set ssh-exported-vars = [$@ssh-exported-vars "TERM_PROGRAM_VERSION="$E:TERM_PROGRAM_VERSION]
} else {
set ssh-exported-vars = [$@ssh-exported-vars "TERM_PROGRAM_VERSION"]
}
set ssh-opts = [$@ssh-opts -o "SendEnv TERM_PROGRAM_VERSION"]
}
set ssh-env = [ set ssh-env = [
"COLORTERM=truecolor" "COLORTERM=truecolor"
@ -288,18 +271,6 @@
} }
} }
# Restore original environment variables
if (str:contains $E:GHOSTTY_SHELL_FEATURES ssh-env) {
for ssh-v $ssh-exported-vars {
if (str:contains $ssh-v =) {
var ssh-var-parts = (str:split &max=2 = $ssh-v)
set-env $ssh-var-parts[0] $ssh-var-parts[1]
} else {
unset-env $ssh-v
}
}
}
if (not-eq $ssh-ret 0) { if (not-eq $ssh-ret 0) {
fail ssh-failed fail ssh-failed
} }

View File

@ -91,28 +91,11 @@ function __ghostty_setup --on-event fish_prompt -d "Setup ghostty integration"
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_env
set -l ssh_opts set -l ssh_opts
set -l ssh_exported_vars
# 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"
if set -q TERM_PROGRAM
set -a ssh_exported_vars "TERM_PROGRAM=$TERM_PROGRAM"
else
set -a ssh_exported_vars "TERM_PROGRAM"
end
set -gx TERM_PROGRAM ghostty
set -a ssh_opts -o "SendEnv TERM_PROGRAM"
if test -n "$TERM_PROGRAM_VERSION"
if set -q TERM_PROGRAM_VERSION
set -a ssh_exported_vars "TERM_PROGRAM_VERSION=$TERM_PROGRAM_VERSION"
else
set -a ssh_exported_vars "TERM_PROGRAM_VERSION"
end
set -a ssh_opts -o "SendEnv TERM_PROGRAM_VERSION"
end
set -a ssh_env "COLORTERM=truecolor" set -a ssh_env "COLORTERM=truecolor"
set -a ssh_env "TERM_PROGRAM=ghostty" set -a ssh_env "TERM_PROGRAM=ghostty"
@ -237,17 +220,6 @@ function __ghostty_setup --on-event fish_prompt -d "Setup ghostty integration"
set ssh_ret $status set ssh_ret $status
end end
# Restore original environment variables
if string match -q '*ssh-env*' -- "$GHOSTTY_SHELL_FEATURES"
for ssh_v in $ssh_exported_vars
if string match -q '*=*' -- $ssh_v
set -gx (string split -m1 '=' -- $ssh_v)
else
set -e $ssh_v
end
end
end
return $ssh_ret return $ssh_ret
end end
end end

View File

@ -245,36 +245,19 @@ _ghostty_deferred_init() {
fi fi
# SSH Integration # SSH Integration
if [[ "$GHOSTTY_SHELL_FEATURES" =~ (ssh-env|ssh-terminfo) ]]; then if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* ]] || [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-terminfo* ]]; 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 ssh_exported_vars local ssh_env ssh_opts
ssh_env=() ssh_env=()
ssh_opts=() ssh_opts=()
ssh_exported_vars=()
# 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")
if [[ -n "${TERM_PROGRAM+x}" ]]; then
ssh_exported_vars+=("TERM_PROGRAM=${TERM_PROGRAM}")
else
ssh_exported_vars+=("TERM_PROGRAM")
fi
export "TERM_PROGRAM=ghostty"
ssh_opts+=(-o "SendEnv TERM_PROGRAM")
if [[ -n "$TERM_PROGRAM_VERSION" ]]; then
if [[ -n "${TERM_PROGRAM_VERSION+x}" ]]; then
ssh_exported_vars+=("TERM_PROGRAM_VERSION=${TERM_PROGRAM_VERSION}")
else
ssh_exported_vars+=("TERM_PROGRAM_VERSION")
fi
ssh_opts+=(-o "SendEnv TERM_PROGRAM_VERSION")
fi
ssh_env+=( ssh_env+=(
"COLORTERM=truecolor" "COLORTERM=truecolor"
@ -284,7 +267,7 @@ _ghostty_deferred_init() {
fi fi
# 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_config ssh_user ssh_hostname
ssh_config=$(command ssh -G "$@" 2>/dev/null) ssh_config=$(command ssh -G "$@" 2>/dev/null)
@ -296,7 +279,7 @@ _ghostty_deferred_init() {
[[ -n "$ssh_user" && -n "$ssh_hostname" ]] && break [[ -n "$ssh_user" && -n "$ssh_hostname" ]] && break
done <<< "$ssh_config" done <<< "$ssh_config"
ssh_target="${ssh_user}@${ssh_hostname}" local ssh_target="${ssh_user}@${ssh_hostname}"
if [[ -n "$ssh_hostname" ]]; then if [[ -n "$ssh_hostname" ]]; then
# Check if terminfo is already cached # Check if terminfo is already cached
@ -360,7 +343,7 @@ _ghostty_deferred_init() {
ssh_env+=(TERM=xterm-256color) ssh_env+=(TERM=xterm-256color)
fi fi
else else
[[ "$GHOSTTY_SHELL_FEATURES" =~ ssh-env ]] && ssh_env+=(TERM=xterm-256color) [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* ]] && ssh_env+=(TERM=xterm-256color)
fi fi
fi fi
@ -374,7 +357,7 @@ _ghostty_deferred_init() {
fi fi
done done
if [[ "$GHOSTTY_SHELL_FEATURES" =~ ssh-env && -z "$ssh_term_override" ]]; then if [[ "$GHOSTTY_SHELL_FEATURES" == *ssh-env* && -z "$ssh_term_override" ]]; then
ssh_env+=(TERM=xterm-256color) ssh_env+=(TERM=xterm-256color)
ssh_term_override="xterm-256color" ssh_term_override="xterm-256color"
fi fi
@ -391,17 +374,6 @@ _ghostty_deferred_init() {
ssh_ret=$? ssh_ret=$?
fi fi
# Restore original environment variables
if [[ "$GHOSTTY_SHELL_FEATURES" =~ ssh-env ]]; then
for ssh_v in "${ssh_exported_vars[@]}"; do
if [[ "$ssh_v" == *=* ]]; then
export "${ssh_v}"
else
unset "${ssh_v}"
fi
done
fi
return $ssh_ret return $ssh_ret
} }
fi fi