prompt.zsh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. preexec() {
  76. case "$TERM" in
  77. xterm*|screen*)
  78. # get the command basename
  79. local cmd=${1##*\/}
  80. if [ -n "${SSH_CLIENT}" ]; then
  81. cmd="%n@%M: ${cmd}"
  82. fi
  83. if [ -n "$TMUX" ]; then
  84. print -Pn "\033k\033\134\033k${cmd}\033\134"
  85. else
  86. print -Pn "\e]0;${cmd}\a"
  87. fi
  88. ;;
  89. esac
  90. }
  91. precmd() {
  92. case "$TERM" in
  93. xterm*|screen*)
  94. local cmd="%~"
  95. if [ -n "${SSH_CLIENT}" ]; then
  96. cmd="%n@%M: ${cmd}"
  97. fi
  98. if [ -n "$TMUX" ]; then
  99. print -Pn "\ek%-3 ${cmd}\e"
  100. else
  101. print -Pn "\e]0;${cmd}\a"
  102. fi
  103. ;;
  104. esac
  105. }
  106. myprompt() {
  107. if [ "$TERM" = "dumb" ]; then
  108. PROMPT="-(%n@%m)-(%(!.%d.%~))->"
  109. else
  110. PROMPT='\
  111. %(!.${PR_RED}%n.${PR_LIGHT_GREEN}%n)\
  112. ${PR_WHITE} at \
  113. ${PR_MAGENTA}%m\
  114. ${PR_WHITE}${PR_OS_SSH}\
  115. ${PR_WHITE} in \
  116. ${PR_GREEN}%(!.%d.%~)\
  117. $(pr_git_prompt)\
  118. %1(j.${PR_WHITE} with ${PR_LIGHT_GREEN}%j${PR_WHITE} jobs.)\
  119. ${PR_WHITE} using ${PR_OS_NAME}
  120. %0(?..${PR_RED}%?)\
  121. %(!.${PR_LIGHT_RED}.${PR_LIGHT_CYAN})>${PR_NO_COLOR}'
  122. RPROMPT='$(pr_battery)'
  123. fi
  124. }
  125. myprompt