prompt: update sorin prompt async usage to be closer to pure

This commit is contained in:
Kaleb Elwert 2017-11-11 15:18:31 -08:00
parent f02dc1af57
commit e159c1a439

View file

@ -32,25 +32,29 @@
# Load dependencies.
pmodload 'helper'
function prompt_sorin_git_info {
# We can safely split on ':' because it isn't allowed in ref names.
IFS=':' read _git_target _git_post_target <<<"$3"
function prompt_sorin_async_callback {
case $1 in
prompt_sorin_async_git)
# 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
# make sure we grab the first one.
_git_target=$(coalesce ${(@)${(z)_git_target}})
# The target actually contains 3 space separated possibilities, so we need to
# make sure we grab the first one.
_git_target=$(coalesce ${(@)${(z)_git_target}})
if [[ -z "$_git_target" ]]; then
# No git target detected, flush the git fragment and redisplay the prompt.
if [[ -n "$_prompt_sorin_git" ]]; then
_prompt_sorin_git=''
zle && zle reset-prompt
fi
else
# Git target detected, update the git fragment and redisplay the prompt.
_prompt_sorin_git="${_git_target}${_git_post_target}"
zle && zle reset-prompt
fi
if [[ -z "$_git_target" ]]; then
# No git target detected, flush the git fragment and redisplay the prompt.
if [[ -n "$_prompt_sorin_git" ]]; then
_prompt_sorin_git=''
zle && zle reset-prompt
fi
else
# Git target detected, update the git fragment and redisplay the prompt.
_prompt_sorin_git="${_git_target}${_git_post_target}"
zle && zle reset-prompt
fi
;;
esac
}
function prompt_sorin_async_git {
@ -61,6 +65,22 @@ function prompt_sorin_async_git {
fi
}
function prompt_sorin_async_tasks {
# Initialize async worker. This needs to be done here and not in
# prompt_sorin_setup so the git formatting can be overridden by other prompts.
if (( !${prompt_prezto_async_init:-0} )); then
async_start_worker prompt_sorin -n
async_register_callback prompt_sorin prompt_sorin_async_callback
typeset -g prompt_prezto_async_init=1
fi
# Kill the old process of slow commands if it is still running.
async_flush_jobs async_sorin_git
# Compute slow commands in the background.
async_job async_sorin_git prompt_sorin_async_git "$PWD"
}
function prompt_sorin_precmd {
setopt LOCAL_OPTIONS
unsetopt XTRACE KSH_ARRAYS
@ -68,9 +88,6 @@ function prompt_sorin_precmd {
# Format PWD.
_prompt_sorin_pwd=$(prompt-pwd)
# Kill the old process of slow commands if it is still running.
async_flush_jobs async_sorin_git
# Handle updating git data. We also clear the git prompt data if we're in a
# different git root now.
if (( $+functions[git-dir] )); then
@ -81,14 +98,12 @@ function prompt_sorin_precmd {
fi
fi
# Run python info (this should be fast and not require any async)
if (( $+functions[python-info] )); then
python-info
fi
# Compute slow commands in the background.
async_job async_sorin_git prompt_sorin_async_git "$PWD"
prompt_sorin_async_tasks
}
function prompt_sorin_setup {
@ -139,8 +154,6 @@ function prompt_sorin_setup {
zstyle ':prezto:module:python:info:virtualenv' format '%f%F{3}(%v)%F{7} '
# Get the async worker set up
async_start_worker async_sorin_git -n
async_register_callback async_sorin_git prompt_sorin_git_info
_sorin_cur_git_root=''
_prompt_sorin_git=''