agnoster.zsh-theme 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. ### Segment drawing
  2. # A few utility functions to make it easy and re-usable to draw segmented prompts
  3. CURRENT_BG=''
  4. SEGMENT_SEPARATOR='⮀'
  5. function segment_start() {
  6. local bg=$1
  7. local fg=$2
  8. if [[ -n $CURRENT_BG && $bg != $CURRENT_BG ]]; then
  9. echo -n " %{%K{$bg}%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
  10. else
  11. echo -n "%{%K{$bg}%}"
  12. fi
  13. [[ -n $fg ]] && fg="%F{$fg}" || fg="%f"
  14. echo -n "%{$fg%} "
  15. CURRENT_BG=$bg
  16. }
  17. function segment_stop() {
  18. if [[ -n $CURRENT_BG ]]; then
  19. echo -n " %{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
  20. else
  21. echo -n "%{%k%}"
  22. fi
  23. echo -n "%{%f%}"
  24. CURRENT_BG=''
  25. }
  26. ### Prompt components
  27. # Each component will draw itself, and hide itself if no information needs to be shown
  28. function prompt_context() {
  29. local user=`whoami`
  30. if [[ ("$user" != "$DEFAULT_USER") || (-n "$SSH_CLIENT") ]]; then
  31. segment_start black
  32. #echo -n "%{%F{yellow}%}$user%{%F{gray}%}@%{%F{green}%}%m%{%f%}"
  33. echo -n "%(!.%{%F{yellow}%}.)$user@%m"
  34. fi
  35. }
  36. function prompt_git() {
  37. if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
  38. ZSH_THEME_GIT_PROMPT_DIRTY='±'
  39. local dirty=$(parse_git_dirty)
  40. local ref
  41. ref=$(git symbolic-ref HEAD 2> /dev/null) || ref="➦ $(git show-ref --head -s --abbrev |head -n1 2> /dev/null)"
  42. if [[ -n $dirty ]]; then
  43. segment_start yellow black
  44. else
  45. segment_start green black
  46. fi
  47. echo -n "${ref/refs\/heads\//⭠ }$dirty"
  48. fi
  49. }
  50. function prompt_dir() {
  51. segment_start blue white
  52. echo -n '%~'
  53. }
  54. function prompt_status() {
  55. local symbols
  56. symbols=()
  57. [[ $RETVAL -ne 0 ]] && symbols+="%{%F{red}%}✘"
  58. [[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}⚡"
  59. jobs=$(jobs -l | wc -l)
  60. [[ $jobs -gt 0 ]] && symbols+="%{%F{cyan}%}⚙"
  61. if [[ -n "$symbols" ]]; then
  62. segment_start black white
  63. echo -n "${symbols}"
  64. fi
  65. }
  66. ## Main prompt
  67. function build_prompt() {
  68. RETVAL=$?
  69. prompt_status
  70. prompt_context
  71. prompt_dir
  72. prompt_git
  73. segment_stop
  74. }
  75. PROMPT='%{%f%b%k%}
  76. $(build_prompt) '