summaryrefslogtreecommitdiffstats
path: root/bashrc/common/prompt.sh
blob: 03115059bf342ca2153674a74dd3fc98c5c8008b (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
108
109
110
111
112
113
# 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 '^# 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="${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"