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

    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)
            git branch &>/dev/null || return 1
            head="$(git symbolic-ref HEAD 2>/dev/null)"
            branch="${head##*/}"
            [[ -n "$(git status 2>/dev/null | \
                grep -F 'nothing to commit')" ]] || state="!"
            printf '(git:%s)' "${branch:-unknown}${state}"
            ;;

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

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

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

        # Return status prompt function
        return)
            ret=$? 
            [[ $ret -ne 0 ]] && printf '<%d>' ${ret}
            ;;

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

# Start with full-fledged prompt
prompt on