# Copyleft ©2014 Debwin

prompt_debwin_help() {
  cat <<EOF
This prompt is color-scheme-able.  You can invoke it thus:

  prompt debwin [ include_time [ c_default [ c_path [ c_rprompt \
                  [ c_time [ c_vcsbranch [ c_vcsstatus ]]]]]]]

where the c_* parameters are colors by name (yellow, red, ...)
and all are optional.

include_time parameter is a boolean (yes/true/1), and if true, 
current time is shown in default prompt. If the parameter is false,
the time is only shown in right prompt (the default).

The prompt shows: command exit status (OK / not OK), current time
(if include_time is true), last three parts of the current path, 
optionally VCS branch and state if vcs_info provides them.

The right prompt shows, all optional: command error, active jobs, 
VCS type, current time (if include_time is false), SSH session 
(user@host), and shell level.

The theme works best with a dark background.
EOF
}

setup_vcs_info() {
  autoload -Uz vcs_info add-zsh-hook && vcs_info
  add-zsh-hook precmd vcs_info
  #
  # In the theme, one can use variables:
  #
  #   $vcs_info_msg_0_  =>  current VCS type (git, svn, hg, ...)
  #   $vcs_info_msg_1_  =>  active branch (master, ...)
  #   $vcs_info_msg_2_  =>  staged/unstaged changes, untracked files, ... 
  #   $vcs_info_msg_3_  =>  pending actions (rebase, merge, ...)
  #
  zstyle ':vcs_info:*' check-for-changes true
  zstyle ':vcs_info:*' use-simple true
  zstyle ':vcs_info:*' max-exports 4
  zstyle ':vcs_info:*' unstagedstr '*'
  zstyle ':vcs_info:*' stagedstr '*'
  zstyle ':vcs_info:*' formats '%s' '%b' '%u%c' '%m'
  zstyle ':vcs_info:*' actionformats '%s' '%b' '%u%c' '|%a'
  zstyle ':vcs_info:*' branchformat '%b:%7.7r'
  zstyle ':vcs_info:*' hgrevformat '%h'

  # git uses reporting from git-core's shell script, not vcs_info
  if ! vcs_info_printsys | grep -q '#git' && [ -e /usr/lib/git-core/git-sh-prompt ]; then
    . /usr/lib/git-core/git-sh-prompt
    GIT_PS1_SHOWDIRTYSTATE=1      # * (unstaged), + (staged)
    GIT_PS1_SHOWSTASHSTATE=1      # $
    GIT_PS1_SHOWUNTRACKEDFILES=1  # %
    GIT_PS1_SHOWUPSTREAM='auto'   # < (behind upstream), > (ahead), <> (diverged), = (no difference)
    zstyle ':vcs_info:git*+pre-get-data:*' hooks get-git-ps1
  fi
}

+vi-get-git-ps1() {
  [[ "$vcs" =~ git(-.+)? ]] && type __git_ps1 &>/dev/null || return
  local branch_info="$(__git_ps1 '%s')"  # example: "master *+$%%|REBASE 1/1"
  vcs_info_msg_0_="$vcs"
  vcs_info_msg_1_="${branch_info%% *}"
  vcs_info_msg_2_="${branch_info#$vcs_info_msg_1_}"
  vcs_info_msg_2_="${vcs_info_msg_2_# }"
  vcs_info_msg_3_="${vcs_info_msg_2_#*|}"
  if [ "$vcs_info_msg_2_" = "$vcs_info_msg_3_" ]; then
    vcs_info_msg_3_=''
  else
    vcs_info_msg_2_="${vcs_info_msg_2_%|$vcs_info_msg_3_}"
    vcs_info_msg_3_="|$vcs_info_msg_3_"
  fi
  ret=1  # all done, prevent vcs_info from proceeding
}

prompt_debwin_setup() {
  setup_vcs_info
  setopt LOCAL_OPTIONS NO_XTRACE
  
  local include_time="${1:-n}"
  [[ "$include_time" =~ [yYtT1].* ]] && include_time='' || unset include_time
  local c_default="${2:-yellow}"
  local c_path="${3:-white}"
  local c_rprompt="${4:-black}"
  local c_time="${5:-red}"
  local c_vcsbranch="${6:-green}"
  local c_vcsstatus="${7:-red}"
  
  local reset='%{%b%f%}'
  
  [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ] && local debian_chroot="$(cat /etc/debian_chroot)"

  local ret_status="%(?,%{%S%B%F{$c_default}%K{$c_default}%}:),%{%S%b%F{red}%K{white}%}:()%{%s%k%F{$c_default}%B%} "
  local curr_time="%{%F{blue}%}[%{%F{$c_time}%}%*%{%F{blue}%}]%{%F{$c_default}%}:"
  local curr_dir="${debian_chroot:+($debian_chroot)}%{%F{$c_path}%}%(4~,.../,)%3~ "
  local vcs_details="\$([ \"\$vcs_info_msg_1_\" ] && echo \"%{%B%F{$c_vcsbranch}%}\$vcs_info_msg_1_%{%B%F{$c_vcsstatus}%}\$vcs_info_msg_2_\$vcs_info_msg_3_ \")"
  local prompt_end="%(!,%{%B%F{red}%},%{%B%F{$c_default}%})%#"
  
  PROMPT="$ret_status${include_time+$curr_time}$curr_dir$vcs_details$prompt_end$reset "
  PROMPT2="%{%B%F{$c_default}%}| %_>$reset "
  
  local prev_err='%(?,,E:%? )'
  local active_jobs='%(1j,j%j ,)'
  local vcs_type='${vcs_info_msg_0_:+[$vcs_info_msg_0_] }'
  local curr_time="${include_time-[%*] }"
  local ssh_session='${SSH_TTY:+[%n@%m] }'
  local shell_level='%(2L,↓%L,)'
  
  RPROMPT="%{%B%F{$c_rprompt}%}$prev_err$active_jobs$vcs_type$curr_time$ssh_session$shell_level$reset"
}

prompt_debwin_preview() {
  if (( ! $#* )); then
    prompt_preview_theme debwin
    print
    prompt_preview_theme debwin true
    print
    prompt_preview_theme debwin true green green black blue red magenta
    print
    prompt_preview_theme debwin false magenta yellow black red red cyan
    print
    prompt_preview_theme debwin true cyan blue black yellow yellow green
  else
    prompt_preview_theme debwin "$@"
  fi
}

prompt_debwin_setup "$@"
