summaryrefslogtreecommitdiffstats
path: root/.bash_prompt
blob: 3b12df6776dd49d6e65a53d5d44e822a245ab4d3 (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
#!/bin/bash

if [[ "${TERM}" =~ ^(xterm|screen)$ ]]; then
    export TERM="${TERM}-256color"
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "${TERM}" in
    xterm-mono|vt220)   color_prompt=-1;;
    xterm*|*256color)   color_prompt=1;;
    putty)              color_prompt=1;;
    *)                  color_prompt=0;;
esac

if [[ "${color_prompt}" = 0 ]] && which tput >/dev/null 2>&1 && tput setaf 1 >&/dev/null; then
    # We have color support; assume it's compliant with Ecma-48
    # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
    # a case would tend to support setf rather than setaf.)
    color_prompt=1
fi

if [[ "${color_prompt}" = 1 ]]; then
    if [[ "${TERM}" =~ 256color|putty || \
        ( -x /usr/bin/tput  && "$(tput colors 2>/dev/null)" = 256 ) ]];
    then
        export TERM256COLORS=1
    fi
fi

for prompt_path in \
    /run/current-system/sw/etc/bash_completion.d/git-prompt.sh \
    /usr/share/git-core/contrib/completion/git-prompt.sh \
    /usr/share/git/completion/git-prompt.sh \
    /usr/lib/git-core/git-sh-prompt;
do
    if [[ -e "${prompt_path}" ]]; then
        export GIT_PS1_SHOWDIRTYSTATE=true
        export GIT_PS1_SHOWSTASHSTATE=true
        export GIT_PS1_SHOWUNTRACKEDFILES=true
        export GIT_PS1_SHOWUPSTREAM="auto"
        source "${prompt_path}"
        break
    fi
done
unset prompt_path

if [[ "${color_prompt}" -gt 0 ]]; then
    PROMPT_COMMAND=__prompt_command
else
    PROMPT_COMMAND=__prompt_command_no_color
fi

# Run 256colors.pl script in 256 color xterm to check out possible colors. To
# get the escape sequence run: `echo $(tput setaf ${color_index}) | hexdump -C`.

function __make_prompt_string() {
    local exit_code="$1"
    local context
    if [[ "${TERM256COLORS:-0}" == 1 ]]; then
        local c_mark='\e[38;5;163m'
        local c_local='\e[38;5;220m'
        local c_at='\e[38;5;160m'
    else
        local c_local='\e[0;97m'
        local c_mark='\e[0;33m'
        local c_at=${c_mark}
    fi
    local c_info='\e[0;37m'
    local c_path='\e[0;32m'
    local c_cntx='\e[96m'                 # `tput setaf 14`
    local c_fail='\e[1;31m'
    local c_ok='\e[0;32m'
    local c_none='\e[0m'                  # `tput sgr0`
    case "${TERM}" in
        linux)
            exit_code="${exit_code:+${exit_code} }"
            ;;
        *)
            if [[ "${exit_code}" == 0 ]]; then
                exit_code="${c_info}0${c_mark} ${c_ok}\342\234\223 "
            elif [[ -n "${exit_code:-}" ]]; then
                exit_code="${c_info}${exit_code} ${c_fail}\342\234\227 "
            fi
    esac
    if grep -q '\<mc\>' /proc/${PPID}/cmdline 2>/dev/null; then
        context="${c_cntx}MC> "
    fi

    PS1=""
    [[ "${HISTFILE}" == /dev/null ]] && PS1="${c_cntx}NOHIST "

    # \[ \] can be safely omitted until new line character because cursor won't
    # be placed here
    PS1+="${exit_code:-}${context:-}${c_mark}[${c_info}"'`date +%H:%M:%S`'"${c_mark}] "
    PS1+="${c_local}\u${c_at}@${c_local}\h: ${c_path}\W${c_info}"
    PS1+='`declare -F __git_ps1 &>/dev/null && __git_ps1 " (%s)"` '
    PS1+="${c_mark}"'$(__prompt_show_shell_level) '
    # \[ \] need to surround color escape sequences in order for bash to
    # correctly compute cursor position
    PS1+="\n\[${c_mark}\]\$\[${c_none}\] "
}

function __prompt_show_shell_level() {
    printf '>%0.s' `seq $SHLVL`
}

function __make_prompt_string_no_color() {
    local exit_code="$1"
    local context=''
    if [[ -n "${exit_code}" ]]; then
        exit_code="${exit_code}}"
    fi
    if grep -q '\<mc\>' /proc/${PPID}/cmdline; then
        context="MC> "
    fi
    PS1=""
    [[ "${HISTFILE}" == /dev/null ]] && PS1="NOHIST "
    PS1+="${exit_code:-}${context:-}"'[`date +%H:%M:%S`] \u@\h: \W'
    PS1+='$(declare -F __git_ps1 &>/dev/null && __git_ps1 " (%s)") '
    PS1+="${c_mark}"'$(__prompt_show_shell_level) \n\$ '
}

function __prompt_command() {
    local code="$?"
    history -a
    __make_prompt_string "${code}"
    # set terminal's title
    local pth="${PWD/#${HOME}/\~}"
    if [[ "${pth}" =~ ^([/~]?[^/]+/).+(/[^/]+/[^/]+)/?$ ]]; then
        # shorten path with ellipsis
        pth="${BASH_REMATCH[1]}"$'\u2026'"${BASH_REMATCH[2]}"
    fi
    local host="${HOSTNAME}"
    if [[ "${host}" =~ ^([^.]+\.).+(\.[^.]+)?$ ]]; then
        host="${BASH_REMATCH[1]}"$'\u2026'"${BASH_REMATCH[2]}"
    fi
    echo -ne "\033]0;${USER}@${host}: ${pth}\007"
}

function __prompt_command_no_color() {
    local code="$?"
    history -a
    __make_prompt_string_no_color "${code}"
}

unset color_prompt