agnoster.zsh-theme 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #
  11. # In addition, I recommend the
  12. # [Solarized theme](https://github.com/altercation/solarized/) and, if you're
  13. # using it on Mac OS X, [iTerm 2](http://www.iterm2.com/) over Terminal.app -
  14. # it has significantly better color fidelity.
  15. #
  16. # # Goals
  17. #
  18. # The aim of this theme is to only show you *relevant* information. Like most
  19. # prompts, it will only show git information when in a git working directory.
  20. # However, it goes a step further: everything from the current user and
  21. # hostname to whether the last call exited with an error to whether background
  22. # jobs are running in this shell will all be displayed automatically when
  23. # appropriate.
  24. ### Segment drawing
  25. # A few utility functions to make it easy and re-usable to draw segmented prompts
  26. CURRENT_BG='NONE'
  27. # Fix odd char on mac
  28. if [[ `uname` == 'Darwin' ]]; then
  29. SEGMENT_SEPARATOR='\ue0b0'
  30. else
  31. SEGMENT_SEPARATOR=''
  32. fi
  33. # Begin a segment
  34. # Takes two arguments, background and foreground. Both can be omitted,
  35. # rendering default background/foreground.
  36. prompt_segment() {
  37. local bg fg
  38. [[ -n $1 ]] && bg="%K{$1}" || bg="%k"
  39. [[ -n $2 ]] && fg="%F{$2}" || fg="%f"
  40. if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then
  41. echo -n " %{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%} "
  42. else
  43. echo -n "%{$bg%}%{$fg%} "
  44. fi
  45. CURRENT_BG=$1
  46. [[ -n $3 ]] && echo -n $3
  47. }
  48. # End the prompt, closing any open segments
  49. prompt_end() {
  50. if [[ -n $CURRENT_BG ]]; then
  51. echo -n " %{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
  52. else
  53. echo -n "%{%k%}"
  54. fi
  55. echo -n "%{%f%}"
  56. CURRENT_BG=''
  57. }
  58. ### Prompt components
  59. # Each component will draw itself, and hide itself if no information needs to be shown
  60. # Context: user@hostname (who am I and where am I)
  61. prompt_context() {
  62. if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
  63. prompt_segment black default "%(!.%{%F{yellow}%}.)$USER@%m"
  64. fi
  65. }
  66. # Git: branch/detached head, dirty status
  67. prompt_git() {
  68. local ref dirty mode repo_path
  69. repo_path=$(git rev-parse --git-dir 2>/dev/null)
  70. if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
  71. dirty=$(parse_git_dirty)
  72. ref=$(git symbolic-ref HEAD 2> /dev/null) || ref="➦ $(git show-ref --head -s --abbrev |head -n1 2> /dev/null)"
  73. if [[ -n $dirty ]]; then
  74. prompt_segment yellow black
  75. else
  76. prompt_segment green black
  77. fi
  78. if [[ -e "${repo_path}/BISECT_LOG" ]]; then
  79. mode=" <B>"
  80. elif [[ -e "${repo_path}/MERGE_HEAD" ]]; then
  81. mode=" >M<"
  82. elif [[ -e "${repo_path}/rebase" || -e "${repo_path}/rebase-apply" || -e "${repo_path}/rebase-merge" || -e "${repo_path}/../.dotest" ]]; then
  83. mode=" >R>"
  84. fi
  85. setopt promptsubst
  86. autoload -Uz vcs_info
  87. zstyle ':vcs_info:*' enable git
  88. zstyle ':vcs_info:*' get-revision true
  89. zstyle ':vcs_info:*' check-for-changes true
  90. zstyle ':vcs_info:*' stagedstr '✚'
  91. zstyle ':vcs_info:git:*' unstagedstr '●'
  92. zstyle ':vcs_info:*' formats ' %u%c'
  93. zstyle ':vcs_info:*' actionformats ' %u%c'
  94. vcs_info
  95. echo -n "${ref/refs\/heads\// }${vcs_info_msg_0_%% }${mode}"
  96. fi
  97. }
  98. prompt_hg() {
  99. local rev status
  100. if $(hg id >/dev/null 2>&1); then
  101. if $(hg prompt >/dev/null 2>&1); then
  102. if [[ $(hg prompt "{status|unknown}") = "?" ]]; then
  103. # if files are not added
  104. prompt_segment red white
  105. st='±'
  106. elif [[ -n $(hg prompt "{status|modified}") ]]; then
  107. # if any modification
  108. prompt_segment yellow black
  109. st='±'
  110. else
  111. # if working copy is clean
  112. prompt_segment green black
  113. fi
  114. echo -n $(hg prompt "☿ {rev}@{branch}") $st
  115. else
  116. st=""
  117. rev=$(hg id -n 2>/dev/null | sed 's/[^-0-9]//g')
  118. branch=$(hg id -b 2>/dev/null)
  119. if `hg st | grep -q "^\?"`; then
  120. prompt_segment red black
  121. st='±'
  122. elif `hg st | grep -q "^[MA]"`; then
  123. prompt_segment yellow black
  124. st='±'
  125. else
  126. prompt_segment green black
  127. fi
  128. echo -n "☿ $rev@$branch" $st
  129. fi
  130. fi
  131. }
  132. # Dir: current working directory
  133. prompt_dir() {
  134. prompt_segment blue black '%~'
  135. }
  136. # Virtualenv: current working virtualenv
  137. prompt_virtualenv() {
  138. local virtualenv_path="$VIRTUAL_ENV"
  139. if [[ -n $virtualenv_path && -n $VIRTUAL_ENV_DISABLE_PROMPT ]]; then
  140. prompt_segment blue black "(`basename $virtualenv_path`)"
  141. fi
  142. }
  143. # Status:
  144. # - was there an error
  145. # - am I root
  146. # - are there background jobs?
  147. prompt_status() {
  148. local symbols
  149. symbols=()
  150. [[ $RETVAL -ne 0 ]] && symbols+="%{%F{red}%}✘"
  151. [[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}⚡"
  152. [[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{cyan}%}⚙"
  153. [[ -n "$symbols" ]] && prompt_segment black default "$symbols"
  154. }
  155. ## Main prompt
  156. build_prompt() {
  157. RETVAL=$?
  158. prompt_status
  159. prompt_virtualenv
  160. prompt_context
  161. prompt_dir
  162. prompt_git
  163. prompt_hg
  164. prompt_end
  165. }
  166. PROMPT='%{%f%b%k%}$(build_prompt) '