Skip to content

Commit

Permalink
prompt: update sorin prompt async usage to be closer to pure
Browse files Browse the repository at this point in the history
  • Loading branch information
belak committed Nov 11, 2017
1 parent f02dc1a commit e159c1a
Showing 1 changed file with 41 additions and 28 deletions.
69 changes: 41 additions & 28 deletions modules/prompt/functions/prompt_sorin_setup
Original file line number Diff line number Diff line change
Expand Up @@ -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"

# 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
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}})

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 {
Expand All @@ -61,16 +65,29 @@ 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

# 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
Expand All @@ -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 {
Expand Down Expand Up @@ -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=''
Expand Down

0 comments on commit e159c1a

Please sign in to comment.