aboutsummaryrefslogtreecommitdiff
path: root/bash/bashrc.d/prompt.bash
blob: 1be284a7b2715b84a33dab703b7cec71c83654f2 (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
# Frontend to controlling prompt
prompt() {
    local -i ret=$?
    local -i colors=$(tput colors 2>/dev/null)
    local color reset branch state info url root

    if [[ $colors -ge 256 ]]; then
        color='\[\e[38;5;10m\]'
        reset='\[\e[0m\]'
    elif [[ $colors -ge 8 ]]; then
        color='\[\e[1;32m\]'
        reset='\[\e[0m\]'
    fi

    case "$1" in

        # Turn complex coloured prompt on
        on)
            PROMPT_COMMAND='history -a'
            PS1='\[\a\][\u@\h:\w]$(prompt return)$(prompt vcs)$(prompt jobs)\$'
            PS1="${color}${PS1}${reset} "
            ;;

        # Revert to simple inexpensive prompt
        off)
            PROMPT_COMMAND=
            PS1='\[\a\]\$ '
            ;;

        # Git prompt function
        git)
            if $(git rev-parse --is-inside-git-dir 2>/dev/null); then
                return 1
            fi
            if ! $(git rev-parse --is-inside-work-tree 2>/dev/null); then
                return 1
            fi
            git status &>/dev/null
            branch=$(git symbolic-ref --quiet HEAD 2>/dev/null) \
                || branch=$(git rev-parse --short HEAD 2>/dev/null) \
                || branch='unknown'
            branch=${branch##*/}
            if ! git diff --quiet --ignore-submodules --cached; then
                state=${state}+
            fi
            if ! git diff-files --quiet --ignore-submodules --; then
                state=${state}!
            fi
            if $(git rev-parse --verify refs/stash &>/dev/null); then
                state=${state}^
            fi
            if [ -n "$(git ls-files --others --exclude-standard)" ]; then
                state=${state}?
            fi
            printf '(git:%s)' "${branch:-unknown}${state}"
            ;;

        # Mercurial prompt function
        hg)
            if ! branch="$(hg branch 2>/dev/null)"; then
                return 1
            fi
            if [[ -n "$(hg status 2>/dev/null)" ]]; then
                state="!"
            fi
            printf '(hg:%s)' "${branch:-unknown}${state}"
            ;;

        # Subversion prompt function
        svn)
            if ! svn info &>/dev/null; then
                return 1
            fi
            info="$(svn info 2>/dev/null)"
            url="$(awk -F': ' '$1 == "URL" {print $2}' \
                <<<"$info")"
            root="$(awk -F': ' '$1 == "Repository Root" {print $2}' \
                <<<"$info")"
            branch=${url/$root}
            branch=${branch#/}
            branch=${branch#branches/}
            branch=${branch%%/*}
            if [[ -n "$(svn status 2>/dev/null)" ]]; then
                state="!"
            fi
            printf '(svn:%s)' "${branch:-unknown}${state}"
            ;;

        # VCS wrapper prompt function
        vcs)
            prompt git || prompt svn || prompt hg
            ;;

        # Return status prompt function
        return)
            if [[ $ret -ne 0 ]]; then
                printf '<%d>' ${ret}
            fi
            ;;

        # Job count prompt function
        jobs)
            if [[ -n "$(jobs)" ]]; then
                printf '{%d}' $(jobs | sed -n '$=')
            fi
            ;;
    esac
}

# Start with full-fledged prompt
prompt on