Created
August 30, 2017 15:29
-
-
Save enviable/44061c902f16f6d4add9227160bf9812 to your computer and use it in GitHub Desktop.
Powerlevel9k async RPROMPT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### 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