Skip to content

Instantly share code, notes, and snippets.

@denysdovhan
Last active December 18, 2019 20:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denysdovhan/e83dec6f09b237acbc24a6bb25fabd13 to your computer and use it in GitHub Desktop.
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)
#!/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