zmodload -i zsh/parameter || return 1

prompt_bart_help () {
    setopt localoptions nocshnullcmd noshnullcmd
    [[ $ZSH_VERSION < 3.1.6-dev-20 ]] &&
	print 'Requires 3.1.6-dev-19 plus zsh-workers/10168.'$'\n'
    <<-\EOF
	This prompt gives the effect of left and right prompts on the upper
	line of a two-line prompt.  It also illustrates including history
	text in the prompt.  The lower line is initialized from the last
	(or only) line of whatever prompt was previously in effect.

	    prompt bart [on|off] [color...]

	You may provide up to five colors, though only three colors (red,
	blue, and the default foreground) are used if no arguments are
	given.  The defaults look best on a light background.

	The "off" token temporarily disables the theme; "on" restores it.
	EOF
    [[ $(read -Ek 1 "?${(%):-%S[press return]%s}") == [Qq] ]] &&
	print -nP '\r%E' && return
    <<-\EOF

	The "upper left prompt" looks like:
	    machine [last command] /current/working/dir
	The first three color arguments apply to these components.  The
	last command is shown in standout mode if the exit status was
	nonzero, or underlined if the job was stopped.

	If the last command is too wide to fit without truncating the
	current directory, it is put on a line by itself.  The current
	directory uses %~, so namedir abbreviation applies.

	The "upper right prompt" looks like:
	    date time
	The fourth color is used for the date, and the first again for the
	time.  As with RPS1, first the date and then the time disappear as
	the upper left prompt grows too wide.  The clock is not live; it
	changes only after each command, as does the rest of the prompt.
	EOF
    [[ $(read -Ek 1 "?${(%):-%S[press return]%s}") == [Qq] ]] &&
	print -nP '\r%E' && return
    <<-\EOF

	When RPS1 (RPROMPT) is set before this prompt is selected and a
	fifth color is specified, that color is turned on before RPS1 is
	displayed and reset after it.  Other color changes within RPS1, if
	any, remain in effect.

	This prompt hijacks psvar[7] through [9] to avoid having to reset
	the entire PS1 string on every command.  It uses TRAPWINCH to set
	the position of the upper right prompt on a window resize, so the
	prompt may not match the window width until the next command.  No
	background colors or hardwired cursor motion escapes are used, and
	it is not necessary to setopt promptsubst.
	EOF
}

prompt_bart_precmd () {
    setopt localoptions noxtrace noksharrays
    local zero='%([BSUbsu]|{*%})'

    # Using psvar here protects against unwanted promptsubst expansions.

    psvar[7]="$history[$[${(%):-%h}-1]]" # Use history text, not just number
    psvar[8]=''				 # No padding until we compute it

    # Compute the size of the upper left prompt and set psvar[9] if needed.
    psvar[9]=()
    ((${#${(f)${(%%)${(S)PS1//$~zero/}}}[1]} > COLUMNS-1)) && psvar[9]=''

    # Compute and set the padding between upper left and right prompts.
    psvar[8]=${(l:COLUMNS-${#${(f)${(%%)${(S)PS1//$~zero/}}}[1]}-1:: :)}
}

prompt_bart_ps1 () {
    setopt localoptions noxtrace noksharrays

    local -ah ps1
    local -h host hist1 hist2 dir space date time rs="%{$reset_color%}"
    local -h eon="%(?.[.%20(?.[%U.%S[))" eoff="%(?.].%20(?.%u].]%s))"

    # Set up the components of the upper line
    host="%{$fg[%m]%}%m$rs"
    hist1="%9(v. . %{$fg[%h]%}$eon%7v$eoff$rs )"
    hist2=$'%9(v.\n'"%{$fg[%h]%}$eon%7v$eoff$rs.)"
    dir="%{$fg[%~]%}%8~$rs"
    space=%8v
    date="%{$fg[%D]%}%D$rs"	# Prefer "%{$fg[%D]%}%W$rs" in the USA?
    time="%{$fg[%@]%}%@$rs"

    # This is just a tad silly ...
    [[ $prompt_theme[1] = oliver ]] && PS1='[%h%0(?..:%?)]%# ' ||
	PS1=${PS1//$prompt_newline/$'\n'}

    # Assemble the new prompt
    ps1=( ${(f)PS1} )		# Split the existing prompt at newlines
    ps1=( "%$[COLUMNS-1]>..>"	# Begin truncation (upper left prompt)
	"$host"
	"$hist1"		# Empty when too wide for one line
	"$dir"
	"%<<"			# End truncation (end upper left prompt)
	"$space"		# Pad line to upper right position
	"%$[COLUMNS-16](l. . $date)"
	"%$[COLUMNS-8](l.. $time)"
	"$hist2"		# Empty when $hist1 is not empty
	$'\n'
	$ps1[-1] )		# Keep last line of the existing prompt
    PS1="${(j::)ps1}"
}

prompt_bart_winch () {
    setopt localoptions noksharrays

    # Delete ourself from TRAPWINCH if not using our precmd insert.
    [[ $functions[precmd] = *prompt_bart_precmd* ]] && prompt_bart_ps1 ||
	functions[TRAPWINCH]="${functions[TRAPWINCH]//prompt_bart_winch}"
}

prompt_bart_setup () {
    setopt localoptions noksharrays

    # A few extra niceties ...
    repeat 1 case "$1:l" in
      (off|disable)
	functions[precmd]="${functions[precmd]//prompt_bart_precmd}"
	functions[TRAPWINCH]="${functions[TRAPWINCH]//prompt_bart_winch}"
	[[ $prompt_theme[1] = bart ]] && PS1=${${(f)PS1}[-1]}
	return 1
	;;
      (on|enable)
	shift
	[[ $prompt_theme[1] = bart ]] && break
	;&
      (*)
	# Abuse the fg assoc to hold our selected colors ...
	fg[%m]=$fg[${1:-red}]
	fg[%h]=$fg[${2:-blue}]
	fg[%~]=$fg[${3:-none}]
	fg[%D]=$fg[${4:-none}]
	fg[%@]=$fg[${1:-red}]
	;;
    esac

    prompt_bart_ps1

    # No RPS1 by default because prompt_off_setup doesn't fix it.
    (($#RPS1 && $# > 4)) && RPS1="%{$fg[$5]%}$RPS1%{$reset_color%}"

    # Paste our special commands into precmd and TRAPWINCH
    functions[precmd]="${functions[precmd]//prompt_*_precmd}
	prompt_bart_precmd"
    functions[TRAPWINCH]="${functions[TRAPWINCH]//prompt_bart_winch}
	prompt_bart_winch"

    return 0
}

[[ -o kshautoload ]] || prompt_bart_setup "$@"
