shell-integration: implement "no-cursor" option

Implement a "no-cursor" option for shell integration. This option acts
like "detect" but doesn't set the cursor shape.
This commit is contained in:
Tim Culverhouse
2023-11-07 16:24:58 -06:00
parent 36dd5ef4ee
commit 4fac674016
5 changed files with 44 additions and 23 deletions

View File

@ -532,6 +532,8 @@ keybind: Keybinds = .{},
/// configure your shell to enable the integration. /// configure your shell to enable the integration.
/// * "detect" - Detect the shell based on the filename. /// * "detect" - Detect the shell based on the filename.
/// * "fish", "zsh" - Use this specific shell injection scheme. /// * "fish", "zsh" - Use this specific shell injection scheme.
/// * "no-cursor" - Detect the shell as in 'detect', but doesn't set cursor
/// shapes
/// ///
/// The default value is "detect". /// The default value is "detect".
@"shell-integration": ShellIntegration = .detect, @"shell-integration": ShellIntegration = .detect,
@ -2182,6 +2184,7 @@ pub const ShellIntegration = enum {
detect, detect,
fish, fish,
zsh, zsh,
@"no-cursor",
}; };
/// OSC 10 and 11 default color reporting format. /// OSC 10 and 11 default color reporting format.

View File

@ -41,8 +41,10 @@ function __ghostty_precmd() {
PS2=$PS2'\[\e]133;B\a\]' PS2=$PS2'\[\e]133;B\a\]'
# Cursor # Cursor
if test "$GHOSTTY_SHELL_INTEGRATION_NO_CURSOR" != "1"; then
PS1=$PS1'\[\e[5 q\]' PS1=$PS1'\[\e[5 q\]'
PS0=$PS0'\[\e[0 q\]' PS0=$PS0'\[\e[0 q\]'
fi
# Command # Command
PS0=$PS0'$(__ghostty_get_current_command)' PS0=$PS0'$(__ghostty_get_current_command)'

View File

@ -51,12 +51,17 @@ status --is-interactive || ghostty_exit
function __ghostty_setup --on-event fish_prompt -d "Setup ghostty integration" function __ghostty_setup --on-event fish_prompt -d "Setup ghostty integration"
functions -e __ghostty_setup functions -e __ghostty_setup
# Change the cursor to a beam on prompt. # Check if we are setting cursors
function __ghostty_set_cursor_beam --on-event fish_prompt -d "Set cursor shape" set --local no_cursor "$GHOSTTY_SHELL_INTEGRATION_NO_CURSOR"
echo -en "\e[5 q"
end if test -z $no_cursor
function __ghostty_reset_cursor --on-event fish_preexec -d "Reset cursor shape" # Change the cursor to a beam on prompt.
echo -en "\e[0 q" function __ghostty_set_cursor_beam --on-event fish_prompt -d "Set cursor shape"
echo -en "\e[5 q"
end
function __ghostty_reset_cursor --on-event fish_preexec -d "Reset cursor shape"
echo -en "\e[0 q"
end
end end
# Setup prompt marking # Setup prompt marking
@ -82,7 +87,7 @@ function __ghostty_setup --on-event fish_prompt -d "Setup ghostty integration"
# Report pwd. This is actually built-in to fish but only for terminals # Report pwd. This is actually built-in to fish but only for terminals
# that match an allowlist and that isn't us. # that match an allowlist and that isn't us.
function __update_cwd_osc --on-variable PWD -d 'Notify capable terminals when $PWD changes' function __update_cwd_osc --on-variable PWD -d 'Notify capable terminals when $PWD changes'
if status --is-command-substitution || set -q INSIDE_EMACS if status --is-command-substitution || set -q INSIDE_EMACS
return return
end end
@ -93,7 +98,9 @@ function __ghostty_setup --on-event fish_prompt -d "Setup ghostty integration"
set --global fish_handle_reflow 1 set --global fish_handle_reflow 1
# Initial calls for first prompt # Initial calls for first prompt
__ghostty_set_cursor_beam if test -z $no_cursor
__ghostty_set_cursor_beam
end
__ghostty_mark_prompt_start __ghostty_mark_prompt_start
__update_cwd_osc __update_cwd_osc
end end

View File

@ -200,21 +200,23 @@ _ghostty_deferred_init() {
functions[_ghostty_preexec]+=" functions[_ghostty_preexec]+="
builtin print -rnu $_ghostty_fd \$'\\e]2;'\"\${(V)1}\"\$'\\a'" builtin print -rnu $_ghostty_fd \$'\\e]2;'\"\${(V)1}\"\$'\\a'"
# Enable cursor shape changes depending on the current keymap. if [[ "$GHOSTTY_SHELL_INTEGRATION_NO_CURSOR" != 1 ]]; then
# This implementation leaks blinking block cursor into external commands # Enable cursor shape changes depending on the current keymap.
# executed from zle. For example, users of fzf-based widgets may find # This implementation leaks blinking block cursor into external commands
# themselves with a blinking block cursor within fzf. # executed from zle. For example, users of fzf-based widgets may find
_ghostty_zle_line_init _ghostty_zle_line_finish _ghostty_zle_keymap_select() { # themselves with a blinking block cursor within fzf.
case ${KEYMAP-} in _ghostty_zle_line_init _ghostty_zle_line_finish _ghostty_zle_keymap_select() {
# Blinking block cursor. case ${KEYMAP-} in
vicmd|visual) builtin print -nu "$_ghostty_fd" '\e[1 q';; # Blinking block cursor.
# Blinking bar cursor. vicmd|visual) builtin print -nu "$_ghostty_fd" '\e[1 q';;
*) builtin print -nu "$_ghostty_fd" '\e[5 q';; # Blinking bar cursor.
esac *) builtin print -nu "$_ghostty_fd" '\e[5 q';;
} esac
# Restore the blinking default shape before executing an external command }
functions[_ghostty_preexec]+=" # Restore the blinking default shape before executing an external command
builtin print -rnu $_ghostty_fd \$'\\e[0 q'" functions[_ghostty_preexec]+="
builtin print -rnu $_ghostty_fd \$'\\e[0 q'"
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

View File

@ -812,6 +812,13 @@ const Subprocess = struct {
.detect => null, .detect => null,
.fish => .fish, .fish => .fish,
.zsh => .zsh, .zsh => .zsh,
.@"no-cursor" => nc: {
// We add an environment variable for the shell integration
// scripts to pick up to prevent setting cursor shapes.
// Setting to "no_cursor" means we will detect the shell
try env.put("GHOSTTY_SHELL_INTEGRATION_NO_CURSOR", "1");
break :nc null;
},
}; };
const dir = opts.resources_dir orelse break :shell null; const dir = opts.resources_dir orelse break :shell null;