fix(zsh): decorate user-defined zsh widget

resolves #5193
This commit is contained in:
Polo 2024-07-06 19:30:48 +02:00 committed by Jan De Dobbeleer
parent ad34a6e530
commit f72c1464ab

View file

@ -40,6 +40,7 @@ function prompt_ohmyposh_preexec() {
if [[ "::FTCS_MARKS::" = "true" ]]; then if [[ "::FTCS_MARKS::" = "true" ]]; then
printf "\033]133;C\007" printf "\033]133;C\007"
fi fi
omp_start_time=$(::OMP:: get millis) omp_start_time=$(::OMP:: get millis)
} }
@ -49,18 +50,23 @@ function prompt_ohmyposh_precmd() {
omp_stack_count=${#dirstack[@]} omp_stack_count=${#dirstack[@]}
omp_elapsed=-1 omp_elapsed=-1
omp_no_exit_code="true" omp_no_exit_code="true"
if [ $omp_start_time ]; then if [ $omp_start_time ]; then
local omp_now=$(::OMP:: get millis --shell=zsh) local omp_now=$(::OMP:: get millis --shell=zsh)
omp_elapsed=$(($omp_now - $omp_start_time)) omp_elapsed=$(($omp_now - $omp_start_time))
omp_no_exit_code="false" omp_no_exit_code="false"
fi fi
if [[ "${omp_pipestatus_cache[-1]}" != "$omp_status_cache" ]]; then if [[ "${omp_pipestatus_cache[-1]}" != "$omp_status_cache" ]]; then
omp_pipestatus_cache=("$omp_status_cache") omp_pipestatus_cache=("$omp_status_cache")
fi fi
count=$((POSH_PROMPT_COUNT + 1)) count=$((POSH_PROMPT_COUNT + 1))
export POSH_PROMPT_COUNT=$count export POSH_PROMPT_COUNT=$count
set_poshcontext set_poshcontext
_set_posh_cursor_position _set_posh_cursor_position
eval "$(::OMP:: print primary --config="$POSH_THEME" --status="$omp_status_cache" --pipestatus="${omp_pipestatus_cache[*]}" --execution-time="$omp_elapsed" --stack-count="$omp_stack_count" --eval --shell=zsh --shell-version="$ZSH_VERSION" --no-status="$omp_no_exit_code")" eval "$(::OMP:: print primary --config="$POSH_THEME" --status="$omp_status_cache" --pipestatus="${omp_pipestatus_cache[*]}" --execution-time="$omp_elapsed" --stack-count="$omp_stack_count" --eval --shell=zsh --shell-version="$ZSH_VERSION" --no-status="$omp_no_exit_code")"
unset omp_start_time unset omp_start_time
} }
@ -86,7 +92,9 @@ function _posh-tooltip() {
zle autosuggest-clear zle autosuggest-clear
fi fi
fi fi
zle .self-insert zle .self-insert
# https://github.com/zsh-users/zsh-autosuggestions - fetch new suggestion after the space # https://github.com/zsh-users/zsh-autosuggestions - fetch new suggestion after the space
if [[ "$(zle -lL autosuggest-fetch)" ]]; then if [[ "$(zle -lL autosuggest-fetch)" ]]; then
# only if suggestions not disabled (variable not set) # only if suggestions not disabled (variable not set)
@ -97,15 +105,18 @@ function _posh-tooltip() {
# Get the first word of command line as tip. # Get the first word of command line as tip.
local tooltip_command=${${(MS)BUFFER##[[:graph:]]*}%%[[:space:]]*} local tooltip_command=${${(MS)BUFFER##[[:graph:]]*}%%[[:space:]]*}
# Ignore an empty/repeated tooltip command. # Ignore an empty/repeated tooltip command.
if [[ -z "$tooltip_command" ]] || [[ "$tooltip_command" = "$omp_tooltip_command" ]]; then if [[ -z "$tooltip_command" ]] || [[ "$tooltip_command" = "$omp_tooltip_command" ]]; then
return return
fi fi
omp_tooltip_command="$tooltip_command" omp_tooltip_command="$tooltip_command"
local tooltip=$(::OMP:: print tooltip --config="$POSH_THEME" --status="$omp_status_cache" --pipestatus="${omp_pipestatus_cache[*]}" --execution-time="$omp_elapsed" --stack-count="$omp_stack_count" --command="$tooltip_command" --shell=zsh --shell-version="$ZSH_VERSION" --no-status="$omp_no_exit_code") local tooltip=$(::OMP:: print tooltip --config="$POSH_THEME" --status="$omp_status_cache" --pipestatus="${omp_pipestatus_cache[*]}" --execution-time="$omp_elapsed" --stack-count="$omp_stack_count" --command="$tooltip_command" --shell=zsh --shell-version="$ZSH_VERSION" --no-status="$omp_no_exit_code")
if [[ -z "$tooltip" ]]; then if [[ -z "$tooltip" ]]; then
return return
fi fi
RPROMPT=$tooltip RPROMPT=$tooltip
zle .reset-prompt zle .reset-prompt
} }
@ -143,20 +154,43 @@ function enable_poshtooltips() {
bindkey " " _posh-tooltip bindkey " " _posh-tooltip
} }
function enable_poshtransientprompt() { # Helper function for posh::decorate_widget
zle -N zle-line-init _posh-zle-line-init # It calls the posh function right after the original definition of the widget
# $1 is the name of the widget to call
# $2 is the posh widget name
posh::call_widget()
{
builtin zle "${1}" &&
${2}
}
# restore broken key bindings # decorate_widget
# https://github.com/JanDeDobbeleer/oh-my-posh/discussions/2617#discussioncomment-3911044 # Allows to preserve any user defined value that may have been defined before posh tries to redefine it.
bindkey '^[[F' end-of-line # Instead, we keep the previous function and decorate it with the posh additions
bindkey '^[[H' beginning-of-line # $1: The name of the widget to decorate
_widgets=$(zle -la) # $2: The name of the posh function to decorate it with
if [[ "${_widgets[(r)down-line-or-beginning-search]}" ]]; then function posh::decorate_widget() {
bindkey '^[[B' down-line-or-beginning-search typeset -F SECONDS
fi local prefix=orig-s$SECONDS-r$RANDOM # unique each time, in case we're sourced more than once
if [[ "${_widgets[(r)up-line-or-beginning-search]}" ]]; then cur_widget=${1}
bindkey '^[[A' up-line-or-beginning-search posh_widget=${2}
fi
case ${widgets[$cur_widget]:-""} in
# Already decorated: do nothing.
user:_posh-decorated-*);;
user:*)
zle -N $prefix-$cur_widget ${widgets[$cur_widget]#*:}
eval "_posh-decorated-${(q)prefix}-${(q)cur_widget}() { posh::call_widget ${(q)prefix}-${(q)cur_widget} ${(q)posh_widget} -- \"\$@\" }"
zle -N $cur_widget _posh-decorated-$prefix-$cur_widget;;
# For now, do not decorate if it's not a user:*
*);;
esac
}
function enable_poshtransientprompt() {
posh::decorate_widget zle-line-init _posh-zle-line-init
} }
if [[ "::TOOLTIPS::" = "true" ]]; then if [[ "::TOOLTIPS::" = "true" ]]; then