agnoster.zsh-theme 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. SEGMENT_SEPARATOR=$'\ue0b0' # 
  43. }
  44. # Begin a segment
  45. # Takes two arguments, background and foreground. Both can be omitted,
  46. # rendering default background/foreground.
  47. prompt_segment() {
  48. local bg fg
  49. [[ -n $1 ]] && bg="%K{$1}" || bg="%k"
  50. [[ -n $2 ]] && fg="%F{$2}" || fg="%f"
  51. if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then
  52. echo -n " %{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%} "
  53. else
  54. echo -n "%{$bg%}%{$fg%} "
  55. fi
  56. CURRENT_BG=$1
  57. [[ -n $3 ]] && echo -n $3
  58. }
  59. # End the prompt, closing any open segments
  60. prompt_end() {
  61. if [[ -n $CURRENT_BG ]]; then
  62. echo -n " %{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
  63. else
  64. echo -n "%{%k%}"
  65. fi
  66. echo -n "%{%f%}"
  67. CURRENT_BG=''
  68. }
  69. ### Prompt components
  70. # Each component will draw itself, and hide itself if no information needs to be shown
  71. # Context: user@hostname (who am I and where am I)
  72. prompt_context() {
  73. if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
  74. prompt_segment black default "%(!.%{%F{yellow}%}.)$USER@%m"
  75. fi
  76. }
  77. # Git: branch/detached head, dirty status
  78. prompt_git() {
  79. local PL_BRANCH_CHAR
  80. () {
  81. local LC_ALL="" LC_CTYPE="en_US.UTF-8"
  82. PL_BRANCH_CHAR=$'\ue0a0' # 
  83. }
  84. local ref dirty mode repo_path
  85. repo_path=$(git rev-parse --git-dir 2>/dev/null)
  86. if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
  87. dirty=$(parse_git_dirty)
  88. ref=$(git symbolic-ref HEAD 2> /dev/null) || ref="➦ $(git rev-parse --short HEAD 2> /dev/null)"
  89. if [[ -n $dirty ]]; then
  90. prompt_segment yellow black
  91. else
  92. prompt_segment green black
  93. fi
  94. if [[ -e "${repo_path}/BISECT_LOG" ]]; then
  95. mode=" <B>"
  96. elif [[ -e "${repo_path}/MERGE_HEAD" ]]; then
  97. mode=" >M<"
  98. elif [[ -e "${repo_path}/rebase" || -e "${repo_path}/rebase-apply" || -e "${repo_path}/rebase-merge" || -e "${repo_path}/../.dotest" ]]; then
  99. mode=" >R>"
  100. fi
  101. setopt promptsubst
  102. autoload -Uz vcs_info
  103. zstyle ':vcs_info:*' enable git
  104. zstyle ':vcs_info:*' get-revision true
  105. zstyle ':vcs_info:*' check-for-changes true
  106. zstyle ':vcs_info:*' stagedstr '✚'
  107. zstyle ':vcs_info:git:*' unstagedstr '●'
  108. zstyle ':vcs_info:*' formats ' %u%c'
  109. zstyle ':vcs_info:*' actionformats ' %u%c'
  110. vcs_info
  111. echo -n "${ref/refs\/heads\//$PL_BRANCH_CHAR }${vcs_info_msg_0_%% }${mode}"
  112. fi
  113. }
  114. prompt_hg() {
  115. local rev status
  116. if $(hg id >/dev/null 2>&1); then
  117. if $(hg prompt >/dev/null 2>&1); then
  118. if [[ $(hg prompt "{status|unknown}") = "?" ]]; then
  119. # if files are not added
  120. prompt_segment red white
  121. st='±'
  122. elif [[ -n $(hg prompt "{status|modified}") ]]; then
  123. # if any modification
  124. prompt_segment yellow black
  125. st='±'
  126. else
  127. # if working copy is clean
  128. prompt_segment green black
  129. fi
  130. echo -n $(hg prompt "☿ {rev}@{branch}") $st
  131. else
  132. st=""
  133. rev=$(hg id -n 2>/dev/null | sed 's/[^-0-9]//g')
  134. branch=$(hg id -b 2>/dev/null)
  135. if `hg st | grep -q "^\?"`; then
  136. prompt_segment red black
  137. st='±'
  138. elif `hg st | grep -q "^[MA]"`; then
  139. prompt_segment yellow black
  140. st='±'
  141. else
  142. prompt_segment green black
  143. fi
  144. echo -n "☿ $rev@$branch" $st
  145. fi
  146. fi
  147. }
  148. # Dir: current working directory
  149. prompt_dir() {
  150. prompt_segment blue black '%~'
  151. }
  152. # Virtualenv: current working virtualenv
  153. prompt_virtualenv() {
  154. local virtualenv_path="$VIRTUAL_ENV"
  155. if [[ -n $virtualenv_path && -n $VIRTUAL_ENV_DISABLE_PROMPT ]]; then
  156. prompt_segment blue black "(`basename $virtualenv_path`)"
  157. fi
  158. }
  159. # Status:
  160. # - was there an error
  161. # - am I root
  162. # - are there background jobs?
  163. prompt_status() {
  164. local symbols
  165. symbols=()
  166. [[ $RETVAL -ne 0 ]] && symbols+="%{%F{red}%}✘"
  167. [[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}⚡"
  168. [[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{cyan}%}⚙"
  169. [[ -n "$symbols" ]] && prompt_segment black default "$symbols"
  170. }
  171. ## Main prompt
  172. build_prompt() {
  173. RETVAL=$?
  174. prompt_status
  175. prompt_virtualenv
  176. prompt_context
  177. prompt_dir
  178. prompt_git
  179. prompt_hg
  180. prompt_end
  181. }
  182. PROMPT='%{%f%b%k%}$(build_prompt) '