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.

174 lines
4.2 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 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. test -n "$(git status --porcelain --ignore-submodules)"
  86. }
  87. ref="$vcs_info_msg_0_"
  88. if [[ -n "$ref" ]]; then
  89. if is_dirty; then
  90. color=yellow
  91. ref="${ref} $PLUSMINUS"
  92. else
  93. color=green
  94. ref="${ref} "
  95. fi
  96. if [[ "${ref/.../}" == "$ref" ]]; then
  97. ref="$BRANCH $ref"
  98. else
  99. ref="$DETACHED ${ref/.../}"
  100. fi
  101. prompt_segment $color $PRIMARY_FG
  102. print -n " $ref"
  103. fi
  104. }
  105. # Dir: current working directory
  106. prompt_dir() {
  107. prompt_segment blue $PRIMARY_FG ' %~ '
  108. }
  109. # Status:
  110. # - was there an error
  111. # - am I root
  112. # - are there background jobs?
  113. prompt_status() {
  114. local symbols
  115. symbols=()
  116. [[ $RETVAL -ne 0 ]] && symbols+="%{%F{red}%}$CROSS"
  117. [[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}$LIGHTNING"
  118. [[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{cyan}%}$GEAR"
  119. [[ -n "$symbols" ]] && prompt_segment $PRIMARY_FG default " $symbols "
  120. }
  121. # Display current virtual environment
  122. prompt_virtualenv() {
  123. if [[ -n $VIRTUAL_ENV ]]; then
  124. color=cyan
  125. prompt_segment $color $PRIMARY_FG
  126. print -Pn " $(basename $VIRTUAL_ENV) "
  127. fi
  128. }
  129. ## Main prompt
  130. prompt_agnoster_main() {
  131. RETVAL=$?
  132. CURRENT_BG='NONE'
  133. for prompt_segment in "${AGNOSTER_PROMPT_SEGMENTS[@]}"; do
  134. [[ -n $prompt_segment ]] && $prompt_segment
  135. done
  136. }
  137. prompt_agnoster_precmd() {
  138. vcs_info
  139. PROMPT='%{%f%b%k%}$(prompt_agnoster_main) '
  140. }
  141. prompt_agnoster_setup() {
  142. autoload -Uz add-zsh-hook
  143. autoload -Uz vcs_info
  144. prompt_opts=(cr subst percent)
  145. add-zsh-hook precmd prompt_agnoster_precmd
  146. zstyle ':vcs_info:*' enable git
  147. zstyle ':vcs_info:*' check-for-changes false
  148. zstyle ':vcs_info:git*' formats '%b'
  149. zstyle ':vcs_info:git*' actionformats '%b (%a)'
  150. }
  151. prompt_agnoster_setup "$@"