prompt_fresh_help() {
  echo "\
options:
        -a      enable almostontop like behaviour
        -r      recompile git-prompt executable"
}

prompt_fresh_setup() {
  # Parse options
  local almostontop=0
  local recompile=0
  while getopts 'ar' opt; do
    case $opt in
      a) local almostontop=1 ;;
      r) local recompile=1 ;;
      *) prompt -h fresh; return 1 ;;
    esac
  done

  autoload -U add-zsh-hook

  # Hook to print the first line of the "two line" prompt, this line is not
  # actually part of the prompt so does not get redrawn, this is preferable
  # since sometimes lines before the prompt can disappear.
  add-zsh-hook precmd fresh_line_one

  if [ $almostontop -eq 1 ]; then
    # Hook to clear the screen then prints the prompt with previous command at
    # the top of the screen.
    add-zsh-hook preexec fresh_almostontop
  else
    add-zsh-hook -d preexec fresh_almostontop
  fi

  [ $recompile -eq 1 ] && prompt_cleanup
  fresh_compile_git_prompt

  if [ "$USER" = "root" ]; then
    local user="%{%F{1}%}%n%{%f%}"
  else
    local user="%{%F{35}%}%n%{%f%}"
  fi

  local userhost=$USER
  if [ "$SSH_CONNECTION" != "" ]; then
    local user="$user@%{%F{244}%}%M%{%f%}"
    local userhost="$userhost@`hostname`"
  fi

  PS1="«$user» "
  PS2="«${(l:${#userhost}:: :)}» "

  prompt_opts=(percent sp subst)
}

prompt_cleanup() {
  [ -f ~/.cache/zsh/git-prompt ] && rm ~/.cache/zsh/git-prompt
}

fresh_line_one() {
  # First get the last commands exit code before doing anything
  local exit_code=$?

  # Construct the time and directory portions of the prompt
  local time_stamp="%{%F{244}%}%D{%H:%M:%S}%{%f%}"
  [[ -n $SANDBOX_HOME ]] && \
    local directory="%{%F{3}%}$SANDBOX_NAME${PWD#$SANDBOX_HOME}%{%f%}" || \
    local directory="%{%F{37}%}%~%{%f%}"

  # Check we are in a git repository
  local git=`~/.cache/zsh/git-prompt`

  # If the last command failed, display its error code at the right
  if [[ $exit_code -ne 0 ]]; then
    local result=" %{%B%F{1}%}$exit_code%{%f%b%}"
  fi

  # If a virtualenv is enabled, display it's basename
  if [[ ! -z "$VIRTUAL_ENV" ]]; then
    local py=" %{%F{4}%}py%{%f%}%{%F{3}%}$(basename $VIRTUAL_ENV)%{%f%}"
  fi

  # If docker-machine env is active, display the machines name
  if [[ ! -z "$DOCKER_MACHINE_NAME" ]]; then
    local docker=" %{%F{6}%}$DOCKER_MACHINE_NAME%{%f%}"
  fi

  # Print the first line of the prompt
  print -P "$time_stamp $directory$git$py$docker$result"
}

fresh_almostontop() {
  clear
  fresh_line_one
  print -P "$PROMPT"'$1'
}

fresh_compile_git_prompt() {
  # Compile a simple C program which parses the output of `git status
  # --procelain` to greatly decrease the time taken to draw the prompt
  [ ! -d ~/.cache/zsh ] && mkdir -p ~/.cache/zsh
  if [ ! -f ~/.cache/zsh/git-prompt ]; then
    cc -std=gnu99 -O3 -DNDEBUG -Wno-unused-result \
      ~/.config/zsh/git-prompt.c -o ~/.cache/zsh/git-prompt
    if [ $? -ne 0 ]; then
      echo "git-prompt was not compiled, is a C99 toolchain installed?"
    fi
  fi
}

prompt_fresh_setup "$@"

# vim:ft=zsh