Skip to content

Instantly share code, notes, and snippets.

@farkmarnum
Created April 20, 2021 19:07
Show Gist options
  • Save farkmarnum/701d923d722b5bb56cf2bbe98631749e to your computer and use it in GitHub Desktop.
Save farkmarnum/701d923d722b5bb56cf2bbe98631749e to your computer and use it in GitHub Desktop.
Abbreviate git branch and show status with color & emojis
# The various escape codes that we can use to color our prompt.
RED="\[\033[0;31m\]"
YELLOW="\[\033[1;33m\]"
GREEN="\[\033[0;32m\]"
BLUE="\[\033[1;34m\]"
ORANGE="\[\033[38;5;214m\]"
LIGHT_BLUE="\[\033[1;94m\]"
LIGHT_YELLOW="\[\033[0;93m\]"
LIGHT_RED="\[\033[1;31m\]"
LIGHT_GREEN="\[\033[1;32m\]"
WHITE="\[\033[1;37m\]"
LIGHT_GRAY="\[\033[0;37m\]"
BOLD="\[\033[1;4m\]"
COLOR_NONE="\[\e[0m\]"
function is_git_repository {
git branch > /dev/null 2>&1
}
# get current status of git repo
function parse_git_dirty {
PROMPT_SYMBOL="🚀"
BRANCH_COLOR=""
STATUS="$(git status 2> /dev/null)"
if [[ $? -ne 0 ]]; then return; fi
if [[ "$STATUS" == *"is ahead of"* ]]; then PROMPT_SYMBOL="👆 "; fi
if [[ "$STATUS" == *"is behind"* ]]; then PROMPT_SYMBOL="👇 "; fi
if [[ "$STATUS" == *"Untracked files:"* ]]; then BRANCH_COLOR=$RED; fi
if [[ "$STATUS" == *"not staged for commit:"* ]]; then BRANCH_COLOR=$LIGHT_RED; fi
if [[ "$STATUS" == *"to be committed:"* ]]; then BRANCH_COLOR=$ORANGE; fi
if [[ "$STATUS" == *"working tree clean"* ]]; then BRANCH_COLOR=$GREEN; fi
}
shorten () {
cat | sed -E "s/(.{${1-30}}).*$/\1…/"
}
parse_git_branch() {
git rev-parse --abbrev-ref HEAD 2> /dev/null | sed -e 's/ch[0-9]\{1,\}\///' | sed -e 's/feature\//✨ /' | sed -e 's/chore\//🔧 /' | sed -e 's/bug\//🐛 /' | shorten
}
function set_bash_prompt () {
parse_git_dirty;
GIT_INFO="$(parse_git_branch) $(parse_git_dirty)"
VENV=$(echo $VIRTUAL_ENV | sed -e 's/.*\/\(.*\)/\1/')
PS1="${VENV:+($VENV) }\[\e[36m\]\u@\h\[\e[00m\]:\[\e[1;34m\]\w\[\e[00m\] ${COLOR_NONE}${BRANCH_COLOR}${GIT_INFO}${COLOR_NONE}${PROMPT_SYMBOL} "
}
# Tell bash to execute this function just before displaying its prompt.
PROMPT_COMMAND=set_bash_prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment