agnoster.zsh-theme 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. local PL_BRANCH_CHAR
  81. () {
  82. local LC_ALL="" LC_CTYPE="en_US.UTF-8"
  83. PL_BRANCH_CHAR=$'\ue0a0' # 
  84. }
  85. local ref dirty mode repo_path
  86. repo_path=$(git rev-parse --git-dir 2>/dev/null)
  87. if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
  88. dirty=$(parse_git_dirty)
  89. ref=$(git symbolic-ref HEAD 2> /dev/null) || ref="➦ $(git rev-parse --short HEAD 2> /dev/null)"
  90. if [[ -n $dirty ]]; then
  91. prompt_segment yellow black
  92. else
  93. prompt_segment green black
  94. fi
  95. if [[ -e "${repo_path}/BISECT_LOG" ]]; then
  96. mode=" <B>"
  97. elif [[ -e "${repo_path}/MERGE_HEAD" ]]; then
  98. mode=" >M<"
  99. elif [[ -e "${repo_path}/rebase" || -e "${repo_path}/rebase-apply" || -e "${repo_path}/rebase-merge" || -e "${repo_path}/../.dotest" ]]; then
  100. mode=" >R>"
  101. fi
  102. setopt promptsubst
  103. autoload -Uz vcs_info
  104. zstyle ':vcs_info:*' enable git
  105. zstyle ':vcs_info:*' get-revision true
  106. zstyle ':vcs_info:*' check-for-changes true
  107. zstyle ':vcs_info:*' stagedstr '✚'
  108. zstyle ':vcs_info:*' unstagedstr '●'
  109. zstyle ':vcs_info:*' formats ' %u%c'
  110. zstyle ':vcs_info:*' actionformats ' %u%c'
  111. vcs_info
  112. echo -n "${ref/refs\/heads\//$PL_BRANCH_CHAR }${vcs_info_msg_0_%% }${mode}"
  113. fi
  114. }
  115. prompt_hg() {
  116. local rev status
  117. if $(hg id >/dev/null 2>&1); then
  118. if $(hg prompt >/dev/null 2>&1); then
  119. if [[ $(hg prompt "{status|unknown}") = "?" ]]; then
  120. # if files are not added
  121. prompt_segment red white
  122. st='±'
  123. elif [[ -n $(hg prompt "{status|modified}") ]]; then
  124. # if any modification
  125. prompt_segment yellow black
  126. st='±'
  127. else
  128. # if working copy is clean
  129. prompt_segment green black
  130. fi
  131. echo -n $(hg prompt "☿ {rev}@{branch}") $st
  132. else
  133. st=""
  134. rev=$(hg id -n 2>/dev/null | sed 's/[^-0-9]//g')
  135. branch=$(hg id -b 2>/dev/null)
  136. if `hg st | grep -q "^\?"`; then
  137. prompt_segment red black
  138. st='±'
  139. elif `hg st | grep -q "^[MA]"`; then
  140. prompt_segment yellow black
  141. st='±'
  142. else
  143. prompt_segment green black
  144. fi
  145. echo -n "☿ $rev@$branch" $st
  146. fi
  147. fi
  148. }
  149. # Dir: current working directory
  150. prompt_dir() {
  151. prompt_segment blue black '%~'
  152. }
  153. # Virtualenv: current working virtualenv
  154. prompt_virtualenv() {
  155. local virtualenv_path="$VIRTUAL_ENV"
  156. if [[ -n $virtualenv_path && -n $VIRTUAL_ENV_DISABLE_PROMPT ]]; then
  157. prompt_segment blue black "(`basename $virtualenv_path`)"
  158. fi
  159. }
  160. # Status:
  161. # - was there an error
  162. # - am I root
  163. # - are there background jobs?
  164. prompt_status() {
  165. local symbols
  166. symbols=()
  167. [[ $RETVAL -ne 0 ]] && symbols+="%{%F{red}%}✘"
  168. [[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}⚡"
  169. [[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{cyan}%}⚙"
  170. [[ -n "$symbols" ]] && prompt_segment black default "$symbols"
  171. }
  172. ## Main prompt
  173. build_prompt() {
  174. RETVAL=$?
  175. prompt_status
  176. prompt_virtualenv
  177. prompt_context
  178. prompt_dir
  179. prompt_git
  180. prompt_hg
  181. prompt_end
  182. }
  183. PROMPT='%{%f%b%k%}$(build_prompt) '