powerline-prompt 3.5 KB

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