agnoster.zsh-theme 12 KB

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