prompt_paradox_setup 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #
  2. # A two-line, Powerline-inspired theme that displays contextual information.
  3. #
  4. # This theme requires a patched Powerline font, get them from
  5. # https://github.com/Lokaltog/powerline-fonts.
  6. #
  7. # Authors:
  8. # Isaac Wolkerstorfer <i@agnoster.net>
  9. # Jeff Sandberg <paradox460@gmail.com>
  10. # Sorin Ionescu <sorin.ionescu@gmail.com>
  11. # Patrick Bos <egpbos@gmail.com>
  12. #
  13. # Screenshots:
  14. # http://i.imgur.com/0XIWX.png
  15. #
  16. # Load dependencies.
  17. pmodload 'helper'
  18. # Define variables.
  19. _prompt_paradox_current_bg='NONE'
  20. _prompt_paradox_segment_separator=''
  21. _prompt_paradox_start_time=$SECONDS
  22. function prompt_paradox_start_segment {
  23. local bg fg
  24. [[ -n "$1" ]] && bg="%K{$1}" || bg="%k"
  25. [[ -n "$2" ]] && fg="%F{$2}" || fg="%f"
  26. if [[ "$_prompt_paradox_current_bg" != 'NONE' && "$1" != "$_prompt_paradox_current_bg" ]]; then
  27. print -n " $bg%F{$_prompt_paradox_current_bg}$_prompt_paradox_segment_separator$fg "
  28. else
  29. print -n "$bg$fg "
  30. fi
  31. _prompt_paradox_current_bg="$1"
  32. [[ -n "$3" ]] && print -n "$3"
  33. }
  34. function prompt_paradox_end_segment {
  35. if [[ -n "$_prompt_paradox_current_bg" ]]; then
  36. print -n " %k%F{$_prompt_paradox_current_bg}$_prompt_paradox_segment_separator"
  37. else
  38. print -n "%k"
  39. fi
  40. print -n "%f"
  41. _prompt_paradox_current_bg=''
  42. }
  43. function prompt_paradox_build_prompt {
  44. prompt_paradox_start_segment black default '%(?::%F{red}✘ )%(!:%F{yellow}⚡ :)%(1j:%F{cyan}⚙ :)%F{blue}%n%F{red}@%F{green}%m%f'
  45. prompt_paradox_start_segment blue black '$_prompt_paradox_pwd'
  46. if [[ -n "$git_info" ]]; then
  47. prompt_paradox_start_segment green black '${(e)git_info[ref]}${(e)git_info[status]}'
  48. fi
  49. if [[ -n "$python_info" ]]; then
  50. prompt_paradox_start_segment white black '${(e)python_info[virtualenv]}'
  51. fi
  52. prompt_paradox_end_segment
  53. }
  54. prompt_paradox_print_elapsed_time() {
  55. local end_time=$(( SECONDS - _prompt_paradox_start_time ))
  56. local hours minutes seconds remainder
  57. if (( end_time >= 3600 )); then
  58. hours=$(( end_time / 3600 ))
  59. remainder=$(( end_time % 3600 ))
  60. minutes=$(( remainder / 60 ))
  61. seconds=$(( remainder % 60 ))
  62. print -P "%B%F{red}>>> elapsed time ${hours}h${minutes}m${seconds}s%b"
  63. elif (( end_time >= 60 )); then
  64. minutes=$(( end_time / 60 ))
  65. seconds=$(( end_time % 60 ))
  66. print -P "%B%F{yellow}>>> elapsed time ${minutes}m${seconds}s%b"
  67. elif (( end_time > 10 )); then
  68. print -P "%B%F{green}>>> elapsed time ${end_time}s%b"
  69. fi
  70. }
  71. function prompt_paradox_precmd {
  72. setopt LOCAL_OPTIONS
  73. unsetopt XTRACE KSH_ARRAYS
  74. # Format PWD.
  75. _prompt_paradox_pwd=$(prompt-pwd)
  76. # Get Git repository information.
  77. if (( $+functions[git-info] )); then
  78. git-info
  79. fi
  80. # Get Python environment information.
  81. if (( $+functions[python-info] )); then
  82. python-info
  83. fi
  84. # Calculate and print the elapsed time.
  85. prompt_paradox_print_elapsed_time
  86. }
  87. function prompt_paradox_preexec {
  88. _prompt_paradox_start_time="$SECONDS"
  89. }
  90. function prompt_paradox_setup {
  91. setopt LOCAL_OPTIONS
  92. unsetopt XTRACE KSH_ARRAYS
  93. prompt_opts=(cr percent sp subst)
  94. # Load required functions.
  95. autoload -Uz add-zsh-hook
  96. # Add hook for calling git-info before each command.
  97. add-zsh-hook preexec prompt_paradox_preexec
  98. add-zsh-hook precmd prompt_paradox_precmd
  99. # Tell prezto we can manage this prompt
  100. zstyle ':prezto:module:prompt' managed 'yes'
  101. # Set editor-info parameters.
  102. zstyle ':prezto:module:editor:info:completing' format '%B%F{red}...%f%b'
  103. zstyle ':prezto:module:editor:info:keymap:primary' format '%B%F{blue}❯%f%b'
  104. zstyle ':prezto:module:editor:info:keymap:primary:overwrite' format '%F{red}♺%f'
  105. zstyle ':prezto:module:editor:info:keymap:alternate' format '%B%F{red}❮%f%b'
  106. # Set git-info parameters.
  107. zstyle ':prezto:module:git:info' verbose 'yes'
  108. zstyle ':prezto:module:git:info:action' format ' ⁝ %s'
  109. zstyle ':prezto:module:git:info:added' format ' ✚'
  110. zstyle ':prezto:module:git:info:ahead' format ' ⬆'
  111. zstyle ':prezto:module:git:info:behind' format ' ⬇'
  112. zstyle ':prezto:module:git:info:branch' format ' %b'
  113. zstyle ':prezto:module:git:info:commit' format '➦ %.7c'
  114. zstyle ':prezto:module:git:info:deleted' format ' ✖'
  115. zstyle ':prezto:module:git:info:dirty' format ' ⁝'
  116. zstyle ':prezto:module:git:info:modified' format ' ✱'
  117. zstyle ':prezto:module:git:info:position' format '%p'
  118. zstyle ':prezto:module:git:info:renamed' format ' ➙'
  119. zstyle ':prezto:module:git:info:stashed' format ' S'
  120. zstyle ':prezto:module:git:info:unmerged' format ' ═'
  121. zstyle ':prezto:module:git:info:untracked' format ' ?'
  122. zstyle ':prezto:module:git:info:keys' format \
  123. 'ref' '$(coalesce "%b" "%p" "%c")' \
  124. 'status' '%s%D%A%B%S%a%d%m%r%U%u'
  125. # %v - virtualenv name.
  126. zstyle ':prezto:module:python:info:virtualenv' format 'virtualenv:%v'
  127. # Define prompts.
  128. PROMPT='
  129. ${(e)$(prompt_paradox_build_prompt)}
  130. ${editor_info[keymap]} '
  131. RPROMPT='%F{blue}[%F{green}%D{%H:%M:%S}%F{blue}]%f'
  132. SPROMPT='zsh: correct %F{red}%R%f to %F{green}%r%f [nyae]? '
  133. }
  134. prompt_paradox_setup "$@"
  135. # vim: ft=zsh