pmodload 'helper'

# %<num>>dsadsa>dsadsa -- truncation
# %~ -- current dir
# %F{color}...%f -- foreground colors
# %B...%b -- bold
# %(x.a.b) -- if
# ${(l)} -- padding

# color chart: https://upload.wikimedia.org/wikipedia/en/1/15/Xterm_256color_chart.svg
# COLOR_PWD='141'
# COLOR_PWD_ROOT='201'
# COLOR_PROMPT_STARTER='85'
# COLOR_TIME='244'
# COLOR_USERNAME='34'
# COLOR_AT='244'
# COLOR_HOSTNAME='162'

COLOR_PWD='cyan'
COLOR_PWD_ROOT='red'
COLOR_PROMPT_STARTER='green'
COLOR_TIME='green'
COLOR_USERHOSTNAME='blue'
COLOR_DURATION='magenta'

NEWLINE=$'\n'

function prompt_abra_precmd {
  setopt LOCAL_OPTIONS
  unsetopt XTRACE KSH_ARRAYS

  # Get Git repository information.
  if [[ -z $NOGIT ]] && (( $+functions[git-info] )); then
    git-info || true
  fi

  PRE_LENGTH=$(( ${COLUMNS} * 2 / 3 ))

  # take current directory and truncate left so that up to $PRE_LENGTH chars are left
  PRE='%${PRE_LENGTH}<...<%~ %<<'
  PRE=$(print -P ${PRE})
  PRE=$(print ${(l:${COLUMNS}:)PRE})
  # print -P '%(!.%F{${COLOR_PWD_ROOT}}.%F{${COLOR_PWD}})${PRE}%f'
  print -P '%F{${COLOR_PWD}}${PRE}%f'

  if [[ -z $PREEXEC_TIME ]]; then
    EXECTIME=0
  else
    EXECTIME=$(( EPOCHREALTIME - PREEXEC_TIME ))
  fi

  if (( EXECTIME > 5 )); then
    SEC=$(( int(rint(EXECTIME % 60)) ))
    MIN=$(( int(rint(EXECTIME / 60 % 60)) ))
    HOUR=$(( int(rint(EXECTIME / 3600)) ))

    DURATION="$(printf '%02d:%02d:%02d ' $HOUR $MIN $SEC)"
  else
    DURATION=''
  fi
}

function prompt_abra_preexec {
  PREEXEC_TIME=$EPOCHREALTIME
}

function prompt_abra_setup {
  setopt LOCAL_OPTIONS
  unsetopt XTRACE KSH_ARRAYS
  prompt_opts=(cr percent subst)

  # Load required functions.
  autoload -Uz add-zsh-hook

  # Add hook for calling git-info before each command.
  add-zsh-hook precmd prompt_abra_precmd
  add-zsh-hook preexec prompt_abra_preexec

  # Set git-info parameters.
  zstyle ':prezto:module:git:info' verbose 'yes'
  zstyle ':prezto:module:git:info:action' format ':%%B%F{yellow}%s%f%%b'
  zstyle ':prezto:module:git:info:added' format ' %%B%F{green}✚%f%%b'
  zstyle ':prezto:module:git:info:ahead' format ' %%B%F{yellow}⬆%f%%b'
  zstyle ':prezto:module:git:info:behind' format ' %%B%F{yellow}⬇%f%%b'
  zstyle ':prezto:module:git:info:branch' format ':%F{green}%b%f'
  zstyle ':prezto:module:git:info:commit' format ':%F{green}%.7c%f'
  zstyle ':prezto:module:git:info:deleted' format ' %%B%F{red}✖%f%%b'
  zstyle ':prezto:module:git:info:modified' format ' %%B%F{blue}✱%f%%b'
  zstyle ':prezto:module:git:info:position' format ':%F{red}%p%f'
  zstyle ':prezto:module:git:info:renamed' format ' %%B%F{magenta}➜%f%%b'
  zstyle ':prezto:module:git:info:stashed' format ' %%B%F{cyan}✭%f%%b'
  zstyle ':prezto:module:git:info:unmerged' format ' %%B%F{yellow}═%f%%b'
  zstyle ':prezto:module:git:info:untracked' format ' %%B%F{white}◼%f%%b'
  zstyle ':prezto:module:git:info:keys' format \
    'prompt' '%b ' \
    'rprompt' '%A%B%S%a%d%m%r%U%u'



  PROMPT=''
  PROMPT=$PROMPT'${git_info:+${(e)git_info[prompt]}}'
  PROMPT=$PROMPT'%(?.%F{$COLOR_PROMPT_STARTER}.%F{red})➤ %f'

  RPROMPT=''
  RPROMPT=$RPROMPT'%(?..%F{red}%?%f )' # last command code if nonzero
  RPROMPT=$RPROMPT'%B%(!.%F{${COLOR_PWD_ROOT}}.%F{${COLOR_USERHOSTNAME}})%n@%M%f%b'
  RPROMPT=$RPROMPT'${git_info[rprompt]} '
  RPROMPT=$RPROMPT'%F{$COLOR_DURATION}$DURATION%f'
  RPROMPT=$RPROMPT'%F{$COLOR_TIME}%*%f'
}

prompt_abra_setup "$@"

