Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- G='\[\e[1;32m\]' # Green
- B='\[\e[1;36m\]' # Cyan (blue)
- Y='\[\e[1;33m\]' # Yellow
- W='\[\e[1;37m\]' # White (bright)
- N='\[\e[0m\]'
- function _git_branch_origin {
- git for-each-ref --format="%(refname:short) %(upstream:short)" refs/heads | \
- while read local remote ; do
- [ -z "$remote" ] && continue
- [ "$1" != "$local" ] && continue
- echo "$remote"
- done
- }
- function _git_branch_status {
- local REMOTE=$(_git_branch_origin $1)
- [ -z "$REMOTE" ] && return
- local DATA=$(git rev-list --left-right "$1"..."$REMOTE" -- 2>/dev/null) || return
- local LEFT_AHEAD=$(grep -c '^<' <<<"$DATA")
- local RIGHT_AHEAD=$(grep -c '^>' <<<"$DATA")
- if [ $LEFT_AHEAD -gt 0 -o $RIGHT_AHEAD -gt 0 ] ; then
- echo -n "("
- [ $LEFT_AHEAD -gt 0 ] && echo -n "+$LEFT_AHEAD"
- [ $RIGHT_AHEAD -gt 0 ] && echo -n "-$RIGHT_AHEAD"
- echo ")"
- fi
- }
- function _gitps {
- local DATA=($(git rev-parse --show-toplevel --abbrev-ref HEAD 2>/dev/null))
- [ -z "$DATA" ] && return 1
- local REPO=$(basename "${DATA[0]}")
- local BRANCH="${DATA[1]}"
- local REPO_PATH="${DATA[0]/c:\///c/}"
- local RELATIVE="$PWD"
- RELATIVE="${RELATIVE/$REPO_PATH/}"
- [ ${#RELATIVE} -le 0 ] && RELATIVE="/"
- local STATUS=$(_git_branch_status $BRANCH)
- export _gitps_repo="$REPO"
- export _gitps_branch="$BRANCH"
- export _gitps_status="$STATUS"
- export _gitps_path="$RELATIVE"
- export _gitps_common="#"
- return 0
- }
- function _normal_ps {
- local P="$PWD"
- [[ "$P" =~ ^"$HOME"(/|$) ]] && P="~${P#$HOME}"
- export _gitps_repo=""
- export _gitps_branch=""
- export _gitps_status=""
- export _gitps_path="$P"
- export _gitps_common="$USER@$HOSTNAME"
- }
- function _choose_ps {
- _gitps || _normal_ps
- }
- export PROMPT_COMMAND=_choose_ps
- C="\[\e[0;38;5;231;48;5;31;1m\]" # White foreground, cyan background
- F="\[\e[0;38;5;50;48;5;31;1m\]" # Green foreground, cyan background
- Y="\[\e[0;33;5;231;48;5;31;1m\]" # Yellow foreground, cyan background
- CG="\[\e[0;38;5;31;48;5;240;22m\]" # Cyan foreground, Gray backround
- G="\[\e[0;38;5;252;48;5;240;1m\]" # Gray background
- GB="\[\e[0;38;5;240;49;22m\]" # Gray foreground, default background
- 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