agnoster.zsh-theme 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. # vim:ft=zsh ts=2 sw=2 sts=2
  2. #
  3. # agnoster's Theme - https://gist.github.com/3712874
  4. # A Powerline-inspired theme for ZSH
  5. #
  6. # # README
  7. #
  8. # In order for this theme to render correctly, you will need a
  9. # [Powerline-patched font](https://github.com/Lokaltog/powerline-fonts).
  10. # Make sure you have a recent version: the code points that Powerline
  11. # uses changed in 2012, and older versions will display incorrectly,
  12. # in confusing ways.
  13. #
  14. # In addition, I recommend the
  15. # [Solarized theme](https://github.com/altercation/solarized/) and, if you're
  16. # using it on Mac OS X, [iTerm 2](http://www.iterm2.com/) over Terminal.app -
  17. # it has significantly better color fidelity.
  18. #
  19. # # Goals
  20. #
  21. # The aim of this theme is to only show you *relevant* information. Like most
  22. # prompts, it will only show git information when in a git working directory.
  23. # However, it goes a step further: everything from the current user and
  24. # hostname to whether the last call exited with an error to whether background
  25. # jobs are running in this shell will all be displayed automatically when
  26. # appropriate.
  27. ### Segment drawing
  28. # A few utility functions to make it easy and re-usable to draw segmented prompts
  29. CURRENT_BG='NONE'
  30. zmodload zsh/parameter
  31. # Special Powerline characters
  32. () {
  33. local LC_ALL="" LC_CTYPE="en_US.UTF-8"
  34. # NOTE: This segment separator character is correct. In 2012, Powerline changed
  35. # the code points they use for their special characters. This is the new code point.
  36. # If this is not working for you, you probably have an old version of the
  37. # Powerline-patched fonts installed. Download and install the new version.
  38. # Do not submit PRs to change this unless you have reviewed the Powerline code point
  39. # history and have new information.
  40. # This is defined using a Unicode escape sequence so it is unambiguously readable, regardless of
  41. # what font the user is viewing this source code in. Do not replace the
  42. # escape sequence with a single literal character.
  43. # Do not change this! Do not make it '\u2b80'; that is the old, wrong code point.
  44. SEGMENT_SEPARATOR=$'\ue0b0'
  45. }
  46. # Begin a segment
  47. # Takes two arguments, background and foreground. Both can be omitted,
  48. # rendering default background/foreground.
  49. prompt_segment() {
  50. local bg fg
  51. [[ -n $1 ]] && bg="%K{$1}" || bg="%k"
  52. [[ -n $2 ]] && fg="%F{$2}" || fg="%f"
  53. if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then
  54. PROMPT+=" %{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%} "
  55. else
  56. PROMPT+="%{$bg%}%{$fg%} "
  57. fi
  58. CURRENT_BG=$1
  59. [[ -n $3 ]] && PROMPT+=$3
  60. }
  61. # End the prompt, closing any open segments
  62. prompt_end() {
  63. if [[ -n $CURRENT_BG ]]; then
  64. PROMPT+=" %{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
  65. else
  66. PROMPT+="%{%k%}"
  67. fi
  68. PROMPT+="%{%f%}"
  69. CURRENT_BG='NONE'
  70. }
  71. ### Prompt components
  72. # Each component will draw itself, and hide itself if no information needs to be shown
  73. # Context: user@hostname (who am I and where am I)
  74. prompt_context() {
  75. if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
  76. prompt_segment black default "%(!.%{%F{yellow}%}.)$USER@%m"
  77. fi
  78. }
  79. # Git: branch/detached head, dirty status
  80. prompt_git() {
  81. (( $+commands[git] )) || return
  82. local PL_BRANCH_CHAR
  83. () {
  84. local LC_ALL="" LC_CTYPE="en_US.UTF-8"
  85. PL_BRANCH_CHAR=$'\ue0a0' # 
  86. }
  87. local ref dirty mode repo_path
  88. if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
  89. repo_path=$(git rev-parse --git-dir 2>/dev/null)
  90. dirty=$(parse_git_dirty)
  91. ref=$(git symbolic-ref HEAD 2> /dev/null) || ref="➦ $(git rev-parse --short HEAD 2> /dev/null)"
  92. if [[ -n $dirty ]]; then
  93. prompt_segment yellow black
  94. else
  95. prompt_segment green black
  96. fi
  97. if [[ -e "${repo_path}/BISECT_LOG" ]]; then
  98. mode=" <B>"
  99. elif [[ -e "${repo_path}/MERGE_HEAD" ]]; then
  100. mode=" >M<"
  101. elif [[ -e "${repo_path}/rebase" || -e "${repo_path}/rebase-apply" || -e "${repo_path}/rebase-merge" || -e "${repo_path}/../.dotest" ]]; then
  102. mode=" >R>"
  103. fi
  104. setopt promptsubst
  105. autoload -Uz vcs_info
  106. zstyle ':vcs_info:*' enable git
  107. zstyle ':vcs_info:*' get-revision true
  108. zstyle ':vcs_info:*' check-for-changes true
  109. zstyle ':vcs_info:*' stagedstr '✚'
  110. zstyle ':vcs_info:*' unstagedstr '●'
  111. zstyle ':vcs_info:*' formats ' %u%c'
  112. zstyle ':vcs_info:*' actionformats ' %u%c'
  113. vcs_info
  114. PROMPT+="${ref/refs\/heads\//$PL_BRANCH_CHAR }${vcs_info_msg_0_%% }${mode}"
  115. fi
  116. }
  117. prompt_bzr() {
  118. (( $+commands[bzr] )) || return
  119. if (bzr status >/dev/null 2>&1); then
  120. status_mod=`bzr status | head -n1 | grep "modified" | wc -m`
  121. status_all=`bzr status | head -n1 | wc -m`
  122. revision=`bzr log | head -n2 | tail -n1 | sed 's/^revno: //'`
  123. if [[ $status_mod -gt 0 ]] ; then
  124. prompt_segment yellow black
  125. PROMPT+="bzr@$revision ✚ "
  126. else
  127. if [[ $status_all -gt 0 ]] ; then
  128. prompt_segment yellow black
  129. PROMPT+="bzr@$revision"
  130. else
  131. prompt_segment green black
  132. PROMPT+="bzr@$revision"
  133. fi
  134. fi
  135. fi
  136. }
  137. prompt_hg() {
  138. (( $+commands[hg] )) || return
  139. local rev st branch
  140. if $(hg id >/dev/null 2>&1); then
  141. if $(hg prompt >/dev/null 2>&1); then
  142. if [[ $(hg prompt "{status|unknown}") = "?" ]]; then
  143. # if files are not added
  144. prompt_segment red white
  145. st=' ±'
  146. elif [[ -n $(hg prompt "{status|modified}") ]]; then
  147. # if any modification
  148. prompt_segment yellow black
  149. st=' ±'
  150. else
  151. # if working copy is clean
  152. prompt_segment green black
  153. fi
  154. PROMPT+="$(hg prompt "☿ {rev}@{branch}")$st"
  155. else
  156. st=""
  157. rev=$(hg id -n 2>/dev/null | sed 's/[^-0-9]//g')
  158. branch=$(hg id -b 2>/dev/null)
  159. if `hg st | grep -q "^\?"`; then
  160. prompt_segment red black
  161. st=' ±'
  162. elif `hg st | grep -q "^[MA]"`; then
  163. prompt_segment yellow black
  164. st=' ±'
  165. else
  166. prompt_segment green black
  167. fi
  168. PROMPT+="☿ $rev@$branch$st"
  169. fi
  170. fi
  171. }
  172. # Dir: current working directory
  173. prompt_dir() {
  174. prompt_segment blue black '%~'
  175. }
  176. # Virtualenv: current working virtualenv
  177. prompt_virtualenv() {
  178. local virtualenv_path="$VIRTUAL_ENV"
  179. if [[ -n $virtualenv_path && -n $VIRTUAL_ENV_DISABLE_PROMPT ]]; then
  180. prompt_segment blue black "(`basename $virtualenv_path`)"
  181. fi
  182. }
  183. # Status:
  184. # - was there an error
  185. # - am I root
  186. # - are there background jobs?
  187. prompt_status() {
  188. local symbols
  189. symbols=()
  190. [[ $RETVAL -ne 0 ]] && symbols+="%{%F{red}%}✘"
  191. [[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}⚡"
  192. [[ ${#jobstates} -ne 0 ]] && symbols+="%{%F{cyan}%}⚙"
  193. [[ -n "$symbols" ]] && prompt_segment black default "$symbols"
  194. }
  195. ## Main prompt
  196. build_prompt() {
  197. RETVAL=$?
  198. PROMPT='%{%f%b%k%}'
  199. prompt_status
  200. prompt_virtualenv
  201. prompt_context
  202. prompt_dir
  203. prompt_git
  204. prompt_bzr
  205. prompt_hg
  206. prompt_end
  207. PROMPT+=' '
  208. }
  209. autoload -U add-zsh-hook
  210. add-zsh-hook precmd build_prompt