agnoster.zsh-theme 12 KB

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