# ZSH Theme - Preview: http://dl.dropbox.com/u/4109351/pics/gnzh-zsh-theme.png
# Based on bira theme

# load some modules
autoload -U colors zsh/terminfo # Used in the colour alias below
colors
setopt prompt_subst

# make some aliases for the colours: (coud use normal escap.seq's too)
for color in RED GREEN YELLOW BLUE MAGENTA CYAN WHITE; do
    eval PR_$color='%{$fg[${(L)color}]%}'
done
eval PR_NO_COLOR="%{$terminfo[sgr0]%}"
eval PR_BOLD="%{$terminfo[bold]%}"

# Check the UID
if [[ $UID -ge 1000 ]]; then # normal user
    # If ssh
    if [[ -n "$SSH_CLIENT"  ||  -n "$SSH2_CLIENT" ]]; then
        eval PR_USER='${PR_GREEN}%n${PR_NO_COLOR}'
        eval PR_USER_OP='${PR_GREEN}%#${PR_NO_COLOR}'
        local PR_ARROW_UP='${PR_GREEN}┌── ${PR_NO_COLOR}'
        local PR_ARROW_DOWN='${PR_GREEN}└${PR_NO_COLOR}'
        local PR_PROMPT='$PR_GREEN▷$PR_NO_COLOR'
    else
        eval PR_USER='${PR_MAGENTA}%n${PR_NO_COLOR}'
        eval PR_USER_OP='${PR_BLUE}%#${PR_NO_COLOR}'
        local PR_PROMPT='$PR_MAGENTA▷$PR_NO_COLOR'
        local PR_ARROW_UP='$PR_MAGENTA┌── ${PR_NO_COLOR}'
        local PR_ARROW_DOWN='$PR_MAGENTA└${PR_NO_COLOR}'
    fi
elif [[ $UID -eq 0 ]]; then # root
    eval PR_USER='${PR_RED}%n${PR_NO_COLOR}'
    eval PR_USER_OP='${PR_RED}%#${PR_NO_COLOR}'
    local PR_ARROW_UP='${PR_RED}┌── ${PR_NO_COLOR}'
    local PR_ARROW_DOWN='${PR_RED}└${PR_NO_COLOR}'
    local PR_PROMPT='$PR_RED▷$PR_NO_COLOR'
fi

# Check if we are on SSH or not
if [[ -n "$SSH_CLIENT"  ||  -n "$SSH2_CLIENT" ]]; then
    eval PR_HOST='${PR_GREEN}%M${PR_NO_COLOR}' #SSH
else
    eval PR_HOST='${PR_BLUE}%M${PR_NO_COLOR}' # no SSH
fi

# local return_code="%(?..%{$PR_RED%}%? ↵%{$PR_NO_COLOR%})"
truncated_pwd() {
    local max_allowed_size=$(( `tput cols` - 30 ))
    local path_name=$(print -rD $PWD)
    local new_path_name=""
    local truncated=0
    while [ ${#path_name} -gt $max_allowed_size ]; do
        truncated=1
        new_path_name=`echo $path_name | cut -d '/' -f 2-`
        if [[ "$new_path_name" == "$path_name" ]]; then
            break
        fi
        path_name=$new_path_name
    done
    if [ $truncated -eq 1 ]; then
        echo "~/.../"$path_name
    else
        echo $path_name
    fi
}

local user_host='$PR_BOLD${PR_USER}$PR_BOLD'
local current_dir='$PR_BOLD%{$PR_BLUE%}$(truncated_pwd)$PR_NO_COLOR%}'
local rvm_ruby=''
if which rvm-prompt &> /dev/null; then
    rvm_ruby='%{$PR_RED%}‹$(rvm-prompt i v g s)›%{$PR_NO_COLOR%}'
else
    if which rbenv &> /dev/null; then
        rvm_ruby='%{$PR_RED%}‹$(rbenv version | sed -e "s/ (set.*$//")›%{$PR_NO_COLOR%}'
    fi
fi

local return_code=""
local git_branch='$(git_prompt_info)'

local PR_DATE='${PR_BOLD}${PR_MAGENTA}[%D{%H:%M}]${PR_NO_COLOR}'

#PROMPT="${user_host} ${current_dir} ${rvm_ruby}${git_branch}$PR_PROMPT  "
separator='${PR_YELLOW}::${PR_NO_COLOR}'
tty | read tty_value
if [[ $tty_value == *pts* ]]; then # if in a tty
    PROMPT="$PR_ARROW_UP${PR_DATE} ${user_host}${separator}${PR_NO_COLOR}${current_dir} ${rvm_ruby}${git_branch}
$PR_ARROW_DOWN$PR_PROMPT  ${PR_NO_COLOR}"
    return_code="%(?.%{$PR_GREEN%}%? ↵%{$PR_NO_COLOR%}.%{$PR_RED%}%? ↵%{$PR_NO_COLOR%})"
else
    eval PR_NO_COLOR="$PR_WHITE"
    PROMPT="${user_host} ${current_dir} ${rvm_ruby} ${git_branch}> ${PR_NO_COLOR}"
    return_code="%(?.%{$PR_GREEN%}%? <%{$PR_NO_COLOR%}.%{$PR_RED%}%? <%{$PR_NO_COLOR%})"
fi
RPS1="  ${return_code}"
ZSH_THEME_GIT_PROMPT_PREFIX="%{$PR_YELLOW%}‹"
ZSH_THEME_GIT_PROMPT_SUFFIX="›%{$PR_NO_COLOR%}"

TMOUT=30
TRAPALRM() {
    zle reset-prompt
}