You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
6.8 KiB

5 years ago
  1. #!/bin/bash
  2. # vim:ft=zsh ts=2 sw=2 sts=2
  3. #
  4. # agnoster's Theme - https://gist.github.com/3712874
  5. # A Powerline-inspired theme for ZSH
  6. #
  7. # # README
  8. #
  9. # In order for this theme to render correctly, you will need a
  10. # [Powerline-patched font](https://github.com/Lokaltog/powerline-fonts).
  11. # Make sure you have a recent version: the code points that Powerline
  12. # uses changed in 2012, and older versions will display incorrectly,
  13. # in confusing ways.
  14. #
  15. # In addition, I recommend the
  16. # [Solarized theme](https://github.com/altercation/solarized/) and, if you're
  17. # using it on Mac OS X, [iTerm 2](http://www.iterm2.com/) over Terminal.app -
  18. # it has significantly better color fidelity.
  19. #
  20. # # Goals
  21. #
  22. # The aim of this theme is to only show you *relevant* information. Like most
  23. # prompts, it will only show git information when in a git working directory.
  24. # However, it goes a step further: everything from the current user and
  25. # hostname to whether the last call exited with an error to whether background
  26. # jobs are running in this shell will all be displayed automatically when
  27. # appropriate.
  28. ### Segment drawing
  29. # A few utility functions to make it easy and re-usable to draw segmented prompts
  30. CURRENT_BG='NONE'
  31. # Special Powerline characters
  32. () {
  33. local LC_ALL="" LC_CTYPE="en_US.UTF-8"
  34. # NOTE: This segment separator character is correct. In 2012, Powerline changed
  35. # the code points they use for their special characters. This is the new code point.
  36. # If this is not working for you, you probably have an old version of the
  37. # Powerline-patched fonts installed. Download and install the new version.
  38. # Do not submit PRs to change this unless you have reviewed the Powerline code point
  39. # history and have new information.
  40. # This is defined using a Unicode escape sequence so it is unambiguously readable, regardless of
  41. # what font the user is viewing this source code in. Do not replace the
  42. # escape sequence with a single literal character.
  43. # Do not change this! Do not make it '\u2b80'; that is the old, wrong code point.
  44. SEGMENT_SEPARATOR=$'\ue0b0'
  45. }
  46. # Begin a segment
  47. # Takes two arguments, background and foreground. Both can be omitted,
  48. # rendering default background/foreground.
  49. prompt_segment() {
  50. local bg fg
  51. [[ -n $1 ]] && bg="%K{$1}" || bg="%k"
  52. [[ -n $2 ]] && fg="%F{$2}" || fg="%f"
  53. if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then
  54. echo -n " %{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%} "
  55. else
  56. echo -n "%{$bg%}%{$fg%} "
  57. fi
  58. CURRENT_BG=$1
  59. [[ -n $3 ]] && echo -n $3
  60. }
  61. # End the prompt, closing any open segments
  62. prompt_end() {
  63. if [[ -n $CURRENT_BG ]]; then
  64. echo -n " %{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
  65. else
  66. echo -n "%{%k%}"
  67. fi
  68. echo -n "%{%f%}"
  69. CURRENT_BG=''
  70. }
  71. ### Prompt components
  72. # Each component will draw itself, and hide itself if no information needs to be shown
  73. # Context: user@hostname (who am I and where am I)
  74. prompt_context() {
  75. if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
  76. prompt_segment black default "%(!.%{%F{yellow}%}.)$USER"
  77. fi
  78. }
  79. # Git: branch/detached head, dirty status
  80. prompt_git() {
  81. (( $+commands[git] )) || return
  82. local PL_BRANCH_CHAR
  83. () {
  84. local LC_ALL="" LC_CTYPE="en_US.UTF-8"
  85. PL_BRANCH_CHAR=$'\ue0a0' # 
  86. }
  87. local ref dirty mode repo_path
  88. repo_path=$(git rev-parse --git-dir 2>/dev/null)
  89. if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
  90. dirty=$(parse_git_dirty)
  91. ref=$(git symbolic-ref HEAD 2> /dev/null) || ref="➦ $(git rev-parse --short HEAD 2> /dev/null)"
  92. if [[ -n $dirty ]]; then
  93. prompt_segment yellow black
  94. else
  95. prompt_segment green black
  96. fi
  97. if [[ -e "${repo_path}/BISECT_LOG" ]]; then
  98. mode=" <B>"
  99. elif [[ -e "${repo_path}/MERGE_HEAD" ]]; then
  100. mode=" >M<"
  101. elif [[ -e "${repo_path}/rebase" || -e "${repo_path}/rebase-apply" || -e "${repo_path}/rebase-merge" || -e "${repo_path}/../.dotest" ]]; then
  102. mode=" >R>"
  103. fi
  104. setopt promptsubst
  105. autoload -Uz vcs_info
  106. zstyle ':vcs_info:*' enable git
  107. zstyle ':vcs_info:*' get-revision true
  108. zstyle ':vcs_info:*' check-for-changes true
  109. zstyle ':vcs_info:*' stagedstr '✚'
  110. zstyle ':vcs_info:*' unstagedstr '●'
  111. zstyle ':vcs_info:*' formats ' %u%c'
  112. zstyle ':vcs_info:*' actionformats ' %u%c'
  113. vcs_info
  114. echo -n "${ref/refs\/heads\//$PL_BRANCH_CHAR }${vcs_info_msg_0_%% }${mode}"
  115. fi
  116. }
  117. prompt_bzr() {
  118. (( $+commands[bzr] )) || return
  119. if (bzr status >/dev/null 2>&1); then
  120. status_mod=`bzr status | head -n1 | grep "modified" | wc -m`
  121. status_all=`bzr status | head -n1 | wc -m`
  122. revision=`bzr log | head -n2 | tail -n1 | sed 's/^revno: //'`
  123. if [[ $status_mod -gt 0 ]] ; then
  124. prompt_segment yellow black
  125. echo -n "bzr@"$revision "✚ "
  126. else
  127. if [[ $status_all -gt 0 ]] ; then
  128. prompt_segment yellow black
  129. echo -n "bzr@"$revision
  130. else
  131. prompt_segment green black
  132. echo -n "bzr@"$revision
  133. fi
  134. fi
  135. fi
  136. }
  137. prompt_hg() {
  138. (( $+commands[hg] )) || return
  139. local rev status
  140. if $(hg id >/dev/null 2>&1); then
  141. if $(hg prompt >/dev/null 2>&1); then
  142. if [[ $(hg prompt "{status|unknown}") = "?" ]]; then
  143. # if files are not added
  144. prompt_segment red white
  145. st='±'
  146. elif [[ -n $(hg prompt "{status|modified}") ]]; then
  147. # if any modification
  148. prompt_segment yellow black
  149. st='±'
  150. else
  151. # if working copy is clean
  152. prompt_segment green black
  153. fi
  154. echo -n $(hg prompt "☿ {rev}@{branch}") $st
  155. else
  156. st=""
  157. rev=$(hg id -n 2>/dev/null | sed 's/[^-0-9]//g')
  158. branch=$(hg id -b 2>/dev/null)
  159. if `hg st | grep -q "^\?"`; then
  160. prompt_segment red black
  161. st='±'
  162. elif `hg st | grep -q "^[MA]"`; then
  163. prompt_segment yellow black
  164. st='±'
  165. else
  166. prompt_segment green black
  167. fi
  168. echo -n "☿ $rev@$branch" $st
  169. fi
  170. fi
  171. }
  172. # Dir: current working directory
  173. prompt_dir() {
  174. prompt_segment blue black '%~'
  175. }
  176. # Virtualenv: current working virtualenv
  177. prompt_virtualenv() {
  178. local virtualenv_path="$VIRTUAL_ENV"
  179. if [[ -n $virtualenv_path && -n $VIRTUAL_ENV_DISABLE_PROMPT ]]; then
  180. prompt_segment blue black "(`basename $virtualenv_path`)"
  181. fi
  182. }
  183. # Status:
  184. # - was there an error
  185. # - am I root
  186. # - are there background jobs?
  187. prompt_status() {
  188. local symbols
  189. symbols=()
  190. [[ $RETVAL -ne 0 ]] && symbols+="%{%F{red}%}✘"
  191. [[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}⚡"
  192. [[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{cyan}%}⚙"
  193. [[ -n "$symbols" ]] && prompt_segment black default "$symbols"
  194. }
  195. ## Main prompt
  196. build_prompt() {
  197. RETVAL=$?
  198. prompt_status
  199. prompt_virtualenv
  200. prompt_context
  201. prompt_dir
  202. prompt_git
  203. prompt_bzr
  204. prompt_hg
  205. prompt_end
  206. }
  207. PROMPT='%{%f%b%k%}$(build_prompt) '