Switch to an alternative method of async git in the sorin prompt

This commit is contained in:
Kaleb Elwert 2020-03-19 11:41:14 -07:00
parent 1b99be879c
commit e07027821b

View file

@ -32,29 +32,25 @@
# Load dependencies. # Load dependencies.
pmodload 'helper' pmodload 'helper'
function prompt_sorin_async_callback { function prompt_sorin_async_git_callback {
case $1 in # We can safely split on ':' because it isn't allowed in ref names.
prompt_sorin_async_git) IFS=':' read _git_target _git_post_target <<<"$3"
# We can safely split on ':' because it isn't allowed in ref names.
IFS=':' read _git_target _git_post_target <<<"$3"
# The target actually contains 3 space separated possibilities, so we need to # The target actually contains 3 space separated possibilities, so we need to
# make sure we grab the first one. # make sure we grab the first one.
_git_target=$(coalesce ${(@)${(z)_git_target}}) _git_target=$(coalesce ${(@)${(z)_git_target}})
if [[ -z "$_git_target" ]]; then if [[ -z "$_git_target" ]]; then
# No git target detected, flush the git fragment and redisplay the prompt. # No git target detected, flush the git fragment and redisplay the prompt.
if [[ -n "$_prompt_sorin_git" ]]; then if [[ -n "$_prompt_sorin_git" ]]; then
_prompt_sorin_git='' _prompt_sorin_git=''
zle && zle reset-prompt zle && zle reset-prompt
fi fi
else else
# Git target detected, update the git fragment and redisplay the prompt. # Git target detected, update the git fragment and redisplay the prompt.
_prompt_sorin_git="${_git_target}${_git_post_target}" _prompt_sorin_git="${_git_target}${_git_post_target}"
zle && zle reset-prompt zle && zle reset-prompt
fi fi
;;
esac
} }
function prompt_sorin_async_git { function prompt_sorin_async_git {
@ -66,19 +62,21 @@ function prompt_sorin_async_git {
} }
function prompt_sorin_async_tasks { function prompt_sorin_async_tasks {
# Initialize async worker. This needs to be done here and not in # If we've got a pending request, cancel it
# prompt_sorin_setup so the git formatting can be overridden by other prompts. if [[ -n "$_prompt_sorin_async_fd" ]] && { true <&$_prompt_sorin_async_fd } 2>/dev/null; then
if (( !${prompt_prezto_async_init:-0} )); then # Close the file descriptor and remove the handler
async_start_worker prompt_sorin -n exec {_prompt_sorin_async_fd}<&-
async_register_callback prompt_sorin prompt_sorin_async_callback zle -F $_prompt_sorin_async_fd
typeset -g prompt_prezto_async_init=1
fi fi
# Kill the old process of slow commands if it is still running. # Fork a process to fetch the git info and open a pipe to read from it
async_flush_jobs prompt_sorin exec {_prompt_sorin_async_fd}< <(
# Fetch and print the suggestion
prompt_sorin_async_git "$PWD"
)
# Compute slow commands in the background. # When the fd is readable, call the response handler
async_job prompt_sorin prompt_sorin_async_git "$PWD" zle -F "$_prompt_sorin_async_fd" prompt_sorin_async_callback
} }
function prompt_sorin_precmd { function prompt_sorin_precmd {
@ -113,7 +111,6 @@ function prompt_sorin_setup {
# Load required functions. # Load required functions.
autoload -Uz add-zsh-hook autoload -Uz add-zsh-hook
autoload -Uz async && async
# Add hook for calling git-info before each command. # Add hook for calling git-info before each command.
add-zsh-hook precmd prompt_sorin_precmd add-zsh-hook precmd prompt_sorin_precmd