summaryrefslogtreecommitdiff
path: root/.bashprompt
blob: 2e0ba7d9e9a31a216741663b8524b95598b82d33 (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
__set-title ()
{
    local title
    local path=$(git root 2> /dev/null)

    if [ -n "${path}" ]
    then
        title=$(basename ${path})
    else
        title="${USER}:${PWD/~/\~}"
    fi

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

__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 string=$1
    shift

    # We need to add \[ \] around CSI sequences so that Bash can tell which
    # characters are non-printing.

    echo -en '\[\033['

    local attributes=($@)
    local nb=$#

    for ((i=0; i<nb; i++))
    do
        a=${attributes[${i}]}
        echo -en ${codes[${a}]}

        if ((i<nb-1))
        then
            echo -en ';'
        else
            echo -en 'm\]'
        fi
    done

    echo -n "${string}"
    echo -en "\[\033[0m\]"
}

__set-prompt ()
{
    local last_status=$1

    PS1=''

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

    PS1+=$(__fontify '\u' green)
    PS1+=$(__fontify : bold white)
    PS1+=$(__fontify '\w' bold blue)
    PS1+=$(__fontify "$(__git_ps1 '(%s)')" red)
    PS1+=$(__fontify '\$' bold white)
    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