1
0

prompt.zsh 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. autoload -U colors && colors
  2. setopt promptsubst
  3. setopt promptpercent
  4. local time='%F{cyan}%B[%D{%H:%M}]%b%f'
  5. local user='%F{yellow}%B%n%b%f'
  6. local host='%F{blue}%B%M%b%f'
  7. local dir='%F{yellow}%~%f'
  8. local git='$(git_prompt_string)'
  9. local nix='$(nix_shell_prompt_string)'
  10. local k8s='$(k8s_prompt_string)'
  11. local right=''
  12. if [ -n "$IN_NIX_SHELL" ]; then
  13. right+="$nix"
  14. fi
  15. if type kubectl > /dev/null; then
  16. right+="$k8s"
  17. fi
  18. local error='%F{red}%B%?%b%f'
  19. PROMPT="$time $user@$host $dir $git%# "
  20. RPROMPT="%(?#$right#$error)"
  21. # Show Git branch/tag, or name-rev if on detached head
  22. parse_git_branch() {
  23. (git symbolic-ref -q HEAD || git rev-parse --short HEAD) 2> /dev/null
  24. }
  25. # Show different symbols as appropriate for various Git repository states
  26. parse_git_state() {
  27. # Compose this value via multiple conditional appends.
  28. local GIT_STATE=""
  29. local GIT_CHANGED="$(git diff --name-only --diff-filter=ACDMRT | wc -l | tr -d ' ')"
  30. local GIT_STAGED="$(git diff --staged --name-status --diff-filter=ACDMRT | wc -l | tr -d ' ')"
  31. local GIT_CONFLICTS="$(git diff --staged --name-status --diff-filter=U | wc -l | tr -d ' ')"
  32. local GIT_UNTRACKED="$(git ls-files --other --exclude-standard | wc -l | tr -d ' ')"
  33. if [ $GIT_STAGED -ne 0 ]; then
  34. GIT_STATE="$GIT_STATE%F{green}+$GIT_STAGED%f"
  35. fi
  36. if [ $GIT_CONFLICTS -ne 0 ]; then
  37. GIT_STATE="$GIT_STATE%F{red}x$GIT_CONFLICTS%f"
  38. fi
  39. if [ $GIT_CHANGED -ne 0 ]; then
  40. GIT_STATE="$GIT_STATE%F{red}*$GIT_CHANGED%f"
  41. fi
  42. if [ $GIT_UNTRACKED -ne 0 ]; then
  43. GIT_STATE="$GIT_STATE.."
  44. fi
  45. if [ $GIT_CHANGED -eq 0 -a $GIT_STAGED -eq 0 -a $GIT_CONFLICTS -eq 0 -a $GIT_UNTRACKED -eq 0 ]; then
  46. GIT_STATE="$GIT_STATE%B%F{green}ok%f%b"
  47. fi
  48. if [[ -n $GIT_STATE ]]; then
  49. echo $GIT_STATE
  50. fi
  51. }
  52. # If inside a Git repository, print its branch and state
  53. git_prompt_string() {
  54. local git_where="$(parse_git_branch)"
  55. if [ -n "$git_where" ]; then
  56. git_where="%B%F{magenta}${git_where#(refs/heads/|tags/)}%f%b"
  57. local NUM_AHEAD="$(git log --oneline @{u}.. 2> /dev/null | wc -l | tr -d ' ')"
  58. if [ "$NUM_AHEAD" -gt 0 ]; then
  59. git_where="$git_where^$NUM_AHEAD"
  60. fi
  61. local NUM_BEHIND="$(git log --oneline ..@{u} 2> /dev/null | wc -l | tr -d ' ')"
  62. if [ "$NUM_BEHIND" -gt 0 ]; then
  63. git_where=$git_where"v$NUM_BEHIND"
  64. fi
  65. echo "($git_where|$(parse_git_state))"
  66. fi
  67. }
  68. nix_shell_prompt_string() {
  69. echo "[%F{yellow}nix%f]"
  70. }
  71. k8s_prompt_string() {
  72. local k8s_context=$(kubectl config current-context)
  73. local k8s_namespace=$(kubectl config get-contexts --no-headers $k8s_context | awk '{ print $5 }')
  74. echo "[%F{magenta}$k8s_context%f|%F{blue}$k8s_namespace%f]"
  75. }