David Matos 2025-12-17 16:48:47 -05:00 committed by GitHub
commit 3c0ffd4852
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 167 additions and 0 deletions

View File

@ -76,6 +76,18 @@ allowing us to automatically integrate with the shell. For details
on the Fish startup process, see the
[Fish documentation](https://fishshell.com/docs/current/language.html).
### Nushell
For `nushell` Ghostty prepends to the `XDG_DATA_DIRS` directory. Nushell automatically
loads configuration files in `<XDG_DATA_DIRS>/nushell/vendor/autoload/*.nu` on startup. These
directories are represented in `Nu` by `$nu.vendor-autoload-dirs`. For more details see
[Nushell documentation](https://www.nushell.sh/book/configuration.html#configuration-overview)
> [!NOTE]
>
> Ghostty implements concretely the `ssh-*` features. The rest of the features are supported mostly out of the box by Nushell.
### Zsh
Automatic [Zsh](https://www.zsh.org/) integration works by temporarily setting

View File

@ -0,0 +1,108 @@
# Enables SSH environment variable compatibility.
# Converts TERM from xterm-ghostty to xterm-256color
# and propagates COLORTERM, TERM_PROGRAM, and TERM_PROGRAM_VERSION
# check your sshd_config on remote host to see if these variables are accepted
def set_ssh_env []: nothing -> record<ssh_term: string, ssh_opts: list<string>> {
return {
ssh_term: "xterm-256color",
ssh_opts: ["-o", "SetEnv COLORTERM=truecolor", "-o", "SendEnv TERM_PROGRAM TERM_PROGRAM_VERSION"]
}
}
# Enables automatic terminfo installation on remote hosts.
# Attempts to install Ghostty's terminfo entry using infocmp and tic when
# connecting to hosts that lack it.
# Requires infocmp to be available locally and tic to be available on remote hosts.
# Caches installations to avoid repeat installations.
def set_ssh_terminfo [
ssh_opts: list<string>,
ssh_args: list<string>
]: [nothing -> record<ssh_term: string, ssh_opts: list<string>>] {
mut ssh_opts = $ssh_opts
let ssh_cfg = ^ssh -G ...($ssh_args)
| lines
| parse "{key} {value}"
| where key in ["user", "hostname"]
| select key value
| transpose -rd
| default { user: $env.USER, hostname: "localhost" }
let ssh_id = $"($ssh_cfg.user)@($ssh_cfg.hostname)"
let ghostty_bin = $env.GHOSTTY_BIN_DIR | path join "ghostty"
let is_cached = (
^$ghostty_bin ...(["+ssh-cache", $"--host=($ssh_id)"])
| complete
| $in.exit_code == 0
)
if not $is_cached {
let ssh_opts_copy = $ssh_opts
let terminfo_data = try {^infocmp -0 -x xterm-ghostty} catch {
print "Warning: Could not generate terminfo data."
return {ssh_term: "xterm-256color", ssh_opts: $ssh_opts_copy}
}
print $"Setting up xterm-ghostty terminfo on ($ssh_cfg.hostname)..."
let ctrl_path = (
try {
mktemp -td $"ghostty-ssh-($ssh_cfg.user).XXXXXX"
} catch {
$"/tmp/ghostty-ssh-($ssh_cfg.user).($nu.pid)"
} | path join "socket"
)
let master_parts = $ssh_opts ++ ["-o" "ControlMaster=yes" "-o" $"ControlPath=($ctrl_path)" "-o" "ControlPersist=60s"] ++ $ssh_args
($terminfo_data) | ^ssh ...(
$master_parts ++
[
'
infocmp xterm-ghostty >/dev/null 2>&1 && exit 0
command -v tic >/dev/null 2>&1 || exit 1
mkdir -p ~/.terminfo 2>/dev/null && tic -x - 2>/dev/null && exit 0
exit 1'
]
)
| complete
| if $in.exit_code != 0 {
print "Warning: Failed to install terminfo."
return {ssh_term: "xterm-256color" ssh_opts: $ssh_opts}
}
^$ghostty_bin ...(["+ssh-cache", $"--add=($ssh_id)"]) o+e>| ignore
$ssh_opts ++= ["-o", $"ControlPath=($ctrl_path)"]
}
return {ssh_term: "xterm-ghostty", ssh_opts: $ssh_opts}
}
# SSH Integration
export def --wrapped ssh [...ssh_args: string]: any -> any {
if ($ssh_args | is-empty) {
return (^ssh)
}
mut session = {ssh_term: "", ssh_opts: []}
let shell_features = $env.GHOSTTY_SHELL_FEATURES | split row ','
if "ssh-env" in $shell_features {
$session = set_ssh_env
}
if "ssh-terminfo" in $shell_features {
$session = set_ssh_terminfo $session.ssh_opts $ssh_args
}
let ssh_parts = $session.ssh_opts ++ $ssh_args
with-env {TERM: $session.ssh_term} {
^ssh ...$ssh_parts
}
}
# Removes Ghostty's data directory from XDG_DATA_DIRS
$env.XDG_DATA_DIRS = (
$env.XDG_DATA_DIRS
| split row ':'
| where {|path| $path != $env.GHOSTTY_SHELL_INTEGRATION_XDG_DIR }
| str join ':'
)

View File

@ -0,0 +1,29 @@
let enable_integration = $env.GHOSTTY_SHELL_FEATURES | split row ','
| where ($it in ["ssh-env" "ssh-terminfo"])
| is-not-empty
let ghostty_ssh_file = $env.GHOSTTY_RESOURCES_DIR
| path join "shell-integration" "nushell" "ghostty-ssh-integration.nu"
let ssh_integration_file = $nu.data-dir | path join "ghostty-ssh-integration.nu"
let ssh_file_exists = $ssh_integration_file | path exists
# TOD0: In case of an update to the `ghostty-ssh-integration.nu` file
# the file wont be updated here, so we need to support
# saving the new file once there is an update
match [$enable_integration $ssh_file_exists] {
[true false] => {
# $nu.data-dir is not created by default
# https://www.nushell.sh/book/configuration.html#startup-variables
$nu.data-dir | path exists | if (not $in) { mkdir $nu.data-dir }
open $ghostty_ssh_file | save $ssh_integration_file
}
[false true] => {
# We need to check if the user disabled `ssh-integration` and thus
# the integration file needs to be removed so it doesnt get sourced by
# the `source-integration.nu` file
rm $ssh_integration_file
}
_ => { }
}

View File

@ -0,0 +1,11 @@
# Sourcing the `ghostty-integration.nu` cant be on the
# `bootstrap-integration.nu` file because it tries to resolve the `sourced`
# file at parsing time, which would make it source nothing.
# But here we rely on the fact that `boostrap-integration.nu` gets parsed
# and executed first, and then we can count on `ssh_integration_file` being available
#https://www.nushell.sh/book/thinking_in_nu.html#example-dynamically-generating-source
const ssh_integration_file = $nu.data-dir | path join "ghostty-ssh-integration.nu"
source (if ($ssh_integration_file | path exists) { $ssh_integration_file } else { null })

View File

@ -129,6 +129,13 @@ fn setupShell(
};
}
if (std.mem.eql(u8, "nu", exe)) {
// Sets up XDG_DATA_DIRS so that it can be picked automatically by
// nushell on startup.
try setupXdgDataDirs(alloc_arena, resource_dir, env);
return null;
}
if (std.mem.eql(u8, "zsh", exe)) {
try setupZsh(resource_dir, env);
return .{