ghostty/src/shell-integration
Mitchell Hashimoto 1a27ce0797 bash: improved 'sudo' command wrapper (#4080)
The previous approach to wrapping `sudo` had a few shortcomings:

1. We were (re)defining our 'sudo' function wrapper in the "precmd"
path. It only needs to be defined once in the shell session.
2. If there was an existing 'sudo' alias, the function definition would
conflict and result in a syntax error.

Fix (1) by hoisting the 'sudo' function into global scope. I also
considered only defining our wrapper if an executable `sudo` binary
could be found (e.g. `-x $(builtin command -v sudo)`, but let's keep the
existing behavior for now. This allows for a `sudo` command to be
installed later in the shell session and still be wrapped.

Address (2) by defining the wrapper function using `function sudo`
(instead of `sudo()`) syntax. An explicit function definition won't
clash with an existing 'sudo' alias, although the alias will continue to
take precedence (i.e. our wrapper won't be called). If the alias is
defined _after_ our 'sudo' function is defined, our function will call
the aliased command.

This ordering is relevant because it can result in different behaviors
depending on when a user defines their aliases relative to sourcing the
shell integration script. Our recommendation remains that users either
use automatic shell injection or manually source the shell integration
script _before_ other things in their `.bashrc`, so that aligns with the
expected behavior of the 'sudo' wrapper with regard to aliases. Given
that, I don't think we need any more explicit user-facing documentation
on this beyond the script-level comments.
2024-12-30 08:44:47 -08:00
..

Shell Integration Code

This is the shell-specific shell-integration code that is used for the shell-integration feature set that Ghostty supports.

This README is meant as developer documentation and not as user documentation. For user documentation, see the main README.

Implementation Details

Bash

Automatic Bash shell integration works by starting Bash in POSIX mode and using the ENV environment variable to load our integration script (bash/ghostty.bash). This prevents Bash from loading its normal startup files, which becomes our script's responsibility (along with disabling POSIX mode).

Bash shell integration can also be sourced manually from bash/ghostty.bash. This also works for older versions of Bash.

# Ghostty shell integration for Bash. This must be at the top of your bashrc!
if [ -n "${GHOSTTY_RESOURCES_DIR}" ]; then
    builtin source "${GHOSTTY_RESOURCES_DIR}/shell-integration/bash/ghostty.bash"
fi

Note

The version of Bash distributed with macOS (/bin/bash) does not support automatic shell integration. You'll need to manually source the shell integration script (as shown above). You can also install a standard version of Bash from Homebrew or elsewhere and set it as your shell.

Elvish

For Elvish, $GHOSTTY_RESOURCES_DIR/src/shell-integration contains an ./elvish/lib/ghostty-integration.elv file.

Elvish, on startup, searches for paths defined in XDG_DATA_DIRS variable for ./elvish/lib/*.elv files and imports them. They are thus made available for use as modules by way of use <filename>.

Ghostty launches Elvish, passing the environment with XDG_DATA_DIRSprepended with $GHOSTTY_RESOURCES_DIR/src/shell-integration. It contains ./elvish/lib/ghostty-integration.elv. The user can then import it by use ghostty-integration, which will run the integration routines.

The Elvish shell integration is supported by the community and is not officially supported by Ghostty. We distribute it for ease of access and use but do not provide support for it. If you experience issues with the Elvish shell integration, I welcome any contributions to fix them. Thank you!

Fish

For Fish, Ghostty prepends to the XDG_DATA_DIRS directory. Fish automatically loads configuration files in <XDG_DATA_DIR>/fish/vendor_conf.d/*.fish on startup, allowing us to automatically integrate with the shell. For details on the Fish startup process, see the Fish documentation.

Zsh

For zsh, Ghostty sets ZDOTDIR so that it loads our configuration from the zsh directory. The existing ZDOTDIR is retained so that after loading the Ghostty shell integration the normal Zsh loading sequence occurs.

if [[ -n $GHOSTTY_RESOURCES_DIR ]]; then
  source "$GHOSTTY_RESOURCES_DIR"/shell-integration/zsh/ghostty-integration
fi