prompt.zsh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #
  2. # Prompt config
  3. #
  4. setopt prompt_subst
  5. # enable colors
  6. autoload colors zsh/terminfo
  7. if [ "$terminfo[colors]" -ge 8 ]; then
  8. colors
  9. fi
  10. for color in RED GREEN YELLOW BLUE MAGENTA CYAN ORANGE WHITE; do
  11. eval PR_$color='%{$terminfo[bold]$fg[${(L)color}]%}'
  12. eval PR_LIGHT_$color='%{$fg[${(L)color}]%}'
  13. done
  14. PR_NO_COLOR="%{$terminfo[sgr0]%}"
  15. export PR_OS_NAME=$(uname -s)
  16. if [ -f /etc/debian_version ]; then
  17. if which lsb_release >/dev/null 2>&1 ; then
  18. PR_OS_NAME="${PR_BLUE}$(lsb_release -is)${PR_WHITE}/${PR_BLUE}$(lsb_release -cs)"
  19. fi
  20. if [ -f /etc/debian_chroot ]; then
  21. PR_OS_NAME="${PR_RED}chroot${PR_WHITE}(${PR_OS_NAME}${PR_WHITE} ${PR_MAGENTA}$(uname -m)${PR_WHITE})"
  22. fi
  23. fi
  24. if [ -n "${SSH_CLIENT}" ]; then
  25. export PR_OS_SSH=" via ${PR_LIGHT_RED}ssh"
  26. else
  27. export PR_OS_SSH=
  28. fi
  29. pr_battery_level() {
  30. if command -v acpi >/dev/null 2>&1 ; then
  31. acpi --battery 2>/dev/null | sed -r 's/^Battery .*, ([0-9]*)%.*$/\1/'
  32. fi
  33. }
  34. pr_battery() {
  35. local battery_level=
  36. local level=$(pr_battery_level)
  37. if [ -n "$level" ] ; then
  38. if [ "$level" -gt 60 ] ; then
  39. local bat_color="${PR_GREEN}"
  40. elif [ "$level" -gt 25 ] ; then
  41. local bat_color="${PR_YELLOW}"
  42. else
  43. local bat_color="${PR_RED}"
  44. fi
  45. battery_level="${PR_WHITE}⌁${bat_color}${level}${PR_WHITE}%%"
  46. fi
  47. echo -n "${battery_level}"
  48. }
  49. pr_git_branch() {
  50. if command -v git >/dev/null 2>&1 ; then
  51. git branch 2>/dev/null | sed -n 's/^\* //p'
  52. fi
  53. }
  54. pr_git_prompt() {
  55. local git_prompt=
  56. local branch=$(pr_git_branch)
  57. if [ -n "$branch" ] ; then
  58. local modified=$(git diff --name-only | wc -l)
  59. local cached=$(git diff --cached --name-only | wc -l)
  60. git_prompt="${PR_YELLOW}${branch}${PR_NO_COLOR}"
  61. if [ "$modified" -gt 0 ]; then
  62. git_prompt="${git_prompt} ${PR_RED}${modified}${PR_NO_COLOR}±"
  63. fi
  64. if [ "$cached" -gt 0 ]; then
  65. git_prompt="${git_prompt} ${PR_LIGHT_GREEN}${cached}${PR_NO_COLOR}±"
  66. fi
  67. local commits=$(git rev-list --no-merges --count origin/${branch}..${branch} 2>/dev/null)
  68. if [ "$commits" -gt 0 ]; then
  69. git_prompt="${git_prompt} ${PR_MAGENTA}${commits}${PR_NO_COLOR}↑"
  70. fi
  71. git_prompt="${PR_WHITE} on ${git_prompt}"
  72. fi
  73. echo -n "${git_prompt}"
  74. }
  75. pr_update_tmux_env() {
  76. if [ -n "$TMUX" ] && command -v tmux >/dev/null 2>&1 ; then
  77. source <(tmux show-environment | sed -r -e '/^-/d' -e 's/^([^=]+)=(.*)$/\1="\2"/')
  78. fi
  79. }
  80. preexec() {
  81. case "$TERM" in
  82. xterm*|screen*)
  83. # get the command basename
  84. local cmd=${1##*\/}
  85. if [ -n "${SSH_CLIENT}" ]; then
  86. cmd="%n@%M: ${cmd}"
  87. fi
  88. if [ -n "$TMUX" ]; then
  89. print -Pn "\033k\033\134\033k${cmd}\033\134"
  90. else
  91. print -Pn "\e]0;${cmd}\a"
  92. fi
  93. ;;
  94. esac
  95. pr_update_tmux_env
  96. }
  97. precmd() {
  98. case "$TERM" in
  99. xterm*|screen*)
  100. local cmd="%~"
  101. if [ -n "${SSH_CLIENT}" ]; then
  102. cmd="%n@%M: ${cmd}"
  103. fi
  104. if [ -n "$TMUX" ]; then
  105. print -Pn "\ek%-3 ${cmd}\e"
  106. else
  107. print -Pn "\e]0;${cmd}\a"
  108. fi
  109. ;;
  110. esac
  111. }
  112. myprompt() {
  113. if [ "$TERM" = "dumb" ]; then
  114. PROMPT="-(%n@%m)-(%(!.%d.%~))->"
  115. else
  116. PROMPT='\
  117. %(!.${PR_RED}%n.${PR_LIGHT_GREEN}%n)\
  118. ${PR_WHITE} at \
  119. ${PR_MAGENTA}%m\
  120. ${PR_WHITE}${PR_OS_SSH}\
  121. ${PR_WHITE} in \
  122. ${PR_GREEN}%(!.%d.%~)\
  123. $(pr_git_prompt)\
  124. %1(j.${PR_WHITE} with ${PR_LIGHT_GREEN}%j${PR_WHITE} jobs.)\
  125. ${PR_WHITE} using ${PR_OS_NAME}
  126. %0(?..${PR_RED}%?)\
  127. %(!.${PR_LIGHT_RED}.${PR_LIGHT_CYAN})>${PR_NO_COLOR}'
  128. RPROMPT='$(pr_battery)'
  129. fi
  130. }
  131. myprompt