Skip to content

Instantly share code, notes, and snippets.

@piec
Forked from madx/my_prompt.sh
Last active August 29, 2015 13:55
Show Gist options
  • Save piec/8789024 to your computer and use it in GitHub Desktop.
Save piec/8789024 to your computer and use it in GitHub Desktop.
#!/bin/bash
function __hbar
{
eval printf '%.0s$2' {1..$1}
}
function __barre
{
__hbar $(tput cols) ─
}
function __my_git_dirty {
git diff --quiet 2>/dev/null || echo -e "*"
}
function __my_git_sha1 {
sha1="$(git log --no-color -1 --pretty=format:%h 2>/dev/null)"
if [ $? = 0 ]; then echo "$sha1"; fi
}
function __my_git_branch {
str="$(/usr/lib/git-core/git-symbolic-ref HEAD 2>/dev/null)"
if [ $? = 0 ]; then
echo " (${str#refs/heads/}$(__my_git_dirty):$(__my_git_sha1))"
fi
}
function __my_git_wd {
gitdir="$(git rev-parse --git-dir 2>/dev/null)"
if [ $? = 0 ]; then
if [ "$(git config --get core.bare)" = "true" ]; then
echo "$(basename "$(pwd)")"
else
if [ "$gitdir" = ".git" ]; then
echo "$(basename "$PWD")"
elif [ "$gitdir" = "." ]; then
dir=$PWD
while [ "$(basename $dir)" != ".git" ]; do
dir=$(dirname $dir)
done
echo "$(basename $(dirname "$dir")) ${PWD#$(dirname "$dir")/}"
else
echo "${PWD#$(dirname $(dirname "$gitdir"))/}"
fi
fi
else
pwd | sed "s/\/home\//\~/"
fi
}
export PS1='$(__barre)\r\[\e[G\e[1;${PC}m\]$(__my_git_wd)\[\e[0m\]$(__my_git_branch)`if [ $__RET -ne 0 ]; then echo " \e[1;41;37m $__RET \e[0m"; fi` \n↳ '
case "$TERM" in
xterm*|rxvt*|screen*)
PROMPT_COMMAND='__RET=$?;echo -ne "\033]0;$USER@$HOSTNAME: $(__my_git_wd)$(__my_git_branch)\007"'
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment