Skip to content

Instantly share code, notes, and snippets.

@enviable
Created August 30, 2017 15:29
Show Gist options
  • Save enviable/44061c902f16f6d4add9227160bf9812 to your computer and use it in GitHub Desktop.
Save enviable/44061c902f16f6d4add9227160bf9812 to your computer and use it in GitHub Desktop.
Powerlevel9k async RPROMPT
### Based on http://www.anishathalye.com/2015/02/07/an-asynchronous-shell-prompt/
POWERLEVEL9K_DISABLE_RPROMPT=true #prevents default drawing of RPROMPT
setopt prompt_subst # enable command substition in prompt
PROMPT='$(left_prompt_segment)' # single quotes to prevent immediate execution
RPROMPT='' # no initial prompt, set dynamically
ASYNC_PROC=0
function precmd() {
function async() {
# uncomment to test
# sleep 2
# save to temp file
printf "%s" "$(build_right_prompt)" >! "/tmp/zsh_prompt_$$"
# signal parent
kill -s USR1 $$
}
# do not clear RPROMPT, let it persist
# kill child if necessary
if [[ "${ASYNC_PROC}" != 0 ]]; then
kill -s HUP $ASYNC_PROC >/dev/null 2>&1 || :
fi
# start background computation
async &!
ASYNC_PROC=$!
}
function TRAPUSR1() {
# read from temp file
RPROMPT="$(cat /tmp/zsh_prompt_$$)"
# reset proc number
ASYNC_PROC=0
# redisplay
zle && zle reset-prompt
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment