Skip to content

Instantly share code, notes, and snippets.

@iiska
Forked from henrik/.bashrc
Created January 15, 2009 13:37
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 iiska/47408 to your computer and use it in GitHub Desktop.
Save iiska/47408 to your computer and use it in GitHub Desktop.
# .bashrc
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[ -z "$PS1" ] && return
# http://en.tldp.org/HOWTO/Bash-Prompt-HOWTO/x869.html
function prompt_command {
hostnam=$(hostname -s)
usernam=$(whoami)
#returnvalue=$(([ $? -eq 0 ] && echo "\[\033[0;32m\]"☺) || echo "\[\033[0;31m\]"☹)
# Find the width of the prompt:
TERMWIDTH=${COLUMNS}
# Add all the accessories below ...
local temp="--(${usernam}@${hostnam})(${PWD/#$HOME/~}$(parse_git_branch 1))--"
let fillsize=${TERMWIDTH}-${#temp}
if [ "$fillsize" -gt "0" ]
then
fill="────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────"
# It's theoretically possible someone could need more
# dashes than above, but very unlikely! HOWTO users,
# the above should be ONE LINE, it may not cut and
# paste properly
fill="${fill:0:${fillsize}}"
fi
}
PROMPT_COMMAND=prompt_command
# http://henrik.nyh.se/2008/12/git-dirty-prompt
function parse_git_dirty {
local ret
if [ -z $1 ]; then
ret="\033[1;31m*\033[1;37m"
else
ret="*"
fi
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo -e $ret
}
# http://www.simplisticcomplexity.com/2008/03/13/show-your-git-branch-name-in-your-prompt/
function parse_git_branch {
ref=$(git-symbolic-ref HEAD 2> /dev/null) || return
echo " : "${ref#refs/heads/}"$(parse_git_dirty $1)"
}
function create_prompt {
local BLUE="\[\033[0;34m\]"
local RED="\[\033[0;31m\]"
local LIGHT_RED="\[\033[1;31m\]"
local GREEN="\[\033[0;32m\]"
local LIGHT_GREEN="\[\033[1;32m\]"
local WHITE="\[\033[1;37m\]"
local LIGHT_GRAY="\[\033[0;37m\]"
echo "$LIGHT_GRAY┌─($WHITE\u@\h$LIGHT_GRAY)\${fill}($WHITE\w\$(parse_git_branch)$LIGHT_GRAY)─┈"
echo "└─(\$(date +%H:%M)) $LIGHT_GRAY$ "
}
export PS1=$(create_prompt)
@iiska
Copy link
Author

iiska commented Apr 9, 2010

This is obsolete and inefficient way to parse current git branch. Recommended way is to use function __git_ps1 from git's bash completions. Like this:

export GIT_PS1_SHOWDIRTYSTATE=1
PS1='\u@\h:\w$(__git_ps1)\$ '

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment