set -g fish_color_git_normal blue

set -g fish_color_git_ahead green
set -g fish_color_git_behind red
set -g fish_color_git_diverged magenta
set -g fish_color_git_staged green
set -g fish_color_git_unstaged yellow
set -g fish_color_git_untracked red
set -g fish_color_git_unmerged red

set -g fish_prompt_git_status_ahead '▲'
set -g fish_prompt_git_status_behind '▼'
set -g fish_prompt_git_status_diverged '⯁'
set -g fish_prompt_git_status_staged '●'
set -g fish_prompt_git_status_unstaged '●'
set -g fish_prompt_git_status_untracked '●'
set -g fish_prompt_git_status_unmerged 'M'
set -g fish_prompt_git_status_stashed 'S'

set -g fish_prompt_git_status_order ahead behind diverged staged unstaged untracked unmerged stashed

# Useful chars
# '✓'
# '⚡'
# '✚'
# '●'
# '▾'
# '✖'
# '➜'
# '⇒'
# '➤'

function __plaid_git_prompt --description 'Write out the git prompt'
    # If git isn't installed, there's nothing we can do
    # Return 1 so the calling prompt can deal with it
    if not command -sq git
        return 1
    end
    set -l branch (git rev-parse --abbrev-ref HEAD 2>/dev/null)
    if test -z $branch
        return
    end

    set -l index (git status --porcelain -b 2>/dev/null)
    set -l rst (set_color normal)

    if test -z "$index"
        printf '%s[%s]%s' (set_color $fish_color_git_normal) $branch $rst
        return
    end

    set -l gs

    printf '%s[%s ' (set_color $fish_color_git_normal) $branch
    for i in $index
        if echo $i | grep '^[AMRCD]' >/dev/null
            set -a gs staged
        end
        if echo $i | grep '^.\{1\}[AMRCD]' >/dev/null
            set -a gs unstaged
        end
        if echo $i | grep '^??' >/dev/null
            set -a gs untracked
        end
        if echo $i | grep '^[ADU*][ADU*]' >/dev/null
            set -a gs unmerged
        end
        if echo $i | grep '^## .*ahead' >/dev/null
            set -a gs ahead
        end
        if echo $i | grep '^## .*behind' >/dev/null
            set -a gs behind
        end
        if echo $i | grep '^## .*diverged' >/dev/null
            set -a gs diverged
        end
        if git rev-parse --verify refs/stash >/dev/null 2>&1
            set -a gs stashed
        end
    end

    for i in $fish_prompt_git_status_order
        if contains $i in $gs
            set -l color_name fish_color_git_$i
            set -l status_name fish_prompt_git_status_$i
            printf '%s%s' (set_color $$color_name) $$status_name
        end
    end

    printf '%s]%s' (set_color $fish_color_git_normal) $rst
end