Last active
December 18, 2019 20:49
-
-
Save denysdovhan/e83dec6f09b237acbc24a6bb25fabd13 to your computer and use it in GitHub Desktop.
Async prompt examples for Spaceship ZSH (zsh-async is required: https://github.com/mafredri/zsh-async)
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
#!/usr/bin/env zsh | |
# zsh-async is required: | |
# https://github.com/mafredri/zsh-async | |
source $PWD/async.zsh | |
async_init | |
# cache variable | |
typeset -Ag prompt_data | |
# section for dir | |
function prompt_dir() { | |
echo '%3~' | |
} | |
# section for git branch | |
function prompt_git() { | |
cd -q $1 | |
$(git rev-parse --is-inside-work-tree) || return | |
git rev-parse --abbrev-ref HEAD | |
} | |
# refresh prompt with new data | |
prompt_refresh() { | |
PROMPT="$prompt_data[prompt_dir] $prompt_data[prompt_git] > " | |
# Redraw the prompt. | |
zle && zle .reset-prompt | |
} | |
prompt_callback() { | |
local job=$1 code=$2 output=$3 exec_time=$4 | |
prompt_data[$job]=$output | |
prompt_refresh | |
} | |
# Start async worker | |
async_start_worker 'prompt' -n | |
# Register callback function for the workers completed jobs | |
async_register_callback 'prompt' prompt_callback | |
# start async jobs before cmd | |
prompt_precmd() { | |
async_job 'prompt' prompt_dir | |
async_job 'prompt' prompt_git $PWD # required | |
} | |
# Setup | |
zmodload zsh/zle | |
autoload -Uz add-zsh-hook | |
add-zsh-hook precmd prompt_precmd | |
PROMPT='> ' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment