prompt_sorin_setup 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #
  2. # A simple theme that displays relevant, contextual information.
  3. #
  4. # Authors:
  5. # Sorin Ionescu <sorin.ionescu@gmail.com>
  6. #
  7. # Screenshots:
  8. # http://i.imgur.com/nrGV6pg.png
  9. #
  10. #
  11. # 16 Terminal Colors
  12. # -- ---------------
  13. # 0 black
  14. # 1 red
  15. # 2 green
  16. # 3 yellow
  17. # 4 blue
  18. # 5 magenta
  19. # 6 cyan
  20. # 7 white
  21. # 8 bright black
  22. # 9 bright red
  23. # 10 bright green
  24. # 11 bright yellow
  25. # 12 bright blue
  26. # 13 bright magenta
  27. # 14 bright cyan
  28. # 15 bright white
  29. #
  30. # Load dependencies.
  31. pmodload 'helper'
  32. function prompt_sorin_async_callback {
  33. case $1 in
  34. prompt_sorin_async_git)
  35. # We can safely split on ':' because it isn't allowed in ref names.
  36. IFS=':' read _git_target _git_post_target <<<"$3"
  37. # The target actually contains 3 space separated possibilities, so we need to
  38. # make sure we grab the first one.
  39. _git_target=$(coalesce ${(@)${(z)_git_target}})
  40. if [[ -z "$_git_target" ]]; then
  41. # No git target detected, flush the git fragment and redisplay the prompt.
  42. if [[ -n "$_prompt_sorin_git" ]]; then
  43. _prompt_sorin_git=''
  44. zle && zle reset-prompt
  45. fi
  46. else
  47. # Git target detected, update the git fragment and redisplay the prompt.
  48. _prompt_sorin_git="${_git_target}${_git_post_target}"
  49. zle && zle reset-prompt
  50. fi
  51. ;;
  52. "[async]")
  53. # Code is 1 for corrupted worker output and 2 for dead worker.
  54. if [[ $2 -eq 2 ]]; then
  55. # Our worker died unexpectedly.
  56. typeset -g prompt_prezto_async_init=0
  57. fi
  58. ;;
  59. esac
  60. }
  61. function prompt_sorin_async_git {
  62. cd -q "$1"
  63. if (( $+functions[git-info] )); then
  64. git-info
  65. print ${git_info[status]}
  66. fi
  67. }
  68. function prompt_sorin_async_tasks {
  69. # Initialize async worker. This needs to be done here and not in
  70. # prompt_sorin_setup so the git formatting can be overridden by other prompts.
  71. if (( !${prompt_prezto_async_init:-0} )); then
  72. async_start_worker prompt_sorin -n
  73. async_register_callback prompt_sorin prompt_sorin_async_callback
  74. typeset -g prompt_prezto_async_init=1
  75. fi
  76. # Kill the old process of slow commands if it is still running.
  77. async_flush_jobs prompt_sorin
  78. # Compute slow commands in the background.
  79. async_job prompt_sorin prompt_sorin_async_git "$PWD"
  80. }
  81. function prompt_sorin_precmd {
  82. setopt LOCAL_OPTIONS
  83. unsetopt XTRACE KSH_ARRAYS
  84. # Format PWD.
  85. _prompt_sorin_pwd=$(prompt-pwd)
  86. # Handle updating git data. We also clear the git prompt data if we're in a
  87. # different git root now.
  88. if (( $+functions[git-dir] )); then
  89. local new_git_root="$(git-dir 2> /dev/null)"
  90. if [[ $new_git_root != $_sorin_cur_git_root ]]; then
  91. _prompt_sorin_git=''
  92. _sorin_cur_git_root=$new_git_root
  93. fi
  94. fi
  95. # Run python info (this should be fast and not require any async)
  96. if (( $+functions[python-info] )); then
  97. python-info
  98. fi
  99. prompt_sorin_async_tasks
  100. }
  101. function prompt_sorin_setup {
  102. setopt LOCAL_OPTIONS
  103. unsetopt XTRACE KSH_ARRAYS
  104. prompt_opts=(cr percent sp subst)
  105. # Load required functions.
  106. autoload -Uz add-zsh-hook
  107. autoload -Uz async && async
  108. # Add hook for calling git-info before each command.
  109. add-zsh-hook precmd prompt_sorin_precmd
  110. # Tell prezto we can manage this prompt
  111. zstyle ':prezto:module:prompt' managed 'yes'
  112. # Set editor-info parameters.
  113. zstyle ':prezto:module:editor:info:completing' format '%B%F{7}...%f%b'
  114. zstyle ':prezto:module:editor:info:keymap:primary' format ' %B%F{1}❯%F{3}❯%F{2}❯%f%b'
  115. zstyle ':prezto:module:editor:info:keymap:primary:overwrite' format ' %F{3}♺%f'
  116. zstyle ':prezto:module:editor:info:keymap:alternate' format ' %B%F{2}❮%F{3}❮%F{1}❮%f%b'
  117. # Set git-info parameters.
  118. zstyle ':prezto:module:git:info' verbose 'yes'
  119. zstyle ':prezto:module:git:info:action' format '%F{7}:%f%%B%F{9}%s%f%%b'
  120. zstyle ':prezto:module:git:info:added' format ' %%B%F{2}✚%f%%b'
  121. zstyle ':prezto:module:git:info:ahead' format ' %%B%F{13}⬆%f%%b'
  122. zstyle ':prezto:module:git:info:behind' format ' %%B%F{13}⬇%f%%b'
  123. zstyle ':prezto:module:git:info:branch' format ' %%B%F{2}%b%f%%b'
  124. zstyle ':prezto:module:git:info:commit' format ' %%B%F{3}%.7c%f%%b'
  125. zstyle ':prezto:module:git:info:deleted' format ' %%B%F{1}✖%f%%b'
  126. zstyle ':prezto:module:git:info:modified' format ' %%B%F{4}✱%f%%b'
  127. zstyle ':prezto:module:git:info:position' format ' %%B%F{13}%p%f%%b'
  128. zstyle ':prezto:module:git:info:renamed' format ' %%B%F{5}➜%f%%b'
  129. zstyle ':prezto:module:git:info:stashed' format ' %%B%F{6}✭%f%%b'
  130. zstyle ':prezto:module:git:info:unmerged' format ' %%B%F{3}═%f%%b'
  131. zstyle ':prezto:module:git:info:untracked' format ' %%B%F{7}◼%f%%b'
  132. zstyle ':prezto:module:git:info:keys' format \
  133. 'status' '%b %p %c:%s%A%B%S%a%d%m%r%U%u'
  134. # Set python-info parameters.
  135. zstyle ':prezto:module:python:info:virtualenv' format '%f%F{3}(%v)%F{7} '
  136. # Set up non-zero return value display
  137. local show_return="✘ "
  138. # Default is to show the return value
  139. if zstyle -T ':prezto:module:prompt' show-return-val; then
  140. show_return+='%? '
  141. fi
  142. # Get the async worker set up.
  143. _sorin_cur_git_root=''
  144. _prompt_sorin_git=''
  145. _prompt_sorin_pwd=''
  146. # Define prompts.
  147. 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]} '
  148. RPROMPT='$python_info[virtualenv]${editor_info[overwrite]}%(?:: %F{1}'
  149. RPROMPT+=${show_return}
  150. RPROMPT+='%f)${VIM:+" %B%F{6}V%f%b"}${_prompt_sorin_git}'
  151. SPROMPT='zsh: correct %F{1}%R%f to %F{2}%r%f [nyae]? '
  152. }
  153. function prompt_sorin_preview {
  154. local +h PROMPT=''
  155. local +h RPROMPT=''
  156. local +h SPROMPT=''
  157. editor-info 2> /dev/null
  158. prompt_preview_theme 'sorin'
  159. }
  160. prompt_sorin_setup "$@"
  161. # vim: ft=zsh