summaryrefslogblamecommitdiffstats
path: root/bashrc/common/prompt.sh
blob: 0c14d84357f266e09f2b148ff8a73d108868beb4 (plain) (tree)
1
2
3
4
5
6
7
8
9




                  



                                       

                                                                                 

                         
                                                            
                                                            
                                                     
                                                  

                       
                                              








                                          
                                                                                  
 


                                                                                                 
                      




                                            








                                                 
            
                                                                          

          


                                                                                           
                                                                                                              






                                                               
                  

















                                                                 
                                                                              
                         



                                                                            











                                                                        
                                                                                                                                    
          
      
 


                            
 
                                                                                   
 
                                                
                                                     
      
 
 
                                     
# 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 PS1VE user host _hostname rc=$? s
    _hostname=$(hostname)

    # username + nodename
    [[ $(id -nu) == "alex" || $(id -u) -eq 0 ]] || user="\u"
    [[ "${_hostname}" == "laptop" ]] || host="@${_hostname}"
    [[ -z "$user" && -z "$host" ]] || host="${host} "
    PS1UH="$(pcolor lcyan)${user}${host}$(pcolor)"

    # working directory
    PS1D="$(pcolor lpurple)$(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)"

    # python virtual env
    [[ -n "${VIRTUAL_ENV}" ]] && PS1VE="$(pcolor lred)<${VIRTUAL_ENV#${WORKON_HOME}/}>$(pcolor) "

    # prompt character
    if [[ $(id -u) -eq 0 ]]; then
	PS1P="$(pcolor lred)\\\$ $(pcolor)"
    else
	PS1P="$(pcolor lblue)\\\$ $(pcolor)"
    fi

    # 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 '^# Changes not staged for commit:' ; then
		a="${a}*"
	    else
		if git status | grep -q '^# Changed but not updated:' ; then
		    a="${a}*"
		fi
	    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="${PS1BO}$(pcolor lgreen)${r:+${r}:}$(pcolor yellow)${b}$(pcolor red)${a}$(pcolor)${PS1BC}"
	fi
    fi

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

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

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

export PROMPT_COMMAND="__make_prompt"