powerline 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env bash
  2. ## Uncomment to disable git info
  3. #POWERLINE_GIT=0
  4. __powerline() {
  5. # Colors
  6. COLOR_RESET='\[\033[m\]'
  7. COLOR_CWD=${COLOR_CWD:-'\[\033[0;34m\]'} # blue
  8. COLOR_GIT=${COLOR_GIT:-'\[\033[0;36m\]'} # cyan
  9. COLOR_SUCCESS=${COLOR_SUCCESS:-'\[\033[0;32m\]'} # green
  10. COLOR_FAILURE=${COLOR_FAILURE:-'\[\033[0;31m\]'} # red
  11. # Symbols
  12. SYMBOL_GIT_BRANCH=${SYMBOL_GIT_BRANCH:-⑂}
  13. SYMBOL_GIT_MODIFIED=${SYMBOL_GIT_MODIFIED:-*}
  14. SYMBOL_GIT_PUSH=${SYMBOL_GIT_PUSH:-↑}
  15. SYMBOL_GIT_PULL=${SYMBOL_GIT_PULL:-↓}
  16. if [[ -z "$PS_SYMBOL" ]]; then
  17. case "$(uname)" in
  18. Darwin) PS_SYMBOL='';;
  19. Linux) PS_SYMBOL='$';;
  20. *) PS_SYMBOL='%';;
  21. esac
  22. fi
  23. __git_info() {
  24. [[ $POWERLINE_GIT = 0 ]] && return # disabled
  25. hash git 2>/dev/null || return # git not found
  26. local git_eng="env LANG=C git" # force git output in English to make our work easier
  27. # get current branch name
  28. local ref=$($git_eng symbolic-ref --short HEAD 2>/dev/null)
  29. if [[ -n "$ref" ]]; then
  30. # prepend branch symbol
  31. ref=$SYMBOL_GIT_BRANCH$ref
  32. else
  33. # get tag name or short unique hash
  34. ref=$($git_eng describe --tags --always 2>/dev/null)
  35. fi
  36. [[ -n "$ref" ]] || return # not a git repo
  37. local marks
  38. # scan first two lines of output from `git status`
  39. while IFS= read -r line; do
  40. if [[ $line =~ ^## ]]; then # header line
  41. [[ $line =~ ahead\ ([0-9]+) ]] && marks+=" $SYMBOL_GIT_PUSH${BASH_REMATCH[1]}"
  42. [[ $line =~ behind\ ([0-9]+) ]] && marks+=" $SYMBOL_GIT_PULL${BASH_REMATCH[1]}"
  43. else # branch is modified if output contains more lines after the header line
  44. marks="$SYMBOL_GIT_MODIFIED$marks"
  45. break
  46. fi
  47. done < <($git_eng status --porcelain --branch 2>/dev/null) # note the space between the two <
  48. # print the git branch segment without a trailing newline
  49. printf " $ref$marks"
  50. }
  51. ps1() {
  52. # Check the exit code of the previous command and display different
  53. # colors in the prompt accordingly.
  54. if [ $? -eq 0 ]; then
  55. local symbol="$COLOR_SUCCESS $PS_SYMBOL $COLOR_RESET"
  56. else
  57. local symbol="$COLOR_FAILURE $PS_SYMBOL $COLOR_RESET"
  58. fi
  59. local cwd="$COLOR_CWD\w$COLOR_RESET"
  60. # Bash by default expands the content of PS1 unless promptvars is disabled.
  61. # We must use another layer of reference to prevent expanding any user
  62. # provided strings, which would cause security issues.
  63. # POC: https://github.com/njhartwell/pw3nage
  64. # Related fix in git-bash: https://github.com/git/git/blob/9d77b0405ce6b471cb5ce3a904368fc25e55643d/contrib/completion/git-prompt.sh#L324
  65. if shopt -q promptvars; then
  66. __powerline_git_info="$(__git_info)"
  67. local git="$COLOR_GIT\${__powerline_git_info}$COLOR_RESET"
  68. else
  69. # promptvars is disabled. Avoid creating unnecessary env var.
  70. local git="$COLOR_GIT$(__git_info)$COLOR_RESET"
  71. fi
  72. PS1="$cwd$git$symbol"
  73. }
  74. PROMPT_COMMAND="ps1${PROMPT_COMMAND:+; $PROMPT_COMMAND}"
  75. }
  76. __powerline
  77. unset __powerline