agnoster.zsh-theme 5.0 KB

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