Skip to content

Instantly share code, notes, and snippets.

@EECOLOR
Last active May 8, 2020 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EECOLOR/ce0a187f83de9f11271bea69844abfb2 to your computer and use it in GitHub Desktop.
Save EECOLOR/ce0a187f83de9f11271bea69844abfb2 to your computer and use it in GitHub Desktop.
.git_sit_awareness.sh
#!/usr/bin/env bash
PROMPT_GREEN='\001'`tput setaf 2`'\002'
PROMPT_PINK='\001'`tput setaf 5`'\002'
PROMPT_PLAIN='\001'`tput op`'\002'
function in_git_repo {
[ -d .git ] || git rev-parse --git-dir > /dev/null 2>&1
}
function git_changed {
if in_git_repo; then
local result=""
local status="$(git status --porcelain)"
if [ "$(echo "$status" | grep ^M)" ]; then
result="$result+"
fi
if [ "$(echo "$status" | grep ^.M)" ]; then
result="$result*"
fi
if [ "$(echo "$status" | grep ^\?\?)" ]; then
result="$result~"
fi
if [ "$(echo "$status" | grep -E '^(D|.D)')" ]; then
result="$result-"
fi
echo "$result"
fi
}
function git_branch {
if in_git_repo; then
branch=$(git branch | grep "*" | cut -d ' ' -f 2-3)
if [ "$branch" = "" ]; then
branch=$(git status | grep "# On branch" | cut -d ' ' -f 4-5)
fi
echo $branch
fi
}
function git_branch_separator {
if in_git_repo; then
echo ":"
fi
}
function git_stash_height {
if in_git_repo; then
stash_height="$(git stash list | wc -l | awk '{ print $1 }')"
if [ $stash_height = "0" ]; then
stash_height=""
fi
echo "$stash_height"
fi
}
function git_stash_height_separator {
if [ "$(git_stash_height)" ]; then
echo ":"
fi
}
function base_prompt {
local base='${debian_chroot:+($debian_chroot)}\001\033[01;32m\002\u'
if [ "$SSH_CONNECTION" ]; then
base="$base@\h"
fi
echo "$base\001\033[00m\002:"
}
export HAVE_GIT_SIT_AWARENESS=1
# tweak this as you see fit
export PS1="$(base_prompt)\001\e[1;34m\002\w\001\e[0m\002\$(git_branch_separator)${PROMPT_PINK}\$(git_changed)${PROMPT_GREEN}\$(git_branch)${PROMPT_PLAIN}\$(git_stash_height_separator)${PROMPT_PINK}\$(git_stash_height)${PROMPT_PLAIN}\$ "
@EECOLOR
Copy link
Author

EECOLOR commented May 8, 2020

Add the following to .bashrc:

. ~/.git_sit_awareness.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment