diff --git a/modules/prompt/functions/prompt_sorin_setup b/modules/prompt/functions/prompt_sorin_setup index c4d12c34..68c50943 100644 --- a/modules/prompt/functions/prompt_sorin_setup +++ b/modules/prompt/functions/prompt_sorin_setup @@ -1,11 +1,17 @@ # # A simple theme that displays relevant, contextual information. # +# - Git branch +# - Python environment +# - Ruby environment (gemset) +# # Authors: # Sorin Ionescu +# Matthew Crenshaw # # Screenshots: # http://i.imgur.com/nrGV6pg.png +# http://i.imgur.com/w2ein8H.png # # @@ -32,36 +38,38 @@ # Load dependencies. pmodload 'helper' -prompt_sorin_git_info() { - if (( _prompt_sorin_precmd_async_pid > 0 )); then - # Append Git status. - if [[ -s "$_prompt_sorin_precmd_async_data" ]]; then - alias typeset='typeset -g' - source "$_prompt_sorin_precmd_async_data" - RPROMPT+='${git_info:+${(e)git_info[status]}}' - unalias typeset - fi - - # Reset PID. - _prompt_sorin_precmd_async_pid=0 - - # Redisplay prompt. - zle && zle reset-prompt +function _prompt_sorin_async_job { + prompt_sorin_style + local output='' + builtin cd -q $1 + if (( $+functions[python-info] )); then + python-info + output+=" ${python_info:+${(e)python_info[virtualenv]}}" + fi + if (( $+functions[ruby-info] )); then + ruby-info + output+=" ${ruby_info:+${(e)ruby_info[version]}}" fi -} - -function prompt_sorin_precmd_async { - # Get Git repository information. if (( $+functions[git-info] )); then git-info - typeset -p git_info >! "$_prompt_sorin_precmd_async_data" + output+=" ${git_info:+${(e)git_info[status]}}" fi + print $output +} + +function _prompt_sorin_callback { + local job=$1 code=$2 output=$3 exec_time=$4 - # Signal completion to parent process. - kill -WINCH $$ + if [[ -n $output ]]; then + # Append to rprompt + RPROMPT+=$output + + # Redisplay prompt. + zle && zle reset-prompt + fi } -function prompt_sorin_precmd { +function _prompt_sorin_precmd { setopt LOCAL_OPTIONS unsetopt XTRACE KSH_ARRAYS @@ -71,30 +79,46 @@ function prompt_sorin_precmd { # Define prompts. RPROMPT='${editor_info[overwrite]}%(?:: %F{1}⏎%f)${VIM:+" %B%F{6}V%f%b"}' - # Kill the old process of slow commands if it is still running. - if (( _prompt_sorin_precmd_async_pid > 0 )); then - kill -KILL "$_prompt_sorin_precmd_async_pid" &>/dev/null - fi + # Kill any currently running async jobs + async_flush_jobs 'prompt_sorin' - # Compute slow commands in the background. - trap prompt_sorin_git_info WINCH - prompt_sorin_precmd_async &! - _prompt_sorin_precmd_async_pid=$! + # We need the worker's env to be up to date, so here we are restarting the worker + async_stop_worker 'prompt_sorin' + async_start_worker 'prompt_sorin' -n -u + async_register_callback 'prompt_sorin' _prompt_sorin_callback + + # Kick off async jobs + async_job 'prompt_sorin' _prompt_sorin_async_job "$(pwd)" } function prompt_sorin_setup { setopt LOCAL_OPTIONS unsetopt XTRACE KSH_ARRAYS prompt_opts=(cr percent subst) - _prompt_sorin_precmd_async_pid=0 - _prompt_sorin_precmd_async_data="${TMPPREFIX}-prompt_sorin_data" # Load required functions. autoload -Uz add-zsh-hook + autoload -Uz async && async + + # Start async worker + # async_start_worker 'prompt_sorin' -n -u + + # Register callback + # async_register_callback 'prompt_sorin' _prompt_sorin_callback # Add hook for calling git-info before each command. - add-zsh-hook precmd prompt_sorin_precmd + add-zsh-hook precmd _prompt_sorin_precmd + + # Apply styles + prompt_sorin_style + # Define prompts. + PROMPT='${SSH_TTY:+"%F{9}%n%f%F{7}@%f%F{3}%m%f "}%F{4}${_prompt_sorin_pwd}%(!. %B%F{1}#%f%b.)${editor_info[keymap]} ' + RPROMPT='' + SPROMPT='zsh: correct %F{1}%R%f to %F{2}%r%f [nyae]? ' +} + +function prompt_sorin_style { # Set editor-info parameters. zstyle ':prezto:module:editor:info:completing' format '%B%F{7}...%f%b' zstyle ':prezto:module:editor:info:keymap:primary' format ' %B%F{1}❯%F{3}❯%F{2}❯%f%b' @@ -119,10 +143,11 @@ function prompt_sorin_setup { zstyle ':prezto:module:git:info:keys' format \ 'status' '$(coalesce "%b" "%p" "%c")%s%A%B%S%a%d%m%r%U%u' - # Define prompts. - PROMPT='${SSH_TTY:+"%F{9}%n%f%F{7}@%f%F{3}%m%f "}%F{4}${_prompt_sorin_pwd}%(!. %B%F{1}#%f%b.)${editor_info[keymap]} ' - RPROMPT='' - SPROMPT='zsh: correct %F{1}%R%f to %F{2}%r%f [nyae]? ' + # Set ruby-info parameters. + zstyle ':prezto:module:ruby:info:version' format ' %F{4}rbenv%f:%F{2}%v%f' + + # Set python-info parameters. + zstyle ':prezto:module:python:info:virtualenv' format ' %F{4}pyenv%f:%F{2}%v%f' } function prompt_sorin_preview {