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/git.zsh | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'lib/git.zsh') diff --git a/lib/git.zsh b/lib/git.zsh index f049f73c2..6f4823458 100644 --- a/lib/git.zsh +++ b/lib/git.zsh @@ -9,14 +9,18 @@ function __git_prompt_git() { GIT_OPTIONAL_LOCKS=0 command git "$@" } -function git_prompt_info() { +function _omz_git_prompt_status() { # If we are on a folder not tracked by git, get out. # Otherwise, check for hide-info at global and local repository level if ! __git_prompt_git rev-parse --git-dir &> /dev/null \ - || [[ "$(__git_prompt_git config --get oh-my-zsh.hide-info 2>/dev/null)" == 1 ]]; then + || [[ "$(__git_prompt_git config --get oh-my-zsh.hide-info 2>/dev/null)" == 1 ]]; then return 0 fi + # Get either: + # - the current branch name + # - the tag name if we are on a tag + # - the short SHA of the current commit local ref ref=$(__git_prompt_git symbolic-ref --short HEAD 2> /dev/null) \ || ref=$(__git_prompt_git describe --tags --exact-match HEAD 2> /dev/null) \ @@ -33,6 +37,20 @@ function git_prompt_info() { echo "${ZSH_THEME_GIT_PROMPT_PREFIX}${ref:gs/%/%%}${upstream:gs/%/%%}$(parse_git_dirty)${ZSH_THEME_GIT_PROMPT_SUFFIX}" } +# 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 + } +else + function git_prompt_info() { + _omz_git_prompt_status + } +fi + # Checks if working tree is dirty function parse_git_dirty() { local STATUS -- cgit v1.2.3-70-g09d2