Skip to content

Instantly share code, notes, and snippets.

@colby
Created April 5, 2016 17:33
Show Gist options
  • Save colby/03132a56a1e909fc2b9742519439ba03 to your computer and use it in GitHub Desktop.
Save colby/03132a56a1e909fc2b9742519439ba03 to your computer and use it in GitHub Desktop.
Display git information in PS1.
export PS1="\n\w\$(stopped_jobs)\$(git_prefix)\$(git_prompt_info)\n\$(last_status)\$ "
# only show status when non zero
function last_status() {
last_status="${STATUS}"
if [ ${last_status} -gt 0 ]; then
echo "${last_status}"
fi
}
# get total amount of stopped jobs
function stopped_jobs() {
job=$(jobs -s | wc -l | sed -e "s/ //g")
if [ ${job} -gt 0 ]; then
echo " ${job}"
else
echo ""
fi
}
# only show '/ <git>' when in a git directory
function git_prefix() {
ISGIT=$(git rev-parse --is-inside-work-tree 2>/dev/null)
if [ ! -z ${ISGIT} ]; then
echo " / "
fi
}
function git_current_branch() {
ref=$(git symbolic-ref HEAD 2>/dev/null)
ref=${ref##refs/heads/}
echo "$ref"
}
function git_prompt_info() {
gitstat=$(git status 2>/dev/null)
if [[ $(echo "${gitstat}" | grep -c "Changes not staged \| Untracked files") -gt 0 ]]; then
GIT_CHAR="${GIT_CHAR}?"
fi
if [[ $(echo "${gitstat}" | grep -c "Changes to be committed") -gt 0 ]]; then
GIT_CHAR="${GIT_CHAR}!"
fi
if [[ $(echo "${gitstat}" | grep -c "nothing to commit") -gt 0 ]]; then
GIT_CHAR=""
fi
if [[ $(echo "${gitstat}" | grep -c "HEAD detached") -gt 0 ]]; then
GIT_CHAR="${GIT_CHAR}%"
fi
if [[ "$(git_current_branch)" ]]; then
echo "$(git_current_branch)$GIT_CHAR"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment