add sudo wrapper as optional shell integration feature

This commit is contained in:
Atanas Pepechkov
2024-01-13 18:56:21 +02:00
parent a925afae84
commit ee1366a0a8
5 changed files with 85 additions and 5 deletions

View File

@ -774,8 +774,9 @@ keybind: Keybinds = .{},
/// Available features: /// Available features:
/// ///
/// - "cursor" - Set the cursor to a blinking bar at the prompt. /// - "cursor" - Set the cursor to a blinking bar at the prompt.
/// - "sudo" - Set sudo wrapper to preserve terminfo.
/// ///
/// Example: "cursor", "no-cursor" /// Example: "cursor", "no-cursor", "sudo", "no-sudo"
@"shell-integration-features": ShellIntegrationFeatures = .{}, @"shell-integration-features": ShellIntegrationFeatures = .{},
/// Sets the reporting format for OSC sequences that request color information. /// Sets the reporting format for OSC sequences that request color information.
@ -2853,6 +2854,7 @@ pub const ShellIntegration = enum {
/// Shell integration features /// Shell integration features
pub const ShellIntegrationFeatures = packed struct { pub const ShellIntegrationFeatures = packed struct {
cursor: bool = true, cursor: bool = true,
sudo: bool = false,
}; };
/// OSC 4, 10, 11, and 12 default color reporting format. /// OSC 4, 10, 11, and 12 default color reporting format.

View File

@ -46,6 +46,30 @@ function __ghostty_precmd() {
PS0=$PS0'\[\e[0 q\]' PS0=$PS0'\[\e[0 q\]'
fi fi
# Sudo
if [[ "$GHOSTTY_SHELL_INTEGRATION_NO_SUDO" != "1" ]] && [[ -n "$TERMINFO" ]]; then
# Wrap `sudo` command to ensure Ghostty terminfo is preserved
sudo() {
builtin local sudo_has_sudoedit_flags="no"
for arg in "$@"; do
# Check if argument is '-e' or '--edit' (sudoedit flags)
if [[ "$arg" == "-e" || $arg == "--edit" ]]; then
sudo_has_sudoedit_flags="yes"
builtin break
fi
# Check if argument is neither an option nor a key-value pair
if [[ "$arg" != -* && "$arg" != *=* ]]; then
builtin break
fi
done
if [[ "$sudo_has_sudoedit_flags" == "yes" ]]; then
builtin command sudo "$@";
else
builtin command sudo TERMINFO="$TERMINFO" "$@";
fi
}
fi
# Command # Command
PS0=$PS0'$(__ghostty_get_current_command)' PS0=$PS0'$(__ghostty_get_current_command)'
PS1=$PS1'\[\e]2;$PWD\a\]' PS1=$PS1'\[\e]2;$PWD\a\]'

View File

@ -64,6 +64,35 @@ function __ghostty_setup --on-event fish_prompt -d "Setup ghostty integration"
end end
end end
# Check if we are setting sudo
set --local no_sudo "$GHOSTTY_SHELL_INTEGRATION_NO_SUDO"
# When using sudo shell integration feature, ensure $TERMINFO is set
# and `sudo` is not already a function or alias
if test -z $no_sudo
and test -n "$TERMINFO"; and test "file" = (type -t sudo 2> /dev/null; or echo "x")
# Wrap `sudo` command to ensure Ghostty terminfo is preserved
function sudo -d "Wrap sudo to preserve terminfo"
set --local sudo_has_sudoedit_flags "no"
for arg in $argv
# Check if argument is '-e' or '--edit' (sudoedit flags)
if string match -q -- "-e" "$arg"; or string match -q -- "--edit" "$arg"
set --local sudo_has_sudoedit_flags "yes"
break
end
# Check if argument is neither an option nor a key-value pair
if not string match -r -q -- "^-" "$arg"; and not string match -r -q -- "=" "$arg"
break
end
end
if test "$sudo_has_sudoedit_flags" = "yes"
command sudo $argv
else
command sudo TERMINFO="$TERMINFO" $argv
end
end
end
# Setup prompt marking # Setup prompt marking
function __ghostty_mark_prompt_start --on-event fish_prompt --on-event fish_cancel --on-event fish_posterror function __ghostty_mark_prompt_start --on-event fish_prompt --on-event fish_cancel --on-event fish_posterror
# If we never got the output end event, then we need to send it now. # If we never got the output end event, then we need to send it now.

View File

@ -218,6 +218,30 @@ _ghostty_deferred_init() {
builtin print -rnu $_ghostty_fd \$'\\e[0 q'" builtin print -rnu $_ghostty_fd \$'\\e[0 q'"
fi fi
# Sudo
if [[ "$GHOSTTY_SHELL_INTEGRATION_NO_SUDO" != "1" ]] && [[ -n "$TERMINFO" ]]; then
# Wrap `sudo` command to ensure Ghostty terminfo is preserved
sudo() {
builtin local sudo_has_sudoedit_flags="no"
for arg in "$@"; do
# Check if argument is '-e' or '--edit' (sudoedit flags)
if [[ "$arg" == "-e" || $arg == "--edit" ]]; then
sudo_has_sudoedit_flags="yes"
builtin break
fi
# Check if argument is neither an option nor a key-value pair
if [[ "$arg" != -* && "$arg" != *=* ]]; then
builtin break
fi
done
if [[ "$sudo_has_sudoedit_flags" == "yes" ]]; then
builtin command sudo "$@";
else
builtin command sudo TERMINFO="$TERMINFO" "$@";
fi
}
fi
# Some zsh users manually run `source ~/.zshrc` in order to apply rc file # Some zsh users manually run `source ~/.zshrc` in order to apply rc file
# changes to the current shell. This is a terrible practice that breaks many # changes to the current shell. This is a terrible practice that breaks many
# things, including our shell integration. For example, Oh My Zsh and Prezto # things, including our shell integration. For example, Oh My Zsh and Prezto

View File

@ -49,6 +49,7 @@ pub fn setup(
// Setup our feature env vars // Setup our feature env vars
if (!features.cursor) try env.put("GHOSTTY_SHELL_INTEGRATION_NO_CURSOR", "1"); if (!features.cursor) try env.put("GHOSTTY_SHELL_INTEGRATION_NO_CURSOR", "1");
if (!features.sudo) try env.put("GHOSTTY_SHELL_INTEGRATION_NO_SUDO", "1");
return shell; return shell;
} }