Created
February 27, 2018 20:44
-
-
Save invisiblefunnel/19b9b8606396e2d30afd3af23511f50c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))\]] ' |
Author
invisiblefunnel
commented
Feb 27, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment