Skip to content

Instantly share code, notes, and snippets.

@paulbaumgart
Forked from boucher/gist:1083564
Created July 15, 2011 01:07
Show Gist options
  • Save paulbaumgart/1083833 to your computer and use it in GitHub Desktop.
Save paulbaumgart/1083833 to your computer and use it in GitHub Desktop.
# ps1
if true; then
OFF="\[\033[0m\]"
BLACK="\[\033[0;30m\]"
RED="\[\033[0;31m\]"
GREEN="\[\033[0;32m\]"
YELLOW="\[\033[0;33m\]"
BLUE="\[\033[0;34m\]"
MAGENTA="\[\033[0;35m\]"
CYAN="\[\033[0;36m\]"
WHITE="\[\033[0;37m\]"
BOLD="\[\033[1m\]"
else
OFF=""
BLACK=""
RED=""
GREEN=""
YELLOW=""
BLUE=""
MAGENTA=""
CYAN=""
WHITE=""
BOLD=""
fi
# __git_ps1 accepts 0 or 1 arguments (i.e., format string)
# returns text to add to bash PS1 prompt (includes branch name)
__git_ps1 ()
{
local g="$(git rev-parse --git-dir 2>/dev/null)"
if [ -n "$g" ]; then
local r
local b
if [ -d "$g/rebase-apply" ]
then
if test -f "$g/rebase-apply/rebasing"
then
r="|REBASE"
elif test -f "$g/rebase-apply/applying"
then
r="|AM"
else
r="|AM/REBASE"
fi
b="$(git symbolic-ref HEAD 2>/dev/null)"
elif [ -f "$g/rebase-merge/interactive" ]
then
r="|REBASE-i"
b="$(cat "$g/rebase-merge/head-name")"
elif [ -d "$g/rebase-merge" ]
then
r="|REBASE-m"
b="$(cat "$g/rebase-merge/head-name")"
elif [ -f "$g/MERGE_HEAD" ]
then
r="|MERGING"
b="$(git symbolic-ref HEAD 2>/dev/null)"
else
if [ -f "$g/BISECT_LOG" ]
then
r="|BISECTING"
fi
if ! b="$(git symbolic-ref HEAD 2>/dev/null)"
then
if ! b="$(git describe --exact-match HEAD 2>/dev/null)"
then
b="$(cut -c1-7 "$g/HEAD")..."
fi
fi
fi
local w
local i
if test -n "$GIT_PS1_SHOWDIRTYSTATE"; then
if test "$(git config --bool bash.showDirtyState)" != "false"; then
git diff --no-ext-diff --ignore-submodules \
--quiet --exit-code || w="*"
if git rev-parse --quiet --verify HEAD >/dev/null; then
git diff-index --cached --quiet \
--ignore-submodules HEAD -- || i="+"
else
i="#"
fi
fi
fi
if [ -n "${1-}" ]; then
printf "$1" "${b##refs/heads/}$w$i$r"
else
printf " (%s)" "${b##refs/heads/}$w$i$r"
fi
fi
}
three_dirs() {
output=""
dirname="$PWD"
for i in {1..3}; do
dir=$(basename "$dirname")
dirname=$(dirname "$dirname")
if [ ! "$dir" = "/" ]; then
output="${output}$(echo ${dir}/ | rev | tr -d '\n')"
else
break
fi
done
if [ "$dirname" = "/" ]; then
output="${output}/"
fi
echo "$output" | rev | tr -d '\n'
}
promptcommand() {
status=$?
ps_pwd="${CYAN}$(three_dirs)${OFF}"
ps_gitbranch=""
ps_stash_depth=""
branch=$(__git_ps1)
if [ "$branch" ]; then
ps_gitbranch="${MAGENTA}$branch${OFF}"
stash_depth=$(git stash list | wc -l | grep -o -E '[0-9]+')
ps_stash_depth=""
if [ "$stash_depth" -ne "0" ]; then
ps_stash_depth="${RED}[${stash_depth}]${OFF}"
fi
fi
ps_prompt="\$"
if [ "$status" -ne "0" ]; then
ps_prompt="${RED}${ps_prompt}${OFF}"
fi
PS1="${GREEN}\u${OFF} ${ps_pwd}${ps_gitbranch}${ps_stash_depth} ${ps_prompt} "
}
PROMPT_COMMAND=promptcommand
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment