Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.22 KB | None | 0 0
  1.  
  2.  
  3. G='\[\e[1;32m\]'    # Green
  4. B='\[\e[1;36m\]'    # Cyan (blue)
  5. Y='\[\e[1;33m\]'    # Yellow
  6. W='\[\e[1;37m\]'    # White (bright)
  7. N='\[\e[0m\]'
  8.  
  9. function _git_branch_origin {
  10.     git for-each-ref --format="%(refname:short) %(upstream:short)" refs/heads | \
  11.     while read local remote ; do
  12.         [ -z "$remote" ] && continue
  13.         [ "$1" != "$local" ] && continue
  14.         echo "$remote"
  15.     done
  16. }
  17.  
  18. function _git_branch_status {
  19.     local REMOTE=$(_git_branch_origin $1)
  20.     [ -z "$REMOTE" ] && return
  21.     local DATA=$(git rev-list --left-right "$1"..."$REMOTE" -- 2>/dev/null) || return
  22.     local LEFT_AHEAD=$(grep -c '^<' <<<"$DATA")
  23.     local RIGHT_AHEAD=$(grep -c '^>' <<<"$DATA")
  24.     if [ $LEFT_AHEAD -gt 0 -o $RIGHT_AHEAD -gt 0 ] ; then
  25.         echo -n "("
  26.         [ $LEFT_AHEAD -gt 0 ] && echo -n "+$LEFT_AHEAD"
  27.         [ $RIGHT_AHEAD -gt 0 ] && echo -n "-$RIGHT_AHEAD"
  28.         echo ")"
  29.     fi
  30. }
  31.  
  32. function _gitps {
  33.     local DATA=($(git rev-parse --show-toplevel --abbrev-ref HEAD 2>/dev/null))
  34.     [ -z "$DATA" ] && return 1
  35.     local REPO=$(basename "${DATA[0]}")
  36.     local BRANCH="${DATA[1]}"
  37.     local REPO_PATH="${DATA[0]/c:\///c/}"
  38.     local RELATIVE="$PWD"
  39.     RELATIVE="${RELATIVE/$REPO_PATH/}"
  40.     [ ${#RELATIVE} -le 0 ] && RELATIVE="/"
  41.     local STATUS=$(_git_branch_status $BRANCH)
  42.    
  43.     export _gitps_repo="$REPO"
  44.     export _gitps_branch="$BRANCH"
  45.     export _gitps_status="$STATUS"
  46.     export _gitps_path="$RELATIVE"
  47.     export _gitps_common="#"
  48.     return 0
  49. }
  50.  
  51. function _normal_ps {
  52.     local P="$PWD"
  53.     [[ "$P" =~ ^"$HOME"(/|$) ]] && P="~${P#$HOME}"
  54.    
  55.     export _gitps_repo=""
  56.     export _gitps_branch=""
  57.     export _gitps_status=""
  58.     export _gitps_path="$P"
  59.     export _gitps_common="$USER@$HOSTNAME"
  60. }
  61.  
  62. function  _choose_ps {
  63.     _gitps || _normal_ps
  64. }
  65.  
  66. export PROMPT_COMMAND=_choose_ps
  67.  
  68.  
  69. C="\[\e[0;38;5;231;48;5;31;1m\]"    # White foreground, cyan background
  70. F="\[\e[0;38;5;50;48;5;31;1m\]" # Green foreground, cyan background
  71. Y="\[\e[0;33;5;231;48;5;31;1m\]"    # Yellow foreground, cyan background
  72. CG="\[\e[0;38;5;31;48;5;240;22m\]"  # Cyan foreground, Gray backround
  73. G="\[\e[0;38;5;252;48;5;240;1m\]"   # Gray background
  74. GB="\[\e[0;38;5;240;49;22m\]"   # Gray foreground, default background
  75.  
  76. export PS1="$F \${_gitps_repo}$C\${_gitps_common}\${_gitps_branch}$Y\${_gitps_status}$C $CG$G\${_gitps_path} $GB$N "
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement