#!/usr/bin/env zsh

# hide EOL sign ('%')
export PROMPT_EOL_MARK=""

# TTY (pure linux) terminal only has 8-bit color support
# (unless you change it in kernel), respect this and downgrade
# the color scheme accordingly (it won't look best, but it's
# still better than no colors)
if [ $TERM = "linux" ]; then
	GREEN="%F{002}"
	RED="%F{001}"
	ORANGE="%F{003}"
	BLUE="%F{004}"
	LBLUE="%F{006}"
	PURPLE="%F{005}"
else
	GREEN="%F{047}"
	RED="%F{196}"
	ORANGE="%F{214}"
	BLUE="%F{027}"
	LBLUE="%F{075}"
	PURPLE="%F{105}"
fi
RESET="%f"

# Signals git status of CWD repository (if any)
git_prompt() {
    ref=$(command git symbolic-ref HEAD 2> /dev/null) || ref=$(command git rev-parse --short HEAD 2> /dev/null) || return 0
    echo -n " $ORANGE${ref#refs/heads/}"

    if [ ! -z "$(git status --short)" ]; then
        echo "$RED+"
    fi
}

# Adds @chroot or @ssh
foreign_prompt() {
	if [ "$(awk '$5=="/" {print $1}' </proc/1/mountinfo)" != "$(awk '$5=="/" {print $1}' </proc/$$/mountinfo)" ]; then
		echo -n "@${ORANGE}chroot"
	fi
	if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then
		echo -n "@${ORANGE}ssh"
	fi
}

setopt promptsubst  # enable command substitution in prompt

# Primary Prompt
[ "$EUID" -eq 0 ] && PS1="$RED%n$RESET" || PS1="$GREEN%n$RESET" # user
PS1+="$(foreign_prompt)"
#PS1+=" $BLUE%~" # Working directory
PS1+=" $BLUE%(5~|%-1~/…/%3~|%4~)" # Shortened working directory
PS1+="\$(git_prompt)"
PS1+=" $PURPLE%(!.#.»)$RESET " # Final symbol (# or »)

# Next line prompt
PS2="$RED\ $RESET"

# Right side prompt (on error)
RPS1="%(?..$RED%? ↵$RESET)"