# numerodix prompt theme

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

prompt numerodix [ 8bit ] [host [user [user input] [curdir] [curdir root]]

where the last arguments are all colors.
This theme works best with a dark background.

If you have the `nexus' or `vga' console fonts or similar, you
can specify the `8bit' option to use 8-bit replacements for the
7-bit characters.
EOF
}

prompt_numerodix_setup () {
  # Some can't be local
  local prompt_gfx_tlc prompt_gfx_mlc prompt_gfx_blc

  if [[ $1 == '8bit' ]]; then
    shift
    prompt_gfx_tlc=$'\xda'
    prompt_gfx_mlc=$'\xc3'
    prompt_gfx_blc=$'\xc0'
    prompt_gfx_hyphen=$'\xc4'
  else
    prompt_gfx_tlc='.'
    prompt_gfx_mlc='|'
    prompt_gfx_blc='\`'
    prompt_gfx_hyphen=' '
  fi

  # Colour scheme
  prompt_numerodix_host=${1:-'$cbmagenta'}    # host
  prompt_numerodix_user=${2:-'$cbwhite'}    # user
  prompt_numerodix_userinput=${3:-'$cwhite'}   # user input
  prompt_numerodix_curdir=${4:-'$cbgreen'}   # current directory user
  if [ "$USER" = 'root' ]; then
    prompt_numerodix_curdir=${5:-'$cbred'}   # current directory root
  fi
  prompt_numerodix_hyphens=${6:-'$cwhite'}    # hyphens

  local prompt_gfx_bbox 
  prompt_gfx_tbox="%{$prompt_numerodix_hyphens%}%{$prompt_numerodix_hyphens%}"
  prompt_gfx_bbox="%{$prompt_numerodix_hyphens%}%{$prompt_numerodix_hyphens%}"

  # This is a cute hack.  Well I like it, anyway.
  prompt_gfx_bbox_to_mbox=$'%{\e[A\r'"$prompt_numerodix_hyphens${prompt_gfx_mlc}$prompt_numerodix_hyphens${prompt_gfx_hyphen}"$'\e[B%}'

  prompt_l_paren="%{$cbwhite%}["
  prompt_r_paren="%{$cbwhite%}]"

  prompt_prev_exit="%(?..$cbred%? )"

  prompt_user_host="%{$prompt_numerodix_user%}%n%{$prompt_numerodix_user%}@%{$prompt_numerodix_host%}%M"

  prompt_line_1a="$prompt_gfx_tbox$prompt_l_paren%{$prompt_numerodix_curdir%}%~$prompt_r_paren%{$prompt_numerodix_hyphens%}"
  prompt_line_1b="$prompt_l_paren$prompt_user_host$prompt_r_paren"

  prompt_char="%(!.#.$)"

  precmd () { prompt_numerodix_precmd; setopt promptsubst }
  preexec () { prompt_numerodix_preexec }
}

prompt_numerodix_precmd () {
  setopt noxtrace localoptions extendedglob
  local prompt_line_1

  prompt_numerodix_choose_prompt

  PS1="$prompt_line_1$prompt_newline%{$prompt_prev_exit%}%{$cbblue%}$prompt_char %{$prompt_numerodix_userinput%}"
  PS2="%{$prompt_gfx_bbox_to_mbox%{$prompt_prev_exit%}$cbblue%}%_$ %{$prompt_numerodix_userinput%}"
  PS3="%{$prompt_gfx_bbox_to_mbox%{$prompt_prev_exit%}$cbblue%}?# %{$prompt_numerodix_userinput%}"
}

prompt_numerodix_choose_prompt () {
  local prompt_line_1a_width=${#${(S%%)prompt_line_1a//\%\{*\%\}}}
  local prompt_line_1b_width=${#${(S%%)prompt_line_1b//\%\{*\%\}}}

  local prompt_padding_size=$(( COLUMNS
                                  - prompt_line_1a_width
                                  - prompt_line_1b_width ))

  # Try to fit in long path and user@host.
  if (( prompt_padding_size > 0 )); then
    local prompt_padding
    eval "prompt_padding=\${(l:${prompt_padding_size}::${prompt_gfx_hyphen}:)_empty_zz}"
    prompt_line_1="$prompt_line_1a$prompt_padding$prompt_line_1b"
    return
  fi

  prompt_padding_size=$(( COLUMNS - prompt_line_1a_width ))

  # Didn't fit; try to fit in just long path.
  if (( prompt_padding_size > 0 )); then
    local prompt_padding
    eval "prompt_padding=\${(l:${prompt_padding_size}::${prompt_gfx_hyphen}:)_empty_zz}"
    prompt_line_1="$prompt_line_1a$prompt_padding"
    return
  fi

  # Still didn't fit; truncate 
  local prompt_pwd_size=$(( COLUMNS - 5 ))
  prompt_line_1="$prompt_gfx_tbox$prompt_l_paren%{$prompt_numerodix_curdir%}%$prompt_pwd_size<...<%~%<<$prompt_r_paren%{$prompt_numerodix_hyphens$prompt_gfx_hyphen%}"
}

prompt_numerodix_preexec () {
  print -n "$reset_color"
}

prompt_numerodix_setup "$@"
