powermy.zsh-theme 6.4 KB

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