Created
February 20, 2013 21:20
-
-
Save jcisio/4999740 to your computer and use it in GitHub Desktop.
Make bash prompt know about git
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
# Colors | |
NoColor="\033[0m" | |
Cyan="\033[0;36m" | |
Green="\033[0;32m" | |
Red="\033[0;31m" | |
Yellow="\033[0;33m" | |
# Chars | |
RootPrompt="\#" | |
NonRootPrompt="\$" | |
# Contextual prompt | |
prompt() { | |
USERNAME=`whoami` | |
HOSTNAME=`hostname -s` | |
#CURRENTPATH=`pwd | sed "s|$HOME|~|g"` | |
# Change the Window title | |
WINDOWTITLE="$USERNAME@$HOSTNAME" | |
echo -ne "\033]0;$WINDOWTITLE\007" | |
# Change the dynamic prompt | |
#LEFTPROMPT="$Yellow$CURRENTPATH" | |
LEFTPROMPT="\[$Cyan\]$USERNAME@$HOSTNAME":"\[$Yellow\]\w" | |
GITSTATUS=$(git status 2> /dev/null) | |
if [ $? -eq 0 ]; then | |
echo $GITSTATUS | grep "not staged" > /dev/null 2>&1 | |
if [ $? -eq 0 ]; then | |
LEFTPROMPT=$LEFTPROMPT"\[$Red\]" | |
else | |
LEFTPROMPT=$LEFTPROMPT"\[$Green\]" | |
fi | |
# BRANCH=`git describe --contains --all HEAD` | |
BRANCH=`git rev-parse --abbrev-ref HEAD` | |
LEFTPROMPT=$LEFTPROMPT" ["$BRANCH"]" | |
fi | |
if [ $EUID -ne 0 ]; then | |
PS1=$LEFTPROMPT"\[$NoColor\] "$NonRootPrompt" " | |
else | |
PS1=$LEFTPROMPT"\[$NoColor\] "$RootPrompt" " | |
fi | |
# echo -e -n $LEFTPROMPT | |
} | |
# Define PROMPT_COMMAND if not already defined (fix: Modifying title on SSH connections) | |
if [ -z "$PROMPT_COMMAND" ]; then | |
case $TERM in | |
xterm*) | |
PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"' | |
;; | |
screen) | |
PROMPT_COMMAND='printf "\033]0;%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"' | |
;; | |
esac | |
fi | |
# Main prompt | |
PROMPT_COMMAND="prompt;$PROMPT_COMMAND" | |
if [ $EUID -ne 0 ]; then | |
PS1=$NonRootPrompt" " | |
else | |
PS1=$RootPrompt" " | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment