summaryrefslogtreecommitdiff
path: root/.bash_prompt
blob: 9b753b3bf380c1b5517af5ddd9463f7236f8fff3 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# 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}" ];;
        (yes)
            true;;
        (*)
            false;;
    esac
}

__have-gitprompt ()
{
    [ -f /usr/lib/git-core/git-sh-prompt ]
}

__set-title ()
{
    local title=${USER}
    if __show-hostname
    then
        title+="@${HOSTNAME}"
    fi
    title+=:

    local path=${PWD/~/\~}
    local git_root=$(git rev-parse --show-toplevel 2> /dev/null)

    if [ -z "${git_root}" -o "${git_root}" = ~ ]
    then
        title+=${path}
    else
        local project=$(basename "${git_root}")
        path=$(realpath --relative-to "${git_root}" "${PWD}")
        if [ "${path}" = . ]
        then
            title+=${project}
        else
            title+=${project}/${path}
        fi
    fi

    # Cf. console_codes(4):
    #
    #   ESC ] 2 ; txt ST        Set window title to txt.
    #
    # ST is a "string terminator", either "ESC \" or "BEL".

    echo -ne "\E]2;${title}\a"
}

# 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=(
        [bold]=1
        [dim]=2
        [red]=31
        [green]=32
        [blue]=34
    )

    local text=$1
    shift

    __start-nonprinting

    echo -en '\E['

    local i
    for ((i=1; i<=$#; i++))
    do
        echo -en ${codes[${!i}]}

        if ((i<$#))
        then
            echo -en ';'
        else
            echo -en 'm'
        fi
    done

    __end-nonprinting

    echo -en "${text}"

    __start-nonprinting
    echo -en '\E[0m'
    __end-nonprinting
}

__set-prompt ()
{
    BUILDING_PS1=t

    PS1="$(__write-context)\n$(__fontify '\$' dim) "

    unset BUILDING_PS1
}

__current-column ()
{
    # Cf. console_codes(4) ยง ECMA-48 Status Report Commands

    local position
    read -sdR -p$'\E[6n' position

    local pattern='\[[0-9]+;([0-9]+)'

    if [[ ${position} =~ ${pattern} ]]
    then
        echo ${BASH_REMATCH[1]}
    else
        echo 0
    fi
}

__signal-no-newline ()
{
    echo $(__fontify ๐Ÿ›‡ bold red)
}

__draw-rule ()
{
    local i line

    for ((i=$(__current-column); i<COLUMNS; i++))
    do
        line+=โ”€
    done

    # If we do not add a newline, whatever comes after will be shoved
    # offscreen when autowrap is off.
    line+='\n'

    __fontify ${line} $@
}

__write-context ()
{
    __fontify '\u' green

    if __show-hostname
    then
        __fontify @ dim
        __fontify '\h' bold green
    fi

    __fontify : dim
    __fontify '\w' bold blue

    if [ ${PS1_SHOWGITSTATUS} ]
    then
        __fontify "$(__git_ps1 '(%s)')" red
    fi
}

__refresh-terminal ()
{
    local last_status=$?

    __set-title

    if [ $(__current-column) -ne 1 ]
    then
        __signal-no-newline
    fi

    if [ ${last_status} -ne 0 ]
    then
        __fontify "${last_status} " bold red
        __draw-rule dim red
    else
        __draw-rule dim
    fi

    __set-prompt
}


PS2='โ€ฆ '

if [ "${TERM}" = dumb ]
then
    PS1='$? \u:\w\$ '
    return
fi

PS2=$(__fontify "${PS2}" dim)

if __have-gitprompt
then
    . /usr/lib/git-core/git-sh-prompt
    GIT_PS1_SHOWDIRTYSTATE=t
    GIT_PS1_SHOWUPSTREAM=auto

    PS1_SHOWGITSTATUS=t
fi

PROMPT_COMMAND=__refresh-terminal