autoload -Uz vcs_info colors && colors

export LESS_TERMCAP_mb="${fg_bold[red]}"
export LESS_TERMCAP_md="${fg_bold[red]}"
export LESS_TERMCAP_me="$reset_color"
export LESS_TERMCAP_so="${fg_bold[yellow]}${bg[blue]}"
export LESS_TERMCAP_se="$reset_color"
export LESS_TERMCAP_us="${fg_bold[green]}"
export LESS_TERMCAP_ue="$reset_color"

. <(dircolors)
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"

zstyle ':completion:*:*:*:*:*' menu select
zstyle ':completion:*' matcher-list 'r:|=*' 'l:|=* r:|=*'
zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#) ([0-9a-z-]#)*=01;34=0=01'
zstyle ':completion:*:cd:*' tag-order local-directories directory-stack path-directories


zstyle ':vcs_info:*' check-for-changes true
zstyle ':vcs_info:*' stagedstr ' %{%F{178}%}+%{%f%b%}'
zstyle ':vcs_info:*' unstagedstr ' %{%F{178}%}*%{%f%b%}'
#zstyle ':vcs_info:*' untrackedstr ' %{%F{39}%}?%{%f%b%}'
zstyle ':vcs_info:git:*' formats '%{%F{76}%}%b%{%f%}%c%u '
zstyle ':vcs_info:git:*' actionformats '%{%F{76}%}%b%{%f%}|%{%F{196}%}%a%{%f%}%c%u '

function preexec() {
    _timer_start="$SECONDS"
}

function _get_time() {
    local d="$1"
    local text

    text="$((d % 60))s"
    if [ "$d" -gt 60 ]; then
        (( d /= 60 ))
        text="$((d % 60))m$text"
        if [ "$d" -gt 60 ]; then
            text="${d}h$text"
        fi
    fi
    echo "$text"
}

function precmd() {
    IFS='|' local timer ctx err="${pipestatus[*]}"

    [ "$_timer_start" -gt 0 ] && timer="$((SECONDS - _timer_start))"
    _timer_start=-1

    ctx="$(kubectx -c)"

    vcs_info

    PROMPT=''
    [ -n "$ctx" ]               && PROMPT+="%{%F{134}%}$ctx/$(kubens -c)%{%f%b%} "
                                   PROMPT+='%{%B%F{32}%}%~%{%f%b%} '
                                   PROMPT+="$vcs_info_msg_0_"
    [ "$timer" -ge 2 ]          && PROMPT+="%{%F{101}%}$(_get_time "$timer")%{%f%b%} "
    [[ "$err" =~ [1-9] ]]       && PROMPT+="%{%F{160}%}${err[*]}%{%f%b%} "

    RPROMPT=''
                             RPROMPT+=' %{%F{71}%}%(1j.%j.)%{%f%b%}'
    [ -n "$VIRTUAL_ENV" ] && RPROMPT+=" %{%F{37}%}${VIRTUAL_ENV:h:t}%{%f%b%}"
}
