powermy.zsh-theme 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. # vim:ft=zsh ts=2 sw=2 sts=2
  2. #
  3. #
  4. # # README
  5. #
  6. # In order for this theme to render correctly, you will need a
  7. # [Powerline-patched font](https://github.com/Lokaltog/powerline-fonts).
  8. #
  9. #
  10. # # Goals
  11. #
  12. # The aim of this theme is to only show you *relevant* information. Like most
  13. # prompts, it will only show git information when in a git working directory.
  14. # However, it goes a step further: everything from the current user and
  15. # hostname to whether the last call exited with an error to whether background
  16. # jobs are running in this shell will all be displayed automatically when
  17. # appropriate.
  18. ### Segment drawing
  19. # A few utility functions to make it easy and re-usable to draw segmented prompts
  20. PREVIOUS_BG='NONE'
  21. if [[ ( -n "$SSH_CLIENT" || -n "$SSH_TTY" ) ]]; then
  22. SEGMENT_SEPARATOR=''
  23. SEGMENT_R_SEPARATOR=''
  24. else
  25. SEGMENT_SEPARATOR=''
  26. SEGMENT_R_SEPARATOR='\ue0b2'
  27. fi
  28. CONTEXT_FG_COLOR='white'
  29. CONTEXT_BG_COLOR='127'
  30. DIR_FG_COLOR='255'
  31. DIR_BG_COLOR='243'
  32. VIRTUALENV_FG_COLOR='236'
  33. VIRTUALENV_BG_COLOR='green'
  34. HOUR_FG_COLOR='236'
  35. HOUR_BG_COLOR='green'
  36. STATUS_WRONG='red'
  37. STATUS_OK='green'
  38. STATUS_ROOT='yellow'
  39. STATUS_JOBS='cyan'
  40. STATUS_BG_COLOR='236'
  41. STATUS_FG_COLOR='default'
  42. # Begin a segment
  43. # Takes two arguments, background and foreground. Both can be omitted,
  44. # rendering default background/foreground.
  45. # an other argument for bold font can be added
  46. prompt_segment() {
  47. local bg fg
  48. [[ -n $1 ]] && bg="%K{$1}" || bg="%k" # set background
  49. [[ -n $2 ]] && fg="%F{$2}" || fg="%f" # set foreground
  50. [[ -n $2 && -n $4 ]] && fg="$4%F{$2}" || fg="$fg" # if bold argument set foreground
  51. if [[ $PREVIOUS_BG != 'NONE' && $1 != $PREVIOUS_BG ]]; then
  52. echo -n " %{$bg%F{$PREVIOUS_BG}%}$SEGMENT_SEPARATOR%{$fg%} "
  53. else
  54. echo -n "%{$bg%}%{$fg%} "
  55. fi
  56. PREVIOUS_BG=$1
  57. [[ -n $3 ]] && echo -n $3 # write text
  58. [[ -n $2 && -n $4 ]] && echo -n "$4:l%F{$2}$bg" # remove bold tag
  59. }
  60. # End the prompt, closing any open segments
  61. prompt_end() {
  62. if [[ -n $PREVIOUS_BG ]]; then
  63. echo -n " %{%k%F{$PREVIOUS_BG}%}$SEGMENT_SEPARATOR"
  64. else
  65. echo -n "%{%k%}"
  66. fi
  67. echo -n "%{%f%}"
  68. PREVIOUS_BG=''
  69. }
  70. prompt_right_segment() {
  71. local bg fg
  72. [[ -n $1 ]] && bg="%K{$1}" || bg="%k"
  73. [[ -n $2 ]] && fg="%F{$2}" || fg="%f"
  74. if [[ $PREVIOUS_BG != 'NONE' && $1 != $PREVIOUS_BG ]]; then
  75. echo -n " %{%F{$1}%}$SEGMENT_R_SEPARATOR%{$bg$fg%} "
  76. else
  77. echo -n "%{%K{default}%F{$1}%}$SEGMENT_R_SEPARATOR%{$bg$fg%}"
  78. fi
  79. PREVIOUS_BG=$1
  80. [[ -n $3 ]] && echo -n $3
  81. }
  82. ### Prompt components
  83. # Each component will draw itself, and hide itself if no information needs to be shown
  84. # Context: user@hostname (who am I and where am I)
  85. prompt_context() {
  86. local user=`whoami`
  87. if [[ "$user" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
  88. prompt_segment $CONTEXT_BG_COLOR $CONTEXT_FG_COLOR "%(!.%{%F{yellow}%}.)$user@%m" "%B"
  89. fi
  90. }
  91. # Git: branch/detached head, dirty status
  92. prompt_git() {
  93. local ref dirty mode repo_path
  94. repo_path=$(git rev-parse --git-dir 2>/dev/null)
  95. if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
  96. dirty=$(parse_git_dirty)
  97. ref=$(git symbolic-ref HEAD 2> /dev/null) || ref="➦ $(git show-ref --head -s --abbrev |head -n1 2> /dev/null)"
  98. if [[ -n $dirty ]]; then
  99. prompt_segment yellow black
  100. else
  101. prompt_segment green black
  102. fi
  103. if [[ -e "${repo_path}/BISECT_LOG" ]]; then
  104. mode=" <B>"
  105. elif [[ -e "${repo_path}/MERGE_HEAD" ]]; then
  106. mode=" >M<"
  107. elif [[ -e "${repo_path}/rebase" || -e "${repo_path}/rebase-apply" || -e "${repo_path}/rebase-merge" || -e "${repo_path}/../.dotest" ]]; then
  108. mode=" >R>"
  109. fi
  110. setopt promptsubst
  111. autoload -Uz vcs_info
  112. zstyle ':vcs_info:*' enable git
  113. zstyle ':vcs_info:*' get-revision true
  114. zstyle ':vcs_info:*' check-for-changes true
  115. zstyle ':vcs_info:*' stagedstr '✚ '
  116. zstyle ':vcs_info:git:*' unstagedstr '● '
  117. zstyle ':vcs_info:*' formats ' %u%c'
  118. zstyle ':vcs_info:*' actionformats ' %u%c'
  119. vcs_info
  120. echo -n "${ref/refs\/heads\// }${vcs_info_msg_0_%% }${mode}"
  121. fi
  122. }
  123. #Mercurial: status of repository
  124. prompt_hg() {
  125. local rev status
  126. if $(hg id >/dev/null 2>&1); then
  127. if $(hg prompt >/dev/null 2>&1); then
  128. if [[ $(hg prompt "{status|unknown}") = "?" ]]; then
  129. # if files are not added
  130. prompt_segment red white
  131. st='±'
  132. elif [[ -n $(hg prompt "{status|modified}") ]]; then
  133. # if any modification
  134. prompt_segment yellow black
  135. st='±'
  136. else
  137. # if working copy is clean
  138. prompt_segment green black
  139. fi
  140. echo -n $(hg prompt "☿ {rev}@{branch}") $st
  141. else
  142. st=""
  143. branch=$(hg id -b 2>/dev/null)
  144. if `hg st | grep -Eq "^\?"`; then
  145. prompt_segment red black
  146. st='±'
  147. elif `hg st | grep -Eq "^(M|A)"`; then
  148. prompt_segment yellow black
  149. st='±'
  150. else
  151. prompt_segment green black
  152. fi
  153. echo -n "☿ $branch" $st
  154. fi
  155. fi
  156. }
  157. # Dir: current working directory
  158. prompt_dir() {
  159. prompt_segment $DIR_BG_COLOR $DIR_FG_COLOR '%~'
  160. }
  161. # Virtualenv: current working virtualenv
  162. prompt_virtualenv() {
  163. local virtualenv_path="$VIRTUAL_ENV"
  164. if [[ -n $virtualenv_path && -n $VIRTUAL_ENV_DISABLE_PROMPT ]]; then
  165. prompt_segment $VIRTUALENV_BG_COLOR $VIRTUALENV_FG_COLOR "\u24d4 `basename $virtualenv_path`"
  166. fi
  167. }
  168. # Status:
  169. # - was there an error
  170. # - am I root
  171. # - are there background jobs?
  172. prompt_status() {
  173. local symbols
  174. symbols=()
  175. [[ $RETVAL -ne 0 ]] && symbols+="%{%F{$STATUS_WRONG}%}✘"
  176. [[ $UID -eq 0 ]] && symbols+="%{%F{$STATUS_ROOT}%}⚡"
  177. [[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{$STATUS_JOBS}%}⚙"
  178. [[ -n "$symbols" ]] && prompt_segment $STATUS_BG_COLOR $STATUS_FG_COLOR "$symbols"
  179. }
  180. # status for right prompt
  181. # - was good in green
  182. # - was wrong in red with error code
  183. prompt_r_status() {
  184. local symbols
  185. symbols=()
  186. [[ $RETVAL -ne 0 ]] && symbols="%{%F{$STATUS_WRONG}%}\u2718 %? " || symbols="%{%F{$STATUS_OK}%}\u2714 %? "
  187. prompt_right_segment $STATUS_BG_COLOR $STATUS_FG_COLOR "$symbols"
  188. }
  189. ## Main prompt
  190. build_prompt() {
  191. RETVAL=$?
  192. prompt_status
  193. prompt_context
  194. prompt_dir
  195. prompt_git
  196. #prompt_hg
  197. prompt_virtualenv
  198. prompt_end
  199. }
  200. ## Right prompt
  201. build_right_prompt() {
  202. RETVAL=$?
  203. prompt_right_segment $HOUR_BG_COLOR $HOUR_FG_COLOR "%D{%H:%M:%S}"
  204. prompt_r_status
  205. }
  206. PROMPT='%{%f%b%k%}$(build_prompt) '
  207. RPROMPT='%{%f%b%k%}$(build_right_prompt)'