Last active
October 11, 2015 17:57
-
-
Save britishtea/3896878 to your computer and use it in GitHub Desktop.
My bash prompt
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
# This bash prompt is loosely based on https://gist.github.com/31967 | |
red=$(tput setaf 1) | |
yellow=$(tput setaf 3) | |
green=$(tput setaf 2) | |
blue=$(tput setaf 6) | |
bold=$(tput bold) | |
reset=$(tput sgr0) | |
# Detects whether the current directory is a git repository | |
is_git_repository() { | |
git branch > /dev/null 2>&1 | |
} | |
# Public: Gets the name of the currently checkout out branch and colorizes it | |
colorized_git_branch () { | |
# Get the name of the currectly checked out branch | |
git_branch=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD) | |
# Capture the output of the "git status" command | |
git_status="$(git status 2> /dev/null)" | |
# Set color based on clean/staged/dirty. | |
if [[ ${git_status} =~ "working directory clean" ]]; then | |
branch_color="${green}" | |
# elif [[ ${git_status} =~ "Changes to be committed" ]]; then | |
# branch_color="${blue}" | |
else | |
branch_color="${red}" | |
fi | |
echo "\[${bold}\]\[${branch_color}\]${git_branch}\[${reset}\]" | |
} | |
# Sets the bash prompt | |
set_bash_prompt() { | |
if is_git_repository; then | |
PS1="\W:$(colorized_git_branch) \$ " | |
else | |
PS1="\W \$ " | |
fi | |
} | |
PROMPT_COMMAND=set_bash_prompt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment