agnoster.zsh-theme 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. ### Theme Configuration Initialization
  39. #
  40. # Override these settings in your ~/.zshrc
  41. # Current working directory
  42. : ${AGNOSTER_DIR_FG:=${CURRENT_FG}}
  43. : ${AGNOSTER_DIR_BG:=blue}
  44. # user@host
  45. : ${AGNOSTER_CONTEXT_FG:=white}
  46. : ${AGNOSTER_CONTEXT_BG:=black}
  47. # Git related
  48. : ${AGNOSTER_GIT_CLEAN_FG:=${CURRENT_FG}}
  49. : ${AGNOSTER_GIT_CLEAN_BG:=green}
  50. : ${AGNOSTER_GIT_DIRTY_FG:=black}
  51. : ${AGNOSTER_GIT_DIRTY_BG:=yellow}
  52. # Bazaar related
  53. : ${AGNOSTER_BZR_CLEAN_FG:=${CURRENT_FG}}
  54. : ${AGNOSTER_BZR_CLEAN_BG:=green}
  55. : ${AGNOSTER_BZR_DIRTY_FG:=black}
  56. : ${AGNOSTER_BZR_DIRTY_BG:=yellow}
  57. # Mercurial related
  58. : ${AGNOSTER_HG_NEWFILE_FG:=white}
  59. : ${AGNOSTER_HG_NEWFILE_BG:=red}
  60. : ${AGNOSTER_HG_CHANGED_FG:=black}
  61. : ${AGNOSTER_HG_CHANGED_BG:=yellow}
  62. : ${AGNOSTER_HG_CLEAN_FG:=${CURRENT_FG}}
  63. : ${AGNOSTER_HG_CLEAN_BG:=green}
  64. # VirtualEnv colors
  65. : ${AGNOSTER_VENV_FG:=black}
  66. : ${AGNOSTER_VENV_BG:=blue}
  67. # AWS Profile colors
  68. : ${AGNOSTER_AWS_PROD_FG:=yellow}
  69. : ${AGNOSTER_AWS_PROD_BG:=red}
  70. : ${AGNOSTER_AWS_FG:=black}
  71. : ${AGNOSTER_AWS_BG:=green}
  72. # Status symbols
  73. : ${AGNOSTER_STATUS_RETVAL_FG:=red}
  74. : ${AGNOSTER_STATUS_ROOT_FG:=yellow}
  75. : ${AGNOSTER_STATUS_JOB_FG:=cyan}
  76. : ${AGNOSTER_STATUS_FG:=white}
  77. : ${AGNOSTER_STATUS_BG:=black}
  78. ## Non-Color settings - set to 'true' to enable
  79. # Show the actual numeric return value rather than a cross symbol.
  80. : ${AGNOSTER_STATUS_RETVAL_NUMERIC:=false}
  81. # Show git working dir in the style "/git/root   master  relative/dir" instead of "/git/root/relative/dir   master"
  82. : ${AGNOSTER_GIT_INLINE:=false}
  83. # Show the git branch status in the prompt rather than the generic branch symbol
  84. : ${AGNOSTER_GIT_BRANCH_STATUS:=true}
  85. # Special Powerline characters
  86. () {
  87. local LC_ALL="" LC_CTYPE="en_US.UTF-8"
  88. # NOTE: This segment separator character is correct. In 2012, Powerline changed
  89. # the code points they use for their special characters. This is the new code point.
  90. # If this is not working for you, you probably have an old version of the
  91. # Powerline-patched fonts installed. Download and install the new version.
  92. # Do not submit PRs to change this unless you have reviewed the Powerline code point
  93. # history and have new information.
  94. # This is defined using a Unicode escape sequence so it is unambiguously readable, regardless of
  95. # what font the user is viewing this source code in. Do not replace the
  96. # escape sequence with a single literal character.
  97. # Do not change this! Do not make it '\u2b80'; that is the old, wrong code point.
  98. SEGMENT_SEPARATOR=$'\ue0b0'
  99. }
  100. # Begin a segment
  101. # Takes two arguments, background and foreground. Both can be omitted,
  102. # rendering default background/foreground.
  103. prompt_segment() {
  104. local bg fg
  105. [[ -n $1 ]] && bg="%K{$1}" || bg="%k"
  106. [[ -n $2 ]] && fg="%F{$2}" || fg="%f"
  107. if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then
  108. echo -n " %{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%} "
  109. else
  110. echo -n "%{$bg%}%{$fg%} "
  111. fi
  112. CURRENT_BG=$1
  113. [[ -n $3 ]] && echo -n $3
  114. }
  115. # End the prompt, closing any open segments
  116. prompt_end() {
  117. if [[ -n $CURRENT_BG ]]; then
  118. echo -n " %{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
  119. else
  120. echo -n "%{%k%}"
  121. fi
  122. echo -n "%{%f%}"
  123. CURRENT_BG=''
  124. }
  125. git_toplevel() {
  126. local repo_root=$(git rev-parse --show-toplevel)
  127. if [[ $repo_root = '' ]]; then
  128. # We are in a bare repo. Use git dir as root
  129. repo_root=$(git rev-parse --git-dir)
  130. if [[ $repo_root = '.' ]]; then
  131. repo_root=$PWD
  132. fi
  133. fi
  134. echo -n $repo_root
  135. }
  136. ### Prompt components
  137. # Each component will draw itself, and hide itself if no information needs to be shown
  138. # Context: user@hostname (who am I and where am I)
  139. prompt_context() {
  140. if [[ "$USERNAME" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
  141. prompt_segment "$AGNOSTER_CONTEXT_BG" "$AGNOSTER_CONTEXT_FG" "%(!.%{%F{$AGNOSTER_STATUS_ROOT_FG}%}.)%n@%m"
  142. fi
  143. }
  144. prompt_git_relative() {
  145. local repo_root=$(git_toplevel)
  146. local path_in_repo=$(pwd | sed "s/^$(echo "$repo_root" | sed 's:/:\\/:g;s/\$/\\$/g')//;s:^/::;s:/$::;")
  147. if [[ $path_in_repo != '' ]]; then
  148. prompt_segment "$AGNOSTER_DIR_BG" "$AGNOSTER_DIR_FG" "$path_in_repo"
  149. fi;
  150. }
  151. # Git: branch/detached head, dirty status
  152. prompt_git() {
  153. (( $+commands[git] )) || return
  154. if [[ "$(command git config --get oh-my-zsh.hide-status 2>/dev/null)" = 1 ]]; then
  155. return
  156. fi
  157. local PL_BRANCH_CHAR
  158. () {
  159. local LC_ALL="" LC_CTYPE="en_US.UTF-8"
  160. PL_BRANCH_CHAR=$'\ue0a0' # 
  161. }
  162. local ref dirty mode repo_path
  163. if [[ "$(command git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]]; then
  164. repo_path=$(command git rev-parse --git-dir 2>/dev/null)
  165. dirty=$(parse_git_dirty)
  166. ref=$(command git symbolic-ref HEAD 2> /dev/null) || \
  167. ref="◈ $(command git describe --exact-match --tags HEAD 2> /dev/null)" || \
  168. ref="➦ $(command git rev-parse --short HEAD 2> /dev/null)"
  169. if [[ -n $dirty ]]; then
  170. prompt_segment "$AGNOSTER_GIT_DIRTY_BG" "$AGNOSTER_GIT_DIRTY_FG"
  171. else
  172. prompt_segment "$AGNOSTER_GIT_CLEAN_BG" "$AGNOSTER_GIT_CLEAN_FG"
  173. fi
  174. if [[ $AGNOSTER_GIT_BRANCH_STATUS == 'true' ]]; then
  175. local ahead behind
  176. ahead=$(command git log --oneline @{upstream}.. 2>/dev/null)
  177. behind=$(command git log --oneline ..@{upstream} 2>/dev/null)
  178. if [[ -n "$ahead" ]] && [[ -n "$behind" ]]; then
  179. PL_BRANCH_CHAR=$'\u21c5'
  180. elif [[ -n "$ahead" ]]; then
  181. PL_BRANCH_CHAR=$'\u21b1'
  182. elif [[ -n "$behind" ]]; then
  183. PL_BRANCH_CHAR=$'\u21b0'
  184. fi
  185. fi
  186. if [[ -e "${repo_path}/BISECT_LOG" ]]; then
  187. mode=" <B>"
  188. elif [[ -e "${repo_path}/MERGE_HEAD" ]]; then
  189. mode=" >M<"
  190. elif [[ -e "${repo_path}/rebase" || -e "${repo_path}/rebase-apply" || -e "${repo_path}/rebase-merge" || -e "${repo_path}/../.dotest" ]]; then
  191. mode=" >R>"
  192. fi
  193. setopt promptsubst
  194. autoload -Uz vcs_info
  195. zstyle ':vcs_info:*' enable git
  196. zstyle ':vcs_info:*' get-revision true
  197. zstyle ':vcs_info:*' check-for-changes true
  198. zstyle ':vcs_info:*' stagedstr '✚'
  199. zstyle ':vcs_info:*' unstagedstr '±'
  200. zstyle ':vcs_info:*' formats ' %u%c'
  201. zstyle ':vcs_info:*' actionformats ' %u%c'
  202. vcs_info
  203. echo -n "${${ref:gs/%/%%}/refs\/heads\//$PL_BRANCH_CHAR }${vcs_info_msg_0_%% }${mode}"
  204. [[ $AGNOSTER_GIT_INLINE == 'true' ]] && prompt_git_relative
  205. fi
  206. }
  207. prompt_bzr() {
  208. (( $+commands[bzr] )) || return
  209. # Test if bzr repository in directory hierarchy
  210. local dir="$PWD"
  211. while [[ ! -d "$dir/.bzr" ]]; do
  212. [[ "$dir" = "/" ]] && return
  213. dir="${dir:h}"
  214. done
  215. local bzr_status status_mod status_all revision
  216. if bzr_status=$(command bzr status 2>&1); then
  217. status_mod=$(echo -n "$bzr_status" | head -n1 | grep "modified" | wc -m)
  218. status_all=$(echo -n "$bzr_status" | head -n1 | wc -m)
  219. revision=${$(command bzr log -r-1 --log-format line | cut -d: -f1):gs/%/%%}
  220. if [[ $status_mod -gt 0 ]] ; then
  221. prompt_segment "$AGNOSTER_BZR_DIRTY_BG" "$AGNOSTER_BZR_DIRTY_FG" "bzr@$revision ✚"
  222. else
  223. if [[ $status_all -gt 0 ]] ; then
  224. prompt_segment "$AGNOSTER_BZR_DIRTY_BG" "$AGNOSTER_BZR_DIRTY_FG" "bzr@$revision"
  225. else
  226. prompt_segment "$AGNOSTER_BZR_CLEAN_BG" "$AGNOSTER_BZR_CLEAN_FG" "bzr@$revision"
  227. fi
  228. fi
  229. fi
  230. }
  231. prompt_hg() {
  232. (( $+commands[hg] )) || return
  233. local rev st branch
  234. if $(command hg id >/dev/null 2>&1); then
  235. if $(command hg prompt >/dev/null 2>&1); then
  236. if [[ $(command hg prompt "{status|unknown}") = "?" ]]; then
  237. # if files are not added
  238. prompt_segment "$AGNOSTER_HG_NEWFILE_BG" "$AGNOSTER_HG_NEWFILE_FG"
  239. st='±'
  240. elif [[ -n $(command hg prompt "{status|modified}") ]]; then
  241. # if any modification
  242. prompt_segment "$AGNOSTER_HG_CHANGED_BG" "$AGNOSTER_HG_CHANGED_FG"
  243. st='±'
  244. else
  245. # if working copy is clean
  246. prompt_segment "$AGNOSTER_HG_CLEAN_BG" "$AGNOSTER_HG_CLEAN_FG"
  247. fi
  248. echo -n ${$(command hg prompt "☿ {rev}@{branch}"):gs/%/%%} $st
  249. else
  250. st=""
  251. rev=$(command hg id -n 2>/dev/null | sed 's/[^-0-9]//g')
  252. branch=$(command hg id -b 2>/dev/null)
  253. if command hg st | command grep -q "^\?"; then
  254. prompt_segment "$AGNOSTER_HG_NEWFILE_BG" "$AGNOSTER_HG_NEWFILE_FG"
  255. st='±'
  256. elif command hg st | command grep -q "^[MA]"; then
  257. prompt_segment "$AGNOSTER_HG_CHANGED_BG" "$AGNOSTER_HG_CHANGED_FG"
  258. st='±'
  259. else
  260. prompt_segment "$AGNOSTER_HG_CLEAN_BG" "$AGNOSTER_HG_CLEAN_FG"
  261. fi
  262. echo -n "☿ ${rev:gs/%/%%}@${branch:gs/%/%%}" $st
  263. fi
  264. fi
  265. }
  266. # Dir: current working directory
  267. prompt_dir() {
  268. if [[ $AGNOSTER_GIT_INLINE == 'true' ]] && $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
  269. # Git repo and inline path enabled, hence only show the git root
  270. prompt_segment "$AGNOSTER_DIR_BG" "$AGNOSTER_DIR_FG" "$(git_toplevel | sed "s:^$HOME:~:")"
  271. else
  272. prompt_segment "$AGNOSTER_DIR_BG" "$AGNOSTER_DIR_FG" '%~'
  273. fi
  274. }
  275. # Virtualenv: current working virtualenv
  276. prompt_virtualenv() {
  277. if [[ -n "$VIRTUAL_ENV" && -n "$VIRTUAL_ENV_DISABLE_PROMPT" ]]; then
  278. prompt_segment "$AGNOSTER_VENV_BG" "$AGNOSTER_VENV_FG" "(${VIRTUAL_ENV:t:gs/%/%%})"
  279. fi
  280. }
  281. # Status:
  282. # - was there an error
  283. # - am I root
  284. # - are there background jobs?
  285. prompt_status() {
  286. local -a symbols
  287. if [[ $AGNOSTER_STATUS_RETVAL_NUMERIC == 'true' ]]; then
  288. [[ $RETVAL -ne 0 ]] && symbols+="%{%F{$AGNOSTER_STATUS_RETVAL_FG}%}$RETVAL"
  289. else
  290. [[ $RETVAL -ne 0 ]] && symbols+="%{%F{$AGNOSTER_STATUS_RETVAL_FG}%}✘"
  291. fi
  292. [[ $UID -eq 0 ]] && symbols+="%{%F{$AGNOSTER_STATUS_ROOT_FG}%}⚡"
  293. [[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{$AGNOSTER_STATUS_JOB_FG}%}⚙"
  294. [[ -n "$symbols" ]] && prompt_segment "$AGNOSTER_STATUS_BG" "$AGNOSTER_STATUS_FG" "$symbols"
  295. }
  296. #AWS Profile:
  297. # - display current AWS_PROFILE name
  298. # - displays yellow on red if profile name contains 'production' or
  299. # ends in '-prod'
  300. # - displays black on green otherwise
  301. prompt_aws() {
  302. [[ -z "$AWS_PROFILE" || "$SHOW_AWS_PROMPT" = false ]] && return
  303. case "$AWS_PROFILE" in
  304. *-prod|*production*) prompt_segment "$AGNOSTER_AWS_PROD_BG" "$AGNOSTER_AWS_PROD_FG" "AWS: ${AWS_PROFILE:gs/%/%%}" ;;
  305. *) prompt_segment "$AGNOSTER_AWS_BG" "$AGNOSTER_AWS_FG" "AWS: ${AWS_PROFILE:gs/%/%%}" ;;
  306. esac
  307. }
  308. ## Main prompt
  309. build_prompt() {
  310. RETVAL=$?
  311. prompt_status
  312. prompt_virtualenv
  313. prompt_aws
  314. prompt_context
  315. prompt_dir
  316. prompt_git
  317. prompt_bzr
  318. prompt_hg
  319. prompt_end
  320. }
  321. PROMPT='%{%f%b%k%}$(build_prompt) '