ohmyzsh/plugins/async-prompt/async-prompt.plugin.zsh
Olivier Mehani 60eb78630d
[plugins/async-prompt] Add decorator for stale RPROMPT
Add ZSH_THEME_ASYNC_PROMPT_OLD_PREFIX and
ZSH_THEME_ASYNC_PROMPT_OLD_SUFFIX for themes to use.

Signed-off-by: Olivier Mehani <olivier.mehani@learnosity.com>
2018-04-27 16:41:52 +10:00

42 lines
1 KiB
Bash

# Asynchronous prompt heavily based on Anish Athalye's [0]
# [0[ https://www.anishathalye.com/2015/02/07/an-asynchronous-shell-prompt/
#
# To use in your theme, define an `rprompt` function that echoes whatever you
# want to have on the right-hand-side prompt.
#
setopt prompt_subst # enable command substition in prompt
ASYNC_PROC=0
function precmd() {
function async() {
# save to temp file
printf "%s" "$(rprompt)" > "/tmp/zsh_prompt_$$"
# signal parent
kill -s USR1 $$
}
# do not clear RPROMPT, let it persist
RPROMPT="${ZSH_THEME_ASYNC_PROMPT_OLD_PREFIX}${RPROMPT}${ZSH_THEME_ASYNC_PROMPT_OLD_SUFFIX}"
# 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
}