summaryrefslogblamecommitdiffstats
path: root/bashrc/common/prompt.sh
blob: 86f6919ee4030e71ca289bddf49232dab38ab711 (plain) (tree)
1
2
3
4
5




                  























                                                       
                                                                                  

                      
                                        








                                                 
            
                                                                          

          


                                                                                           
                                                                                                              






                                                               
                  
































                                                                                                                    
          
      
 


                            
 

                                                                            
                                                

                                               
 
 
                                     
# command prompt

_load group prompt
_load node  prompt

export PS2="$(pcolor lblue)> $(pcolor)"
export PS3="$(color lblue)#? $(color)"

__make_prompt() {
    local PS1UH PS1D PS1R PS1F PS1P PS1BO PS1BC rc=$? s

    # username + nodename
    if [[ $(id -u) -eq 0 ]]; then
	PS1UH="$(pcolor lred)\h$(pcolor)"
    else
	PS1UH="$(pcolor lgreen)\u@\h$(pcolor)"
    fi

    # working directory
    PS1D="$(pcolor lblue)$(pwd -P)$(pcolor)"

    # return code
    if [[ $rc -eq 0 ]]; then
	PS1R="$(pcolor lgreen)0$(pcolor)"
    else
	PS1R="$(pcolor red)${rc}$(pcolor)"
    fi

    # environment flags
    [[ "${TERM#screen}" != "${TERM}" ]] && PS1F="${PS1F}$(pcolor lcyan)s$(pcolor)"

    # prompt character
    PS1P="$(pcolor lblue)\\\$ $(pcolor)"

    # open/closing brackets
    PS1BO="$(pcolor lgray)[$(pcolor)"
    PS1BC="$(pcolor lgray)]$(pcolor)"

    if [[ -d ".svn" ]] ; then
	local r=
	if hash svnversion >/dev/null 2>&1 ; then
	    r=$(svnversion 2>/dev/null )
	else
	    r=$(svn info | sed -n -e '/^Revision: \([0-9]*\).*$/s//\1/p' )
	fi

	s="$(pcolor yellow)r$r$(pcolor red)$(svn status | grep -q -v '^?' && echo -n "*" )"
    else
	local d=$(git rev-parse --git-dir 2>/dev/null ) b= r= a=
	if [[ -n "${d}" && "${d}" != "." && "${PWD}" == "${PWD##$d}" && ! -f "${d}/../.promptignore" ]] ; then
	    if [[ -d "${d}/../.dotest" ]] ; then
		if [[ -f "${d}/../.dotest/rebase" ]] ; then
		    r="rebase"
		elif [[ -f "${d}/../.dotest/applying" ]] ; then
		    r="am"
		else
		    r="???"
		fi
		b=$(git symbolic-ref HEAD 2>/dev/null )
	    elif [[ -f "${d}/.dotest-merge/interactive" ]] ; then
		r="rebase-i"
		b=$(<${d}/.dotest-merge/head-name)
	    elif [[ -d "${d}/../.dotest-merge" ]] ; then
		r="rebase-m"
		b=$(<${d}/.dotest-merge/head-name)
	    elif [[ -f "${d}/MERGE_HEAD" ]] ; then
		r="merge"
		b=$(git symbolic-ref HEAD 2>/dev/null )
	    elif [[ -f "${d}/BISECT_LOG" ]] ; then
		r="bisect"
		b=$(git symbolic-ref HEAD 2>/dev/null )"???"
	    else
		r=""
		b=$(git symbolic-ref HEAD 2>/dev/null )
	    fi

	    if git status | grep -q '^# Changed but not updated:' ; then
		a="${a}*"
	    fi

	    if git status | grep -q '^# Changes to be committed:' ; then
		a="${a}+"
	    fi

	    if git status | grep -q '^# Untracked files:' ; then
		a="${a}?"
	    fi

	    b=${b#refs/heads/}
	    b=${b// }
	    [[ -n "${r}${b}${a}" ]] && s="$(pcolor lgreen)${r:+${r}:}$(pcolor yellow)${b}$(pcolor red)${a}$(pcolor)"
	fi
    fi

    if [[ -n "${s}" ]]; then
	s="${s} "
    fi

    export PS1="${PS1UH} ${PS1BO}${PS1R}${PS1F}${PS1BC} ${PS1D} ${s}${PS1P}"

    if [[ "${TERM#screen}" != "${TERM}" ]]; then
	echo -ne "\ek$(id -un)@$(hostname)\e\\"
    fi
}

export PROMPT_COMMAND="__make_prompt"