Created
September 26, 2012 17:17
-
-
Save skizzybiz/3789296 to your computer and use it in GitHub Desktop.
Multiline Bash command prompt with user, host, path, git status, right-aligned timestamp.
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
# Define colors | |
RESET='\[\033[00m\]' | |
FGBK='\[\033[0;30m\]' # foreground black | |
FGRD='\[\033[0;31m\]' # foreground red | |
FGGN='\[\033[0;32m\]' # foreground green | |
FGYL='\[\033[0;33m\]' # foreground yellow | |
FGBL='\[\033[0;34m\]' # foreground blue | |
FGMG='\[\033[0;35m\]' # foreground magenta | |
FGCY='\[\033[0;36m\]' # foreground cyan | |
FGGR='\[\033[0;37m\]' # foreground gray | |
FGBKBD='\[\033[1;30m\]' # foreground black bright | |
FGRDBD='\[\033[1;31m\]' # foreground red bright | |
FGGNBD='\[\033[1;32m\]' # foreground green bright | |
FGYLBD='\[\033[1;33m\]' # foreground yellow bright | |
FGBLBD='\[\033[1;34m\]' # foreground blue bright | |
FGMGBD='\[\033[1;35m\]' # foreground magenta bright | |
FGCYBD='\[\033[1;36m\]' # foreground cyan bright | |
FGGRBD='\[\033[1;37m\]' # foreground gray bright | |
BGBK='\[\033[40m\]' # background black | |
BGRD='\[\033[41m\]' # background red | |
BGGN='\[\033[42m\]' # background green | |
BGYL='\[\033[43m\]' # background yellow | |
BGBL='\[\033[44m\]' # background blue | |
BGMG='\[\033[45m\]' # background magenta | |
BGCY='\[\033[46m\]' # background cyan | |
BGGR='\[\033[47m\]' # background gray | |
# Give the hostname an emphasized color | |
HOSTCOLOR="$FGGRBD" | |
# Consider using a red background for an important server | |
# HOSTCOLOR="$BGRD" | |
# You can also set black text with a yellow background | |
# HOSTCOLOR="$FGBK$BGYL" | |
# Detect whether the current directory is a git repository. | |
function is_git_repository { | |
git branch > /dev/null 2>&1 | |
} | |
# Determine the branch/state information for this git repository. | |
function set_git_branch { | |
# 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 | |
state=$FGGN | |
elif [[ ${git_status} =~ "Changes to be committed" ]]; then | |
state=$FGYLBD | |
else | |
state=$FGYLBD | |
fi | |
# Set arrow icon based on status against remote. | |
remote_pattern="# Your branch is (.*) of" | |
if [[ ${git_status} =~ ${remote_pattern} ]]; then | |
if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then | |
remote="↑" | |
else | |
remote="↓" | |
fi | |
else | |
remote="" | |
fi | |
diverge_pattern="# Your branch and (.*) have diverged" | |
if [[ ${git_status} =~ ${diverge_pattern} ]]; then | |
remote="↕" | |
fi | |
# Get the name of the branch. | |
branch_pattern="^# On branch ([^${IFS}]*)" | |
if [[ ${git_status} =~ ${branch_pattern} ]]; then | |
branch=${BASH_REMATCH[1]} | |
fi | |
# Set the final branch string. | |
BRANCH="${state}[${branch}]${remote}$RESET" | |
} | |
# Return the prompt symbol to use, colorized based on the return value of the | |
# previous command. | |
function set_prompt_symbol () { | |
# echo "set_prompt_symbol" | |
if test $1 -eq 0 ; then | |
PROMPT_SYMBOL="⌘" | |
else | |
PROMPT_SYMBOL="\[\033[0;31m\]⌘\[\033[0m\]" | |
fi | |
} | |
# Set the full bash prompt. | |
function set_bash_prompt () { | |
# echo "set_bash_prompt" | |
# Set the PROMPT_SYMBOL variable. We do this first so we don't lose the | |
# return value of the last command. | |
set_prompt_symbol $? | |
# Set the BRANCH variable. | |
if is_git_repository ; then | |
set_git_branch | |
else | |
BRANCH='' | |
fi | |
# Fill spaces between the left and right halves | |
strippedbranch=`echo $BRANCH | sed 's|\\\\\\[[^]]*\\]||g'` | |
lefthalf="`whoami`@`hostname -s` `pwd | sed "s|$HOME|~|"` $strippedbranch" | |
righthalf=`date '+%a %b %d %T'` | |
let fillsize=${COLUMNS}-${#lefthalf}-${#righthalf}-1 | |
fill=`printf ' %.0s' {1..300}` # 300 spaces | |
fill=${fill:0:$fillsize} | |
# Set the bash prompt variable. | |
PS1="\n$FGGR\u@$HOSTCOLOR\h$RESET $FGGR\w$RESET ${BRANCH}$fill$FGGR\d \T$RESET\n\ | |
${PROMPT_SYMBOL} " | |
} | |
# Tell bash to execute this function just before displaying its prompt. | |
PROMPT_COMMAND=set_bash_prompt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To enable this prompt: