From 083cc2c8e8742bab8cce8c73a3e96f398e6b2da7 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Thu, 7 Mar 2024 14:39:05 +0100 Subject: feat(async)!: implement async prompt API and apply to git prompt (#12257) BREAKING CHANGE: the `git_prompt_info` prompt function has been reworked by default to use the new async prompt feature. If you're experiencing issues see #12257. Co-authored-by: Carlo Sala --- lib/async_prompt.zsh | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 lib/async_prompt.zsh (limited to 'lib/async_prompt.zsh') diff --git a/lib/async_prompt.zsh b/lib/async_prompt.zsh new file mode 100644 index 000000000..c6d03328b --- /dev/null +++ b/lib/async_prompt.zsh @@ -0,0 +1,140 @@ +# The async code is taken from +# https://github.com/zsh-users/zsh-autosuggestions/blob/master/src/async.zsh +# https://github.com/woefe/git-prompt.zsh/blob/master/git-prompt.zsh + +zmodload zsh/system + +# For now, async prompt function handlers are set up like so: +# First, define the async function handler and add the function name +# to the _omz_async_functions array: +# +# function _git_prompt_status_async { +# # Do some expensive operation that outputs to stdout +# } +# _omz_register_handler _git_prompt_status_async +# +# Then add a stub prompt function in `$PROMPT` or similar prompt variables, +# which will show the output of "$_OMZ_ASYNC_OUTPUT[handler_name]": +# +# function git_prompt_status { +# echo -n $_OMZ_ASYNC_OUTPUT[_git_prompt_status] +# } +# +# RPROMPT='$(git_prompt_status)' +# +# This API is subject to change and optimization. Rely on it at your own risk. + +function _omz_register_handler { + setopt localoptions noksharrays + typeset -ga _omz_async_functions + # we want to do nothing if there's no $1 function or we already set it up + if [[ -z "$1" ]] || (( ! ${+functions[$1]} )) \ + || (( ${_omz_async_functions[(Ie)$1]} )); then + return + fi + _omz_async_functions+=("$1") + # let's add the hook to async_request if it's not there yet + if (( ! ${precmd_functions[(Ie)_omz_async_request]} )) \ + && (( ${+functions[_omz_async_request]})); then + autoload -Uz add-zsh-hook + add-zsh-hook precmd _omz_async_request + fi +} + +# Set up async handlers and callbacks +function _omz_async_request { + typeset -gA _OMZ_ASYNC_FDS _OMZ_ASYNC_PIDS _OMZ_ASYNC_OUTPUT + + # executor runs a subshell for all async requests based on key + local handler + for handler in ${_omz_async_functions}; do + (( ${+functions[$handler]} )) || continue + + local fd=${_OMZ_ASYNC_FDS[$handler]:--1} + local pid=${_OMZ_ASYNC_PIDS[$handler]:--1} + + # If we've got a pending request, cancel it + if (( fd != -1 && pid != -1 )) && { true <&$fd } 2>/dev/null; then + # Close the file descriptor and remove the handler + exec {fd}<&- + zle -F $fd + + # Zsh will make a new process group for the child process only if job + # control is enabled (MONITOR option) + if [[ -o MONITOR ]]; then + # Send the signal to the process group to kill any processes that may + # have been forked by the async function handler + kill -TERM -$pid 2>/dev/null + else + # Kill just the child process since it wasn't placed in a new process + # group. If the async function handler forked any child processes they may + # be orphaned and left behind. + kill -TERM $pid 2>/dev/null + fi + fi + + # Define global variables to store the file descriptor, PID and output + _OMZ_ASYNC_FDS[$handler]=-1 + _OMZ_ASYNC_PIDS[$handler]=-1 + + # Fork a process to fetch the git status and open a pipe to read from it + exec {fd}< <( + # Tell parent process our PID + builtin echo ${sysparams[pid]} + # Store handler name for callback + builtin echo $handler + # Run the async function handler + $handler + ) + + # Save FD for handler + _OMZ_ASYNC_FDS[$handler]=$fd + + # There's a weird bug here where ^C stops working unless we force a fork + # See https://github.com/zsh-users/zsh-autosuggestions/issues/364 + command true + + # Save the PID from the handler child process + read pid <&$fd + _OMZ_ASYNC_PIDS[$handler]=$pid + + # When the fd is readable, call the response handler + zle -F "$fd" _omz_async_callback + done +} + +# Called when new data is ready to be read from the pipe +function _omz_async_callback() { + emulate -L zsh + + local fd=$1 # First arg will be fd ready for reading + local err=$2 # Second arg will be passed in case of error + + if [[ -z "$err" || "$err" == "hup" ]]; then + # Get handler name from first line + local handler + read handler <&$fd + + # Store old output which is supposed to be already printed + local old_output="${_OMZ_ASYNC_OUTPUT[$handler]}" + + # Read output from fd + _OMZ_ASYNC_OUTPUT[$handler]="$(cat <&$fd)" + + # Repaint prompt if output has changed + if [[ "$old_output" != "${_OMZ_ASYNC_OUTPUT[$handler]}" ]]; then + zle reset-prompt + zle -R + fi + + # Close the fd + exec {fd}<&- + fi + + # Always remove the handler + zle -F "$fd" + + # Unset global FD variable to prevent closing user created FDs in the precmd hook + _OMZ_ASYNC_FDS[$handler]=-1 + _OMZ_ASYNC_PIDS[$handler]=-1 +} -- cgit v1.2.3-70-g09d2 From 06753e8146332aa787857fc5cc41caa2b5f753f0 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Sat, 9 Mar 2024 18:22:35 +0100 Subject: fix(async): register the git prompt async handler correctly (#12267) This fix conditionally registers the git prompt async handler only if `git_prompt_info` is used anywhere in the prompt variables. This is done in the proper order, so that the async request is processed once the handler has been registered. This fix also passes the return value of the previous command to each of the async handlers, in case they are needed. --- lib/async_prompt.zsh | 12 +++++++++--- lib/git.zsh | 20 +++++++++++++++++++- 2 files changed, 28 insertions(+), 4 deletions(-) (limited to 'lib/async_prompt.zsh') diff --git a/lib/async_prompt.zsh b/lib/async_prompt.zsh index c6d03328b..384e49d33 100644 --- a/lib/async_prompt.zsh +++ b/lib/async_prompt.zsh @@ -5,8 +5,8 @@ zmodload zsh/system # For now, async prompt function handlers are set up like so: -# First, define the async function handler and add the function name -# to the _omz_async_functions array: +# First, define the async function handler and register the handler +# with _omz_register_handler: # # function _git_prompt_status_async { # # Do some expensive operation that outputs to stdout @@ -17,7 +17,7 @@ zmodload zsh/system # which will show the output of "$_OMZ_ASYNC_OUTPUT[handler_name]": # # function git_prompt_status { -# echo -n $_OMZ_ASYNC_OUTPUT[_git_prompt_status] +# echo -n $_OMZ_ASYNC_OUTPUT[_git_prompt_status_async] # } # # RPROMPT='$(git_prompt_status)' @@ -43,6 +43,7 @@ function _omz_register_handler { # Set up async handlers and callbacks function _omz_async_request { + local -i ret=$? typeset -gA _OMZ_ASYNC_FDS _OMZ_ASYNC_PIDS _OMZ_ASYNC_OUTPUT # executor runs a subshell for all async requests based on key @@ -83,6 +84,8 @@ function _omz_async_request { builtin echo ${sysparams[pid]} # Store handler name for callback builtin echo $handler + # Set exit code for the handler if used + (exit $ret) # Run the async function handler $handler ) @@ -138,3 +141,6 @@ function _omz_async_callback() { _OMZ_ASYNC_FDS[$handler]=-1 _OMZ_ASYNC_PIDS[$handler]=-1 } + +autoload -Uz add-zsh-hook +add-zsh-hook precmd _omz_async_request diff --git a/lib/git.zsh b/lib/git.zsh index 8fe999095..96df5589d 100644 --- a/lib/git.zsh +++ b/lib/git.zsh @@ -40,11 +40,29 @@ function _omz_git_prompt_status() { # Enable async prompt by default unless the setting is at false / no if zstyle -t ':omz:alpha:lib:git' async-prompt; then function git_prompt_info() { - _omz_register_handler _omz_git_prompt_status if [[ -n "$_OMZ_ASYNC_OUTPUT[_omz_git_prompt_status]" ]]; then echo -n "$_OMZ_ASYNC_OUTPUT[_omz_git_prompt_status]" fi } + + # Conditionally register the async handler, only if it's needed in $PROMPT + # or any of the other prompt variables + function _defer_async_git_register() { + # Check if git_prompt_info is used in a prompt variable + case "${PS1}:${PS2}:${PS3}:${PS4}:${RPS1}:${RPS2}:${RPS3}:${RPS4}" in + *(\$\(git_prompt_info\)|\`git_prompt_info\`)*) + _omz_register_handler _omz_git_prompt_status + return + ;; + esac + + add-zsh-hook -d precmd _defer_async_git_register + unset -f _defer_async_git_register + } + + # Register the async handler first. This needs to be done before + # the async request prompt is run + precmd_functions=(_defer_async_git_register $precmd_functions) else function git_prompt_info() { _omz_git_prompt_status -- cgit v1.2.3-70-g09d2 From b43b84abc77850a3734c127c38afdd7cf7739dc6 Mon Sep 17 00:00:00 2001 From: Loïc Yhuel Date: Wed, 3 Apr 2024 19:42:47 +0200 Subject: fix(async): avoid blocking the shell while waiting (#12304) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Marc Cornellà --- lib/async_prompt.zsh | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'lib/async_prompt.zsh') diff --git a/lib/async_prompt.zsh b/lib/async_prompt.zsh index 384e49d33..ac95bcd73 100644 --- a/lib/async_prompt.zsh +++ b/lib/async_prompt.zsh @@ -82,10 +82,8 @@ function _omz_async_request { exec {fd}< <( # Tell parent process our PID builtin echo ${sysparams[pid]} - # Store handler name for callback - builtin echo $handler # Set exit code for the handler if used - (exit $ret) + () { return $ret } # Run the async function handler $handler ) @@ -98,8 +96,7 @@ function _omz_async_request { command true # Save the PID from the handler child process - read pid <&$fd - _OMZ_ASYNC_PIDS[$handler]=$pid + read -u $fd "_OMZ_ASYNC_PIDS[$handler]" # When the fd is readable, call the response handler zle -F "$fd" _omz_async_callback @@ -114,15 +111,14 @@ function _omz_async_callback() { local err=$2 # Second arg will be passed in case of error if [[ -z "$err" || "$err" == "hup" ]]; then - # Get handler name from first line - local handler - read handler <&$fd + # Get handler name from fd + local handler="${(k)_OMZ_ASYNC_FDS[(r)$fd]}" # Store old output which is supposed to be already printed local old_output="${_OMZ_ASYNC_OUTPUT[$handler]}" # Read output from fd - _OMZ_ASYNC_OUTPUT[$handler]="$(cat <&$fd)" + IFS= read -r -u $fd -d '' "_OMZ_ASYNC_OUTPUT[$handler]" # Repaint prompt if output has changed if [[ "$old_output" != "${_OMZ_ASYNC_OUTPUT[$handler]}" ]]; then -- cgit v1.2.3-70-g09d2 From 9d529c41cc82580d0a947ce8bcf5ff7775585fe5 Mon Sep 17 00:00:00 2001 From: Loïc Yhuel Date: Thu, 4 Apr 2024 16:20:20 +0200 Subject: perf(async): avoid executing `true` if not required (#12318) The issue which required "command true" was fixed in zsh 5.8. --- lib/async_prompt.zsh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'lib/async_prompt.zsh') diff --git a/lib/async_prompt.zsh b/lib/async_prompt.zsh index ac95bcd73..a83c57f35 100644 --- a/lib/async_prompt.zsh +++ b/lib/async_prompt.zsh @@ -3,6 +3,7 @@ # https://github.com/woefe/git-prompt.zsh/blob/master/git-prompt.zsh zmodload zsh/system +autoload -Uz is-at-least # For now, async prompt function handlers are set up like so: # First, define the async function handler and register the handler @@ -93,7 +94,8 @@ function _omz_async_request { # There's a weird bug here where ^C stops working unless we force a fork # See https://github.com/zsh-users/zsh-autosuggestions/issues/364 - command true + # and https://github.com/zsh-users/zsh-autosuggestions/pull/612 + is-at-least 5.8 || command true # Save the PID from the handler child process read -u $fd "_OMZ_ASYNC_PIDS[$handler]" -- cgit v1.2.3-70-g09d2 From f78c6b90fc18e2d0bb119ff549207463ce1d57a5 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Tue, 16 Apr 2024 18:07:10 +0200 Subject: fix(async): fix crash on zsh < 5.0.6 (#12358) --- lib/async_prompt.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/async_prompt.zsh') diff --git a/lib/async_prompt.zsh b/lib/async_prompt.zsh index a83c57f35..db48446e7 100644 --- a/lib/async_prompt.zsh +++ b/lib/async_prompt.zsh @@ -124,7 +124,7 @@ function _omz_async_callback() { # Repaint prompt if output has changed if [[ "$old_output" != "${_OMZ_ASYNC_OUTPUT[$handler]}" ]]; then - zle reset-prompt + zle .reset-prompt zle -R fi -- cgit v1.2.3-70-g09d2 From 5eaebdf0fe870a8c599c89c6b70a0c5ee8c78b42 Mon Sep 17 00:00:00 2001 From: Carlo Sala Date: Thu, 20 Mar 2025 20:02:34 +0100 Subject: fix(termsupport): ensure ohmyzsh can run with `set -eu` Closes #12870 --- lib/async_prompt.zsh | 3 ++- lib/termsupport.zsh | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'lib/async_prompt.zsh') diff --git a/lib/async_prompt.zsh b/lib/async_prompt.zsh index db48446e7..151e24b8c 100644 --- a/lib/async_prompt.zsh +++ b/lib/async_prompt.zsh @@ -26,7 +26,7 @@ autoload -Uz is-at-least # This API is subject to change and optimization. Rely on it at your own risk. function _omz_register_handler { - setopt localoptions noksharrays + setopt localoptions noksharrays unset typeset -ga _omz_async_functions # we want to do nothing if there's no $1 function or we already set it up if [[ -z "$1" ]] || (( ! ${+functions[$1]} )) \ @@ -44,6 +44,7 @@ function _omz_register_handler { # Set up async handlers and callbacks function _omz_async_request { + setopt localoptions noksharrays unset local -i ret=$? typeset -gA _OMZ_ASYNC_FDS _OMZ_ASYNC_PIDS _OMZ_ASYNC_OUTPUT diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh index 087bae9bb..d2fbf42cd 100644 --- a/lib/termsupport.zsh +++ b/lib/termsupport.zsh @@ -47,7 +47,7 @@ fi # Runs before showing the prompt function omz_termsupport_precmd { - [[ "${DISABLE_AUTO_TITLE:-}" != true ]] || return + [[ "${DISABLE_AUTO_TITLE:-}" != true ]] || return 0 title "$ZSH_THEME_TERM_TAB_TITLE_IDLE" "$ZSH_THEME_TERM_TITLE_IDLE" } @@ -145,6 +145,7 @@ esac # Identifies the directory using a file: URI scheme, including # the host name to disambiguate local vs. remote paths. function omz_termsupport_cwd { + setopt localoptions unset # Percent-encode the host and path names. local URL_HOST URL_PATH URL_HOST="$(omz_urlencode -P $HOST)" || return 1 -- cgit v1.2.3-70-g09d2