# Filename: prompt_adam3_setup
# Purpose: a zsh-prompt that changes the color of your prompt based on
# the hostname of your current host
# Authors: Dennis Eriksen <https://dnns.no>
# Bug-Reports: Email <idgatt@dnns.no>
# License: This file is licensed under the GPL v2.
################################################################################
# adam3 prompt theme
prompt_adam3_help () {
cat <<'EOF'
This prompt changes the color of your prompt based on the
hostname of your current host. This can be quite nice if you log in to a lot of
different hosts (if you can manage to distribute the prompt, of course).
EOF
}
prompt_adam3_setup () {
setopt localoptions extendedglob
autoload -Uz vcs_info
# Colors
# Prefer positional arguments. Use environment if posititionals are not
# provided.
local hostcolor=${1:-${PROMPT_ADAM3_COLOR1:-'multi'}}
local usercolor=${2:-${PROMPT_ADAM3_COLOR2:-'blue'}}
local dircolor=${3:-${PROMPT_ADAM3_COLOR3:-'cyan'}}
local timecolor=${4:-${PROMPT_ADAM3_COLOR4:-'green'}}
# Prepare prompt. Start with username
typeset -g PROMPT_ADAM3_PS1="%K{$usercolor}%F{white}%n@%f%k"
# DIR in single-line-mode
typeset -g PROMPT_ADAM3_SLM_DIR="%B%F{$dircolor}%-40<..<%(5~|%-1~/../%3~|%4~)%f%b%<< "
# DIR in double-line-mode
typeset -g PROMPT_ADAM3_DLM_DIR="%B%F{$dircolor}%-10<..<%~%f%b%<<"
# time
typeset -g PROMPT_ADAM3_RPS1="%F{$timecolor}%*%f"
# Promptchar (including exitcode)
typeset -g PROMPT_ADAM3_PROMPTCHAR='%(?..[%F{red}%?%f] )%B%F{%(!.red.white)}%#%f%b '
# same as above, but with $ instead of % as promptchar for unprivileged users
#typeset -g PROMPT_ADAM3_PROMPTCHAR='%B%(!.%F{red}#.F{white}$)%f%b '
# Set some styles for vcs_info
zstyle ':vcs_info:*' enable git svn
zstyle ':vcs_info:*' check-for-changes true
zstyle ':vcs_info:*' check-for-staged-changes true
zstyle ':vcs_info:*' stagedstr ' S'
zstyle ':vcs_info:*' unstagedstr ' U'
zstyle ':vcs_info:*' formats "%s %F{green}%b%F{red}%u%c%f "
zstyle ':vcs_info:git*' actionformats "%s %r/%S %F{green}%b%F{red}%u%c%a%f "
# Change color of host, based on hostname
if [[ $hostcolor == 'multi' ]]; then
# Here we take the charset-number of each character and add them together.
local -i hostnum i
local c
for i in {1..$#HOST}; do
c=$HOST[$i]
(( hostnum += #c ))
done
# define available color-bombos
local -a colors=( black,red black,green black,yellow black,blue \
black,magenta black,cyan red,white red,yellow \
red,cyan green,black green,blue yellow,black \
yellow,blue blue,white blue,red blue,yellow \
magenta,white magenta,yellow cyan,white cyan,blue \
white,black white,red white,blue white,magenta )
# Select color
hostcolor=${colors[$hostnum % $#colors + 1]}
# Go bold?
local B b
((hostnum % 2)) && B=%B b=%b
# for testing purposes
#hostcolor=${colors[$RANDOM % $#colors + 1]}
#(( $RANDOM % 2 )) && B=%B b=%b || B= b=
# This is where we set up the actual prompt.
PROMPT_ADAM3_PS1+="$B%K{${hostcolor%,*}}%F{${hostcolor##*,}}%m%f%k$b "
else
# If one wants a specific color, just set ut, plain and simple
PROMPT_ADAM3_PS1+="%K{$hostcolor}%m%k "
fi
add-zsh-hook precmd prompt_adam3_precmd
}
prompt_adam3_precmd () {
setopt localoptions extendedglob noxtrace
# vcs
vcs_info #$vcs_info_msg_0_
local prom
prom=$PROMPT_ADAM3_PS1
prom+=$PROMPT_ADAM3_SLM_DIR
prom+=$vcs_info_msg_0_
prom+=$PROMPT_ADAM3_PROMPTCHAR
# Calculate prompt-length by first removing all zero-length characters. From
# prompt_bart_setup in zsh.
local prompt_length=${#${(S%%)prom//\%([BSUbfksu]|[FK]\{*\})/}}
# If the prompt is over half the terminal, we go into multiline-mode.
if (( (COLUMNS / 2) < prompt_length )); then
prom=$PROMPT_ADAM3_PS1 # reset prompt
prom+=$PROMPT_ADAM3_DLM_DIR # dir
# We need to recalculate prompt_length to know where to put time
prompt_length=${#${(S%%)prom//\%([BSUbfksu]|[FK]\{*\})/}}
# Add time. Left-pad with spaces.
prom+=${(l:$((COLUMNS - prompt_length + 1 )):)PROMPT_ADAM3_RPS1}
prom+=$'\n' # newline
prom+=$vcs_info_msg_0_ # vcs
prom+=$PROMPT_ADAM3_PROMPTCHAR # promptchar
RPS1= # RPS1 does not support multiline, so reset it when we're in multiline
else
RPS1=$PROMPT_ADAM3_RPS1
fi
PS1=$prom
(( ${+VIRTUAL_ENV} )) && PS1="%F{208}(${VIRTUAL_ENV:t})%f ${PS1}"
PS2="${PS1}%_> %b%f%k"
PS3="${PS1}?# %b%f%k"
}
prompt_adam3_setup "${@}"
# END OF FILE #################################################################
# vim: filetype=zsh