summaryrefslogtreecommitdiffstats
path: root/bashrc/common/prompt.sh
blob: c85f62b76eba2145be974567f41b83445c7bb19c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 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
	_hostname=$(hostname)
	echo -ne "\ek$(id -un)@${_hostname%%\.*}\e\\"
    fi
}

export PROMPT_COMMAND="__make_prompt"