typeset -g POWERLEVEL9K_VCS_BRANCH_ICON='\uF126 '
typeset -g POWERLEVEL9K_VCS_CLEAN_BACKGROUND=$black
typeset -g POWERLEVEL9K_VCS_MODIFIED_BACKGROUND=$black
typeset -g POWERLEVEL9K_VCS_UNTRACKED_BACKGROUND=$black
typeset -g POWERLEVEL9K_VCS_CONFLICTED_BACKGROUND=$black
typeset -g POWERLEVEL9K_VCS_LOADING_BACKGROUND=$black

function my_git_formatter() {
  emulate -L zsh

  if [[ -n $P9K_CONTENT ]]; then
    typeset -g my_git_format=$P9K_CONTENT
    return
  fi

  if (( $1 )); then
    # Styling for up-to-date Git status.
    local       meta='%F{grey}'    # grey foreground
    local      clean='%F{green}'   # green foreground
    local   modified='%F{yellow}'  # yellow foreground
    local  untracked='%F{blue}'    # blue foreground
    local conflicted='%F{red}'     # red foreground
    local     action='%F{purple}'  # purple foreground
  else
    # Styling for incomplete and stale Git status.
    local       meta='%F{grey}'  # grey foreground
    local      clean='%F{grey}'  # grey foreground
    local   modified='%F{grey}'  # grey foreground
    local  untracked='%F{grey}'  # grey foreground
    local conflicted='%F{grey}'  # grey foreground
    local     action='%F{grey}'  # grey foreground
  fi

  local res

  if [[ -n $VCS_STATUS_LOCAL_BRANCH ]]; then
    local branch=${(V)VCS_STATUS_LOCAL_BRANCH}
    (( $#branch > 32 )) && branch[13,-13]="…"
    res+="${clean}${(g::)POWERLEVEL9K_VCS_BRANCH_ICON}${branch//\%/%%}"
  fi

  if [[ -n $VCS_STATUS_TAG && -z $VCS_STATUS_LOCAL_BRANCH ]]; then
    local tag=${(V)VCS_STATUS_TAG}
    (( $#tag > 32 )) && tag[13,-13]="…"
    res+="${meta}󰓹 ${clean}${tag//\%/%%}"
  fi

  if [[ -z $VCS_STATUS_LOCAL_BRANCH && -z $VCS_STATUS_TAG ]]; then
    res+="${meta} ${clean}${VCS_STATUS_COMMIT[1,8]}"
  fi

  if [[ -n ${VCS_STATUS_REMOTE_BRANCH:#$VCS_STATUS_LOCAL_BRANCH} ]]; then
    res+="${meta}:${clean}${(V)VCS_STATUS_REMOTE_BRANCH//\%/%%}"
  fi

  if [[ $VCS_STATUS_COMMIT_SUMMARY == (|*[^[:alnum:]])(wip|WIP)(|[^[:alnum:]]*) ]]; then
    res+=" ${modified}󰖷 "
  fi

  integer num_staged_modified=$((VCS_STATUS_NUM_STAGED - VCS_STATUS_NUM_STAGED_NEW - VCS_STATUS_NUM_STAGED_DELETED))
  (( VCS_STATUS_COMMITS_BEHIND      )) && res+=" ${clean}⇣${VCS_STATUS_COMMITS_BEHIND}"
  (( VCS_STATUS_COMMITS_AHEAD       )) && res+=" ${clean}⇡${VCS_STATUS_COMMITS_AHEAD}"
  (( VCS_STATUS_PUSH_COMMITS_BEHIND )) && res+=" ${clean}⇠${VCS_STATUS_PUSH_COMMITS_BEHIND}"
  (( VCS_STATUS_PUSH_COMMITS_AHEAD  )) && res+=" ${clean}⇢${VCS_STATUS_PUSH_COMMITS_AHEAD}"
  (( VCS_STATUS_STASHES             )) && res+=" ${clean}*${VCS_STATUS_STASHES}"
  [[ -n ${VCS_STATUS_ACTION}        ]] && res+=" ${action}${VCS_STATUS_ACTION}"
  (( VCS_STATUS_NUM_CONFLICTED      )) && res+=" ${conflicted}~${VCS_STATUS_NUM_CONFLICTED}"
  (( VCS_STATUS_NUM_STAGED_NEW      )) && res+=" ${clean}+${VCS_STATUS_NUM_STAGED_NEW}"
  (( VCS_STATUS_NUM_STAGED_DELETED  )) && res+=" ${conflicted}-${VCS_STATUS_NUM_STAGED_DELETED}"
  (( num_staged_modified            )) && res+=" ${modified}~${num_staged_modified}"
  (( VCS_STATUS_NUM_UNSTAGED        )) && res+=" ${modified}!${VCS_STATUS_NUM_UNSTAGED}"
  (( VCS_STATUS_NUM_UNTRACKED       )) && res+=" ${untracked}?${VCS_STATUS_NUM_UNTRACKED}"
  (( VCS_STATUS_HAS_UNSTAGED == -1  )) && res+=" ${modified}─"

  typeset -g my_git_format=$res
}

functions -M my_git_formatter 2>/dev/null

typeset -g POWERLEVEL9K_VCS_MAX_INDEX_SIZE_DIRTY=-1
typeset -g POWERLEVEL9K_VCS_DISABLED_WORKDIR_PATTERN='~'
typeset -g POWERLEVEL9K_VCS_DISABLE_GITSTATUS_FORMATTING=true
typeset -g POWERLEVEL9K_VCS_CONTENT_EXPANSION='${$((my_git_formatter(1)))+${my_git_format}}'
typeset -g POWERLEVEL9K_VCS_LOADING_CONTENT_EXPANSION='${$((my_git_formatter(0)))+${my_git_format}}'
typeset -g POWERLEVEL9K_VCS_{STAGED,UNSTAGED,UNTRACKED,CONFLICTED,COMMITS_AHEAD,COMMITS_BEHIND}_MAX_NUM=-1
typeset -g POWERLEVEL9K_VCS_VISUAL_IDENTIFIER_EXPANSION=
typeset -g POWERLEVEL9K_VCS_BACKENDS=(git)
