my xfce4 dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
4.7 KiB

3 years ago
  1. # Simple Zsh prompt with Git status.
  2. # Source gitstatus.plugin.zsh from $GITSTATUS_DIR or from the same directory
  3. # in which the current script resides if the variable isn't set.
  4. source "${GITSTATUS_DIR:-${${(%):-%x}:h}}/gitstatus.plugin.zsh" || return
  5. # Sets GITSTATUS_PROMPT to reflect the state of the current git repository. Empty if not
  6. # in a git repository. In addition, sets GITSTATUS_PROMPT_LEN to the number of columns
  7. # $GITSTATUS_PROMPT will occupy when printed.
  8. #
  9. # Example:
  10. #
  11. # GITSTATUS_PROMPT='master ⇣42⇡42 ⇠42⇢42 *42 merge ~42 +42 !42 ?42'
  12. # GITSTATUS_PROMPT_LEN=39
  13. #
  14. # master current branch
  15. # ⇣42 local branch is 42 commits behind the remote
  16. # ⇡42 local branch is 42 commits ahead of the remote
  17. # ⇠42 local branch is 42 commits behind the push remote
  18. # ⇢42 local branch is 42 commits ahead of the push remote
  19. # *42 42 stashes
  20. # merge merge in progress
  21. # ~42 42 merge conflicts
  22. # +42 42 staged changes
  23. # !42 42 unstaged changes
  24. # ?42 42 untracked files
  25. function gitstatus_prompt_update() {
  26. emulate -L zsh
  27. typeset -g GITSTATUS_PROMPT=''
  28. typeset -gi GITSTATUS_PROMPT_LEN=0
  29. # Call gitstatus_query synchronously. Note that gitstatus_query can also be called
  30. # asynchronously; see documentation in gitstatus.plugin.zsh.
  31. gitstatus_query 'MY' || return 1 # error
  32. [[ $VCS_STATUS_RESULT == 'ok-sync' ]] || return 0 # not a git repo
  33. local clean='%76F' # green foreground
  34. local modified='%178F' # yellow foreground
  35. local untracked='%39F' # blue foreground
  36. local conflicted='%196F' # red foreground
  37. local p
  38. local where # branch name, tag or commit
  39. if [[ -n $VCS_STATUS_LOCAL_BRANCH ]]; then
  40. where=$VCS_STATUS_LOCAL_BRANCH
  41. elif [[ -n $VCS_STATUS_TAG ]]; then
  42. p+='%f#'
  43. where=$VCS_STATUS_TAG
  44. else
  45. p+='%f@'
  46. where=${VCS_STATUS_COMMIT[1,8]}
  47. fi
  48. (( $#where > 32 )) && where[13,-13]="…" # truncate long branch names and tags
  49. p+="${clean}${where//\%/%%}" # escape %
  50. # ⇣42 if behind the remote.
  51. (( VCS_STATUS_COMMITS_BEHIND )) && p+=" ${clean}${VCS_STATUS_COMMITS_BEHIND}"
  52. # ⇡42 if ahead of the remote; no leading space if also behind the remote: ⇣42⇡42.
  53. (( VCS_STATUS_COMMITS_AHEAD && !VCS_STATUS_COMMITS_BEHIND )) && p+=" "
  54. (( VCS_STATUS_COMMITS_AHEAD )) && p+="${clean}${VCS_STATUS_COMMITS_AHEAD}"
  55. # ⇠42 if behind the push remote.
  56. (( VCS_STATUS_PUSH_COMMITS_BEHIND )) && p+=" ${clean}${VCS_STATUS_PUSH_COMMITS_BEHIND}"
  57. (( VCS_STATUS_PUSH_COMMITS_AHEAD && !VCS_STATUS_PUSH_COMMITS_BEHIND )) && p+=" "
  58. # ⇢42 if ahead of the push remote; no leading space if also behind: ⇠42⇢42.
  59. (( VCS_STATUS_PUSH_COMMITS_AHEAD )) && p+="${clean}${VCS_STATUS_PUSH_COMMITS_AHEAD}"
  60. # *42 if have stashes.
  61. (( VCS_STATUS_STASHES )) && p+=" ${clean}*${VCS_STATUS_STASHES}"
  62. # 'merge' if the repo is in an unusual state.
  63. [[ -n $VCS_STATUS_ACTION ]] && p+=" ${conflicted}${VCS_STATUS_ACTION}"
  64. # ~42 if have merge conflicts.
  65. (( VCS_STATUS_NUM_CONFLICTED )) && p+=" ${conflicted}~${VCS_STATUS_NUM_CONFLICTED}"
  66. # +42 if have staged changes.
  67. (( VCS_STATUS_NUM_STAGED )) && p+=" ${modified}+${VCS_STATUS_NUM_STAGED}"
  68. # !42 if have unstaged changes.
  69. (( VCS_STATUS_NUM_UNSTAGED )) && p+=" ${modified}!${VCS_STATUS_NUM_UNSTAGED}"
  70. # ?42 if have untracked files. It's really a question mark, your font isn't broken.
  71. (( VCS_STATUS_NUM_UNTRACKED )) && p+=" ${untracked}?${VCS_STATUS_NUM_UNTRACKED}"
  72. GITSTATUS_PROMPT="${p}%f"
  73. # The length of GITSTATUS_PROMPT after removing %f and %F.
  74. GITSTATUS_PROMPT_LEN="${(m)#${${GITSTATUS_PROMPT//\%\%/x}//\%(f|<->F)}}"
  75. }
  76. # Start gitstatusd instance with name "MY". The same name is passed to
  77. # gitstatus_query in gitstatus_prompt_update. The flags with -1 as values
  78. # enable staged, unstaged, conflicted and untracked counters.
  79. gitstatus_stop 'MY' && gitstatus_start -s -1 -u -1 -c -1 -d -1 'MY'
  80. # On every prompt, fetch git status and set GITSTATUS_PROMPT.
  81. autoload -Uz add-zsh-hook
  82. add-zsh-hook precmd gitstatus_prompt_update
  83. # Enable/disable the right prompt options.
  84. setopt no_prompt_bang prompt_percent prompt_subst
  85. # Customize prompt. Put $GITSTATUS_PROMPT in it to reflect git status.
  86. #
  87. # Example:
  88. #
  89. # user@host ~/projects/skynet master ⇡42
  90. # % █
  91. #
  92. # The current directory gets truncated from the left if the whole prompt doesn't fit on the line.
  93. PROMPT='%70F%n@%m%f ' # green user@host
  94. PROMPT+='%39F%$((-GITSTATUS_PROMPT_LEN-1))<…<%~%<<%f' # blue current working directory
  95. PROMPT+='${GITSTATUS_PROMPT:+ $GITSTATUS_PROMPT}' # git status
  96. PROMPT+=$'\n' # new line
  97. PROMPT+='%F{%(?.76.196)}%#%f ' # %/# (normal/root); green/red (ok/error)