Skip to content

Instantly share code, notes, and snippets.

@bradjasper
Created November 12, 2013 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradjasper/7436681 to your computer and use it in GitHub Desktop.
Save bradjasper/7436681 to your computer and use it in GitHub Desktop.
# ANSI color codes
RESET="\[\033[0m\]" # reset
HICOLOR="\[\033[1m\]" # hicolor
UNDERLINE="\[\033[4m\]" # underline
INVERSE="\[\033[7m\]" # inverse background and foreground
FBLACK="\[\033[30m\]" # foreground black
FRED="\[\033[31m\]" # foreground red
FGREEN="\[\033[32m\]" # foreground green
FYELLOW="\[\033[33m\]" # foreground yellow
FBLUE="\[\033[34m\]" # foreground blue
FMAGENTA="\[\033[35m\]" # foreground magenta
FCYAN="\[\033[36m\]" # foreground cyan
FWHITE="\[\033[37m\]" # foreground white
BBLACK="\[\033[40m\]" # background black
BRED="\[\033[41m\]" # background red
BGREEN="\[\033[42m\]" # background green
BYELLOW="\[\033[43m\]" # background yellow
BBLUE="\[\033[44m\]" # background blue
BMAGENTA="\[\033[45m\]" # background magenta
BCYAN="\[\033[46m\]" # background cyan
BWHITE="\[\033[47m\]" # background white
function seconds_since_last_commit {
now=`date +%s`
last_commit=`git log --pretty=format:'%at' -1`
echo $((now-last_commit))
}
function minutes_since_last_commit {
seconds_since_last_commit=$(seconds_since_last_commit)
echo $((seconds_since_last_commit/60))
}
# Git prompt components
function time_since_last_commit {
seconds_since_last_commit=$(seconds_since_last_commit)
if [ $seconds_since_last_commit -gt 86400 ]; then
echo -n $((seconds_since_last_commit/86400))
echo "d"
elif [ $seconds_since_last_commit -gt 3600 ]; then
echo -n $((seconds_since_last_commit/3600))
echo "h"
elif [ $seconds_since_last_commit -gt 60 ]; then
echo -n $((seconds_since_last_commit/60))
echo "m"
else
echo -n $seconds_since_last_commit
echo "s"
fi
}
# This code was auto generated by with these options:
# http://andrewray.me/bash-prompt-builder/index.html#git=1&color-git=3&color-git-prefix=3&git-ahead=1&color-git-ahead=6b&git-modified=1&color-git-modified=3&git-conflicted=1&color-git-conflicted=1&color-git-revno=3&git-bisect=1&color-git-bisect=5&color-option-submodule=5&git-ontag=1&color-git-ontag=3&hg=1&color-hg=5&hg-prefix=1&color-hg-prefix=5&hg-modified=1&color-hg-modified=5&hg-conflicted=1&color-hg-conflicted=1&hg-revno=1&color-hg-revno=5&hg-bisect=1&color-hg-bisect=3&hg-patches=1&color-hg-patches=3&svn=1&color-svn=6&svn-modified=1&color-svn-modified=6&svn-revno=1&color-svn-revno=6&option-modified=%E2%96%B3&option-conflict=%E2%98%A2&color-option-conflict=3&max-conflicted-files=2&option-nobranch=no%20branch!&color-option-nobranch=1&bisecting-text=bisecting&submodule-text=%5Bsubmodule%5D%20
MAX_CONFLICTED_FILES=2
DELTA_CHAR="△"
CONFLICT_CHAR="☢"
BISECTING_TEXT="bisecting"
NOBRANCH_TEXT="no branch!"
REBASE_TEXT="✂ ʀebase"
SUBMODULE_TEXT=""
# Colors for prompt
COLOR_RED=$(tput sgr0 && tput setaf 1)
COLOR_GREEN=$(tput sgr0 && tput setaf 2)
COLOR_YELLOW=$(tput sgr0 && tput setaf 3)
COLOR_BLUE=$(tput sgr0 && tput setaf 4)
COLOR_MAGENTA=$(tput sgr0 && tput setaf 5)
COLOR_CYAN=$(tput sgr0 && tput setaf 6)
COLOR_GRAY=$(tput sgr0 && tput setaf 7)
COLOR_WHITE=$(tput sgr0 && tput setaf 7 && tput bold)
COLOR_LIGHTRED=$(tput sgr0 && tput setaf 1 && tput bold)
COLOR_LIGHTGREEN=$(tput sgr0 && tput setaf 2 && tput bold)
COLOR_LIGHTYELLOW=$(tput sgr0 && tput setaf 3 && tput bold)
COLOR_LIGHTBLUE=$(tput sgr0 && tput setaf 4 && tput bold)
COLOR_LIGHTMAGENTA=$(tput sgr0 && tput setaf 5 && tput bold)
COLOR_LIGHTCYAN=$(tput sgr0 && tput setaf 6 && tput bold)
COLOR_RESET=$(tput sgr0)
_git_dir=""
_git_svn_dir=""
function _git_check {
_git_dir=`git rev-parse --show-toplevel 2> /dev/null`
if [[ "$_git_dir" == "" ]]; then
return 1
else
_gsvn_check=`cd "$_git_dir"; ls .git/svn/.metadata 2> /dev/null`
if [[ ! -z "$_gsvn_check" ]]; then
_git_svn_dir=$_git_dir
fi
return 0
fi
}
function is_submodule() {
local parent_git=`cd "$_git_dir/.." && git rev-parse --show-toplevel 2> /dev/null`
if [[ -n $parent_git ]]; then
local submodules=`cd "$parent_git" && git submodule --quiet foreach 'echo $path'`
for line in $submodules; do
cd "$parent_git/$line"
if [[ `pwd` = $_git_dir ]]; then return 0; fi
done
fi
return 1
}
dvcs_function="
# Figure out what repo we are in
_git_check
# Build the prompt!
prompt=\"\"
# If we are in git ...
if [ -n \"\$_git_dir\" ]; then
# find current branch
gitBranch=\$(git symbolic-ref HEAD 2> /dev/null)
gitStatus=\`git status\`
# Figure out if we are rebasing
if [[ -d \"\$_git_dir/.git/rebase-apply\" || -d \"\$_git_dir/.git/rebase-merge\" ]]; then
is_rebase=1
fi
# Figure out current branch, or if we are bisecting, or lost in space
bisecting=\"\"
if [ -z \"\$gitBranch\" ]; then
if [ -n \"\$is_rebase\" ]; then
rebase_prompt=\" \\[\$COLOR_LIGHT_CYAN\\]\"
rebase_prompt=\$rebase_prompt\"\\[`tput sc`\\] \\[`tput rc`\\]\\[\$REBASE_TEXT\\] \"
rebase_prompt=\$rebase_prompt\"\\[\$COLOR_YELLOW\\]\"
else
bisect=\$(git rev-list --bisect 2> /dev/null | cut -c1-7)
if [ -z \"\$bisect\" ]; then
gitBranch=\"\\[\$COLOR_RED\\]\$NOBRANCH_TEXT\\[\$COLOR_YELLOW\\]\"
else
bisecting=\"\\[\$COLOR_MAGENTA\\]\$BISECTING_TEXT:\"\$bisect\"\\[\$COLOR_YELLOW\\]\"
gitBranch=\"\"
fi
fi
fi
gitBranch=\${gitBranch#refs/heads/}
#: git-svn
if [ -z \"\$bisect\" ]; then
if [ -n \"\$_git_svn_dir\" ]; then
gitBranch=\"\\[\$COLOR_DARK_BLUE\\]git-svn\\[\$COLOR_YELLOW\\] \$gitBranch\"
fi
fi
if [ -z \"\$is_rebase\" ]; then
# changed *tracked* files in local directory?
gitChange=\$(echo \$gitStatus | ack 'modified:|deleted:|new file:')
if [ -n \"\$gitChange\" ]; then
gitChange=\"\\[\$COLOR_YELLOW\\] \\[`tput sc`\\] \\[`tput rc`\\]\\[\$DELTA_CHAR\\] \"
fi
fi
# output the branch and changed character if present
prompt=\$prompt\"\\[\$COLOR_YELLOW\\] (\"
if is_submodule; then
prompt=\$prompt\"\\[\$COLOR_MAGENTA\\]\$SUBMODULE_TEXT\\[\$COLOR_YELLOW\\]\"
fi
prompt=\$prompt\$prefix\$gitBranch\$bisecting
tag=\`git describe --tags --exact 2> /dev/null\`
if [ -n \"\$tag\" ]; then
prompt=\"\$prompt\\[\$COLOR_YELLOW\\] \\\"\$tag\\\"\\[\$COLOR_YELLOW\\]\"
fi
prompt=\$prompt\":\$(time_since_last_commit)\$gitChange\\[\$COLOR_YELLOW\\])\\[\$COLOR_RESET\\]\"
# How many local commits do you have ahead of origin?
num=\$(echo \$gitStatus | grep \"Your branch is ahead of\" | awk '{split(\$0,a,\" \"); print a[13];}') || return
if [ -n \"\$num\" ]; then
prompt=\$prompt\"\\[\$COLOR_CYAN\\] +\$num\"
fi
# any conflicts? (sed madness is to remove line breaks)
files=\$(git ls-files -u | cut -f 2 | sort -u | sed '$(($MAX_CONFLICTED_FILES+1)),1000d' | sed -e :a -e '\$!N;s/\\\n/, /;ta' -e 'P;D')
fi
# Show conflicted files if any
if [ -n \"\$files\" ]; then
prompt=\$prompt\" \\[\$COLOR_RED\\](\\[\$COLOR_YELLOW\\]\"
prompt=\$prompt\"\\[`tput sc`\\] \\[`tput rc`\\]\\[\$COLOR_YELLOW\\]\\[\$CONFLICT_CHAR\\] \"
prompt=\$prompt\"\\[\$COLOR_RED\\] \${files})\"
fi
echo \$prompt|tr -d \" \""
export PS1="$FBLUE\h$FWHITE:$FCYAN\w$RESET\$(${dvcs_function})\[$COLOR_RESET\]$>"
# Old PS1's I've tried
#PS1="\$(${dvcs_function})\[$COLOR_RESET\] \$ "
#export PS1="$FBLUE\h$FWHITE:$FCYAN\w$RESET \$(grb_git_prompt)>"
#export PS1='\[\033[G\]\[\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(grb_git_prompt)$> \]'
#export PS1='[\[\033[1;32m\]\D{%Y-%m-%d %H:%M:%S}\[\033[0m\]] \[\033[1;32m\]\u\[\033[0m\]\[\033[1;32m\]@\h\[\033[0m\]:\[\033[1;36m\]\w\[\033[0m\]\n#'
#export PS1='\[\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(grb_git_prompt)$> \]'
#export PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$> '
#export PS1='\[\e[0:35m⌘\e[m \e[0:36m\w/\e[m \e[0:33m`git branch 2> /dev/null | grep -e ^* | sed -E s/^\\\\\*\ \(.+\)$/\(\\\\\1\)\ /`\e[m\]'
#export PS1='\e[0:35m⌘\e[m \e[0:36m\w/\e[m \e[0:33m`git branch 2> /dev/null | grep -e ^* | sed -E s/^\\\\\*\ \(.+\)$/\(\\\\\1\)\ /`\e[m'
#export PS1='\[\e[0:35m⌘\e[m \e[0:36m\w/\e[m \e[0:33m`git branch 2> /dev/null | grep -e ^* | sed -E s/^\\\\\*\ \(.+\)$/\(\\\\\1\)\ /`\e[m\]'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment