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.

179 lines
4.3 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  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://gist.github.com/1595572).
  10. #
  11. # In addition, I recommend the
  12. # [Solarized theme](https://github.com/altercation/solarized/) and, if you're
  13. # using it on Mac OS X, [iTerm 2](http://www.iterm2.com/) over Terminal.app -
  14. # it has significantly better color fidelity.
  15. #
  16. # # Goals
  17. #
  18. # The aim of this theme is to only show you *relevant* information. Like most
  19. # prompts, it will only show git information when in a git working directory.
  20. # However, it goes a step further: everything from the current user and
  21. # hostname to whether the last call exited with an error to whether background
  22. # jobs are running in this shell will all be displayed automatically when
  23. # appropriate.
  24. ### Segments of the prompt, default order declaration
  25. typeset -aHg AGNOSTER_PROMPT_SEGMENTS=(
  26. prompt_status
  27. prompt_context
  28. prompt_virtualenv
  29. prompt_dir
  30. prompt_git
  31. prompt_end
  32. )
  33. ### Segment drawing
  34. # A few utility functions to make it easy and re-usable to draw segmented prompts
  35. CURRENT_BG='NONE'
  36. if [[ -z "$PRIMARY_FG" ]]; then
  37. PRIMARY_FG=black
  38. fi
  39. # Characters
  40. SEGMENT_SEPARATOR="\ue0b0"
  41. PLUSMINUS="\u00b1"
  42. BRANCH="\ue0a0"
  43. DETACHED="\u27a6"
  44. CROSS="\u2718"
  45. LIGHTNING="\u26a1"
  46. GEAR="\u2699"
  47. # Begin a segment
  48. # Takes two arguments, background and foreground. Both can be omitted,
  49. # rendering default background/foreground.
  50. prompt_segment() {
  51. local bg fg
  52. [[ -n $1 ]] && bg="%K{$1}" || bg="%k"
  53. [[ -n $2 ]] && fg="%F{$2}" || fg="%f"
  54. if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then
  55. print -n "%{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%}"
  56. else
  57. print -n "%{$bg%}%{$fg%}"
  58. fi
  59. CURRENT_BG=$1
  60. [[ -n $3 ]] && print -n $3
  61. }
  62. # End the prompt, closing any open segments
  63. prompt_end() {
  64. if [[ -n $CURRENT_BG ]]; then
  65. print -n "%{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
  66. else
  67. print -n "%{%k%}"
  68. fi
  69. print -n "%{%f%}"
  70. CURRENT_BG=''
  71. }
  72. ### Prompt components
  73. # Each component will draw itself, and hide itself if no information needs to be shown
  74. # Context: user@hostname (who am I and where am I)
  75. prompt_context() {
  76. local user=`whoami`
  77. if [[ "$user" != "$DEFAULT_USER" || -n "$SSH_CONNECTION" ]]; then
  78. prompt_segment $PRIMARY_FG default " %(!.%{%F{yellow}%}.)$user@%m "
  79. fi
  80. }
  81. # Git: branch/detached head, dirty status
  82. prompt_git() {
  83. local color ref
  84. is_dirty() {
  85. if [[ "${PWD}" == "${HOME}/dotfiles" ]]
  86. then
  87. test -n "$(dot status --porcelain --ignore-submodules)"
  88. else
  89. test -n "$(git status --porcelain --ignore-submodules)"
  90. fi
  91. }
  92. ref="$vcs_info_msg_0_"
  93. if [[ -n "$ref" ]]; then
  94. if is_dirty; then
  95. color=yellow
  96. ref="${ref} $PLUSMINUS"
  97. else
  98. color=green
  99. ref="${ref} "
  100. fi
  101. if [[ "${ref/.../}" == "$ref" ]]; then
  102. ref="$BRANCH $ref"
  103. else
  104. ref="$DETACHED ${ref/.../}"
  105. fi
  106. prompt_segment $color $PRIMARY_FG
  107. print -n " $ref"
  108. fi
  109. }
  110. # Dir: current working directory
  111. prompt_dir() {
  112. prompt_segment blue $PRIMARY_FG ' %~ '
  113. }
  114. # Status:
  115. # - was there an error
  116. # - am I root
  117. # - are there background jobs?
  118. prompt_status() {
  119. local symbols
  120. symbols=()
  121. [[ $RETVAL -ne 0 ]] && symbols+="%{%F{red}%}$CROSS"
  122. [[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}$LIGHTNING"
  123. [[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{cyan}%}$GEAR"
  124. [[ -n "$symbols" ]] && prompt_segment $PRIMARY_FG default " $symbols "
  125. }
  126. # Display current virtual environment
  127. prompt_virtualenv() {
  128. if [[ -n $VIRTUAL_ENV ]]; then
  129. color=cyan
  130. prompt_segment $color $PRIMARY_FG
  131. print -Pn " $(basename $VIRTUAL_ENV) "
  132. fi
  133. }
  134. ## Main prompt
  135. prompt_agnoster_main() {
  136. RETVAL=$?
  137. CURRENT_BG='NONE'
  138. for prompt_segment in "${AGNOSTER_PROMPT_SEGMENTS[@]}"; do
  139. [[ -n $prompt_segment ]] && $prompt_segment
  140. done
  141. }
  142. prompt_agnoster_precmd() {
  143. vcs_info
  144. PROMPT='%{%f%b%k%}$(prompt_agnoster_main) '
  145. }
  146. prompt_agnoster_setup() {
  147. autoload -Uz add-zsh-hook
  148. autoload -Uz vcs_info
  149. prompt_opts=(cr subst percent)
  150. add-zsh-hook precmd prompt_agnoster_precmd
  151. zstyle ':vcs_info:*' enable git
  152. zstyle ':vcs_info:*' check-for-changes false
  153. zstyle ':vcs_info:git*' formats '%b'
  154. zstyle ':vcs_info:git*' actionformats '%b (%a)'
  155. }
  156. prompt_agnoster_setup "$@"