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.

104 lines
4.4 KiB

3 years ago
  1. # Simple Bash prompt with Git status.
  2. # Source gitstatus.plugin.sh from $GITSTATUS_DIR or from the same directory
  3. # in which the current script resides if the variable isn't set.
  4. if [[ -n "${GITSTATUS_DIR:-}" ]]; then
  5. source "$GITSTATUS_DIR" || return
  6. elif [[ "${BASH_SOURCE[0]}" == */* ]]; then
  7. source "${BASH_SOURCE[0]%/*}/gitstatus.plugin.sh" || return
  8. else
  9. source gitstatus.plugin.sh || return
  10. fi
  11. # Sets GITSTATUS_PROMPT to reflect the state of the current git repository.
  12. # The value is empty if not in a git repository. Forwards all arguments to
  13. # gitstatus_query.
  14. #
  15. # Example value of GITSTATUS_PROMPT: master ⇣42⇡42 ⇠42⇢42 *42 merge ~42 +42 !42 ?42
  16. #
  17. # master current branch
  18. # ⇣42 local branch is 42 commits behind the remote
  19. # ⇡42 local branch is 42 commits ahead of the remote
  20. # ⇠42 local branch is 42 commits behind the push remote
  21. # ⇢42 local branch is 42 commits ahead of the push remote
  22. # *42 42 stashes
  23. # merge merge in progress
  24. # ~42 42 merge conflicts
  25. # +42 42 staged changes
  26. # !42 42 unstaged changes
  27. # ?42 42 untracked files
  28. function gitstatus_prompt_update() {
  29. GITSTATUS_PROMPT=""
  30. gitstatus_query "$@" || return 1 # error
  31. [[ "$VCS_STATUS_RESULT" == ok-sync ]] || return 0 # not a git repo
  32. local reset=$'\001\e[0m\002' # no color
  33. local clean=$'\001\e[38;5;076m\002' # green foreground
  34. local untracked=$'\001\e[38;5;014m\002' # teal foreground
  35. local modified=$'\001\e[38;5;011m\002' # yellow foreground
  36. local conflicted=$'\001\e[38;5;196m\002' # 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+="${reset}#"
  43. where="$VCS_STATUS_TAG"
  44. else
  45. p+="${reset}@"
  46. where="${VCS_STATUS_COMMIT:0:8}"
  47. fi
  48. (( ${#where} > 32 )) && where="${where:0:12}${where: -12}" # truncate long branch names and tags
  49. p+="${clean}${where}"
  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}${reset}"
  73. }
  74. # Start gitstatusd in the background.
  75. gitstatus_stop && gitstatus_start -s -1 -u -1 -c -1 -d -1
  76. # On every prompt, fetch git status and set GITSTATUS_PROMPT.
  77. PROMPT_COMMAND=gitstatus_prompt_update
  78. PROMPT_DIRTRIM=3
  79. # Enable promptvars so that ${GITSTATUS_PROMPT} in PS1 is expanded.
  80. shopt -s promptvars
  81. # Customize prompt. Put $GITSTATUS_PROMPT in it reflect git status.
  82. #
  83. # Example:
  84. #
  85. # user@host ~/projects/skynet master ⇡42
  86. # $ █
  87. PS1='\[\033[01;32m\]\u@\h\[\033[00m\] ' # green user@host
  88. PS1+='\[\033[01;34m\]\w\[\033[00m\]' # blue current working directory
  89. PS1+='${GITSTATUS_PROMPT:+ $GITSTATUS_PROMPT}' # git status (requires promptvars option)
  90. PS1+='\n\[\033[01;$((31+!$?))m\]\$\[\033[00m\] ' # green/red (success/error) $/# (normal/root)
  91. PS1+='\[\e]0;\u@\h: \w\a\]' # terminal title: user@host: dir