summaryrefslogtreecommitdiff
path: root/.bashprompt
blob: d32dbf40483fadc42826db06b112834a65a63afa (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Environment variables:
#
# PS1_SHOWHOSTNAME
#       'auto' (default):       Only show hostname over SSH
#       'yes':                  Always show hostname
#       anything else:          Never show hostname


__show-hostname ()
{
    case "${PS1_SHOWHOSTNAME}" in
        (''|auto)
            [ "${SSH_CONNECTION}" ]
            return $?;;
        (yes)
            return 0;;
        (*)
            return 1;;
    esac
}

__set-title ()
{
    local title
    local git_root=$(git root 2> /dev/null)

    if [ ${git_root} ]
    then
        title=$(basename ${git_root})
    else
        local path=${PWD/~/\~}

        if __show-hostname
        then
            title="${USER}@${HOSTNAME}:${path}"
        else
            title="${USER}:${path}"
        fi
    fi

    echo -ne "\033]2;${title}\007"
}

# In order to know how much space PS1 takes, Bash needs us to delimit
# non-printing characters with \[ and \].
__start-nonprinting ()
{
    if [ ${BUILDING_PS1} ]
    then
        echo -en '\['
    fi
}

__end-nonprinting ()
{
    if [ ${BUILDING_PS1} ]
    then
        echo -en '\]'
    fi
}

__fontify ()
{
    local -A codes
    codes[red]='31'
    codes[green]='32'
    codes[blue]='34'
    codes[white]='37'
    codes[bold]='1'
    codes[dim]='2'
    codes[clear]='0'

    local text=$1
    shift

    output=$(__start-nonprinting)
    output+='\033['

    for ((i=1; i<=$#; i++))
    do
        output+=${codes[${!i}]}

        if ((i<$#))
        then
            output+=';'
        else
            output+='m'
        fi
    done

    output+=$(__end-nonprinting)

    output+="${text}"
    output+=$(__start-nonprinting)
    output+='\033[0m'
    output+=$(__end-nonprinting)

    echo -en "${output}"
}

__set-prompt ()
{
    BUILDING_PS1=t

    local last_status=$1

    PS1=''

    if [ ${last_status} -ne 0 ]
    then
        PS1+=$(__fontify "${last_status} " bold red) 
    fi

    PS1+=$(__fontify '\u' green)

    if __show-hostname
    then
        PS1+=$(__fontify '@\H' dim green)
    fi

    PS1+=$(__fontify : dim)
    PS1+=$(__fontify '\w' bold blue)
    PS1+=$(__fontify "$(__git_ps1 '(%s)')" red)
    PS1+=$(__fontify '\$' dim)
    PS1+=' '

    BUILDING_PS1=
}

__draw-rule ()
{
    rule=''
    for ((i=0; i<COLUMNS; i++))
    do
        rule+=—
    done
    __fontify ${rule} dim
}

__refresh-terminal ()
{
    local last_status=$?

    __set-title
    __set-prompt ${last_status}
    __draw-rule
}


. /usr/lib/git-core/git-sh-prompt
GIT_PS1_SHOWDIRTYSTATE=t
GIT_PS1_SHOWUPSTREAM=auto

export PROMPT_COMMAND=__refresh-terminal