Skip to content

Instantly share code, notes, and snippets.

@rev087
Created June 15, 2014 17:02
Show Gist options
  • Save rev087/4e23de2d0ac17e563179 to your computer and use it in GitHub Desktop.
Save rev087/4e23de2d0ac17e563179 to your computer and use it in GitHub Desktop.
function find_git_branch {
local dir=. head
until [ "$dir" -ef / ]; do
if [ -f "$dir/.git/HEAD" ]; then
head=$(< "$dir/.git/HEAD")
if [[ $head == ref:\ refs/heads/* ]]; then
git_branch="${head#*/*/}"
elif [[ $head != '' ]]; then
git_branch='(detached)'
else
git_branch='(unknown)'
fi
return
fi
dir="../$dir"
done
git_branch=''
}
PROMPT_COMMAND="hipShell; $PROMPT_COMMAND"
hipShell() {
find_git_branch
PS1=""
PS1+="\[$(tput setaf 0)\]\# " # black
PS1+="\[$(tput setaf 7)\]\u" # white, username
PS1+="\[$(tput setaf 0)\]@" # black, string @
PS1+="\[$(tput setaf 7)\]\h" # white, hostname up to first .
PS1+="\[$(tput setaf 0)\]:" # black, string :
PS1+="\[$(tput setaf 4)\]\[$(tput bold)\]\W" # blue, bold, CWD basename
PS1+="\[$(tput sgr0)\] " # clear styles, space
if [[ -n "$git_branch" ]]; then
PS1+="\[$(tput setaf 0)\][" # black, string [
# if [[ -n "$(git status -s 2> /dev/null)" ]]; then # git repo dirty
# PS1+="\[$(tput setaf 1)\]$git_branch" # red, git branch
# else
PS1+="\[$(tput setaf 2)\]$git_branch" # green, git branch
# fi
PS1+="\[$(tput setaf 0)\]]" # Black, string ]
else
PS1+="\[$(tput setaf 0)\]\$" # black, $
fi
PS1+="\[$(tput sgr0)\] " # Reset styles, space
export PS1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment