magicmace.zsh-theme 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # vim:et sts=2 sw=2 ft=zsh
  2. #
  3. # magicmace theme
  4. # Ideas and code taken from:
  5. # xero's zsh prompt <http://code.xero.nu/dotfiles>
  6. # eriner's eriner prompt <https://github.com/zimfw/zimfw/blob/master/modules/prompt/themes/eriner.zsh-theme>
  7. #
  8. # Requires the `git-info` zmodule to be included in the .zimrc file.
  9. # Global variables
  10. function {
  11. COLOR_ROOT="%F{red}"
  12. COLOR_USER="%F{cyan}"
  13. COLOR_NORMAL="%F{white}"
  14. COLOR_ERROR="%F{red}"
  15. if (( ${EUID} )); then
  16. COLOR_USER_LEVEL=${COLOR_USER}
  17. else
  18. COLOR_USER_LEVEL=${COLOR_ROOT}
  19. fi
  20. }
  21. # Status:
  22. # - was there an error?
  23. # - are there background jobs?
  24. # - are we in a ranger session?
  25. prompt_magicmace_status() {
  26. local symbols=""
  27. (( ${RETVAL} )) && symbols+="${COLOR_ERROR}${RETVAL}${COLOR_NORMAL}" # $? for error.
  28. (( $(jobs -l | wc -l) > 0 )) && symbols+='b' # 'b' for background.
  29. (( ${RANGER_LEVEL} )) && symbols+='r' # 'r' for... you guessed it!
  30. [[ -n ${symbols} ]] && print -n "─${COLOR_NORMAL}${symbols}${COLOR_USER_LEVEL}─"
  31. }
  32. prompt_magicmace_git() {
  33. [[ -n ${git_info} ]] && print -n "${(e)git_info[prompt]}"
  34. }
  35. prompt_magicmace_precmd() {
  36. # While it would be apt to have this as a local variable in prompt_status(),
  37. # $? (returned value) and ${(%):-%?} ("The return status of the last command
  38. # executed just before the prompt") both change before executing the function.
  39. # Is this perhaps because prompt_status _is_ here?
  40. # We could also just set $? as an argument, and thus get our nifty local variable,
  41. # but that's stretching it, and makes the code harder to read.
  42. RETVAL=$?
  43. (( ${+functions[git-info]} )) && git-info
  44. }
  45. prompt_magicmace_setup() {
  46. autoload -Uz add-zsh-hook && add-zsh-hook precmd prompt_magicmace_precmd
  47. autoload -Uz colors && colors
  48. prompt_opts=(cr percent sp subst)
  49. zstyle ':zim:git-info:branch' format '%b'
  50. zstyle ':zim:git-info:commit' format '%c...'
  51. zstyle ':zim:git-info:dirty' format '*'
  52. zstyle ':zim:git-info:ahead' format '↑'
  53. zstyle ':zim:git-info:behind' format '↓'
  54. zstyle ':zim:git-info:keys' format \
  55. 'prompt' '─[${COLOR_NORMAL}%b%c%D%A%B${COLOR_USER_LEVEL}]'
  56. # Call git directly, ignoring aliases under the same name.
  57. PS1='${COLOR_USER_LEVEL}$(prompt_magicmace_status)[${COLOR_NORMAL}$(short_pwd)${COLOR_USER_LEVEL}]$(prompt_magicmace_git)── ─%f '
  58. RPS1=''
  59. }
  60. prompt_magicmace_setup "${@}"