summaryrefslogtreecommitdiff
path: root/.config/fish/functions/fish_prompt.fish
blob: 494b07f91ac47a039f7f46c361d7b9ff2b1578bd (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
function fish_prompt
    set -l cmd_status $status
    set -l cmd_duration (math -s0 $CMD_DURATION / 1000)
    set CMD_DURATION 0
    set -l is_git (git rev-parse --is-inside-work-tree 2>/dev/null)

    echo

    set_color --bold cyan
    if [ "$is_git" = true ]
        printf "%s" (git rev-parse --show-toplevel | awk -F/ '{ print $NF }')
        set -l git_path (git rev-parse --show-prefix | sed 's#/$##')
        [ -n "$git_path" ] && printf '/%s' $git_path
    else
        printf (prompt_pwd -d0)
    end
    set_color normal

    if [ "$is_git" = true ]
        printf " on "
        set_color --bold magenta
        printf " "(git branch --show-current)

        function git_status
            set -l s (git status --porcelain -b | string split0)

            git rev-parse --verify refs/stash &>/dev/null && printf '$'
            echo $s | grep -q '^U[UDA]|^AA|^DD|^[DA]U'    && printf "="
            echo $s | grep -q '^[MARCDU ]D|^D[ UM]'     && printf "✘"
            echo $s | grep -q '^R[ MD]'                   && printf "»"
            echo $s | grep -q '^[ MARC]M'                 && printf "!"
            echo $s | grep -q '^A[ MDAU]|^M[ MD]|^UA'    && printf "+"
            echo $s | grep -q '^\?\?'                    && printf "?"

            set -l ahead (echo $s | grep -q '^## [^ ]\+ .*ahead'; echo $status)
            set -l behind (echo $s | grep -q '^## [^ ]\+ .*behind'; echo $status)
            if [ "$ahead" -eq 0 ] && [ "$behind" -eq 0 ]
                printf "⇕"
            elif [ "$ahead" -eq 0 ] 
                printf "⇡"
            elif [ "$behind" -eq 0 ]
                printf "⇣"
            end
        end

        set -l stat (git_status)
        if [ -n "$stat" ]
            set_color --bold red
            printf " [%s]" "$stat"
        end

        set_color normal
    end

    if [ "$cmd_duration" -gt 1 ]
        printf " took"
        set_color --bold yellow

        set -l secs (math -s1 $cmd_duration % 60)
        set -l mins (math -s0 $cmd_duration / 60 % 60)
        set -l hours (math -s0 $cmd_duration / 3600)

        [ "$hours" -gt 0 ] && printf " %dh" "$hours"
        [ "$mins" -gt 0 ] && printf " %dm" "$mins"
        [ "$secs" -gt 0 ] && printf " %ds" "$secs"
        set_color normal
    end

    echo

    if jobs &>/dev/null
        set -l jobs (jobs | wc -l)
        set_color --bold blue
        printf "✦ "
        [ "$jobs" -gt 1 ] && printf "%d " "$jobs"
        set_color normal
    end

    if [ "$cmd_status" -eq 0 ]
        set_color green
    else
        set_color red
    end
    printf "❯ "
    set_color normal
end