Skip to content

Instantly share code, notes, and snippets.

@invisiblefunnel
Created February 27, 2018 20:44
Show Gist options
  • Save invisiblefunnel/19b9b8606396e2d30afd3af23511f50c to your computer and use it in GitHub Desktop.
Save invisiblefunnel/19b9b8606396e2d30afd3af23511f50c to your computer and use it in GitHub Desktop.
function parse_git_branch {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
function color_git_branch {
# '$1' represents the first variable passed to the script
if [[ "x$1" = "x" ]]; then
echo -e "$1"
else
local status="$(git status 2> /dev/null | tail -n1)"
if [[ $status == "nothing to commit, working tree clean" ]]; then
# Non-printable chars (color escape sequences, etc.) must be escaped
# in order for bash to keep track of the cursor position
# \001 and \002 replace escaped brackets inside of `echo -e`
echo -e '\001\033[0;32m\002'"$1"'\001\033[0m\002' # green + clear
else
echo -e '\001\033[0;31m\002'"$1"'\001\033[0m\002' # red + clear
fi
fi
}
export PS1='$(basename $(pwd))[\[$(color_git_branch $(parse_git_branch))\]] '
@invisiblefunnel
Copy link
Author

screenshot 2018-02-27 12 45 03

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