agnoster.zsh-theme 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. # Special Powerline characters
  31. () {
  32. local LC_ALL="" LC_CTYPE="en_US.UTF-8"
  33. # NOTE: This segment separator character is correct. In 2012, Powerline changed
  34. # the code points they use for their special characters. This is the new code point.
  35. # If this is not working for you, you probably have an old version of the
  36. # Powerline-patched fonts installed. Download and install the new version.
  37. # Do not submit PRs to change this unless you have reviewed the Powerline code point
  38. # history and have new information.
  39. # This is defined using a Unicode escape sequence so it is unambiguously readable, regardless of
  40. # what font the user is viewing this source code in. Do not replace the
  41. # escape sequence with a single literal character.
  42. # Do not change this! Do not make it '\u2b80'; that is the old, wrong code point.
  43. SEGMENT_SEPARATOR=$'\ue0b0'
  44. }
  45. # Begin a segment
  46. # Takes two arguments, background and foreground. Both can be omitted,
  47. # rendering default background/foreground.
  48. prompt_segment() {
  49. local bg fg
  50. [[ -n $1 ]] && bg="%K{$1}" || bg="%k"
  51. [[ -n $2 ]] && fg="%F{$2}" || fg="%f"
  52. if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then
  53. echo -n " %{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%} "
  54. else
  55. echo -n "%{$bg%}%{$fg%} "
  56. fi
  57. CURRENT_BG=$1
  58. [[ -n $3 ]] && echo -n $3
  59. }
  60. # End the prompt, closing any open segments
  61. prompt_end() {
  62. if [[ -n $CURRENT_BG ]]; then
  63. echo -n " %{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
  64. else
  65. echo -n "%{%k%}"
  66. fi
  67. echo -n "%{%f%}"
  68. CURRENT_BG=''
  69. }
  70. ### Prompt components
  71. # Each component will draw itself, and hide itself if no information needs to be shown
  72. # Context: user@hostname (who am I and where am I)
  73. prompt_context() {
  74. if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
  75. prompt_segment black default "%(!.%{%F{yellow}%}.)$USER@%m"
  76. fi
  77. }
  78. # Git: branch/detached head, dirty status
  79. prompt_git() {
  80. (( $+commands[git] )) || return
  81. local PL_BRANCH_CHAR
  82. () {
  83. local LC_ALL="" LC_CTYPE="en_US.UTF-8"
  84. PL_BRANCH_CHAR=$'\ue0a0' # 
  85. }
  86. local ref dirty mode repo_path
  87. repo_path=$(git rev-parse --git-dir 2>/dev/null)
  88. if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
  89. dirty=$(parse_git_dirty)
  90. ref=$(git symbolic-ref HEAD 2> /dev/null) || ref="➦ $(git rev-parse --short HEAD 2> /dev/null)"
  91. if [[ -n $dirty ]]; then
  92. prompt_segment yellow black
  93. else
  94. prompt_segment green black
  95. fi
  96. if [[ -e "${repo_path}/BISECT_LOG" ]]; then
  97. mode=" <B>"
  98. elif [[ -e "${repo_path}/MERGE_HEAD" ]]; then
  99. mode=" >M<"
  100. elif [[ -e "${repo_path}/rebase" || -e "${repo_path}/rebase-apply" || -e "${repo_path}/rebase-merge" || -e "${repo_path}/../.dotest" ]]; then
  101. mode=" >R>"
  102. fi
  103. setopt promptsubst
  104. autoload -Uz vcs_info
  105. zstyle ':vcs_info:*' enable git
  106. zstyle ':vcs_info:*' get-revision true
  107. zstyle ':vcs_info:*' check-for-changes true
  108. zstyle ':vcs_info:*' stagedstr '✚'
  109. zstyle ':vcs_info:*' unstagedstr '●'
  110. zstyle ':vcs_info:*' formats ' %u%c'
  111. zstyle ':vcs_info:*' actionformats ' %u%c'
  112. vcs_info
  113. echo -n "${ref/refs\/heads\//$PL_BRANCH_CHAR }${vcs_info_msg_0_%% }${mode}"
  114. fi
  115. }
  116. prompt_bzr() {
  117. (( $+commands[bzr] )) || return
  118. if (bzr status >/dev/null 2>&1); then
  119. status_mod=`bzr status | head -n1 | grep "modified" | wc -m`
  120. status_all=`bzr status | head -n1 | wc -m`
  121. revision=`bzr log | head -n2 | tail -n1 | sed 's/^revno: //'`
  122. if [[ $status_mod -gt 0 ]] ; then
  123. prompt_segment yellow black
  124. echo -n "bzr@"$revision "✚ "
  125. else
  126. if [[ $status_all -gt 0 ]] ; then
  127. prompt_segment yellow black
  128. echo -n "bzr@"$revision
  129. else
  130. prompt_segment green black
  131. echo -n "bzr@"$revision
  132. fi
  133. fi
  134. fi
  135. }
  136. prompt_hg() {
  137. (( $+commands[hg] )) || return
  138. local rev status
  139. if $(hg id >/dev/null 2>&1); then
  140. if $(hg prompt >/dev/null 2>&1); then
  141. if [[ $(hg prompt "{status|unknown}") = "?" ]]; then
  142. # if files are not added
  143. prompt_segment red white
  144. st='±'
  145. elif [[ -n $(hg prompt "{status|modified}") ]]; then
  146. # if any modification
  147. prompt_segment yellow black
  148. st='±'
  149. else
  150. # if working copy is clean
  151. prompt_segment green black
  152. fi
  153. echo -n $(hg prompt "☿ {rev}@{branch}") $st
  154. else
  155. st=""
  156. rev=$(hg id -n 2>/dev/null | sed 's/[^-0-9]//g')
  157. branch=$(hg id -b 2>/dev/null)
  158. if `hg st | grep -q "^\?"`; then
  159. prompt_segment red black
  160. st='±'
  161. elif `hg st | grep -q "^[MA]"`; then
  162. prompt_segment yellow black
  163. st='±'
  164. else
  165. prompt_segment green black
  166. fi
  167. echo -n "☿ $rev@$branch" $st
  168. fi
  169. fi
  170. }
  171. # Dir: current working directory
  172. prompt_dir() {
  173. prompt_segment blue black '%~'
  174. }
  175. # Virtualenv: current working virtualenv
  176. prompt_virtualenv() {
  177. local virtualenv_path="$VIRTUAL_ENV"
  178. if [[ -n $virtualenv_path && -n $VIRTUAL_ENV_DISABLE_PROMPT ]]; then
  179. prompt_segment blue black "(`basename $virtualenv_path`)"
  180. fi
  181. }
  182. # Status:
  183. # - was there an error
  184. # - am I root
  185. # - are there background jobs?
  186. prompt_status() {
  187. local symbols
  188. symbols=()
  189. [[ $RETVAL -ne 0 ]] && symbols+="%{%F{red}%}✘"
  190. [[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}⚡"
  191. [[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{cyan}%}⚙"
  192. [[ -n "$symbols" ]] && prompt_segment black default "$symbols"
  193. }
  194. ## Main prompt
  195. build_prompt() {
  196. RETVAL=$?
  197. prompt_status
  198. prompt_virtualenv
  199. prompt_context
  200. prompt_dir
  201. prompt_git
  202. prompt_bzr
  203. prompt_hg
  204. prompt_end
  205. }
  206. PROMPT='%{%f%b%k%}$(build_prompt) '