Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 31 additions & 34 deletions modules/prompt/functions/prompt_sorin_setup
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,25 @@
# Load dependencies.
pmodload 'helper'

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So are we already using both zle and zsh-async? It does seem weird that we'd be using two async frameworks...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zle is a zsh built-in and does a ton of stuff related to line editing. For some reason there's also some functionality in there around managing file descriptors for sub-shells.

fi
;;
esac
function prompt_sorin_async_git_callback {
# 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_git {
Expand All @@ -66,19 +62,21 @@ function prompt_sorin_async_git {
}

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
# If we've got a pending request, cancel it
if [[ -n "$_prompt_sorin_async_fd" ]] && { true <&$_prompt_sorin_async_fd } 2>/dev/null; then
# Close the file descriptor and remove the handler
exec {_prompt_sorin_async_fd}<&-
zle -F $_prompt_sorin_async_fd
fi

# Kill the old process of slow commands if it is still running.
async_flush_jobs prompt_sorin
# Fork a process to fetch the git info and open a pipe to read from it
exec {_prompt_sorin_async_fd}< <(
# Fetch and print the suggestion
prompt_sorin_async_git "$PWD"
)

# Compute slow commands in the background.
async_job prompt_sorin prompt_sorin_async_git "$PWD"
# When the fd is readable, call the response handler
zle -F "$_prompt_sorin_async_fd" prompt_sorin_async_callback
}

function prompt_sorin_precmd {
Expand Down Expand Up @@ -113,7 +111,6 @@ function prompt_sorin_setup {

# Load required functions.
autoload -Uz add-zsh-hook
autoload -Uz async && async

# Add hook for calling git-info before each command.
add-zsh-hook precmd prompt_sorin_precmd
Expand Down