summaryrefslogblamecommitdiffstats
path: root/bashrc/common/prompt.sh
blob: 0d16f1d5a4147eb9ee698a7efa6491019595dfc4 (plain) (tree)



































































































                                                                                                                    
# command prompt

_load group prompt
_load node  prompt

returncolor() {
	rc=$?

	if [[ $1 -eq 0 ]]; then
		[[ $rc -eq 0 ]] && echo 0
	else
		[[ $rc -ne 0 ]] && echo $rc
	fi

	return $rc
}

# 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)\w$(pcolor)"

# return code
PS1R="$(pcolor red)\$(returncolor 1)$(pcolor lgreen)\$(returncolor 0)$(pcolor)"

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

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

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

# git/svn magic
ps_scm_f() {
	local s=

	if [[ -d ".svn" ]] ; then
		local r=$(svn info | sed -n -e '/^Revision: \([0-9]*\).*$/s//\1/p' )
		s="$(color yellow)r$r$(color 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}" && ! -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="$(color lgreen)${r:+${r}:}$(color yellow)${b}$(color red)${a}"
		fi
	fi

	echo -n "${s:+${s} }"
}

export PS1="${PS1UH} ${PS1BO}${PS1R}${PS1F}${PS1BC} ${PS1D} \$(ps_scm_f)${PS1P}"

unset PS1UH PS1D PS1R PS1F PS1P PS1BO PS1BC