mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-10-17 15:21:50 +00:00
refactor: replace ghostty wrapper with proper CLI actions for terminfo cache management
- Add +list-ssh-cache and +clear-ssh-cache CLI actions - Remove ghostty() wrapper functions from all shell integrations - Improve variable naming in shell scripts for readability Addresses @00-kat's feedback about CLI discoverability and naming consistency. The new CLI actions follow established Ghostty patterns and are discoverable via `ghostty --help`, while maintaining clean separation of concerns between shell logic and cache management.
This commit is contained in:
@@ -246,80 +246,76 @@ _ghostty_deferred_init() {
|
||||
|
||||
# SSH Integration
|
||||
if [[ "$GHOSTTY_SHELL_FEATURES" =~ ssh-(env|terminfo) ]]; then
|
||||
|
||||
if [[ "$GHOSTTY_SHELL_FEATURES" =~ ssh-terminfo ]]; then
|
||||
readonly _cache_script="${GHOSTTY_RESOURCES_DIR}/shell-integration/shared/ghostty-ssh-cache"
|
||||
|
||||
# Wrap ghostty command to provide cache management commands
|
||||
ghostty() {
|
||||
case "$1" in
|
||||
ssh-cache-list) "$_cache_script" list ;;
|
||||
ssh-cache-clear) "$_cache_script" clear ;;
|
||||
*) builtin command ghostty "$@" ;;
|
||||
esac
|
||||
}
|
||||
readonly _CACHE="${GHOSTTY_RESOURCES_DIR}/shell-integration/shared/ghostty-ssh-cache"
|
||||
fi
|
||||
|
||||
# SSH wrapper
|
||||
ssh() {
|
||||
local -a e o c
|
||||
|
||||
local -a env opts ctrl
|
||||
env=()
|
||||
opts=()
|
||||
ctrl=()
|
||||
|
||||
# Set up env vars first so terminfo installation inherits them
|
||||
if [[ "$GHOSTTY_SHELL_FEATURES" =~ ssh-env ]]; then
|
||||
local vars=(
|
||||
local -a vars
|
||||
vars=(
|
||||
COLORTERM=truecolor
|
||||
TERM_PROGRAM=ghostty
|
||||
${GHOSTTY_VERSION:+TERM_PROGRAM_VERSION=$GHOSTTY_VERSION}
|
||||
)
|
||||
for v in "${vars[@]}"; do
|
||||
builtin export "${v?}"
|
||||
o+=(-o "SendEnv ${v%=*}" -o "SetEnv $v")
|
||||
export "${v?}"
|
||||
opts+=(-o "SendEnv ${v%=*}" -o "SetEnv $v")
|
||||
done
|
||||
fi
|
||||
|
||||
# Install terminfo if needed, reuse control connection for main session
|
||||
if [[ "$GHOSTTY_SHELL_FEATURES" =~ ssh-terminfo ]]; then
|
||||
# Get target (only when needed for terminfo)
|
||||
builtin local t
|
||||
t=$(builtin command ssh -G "$@" 2>/dev/null | awk '/^(user|hostname) /{print $2}' | paste -sd'@')
|
||||
local target
|
||||
target=$(command ssh -G "$@" 2>/dev/null | awk '/^(user|hostname) /{print $2}' | paste -sd'@')
|
||||
|
||||
if [[ -n $t ]] && "$_cache_script" chk "$t"; then
|
||||
e+=(TERM=xterm-ghostty)
|
||||
elif builtin command -v infocmp >/dev/null 2>&1; then
|
||||
local ti
|
||||
ti=$(infocmp -x xterm-ghostty 2>/dev/null) || builtin echo "Warning: xterm-ghostty terminfo not found locally." >&2
|
||||
if [[ -n $ti ]]; then
|
||||
builtin echo "Setting up Ghostty terminfo on remote host..." >&2
|
||||
local cp
|
||||
cp="/tmp/ghostty-ssh-$USER-$RANDOM-$(date +%s)"
|
||||
case $(builtin echo "$ti" | builtin command ssh "${o[@]}" -o ControlMaster=yes -o ControlPath="$cp" -o ControlPersist=60s "$@" '
|
||||
if [[ -n "$target" ]] && "$_CACHE" chk "$target"; then
|
||||
env+=(TERM=xterm-ghostty)
|
||||
elif command -v infocmp >/dev/null 2>&1; then
|
||||
local tinfo
|
||||
tinfo=$(infocmp -x xterm-ghostty 2>/dev/null) || echo "Warning: xterm-ghostty terminfo not found locally." >&2
|
||||
if [[ -n "$tinfo" ]]; then
|
||||
echo "Setting up Ghostty terminfo on remote host..." >&2
|
||||
local cpath
|
||||
cpath="/tmp/ghostty-ssh-$USER-$RANDOM-$(date +%s)"
|
||||
case $(echo "$tinfo" | command ssh "${opts[@]}" -o ControlMaster=yes -o ControlPath="$cpath" -o ControlPersist=60s "$@" '
|
||||
infocmp xterm-ghostty >/dev/null 2>&1 && echo OK && exit
|
||||
command -v tic >/dev/null 2>&1 || { echo NO_TIC; exit 1; }
|
||||
mkdir -p ~/.terminfo 2>/dev/null && tic -x - 2>/dev/null && echo OK || echo FAIL
|
||||
') in
|
||||
OK)
|
||||
builtin echo "Terminfo setup complete." >&2
|
||||
[[ -n $t ]] && "$_cache_script" add "$t"
|
||||
e+=(TERM=xterm-ghostty)
|
||||
c+=(-o "ControlPath=$cp")
|
||||
echo "Terminfo setup complete." >&2
|
||||
[[ -n "$target" ]] && "$_CACHE" add "$target"
|
||||
env+=(TERM=xterm-ghostty)
|
||||
ctrl+=(-o "ControlPath=$cpath")
|
||||
;;
|
||||
*) builtin echo "Warning: Failed to install terminfo." >&2 ;;
|
||||
*) echo "Warning: Failed to install terminfo." >&2 ;;
|
||||
esac
|
||||
fi
|
||||
else
|
||||
builtin echo "Warning: infocmp not found locally. Terminfo installation unavailable." >&2
|
||||
echo "Warning: infocmp not found locally. Terminfo installation unavailable." >&2
|
||||
fi
|
||||
fi
|
||||
|
||||
# Fallback TERM only if terminfo didn't set it
|
||||
if [[ "$GHOSTTY_SHELL_FEATURES" =~ ssh-env ]]; then
|
||||
[[ $TERM == xterm-ghostty && ! " ${(j: :)e} " =~ " TERM=" ]] && e+=(TERM=xterm-256color)
|
||||
[[ $TERM == xterm-ghostty && ! " ${env[*]} " =~ " TERM=" ]] && env+=(TERM=xterm-256color)
|
||||
fi
|
||||
|
||||
# Execute
|
||||
if (( ${#e} > 0 )); then
|
||||
env "${e[@]}" ssh "${o[@]}" "${c[@]}" "$@"
|
||||
if [[ ${#env[@]} -gt 0 ]]; then
|
||||
env "${env[@]}" command ssh "${opts[@]}" "${ctrl[@]}" "$@"
|
||||
else
|
||||
builtin command ssh "${o[@]}" "${c[@]}" "$@"
|
||||
command ssh "${opts[@]}" "${ctrl[@]}" "$@"
|
||||
fi
|
||||
}
|
||||
fi
|
||||
|
Reference in New Issue
Block a user