summaryrefslogtreecommitdiff
path: root/src/.bashrc_ps1
blob: 33daa0dc018a6ebce7ec57bd8f80ef4b964fb05f (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
# Dynamic PS1: exit code, time, user, current directory, git, colorized
function update_ps1 {
    local exit_code=$?

    # Color exit code: green if 0, red otherwise
    if [[ $exit_code -eq 0 ]]; then
        local exit_display="\[\033[1;32m\]$exit_code\[\033[0m\]"  # green
    else
        local exit_display="\[\033[1;31m\]$exit_code\[\033[0m\]"  # red
    fi

    # Symbol always white
    local symbol="\[\033[1;37m\]\$\[\033[0m\]"

    # White brackets
    local open_bracket="\[\033[1;37m\][\[\033[0m\]"
    local close_bracket="\[\033[1;37m\]]\[\033[0m\]"

    # Time in HH:MM:SS
    #local time="\[\033[1;37m\]\t\[\033[0m\]"

    # User
    #local user="\[\033[1;37m\]\u\[\033[0m\]"
    local user="\[\033[1;36m\]\u\[\033[0m\]"

    # Current folder only, ~ for home
    local folder="\[\033[1;33m\]\W\[\033[0m\]"

    # Git status
    local git_status=""
    if [[ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" == true ]]; then
        if [[ -z $(git log origin/master..HEAD 2>/dev/null) ]]; then
            git_status="\[\033[1;38;5;10m\](git)\[\033[0m\]"  # green if up to date
        else
            git_status="\[\033[1;38;5;9m\](git)\[\033[0m\]"   # red if ahead
        fi
    fi

    # Build the prompt
    export PS1="${open_bracket} ${exit_display} ${user} ${folder} ${git_status}${close_bracket} ${symbol} "
}


# Update PS1 after every command
PROMPT_COMMAND='update_ps1'