fish: add semantic prompt (OSC 133) support

This commit is contained in:
Daniel Lundin 2024-03-25 23:31:46 +01:00
parent 6a52cd262c
commit fab020b373
3 changed files with 148 additions and 9 deletions

View file

@ -0,0 +1,91 @@
function fish_prompt
switch "$fish_key_bindings"
case fish_hybrid_key_bindings fish_vi_key_bindings
set STARSHIP_KEYMAP "$fish_bind_mode"
case '*'
set STARSHIP_KEYMAP insert
end
set STARSHIP_CMD_PIPESTATUS $pipestatus
set STARSHIP_CMD_STATUS $status
# Account for changes in variable name between v2.7 and v3.0
set STARSHIP_DURATION "$CMD_DURATION$cmd_duration"
set STARSHIP_JOBS (count (jobs -p))
if test "$TRANSIENT" = 1
# Clear from cursor to end of screen as `commandline -f repaint` does not do this
# See https://github.com/fish-shell/fish-shell/issues/8418
printf \e\[0J
if type -q starship_transient_prompt_func
starship_transient_prompt_func
else
printf "\n\e[1;32m%%\e[0m "
end
else
/usr/bin/starship prompt --terminal-width="$COLUMNS" --status=$STARSHIP_CMD_STATUS --pipestatus="$STARSHIP_CMD_PIPESTATUS" --keymap=$STARSHIP_KEYMAP --cmd-duration=$STARSHIP_DURATION --jobs=$STARSHIP_JOBS
end
end
function fish_right_prompt
switch "$fish_key_bindings"
case fish_hybrid_key_bindings fish_vi_key_bindings
set STARSHIP_KEYMAP "$fish_bind_mode"
case '*'
set STARSHIP_KEYMAP insert
end
set STARSHIP_CMD_PIPESTATUS $pipestatus
set STARSHIP_CMD_STATUS $status
# Account for changes in variable name between v2.7 and v3.0
set STARSHIP_DURATION "$CMD_DURATION$cmd_duration"
set STARSHIP_JOBS (count (jobs -p))
if test "$TRANSIENT" = 1
if type -q starship_transient_rprompt_func
starship_transient_rprompt_func
else
printf ""
end
else
/usr/bin/starship prompt --right --terminal-width="$COLUMNS" --status=$STARSHIP_CMD_STATUS --pipestatus="$STARSHIP_CMD_PIPESTATUS" --keymap=$STARSHIP_KEYMAP --cmd-duration=$STARSHIP_DURATION --jobs=$STARSHIP_JOBS
end
end
# Disable virtualenv prompt, it breaks starship
set -g VIRTUAL_ENV_DISABLE_PROMPT 1
# Remove default mode prompt
builtin functions -e fish_mode_prompt
set -gx STARSHIP_SHELL fish
# Transience related functions
function reset-transient --on-event fish_postexec
set -g TRANSIENT 0
end
function transient_execute
if commandline --is-valid
set -g TRANSIENT 1
commandline -f repaint
else
set -g TRANSIENT 0
end
commandline -f execute
end
# --user is the default, but listed anyway to make it explicit.
function enable_transience --description 'enable transient prompt keybindings'
bind --user \r transient_execute
bind --user -M insert \r transient_execute
end
# Erase the transient prompt related key bindings.
# --user is the default, but listed anyway to make it explicit.
# Erasing a user binding will revert to the preset.
function disable_transience --description 'remove transient prompt keybindings'
bind --user -e \r
bind --user -M insert -e \r
end
# Set up the session key that will be used to store logs
# We don't use `random [min] [max]` because it is unavailable in older versions of fish shell
set -gx STARSHIP_SESSION_KEY (string sub -s1 -l16 (random)(random)(random)(random)(random)0000000000000000)
enable_transience

View file

@ -0,0 +1,57 @@
#!/usr/bin/fish
# SPDX-License-Identifier: CC0-1.0
if status --is-interactive
set _fishprompt_aid "fish"$fish_pid
set _fishprompt_started 0
# empty if running; or a numeric exit code; or CANCEL
set _fishprompt_postexec ""
functions -c fish_prompt _fishprompt_saved_prompt
set _fishprompt_prompt_count 0
set _fishprompt_disp_count 0
function _fishprompt_start --on-event fish_prompt
set _fishprompt_prompt_count (math $_fishprompt_prompt_count + 1)
# don't use post-exec, because it is called *before* omitted-newline output
if [ -n "$_fishprompt_postexec" ]
printf "\033]133;D;%s;aid=%s\007" "$_fishprompt_postexec" $_fishprompt_aid
end
printf "\033]133;A;aid=%s;cl=m\007" $_fishprompt_aid
end
function fish_prompt
set _fishprompt_disp_count (math $_fishprompt_disp_count + 1)
printf "\033]133;P;k=i\007%b\033]133;B\007" (string join "\n" (_fishprompt_saved_prompt))
set _fishprompt_started 1
set _fishprompt_postexec ""
end
function _fishprompt_preexec --on-event fish_preexec
if [ "$_fishprompt_started" = 1 ]
printf "\033]133;C;\007"
end
set _fishprompt_started 0
end
function _fishprompt_postexec --on-event fish_postexec
set _fishprompt_postexec $status
_fishprompt_start
end
function __fishprompt_cancel --on-event fish_cancel
set _fishprompt_postexec CANCEL
_fishprompt_start
end
function _fishprompt_exit --on-process %self
if [ "$_fishprompt_started" = 1 ]
printf "\033]133;Z;aid=%s\007" $_fishprompt_aid
end
end
if functions -q fish_right_prompt
functions -c fish_right_prompt _fishprompt_saved_right_prompt
function fish_right_prompt
printf "\033]133;P;k=r\007%b\033]133;B\007" (string join "\n" (_fishprompt_saved_right_prompt))
end
end
end

View file

@ -34,15 +34,6 @@ if status is-interactive
## History
atuin init fish | source
## Prompt
function starship_transient_prompt_func
starship module character
end
starship init fish | source
enable_transience
end
## Direnv