agnoster.zsh-theme 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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](https://iterm2.com/) over Terminal.app -
  17. # it has significantly better color fidelity.
  18. #
  19. # If using with "light" variant of the Solarized color schema, set
  20. # SOLARIZED_THEME variable to "light". If you don't specify, we'll assume
  21. # you're using the "dark" variant.
  22. #
  23. # # Goals
  24. #
  25. # The aim of this theme is to only show you *relevant* information. Like most
  26. # prompts, it will only show git information when in a git working directory.
  27. # However, it goes a step further: everything from the current user and
  28. # hostname to whether the last call exited with an error to whether background
  29. # jobs are running in this shell will all be displayed automatically when
  30. # appropriate.
  31. ### Segment drawing
  32. # A few utility functions to make it easy and re-usable to draw segmented prompts
  33. CURRENT_BG='NONE'
  34. case ${SOLARIZED_THEME:-dark} in
  35. light) CURRENT_FG='white';;
  36. *) CURRENT_FG='black';;
  37. esac
  38. # Special Powerline characters
  39. () {
  40. local LC_ALL="" LC_CTYPE="en_US.UTF-8"
  41. # NOTE: This segment separator character is correct. In 2012, Powerline changed
  42. # the code points they use for their special characters. This is the new code point.
  43. # If this is not working for you, you probably have an old version of the
  44. # Powerline-patched fonts installed. Download and install the new version.
  45. # Do not submit PRs to change this unless you have reviewed the Powerline code point
  46. # history and have new information.
  47. # This is defined using a Unicode escape sequence so it is unambiguously readable, regardless of
  48. # what font the user is viewing this source code in. Do not replace the
  49. # escape sequence with a single literal character.
  50. # Do not change this! Do not make it '\u2b80'; that is the old, wrong code point.
  51. SEGMENT_SEPARATOR=$'\ue0b0'
  52. }
  53. # Begin a segment
  54. # Takes two arguments, background and foreground. Both can be omitted,
  55. # rendering default background/foreground.
  56. prompt_segment() {
  57. local bg fg
  58. [[ -n $1 ]] && bg="%K{$1}" || bg="%k"
  59. [[ -n $2 ]] && fg="%F{$2}" || fg="%f"
  60. if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then
  61. echo -n " %{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%} "
  62. else
  63. echo -n "%{$bg%}%{$fg%} "
  64. fi
  65. CURRENT_BG=$1
  66. [[ -n $3 ]] && echo -n $3
  67. }
  68. # End the prompt, closing any open segments
  69. prompt_end() {
  70. if [[ -n $CURRENT_BG ]]; then
  71. echo -n " %{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
  72. else
  73. echo -n "%{%k%}"
  74. fi
  75. echo -n "%{%f%}"
  76. CURRENT_BG=''
  77. }
  78. ### Prompt components
  79. # Each component will draw itself, and hide itself if no information needs to be shown
  80. # Context: user@hostname (who am I and where am I)
  81. prompt_context() {
  82. if [[ "$USERNAME" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
  83. prompt_segment black default "%(!.%{%F{yellow}%}.)%n@%m"
  84. fi
  85. }
  86. # Git: branch/detached head, dirty status
  87. prompt_git() {
  88. (( $+commands[git] )) || return
  89. if [[ "$(command git config --get oh-my-zsh.hide-status 2>/dev/null)" = 1 ]]; then
  90. return
  91. fi
  92. local PL_BRANCH_CHAR
  93. () {
  94. local LC_ALL="" LC_CTYPE="en_US.UTF-8"
  95. PL_BRANCH_CHAR=$'\ue0a0' # 
  96. }
  97. local ref dirty mode repo_path
  98. if [[ "$(command git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]]; then
  99. repo_path=$(command git rev-parse --git-dir 2>/dev/null)
  100. dirty=$(parse_git_dirty)
  101. ref=$(command git symbolic-ref HEAD 2> /dev/null) || \
  102. ref="◈ $(command git describe --exact-match --tags HEAD 2> /dev/null)" || \
  103. ref="➦ $(command git rev-parse --short HEAD 2> /dev/null)"
  104. if [[ -n $dirty ]]; then
  105. prompt_segment yellow black
  106. else
  107. prompt_segment green $CURRENT_FG
  108. fi
  109. local ahead behind
  110. ahead=$(command git log --oneline @{upstream}.. 2>/dev/null)
  111. behind=$(command git log --oneline ..@{upstream} 2>/dev/null)
  112. if [[ -n "$ahead" ]] && [[ -n "$behind" ]]; then
  113. PL_BRANCH_CHAR=$'\u21c5'
  114. elif [[ -n "$ahead" ]]; then
  115. PL_BRANCH_CHAR=$'\u21b1'
  116. elif [[ -n "$behind" ]]; then
  117. PL_BRANCH_CHAR=$'\u21b0'
  118. fi
  119. if [[ -e "${repo_path}/BISECT_LOG" ]]; then
  120. mode=" <B>"
  121. elif [[ -e "${repo_path}/MERGE_HEAD" ]]; then
  122. mode=" >M<"
  123. elif [[ -e "${repo_path}/rebase" || -e "${repo_path}/rebase-apply" || -e "${repo_path}/rebase-merge" || -e "${repo_path}/../.dotest" ]]; then
  124. mode=" >R>"
  125. fi
  126. setopt promptsubst
  127. autoload -Uz vcs_info
  128. zstyle ':vcs_info:*' enable git
  129. zstyle ':vcs_info:*' get-revision true
  130. zstyle ':vcs_info:*' check-for-changes true
  131. zstyle ':vcs_info:*' stagedstr '✚'
  132. zstyle ':vcs_info:*' unstagedstr '±'
  133. zstyle ':vcs_info:*' formats ' %u%c'
  134. zstyle ':vcs_info:*' actionformats ' %u%c'
  135. vcs_info
  136. echo -n "${${ref:gs/%/%%}/refs\/heads\//$PL_BRANCH_CHAR }${vcs_info_msg_0_%% }${mode}"
  137. fi
  138. }
  139. prompt_bzr() {
  140. (( $+commands[bzr] )) || return
  141. # Test if bzr repository in directory hierarchy
  142. local dir="$PWD"
  143. while [[ ! -d "$dir/.bzr" ]]; do
  144. [[ "$dir" = "/" ]] && return
  145. dir="${dir:h}"
  146. done
  147. local bzr_status status_mod status_all revision
  148. if bzr_status=$(command bzr status 2>&1); then
  149. status_mod=$(echo -n "$bzr_status" | head -n1 | grep "modified" | wc -m)
  150. status_all=$(echo -n "$bzr_status" | head -n1 | wc -m)
  151. revision=${$(command bzr log -r-1 --log-format line | cut -d: -f1):gs/%/%%}
  152. if [[ $status_mod -gt 0 ]] ; then
  153. prompt_segment yellow black "bzr@$revision ✚"
  154. else
  155. if [[ $status_all -gt 0 ]] ; then
  156. prompt_segment yellow black "bzr@$revision"
  157. else
  158. prompt_segment green black "bzr@$revision"
  159. fi
  160. fi
  161. fi
  162. }
  163. prompt_hg() {
  164. (( $+commands[hg] )) || return
  165. local rev st branch
  166. if $(command hg id >/dev/null 2>&1); then
  167. if $(command hg prompt >/dev/null 2>&1); then
  168. if [[ $(command hg prompt "{status|unknown}") = "?" ]]; then
  169. # if files are not added
  170. prompt_segment red white
  171. st='±'
  172. elif [[ -n $(command hg prompt "{status|modified}") ]]; then
  173. # if any modification
  174. prompt_segment yellow black
  175. st='±'
  176. else
  177. # if working copy is clean
  178. prompt_segment green $CURRENT_FG
  179. fi
  180. echo -n ${$(command hg prompt "☿ {rev}@{branch}"):gs/%/%%} $st
  181. else
  182. st=""
  183. rev=$(command hg id -n 2>/dev/null | sed 's/[^-0-9]//g')
  184. branch=$(command hg id -b 2>/dev/null)
  185. if command hg st | command grep -q "^\?"; then
  186. prompt_segment red black
  187. st='±'
  188. elif command hg st | command grep -q "^[MA]"; then
  189. prompt_segment yellow black
  190. st='±'
  191. else
  192. prompt_segment green $CURRENT_FG
  193. fi
  194. echo -n "☿ ${rev:gs/%/%%}@${branch:gs/%/%%}" $st
  195. fi
  196. fi
  197. }
  198. # Dir: current working directory
  199. prompt_dir() {
  200. prompt_segment blue $CURRENT_FG '%~'
  201. }
  202. # Virtualenv: current working virtualenv
  203. prompt_virtualenv() {
  204. if [[ -n "$VIRTUAL_ENV" && -n "$VIRTUAL_ENV_DISABLE_PROMPT" ]]; then
  205. prompt_segment blue black "(${VIRTUAL_ENV:t:gs/%/%%})"
  206. fi
  207. }
  208. # Status:
  209. # - was there an error
  210. # - am I root
  211. # - are there background jobs?
  212. prompt_status() {
  213. local -a symbols
  214. [[ $RETVAL -ne 0 ]] && symbols+="%{%F{red}%}✘"
  215. [[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}⚡"
  216. [[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{cyan}%}⚙"
  217. [[ -n "$symbols" ]] && prompt_segment black default "$symbols"
  218. }
  219. #AWS Profile:
  220. # - display current AWS_PROFILE name
  221. # - displays yellow on red if profile name contains 'production' or
  222. # ends in '-prod'
  223. # - displays black on green otherwise
  224. prompt_aws() {
  225. [[ -z "$AWS_PROFILE" || "$SHOW_AWS_PROMPT" = false ]] && return
  226. case "$AWS_PROFILE" in
  227. *-prod|*production*) prompt_segment red yellow "AWS: ${AWS_PROFILE:gs/%/%%}" ;;
  228. *) prompt_segment green black "AWS: ${AWS_PROFILE:gs/%/%%}" ;;
  229. esac
  230. }
  231. ## Main prompt
  232. build_prompt() {
  233. RETVAL=$?
  234. prompt_status
  235. prompt_virtualenv
  236. prompt_aws
  237. prompt_context
  238. prompt_dir
  239. prompt_git
  240. prompt_bzr
  241. prompt_hg
  242. prompt_end
  243. }
  244. PROMPT='%{%f%b%k%}$(build_prompt) '