# 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: Distributed under the same BSD-ish license as zsh itself.
################################################################################
# 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).
Global variables that can be used to change/manipulate the prompt, and their
defaults:
PROMPT_ADAM3_COLOR1=multi # host-color. 'multi' means color changes w/hostname
PROMPT_ADAM3_COLOR2=white,blue # user color
PROMPT_ADAM3_COLOR3=39,none,1 # color for directory
PROMPT_ADAM3_COLOR4=66 # color for time
PROMTP_ADAM3_COLOR6=208 # virtualenv color
# PROMPT_ADAM3_COLOR{1,2,3,4,6} accept comma-separated values for
# "foreground, background, bold". Fore- and background color takes both
# color names and color values. Bold can be set with 1 for bold, and 0 for
# not bold. For example, "blue,white,1" means bold white text on blue
# background. Values can be dropped from the right, so "blue,white" means
# blue text on white background, and "blue" means just blue text.
PROMPT_ADAM3_COLOR5=white # default prompt-color. Only takes a single color.
PROMPT_ADAM3_TIME= # Set to 'false' if you don't want time on RPS1
PROMPT_ADAM3_AOUH= # Set to 'true' if you want "user@host" to show on
# localhost. Else it will only be shown when on remote host
# (through ssh), or when you are root.
PROMPT_ADAM3_PUSER='%n@'
PROMPT_ADAM3_PHOST='%m'
PROMPT_ADAM3_PdirS='%-40<..<%(5~|%-1~/../%3~|%4~)%<< '
PROMPT_ADAM3_PdirD='%-10<..<%~%<<'
PROMPT_ADAM3_PTIME='%D{%H:%M:%S}'
PROMPT_ADAM3_SHLVL='%(10L.%F{58}%L%f .)'
PROMPT_ADAM3_PROMPTCHAR="%(?..[%F{red}%?%f] )%B%F{%(!.red.$PROMPT_ADAM3_COLOR5)}%#%f%b "
EOF
}
prompt_adam3_setup () {
emulate -L zsh
setopt extendedglob
autoload -Uz vcs_info
# Colors
# Prefer positional arguments. Use environment if posititionals are not
# provided.
# Colors are given in a comma-separated way. "foreground,bacground,bold".
# Both fore- and background-colors may be given by name or number. Bold
# should be given with 1 for bold, 0 for no bold. Arguments can be dropped
# from the right. I.e. "blue,white,1" for bold blue text on white background.
# "blue,white" for blue (non-bold) on white background. Or just "blue" for
# just blue text and no given background. 'none' can be given as a color, and
# indicates no color. So, bold blue text with no background is "blue,none,1".
local -A prom color
color[host]=${1:-${PROMPT_ADAM3_COLOR1:-multi}} # host color
color[user]=${2:-${PROMPT_ADAM3_COLOR2:-white,blue}} # user color
color[dirS]=${3:-${PROMPT_ADAM3_COLOR3:-39,none,1}} # dir (single-mode) color
color[dirD]=${3:-${PROMPT_ADAM3_COLOR3:-39,none,1}} # dir (double mode) color
color[time]=${4:-${PROMPT_ADAM3_TIME:-${PROMPT_ADAM3_COLOR4:-66}}} # time color
color[char]=${5:-${PROMPT_ADAM3_COLOR5:-white}} # default promptchar-color
color[venv]=${6:-${PROMPT_ADAM3_COLOR6:-208}} # virtualenv color
# Username
prom[user]=${PROMPT_ADAM3_PUSER:-'%n@'}
# Hostname
prom[host]=${PROMPT_ADAM3_PHOST:-'%m'}
# DIR in single-line-mode
prom[dirS]=${PROMPT_ADAM3_PdirS:-'%-40<..<%(5~|%-1~/../%3~|%~)%<< '}
# DIR in double-line-mode
prom[dirD]=${PROMPT_ADAM3_PdirD:-'%-10<..<%~%<<'}
# Time
[[ $color[time] != false ]] && prom[time]=${PROMPT_ADAM3_PTIME:-'%D{%H:%M:%S}'}
# SHLVL
prom[shlvl]=${PROMPT_ADAM3_SHLVL:-'%(10L.%F{58}%L%f .)'}
# venv
prom[venv]="%F{$color[venv]}(SUBSTITUTE)%f"
# Promptchar (including exitcode)
typeset -g PROMPT_ADAM3_PROMPTCHAR=${PROMPT_ADAM3_PROMPTCHAR:-"%(?..[%F{red}%?%f] )%B%(!.%F{red}.%F{$color[char]})%#%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 '
local c # scope up a throwaway-variable
# Change color of host, based on hostname
if [[ $color[host] == 'multi' ]]; then
# Here we take the charset-number of each character in the hostname, and
# add them together.
local -i hostnum i
for i in {1..$#HOST}; do
c=$HOST[$i]
(( hostnum += #c ))
done
# color-combos. fg,bg
local -a colors
colors=( red,black green,black yellow,black blue,black \
magenta,black cyan,black white,red yellow,red \
cyan,red black,green blue,green black,yellow \
blue,yellow white,blue red,blue yellow,blue \
white,magenta yellow,magenta white,cyan blue,cyan \
black,white red,white blue,white magenta,white )
# Select color
color[host]=${colors[$hostnum % $#colors + 1]}
# Go bold?
((hostnum % 2)) && color[host]+=',1'
# for testing purposes
#color[host]=${colors[$RANDOM % $#colors + 1]}
#((RANDOM % 2)) && color[host]+=',1'
fi
# colorize the prompt-parts. See footnotes for extra comments.
for item in user host dirS dirD time; do
c=(${(s.,.)color[$item]}) # cheaper to do this expansion just once
[[ ${c[1]:-none} != none ]] && prom[$item]="%F{${c[1]}}$prom[${item}]%f"
[[ ${c[2]:-none} != none ]] && prom[$item]="%K{${c[2]}}$prom[${item}]%k"
(( ${c[3]} )) && prom[$item]="%B$prom[${item}]%b"
done
# Make each prompt-part global
typeset -g PROMPT_ADAM3_PS1
# Show user@host if using ssh, PROMPT_ADAM3_AOUH is set, or you are root
[[ -n $SSH_CLIENT || -n $SSH_TTY || -n $SSH_CONNECTION \
|| -n $PROMPT_ADAM3_AOUH || $UID == 0 ]] \
&& PROMPT_ADAM3_PS1="$prom[user]$prom[host] "
typeset -g PROMPT_ADAM3_SLM_DIR=$prom[dirS]
typeset -g PROMPT_ADAM3_DLM_DIR=$prom[dirD]
typeset -g PROMPT_ADAM3_RPS1=$prom[shlvl]$prom[time]
typeset -g PROMPT_ADAM3_VENV=$prom[venv]
# Set up hook
add-zsh-hook precmd prompt_adam3_precmd
}
prompt_adam3_precmd () {
emulate -LR zsh
setopt extendedglob
# vcs
vcs_info #$vcs_info_msg_0_
local prom venv_info
(( ${+VIRTUAL_ENV} )) && venv_info="${PROMPT_ADAM3_VENV/SUBSTITUTE/${VIRTUAL_ENV:t}} "
prom=${PROMPT_ADAM3_PS1}
prom+=$PROMPT_ADAM3_SLM_DIR
prom+=$venv_info
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 zlchars='%([BSUbfksu]|[FK]{*})' # regex for zero-length chars
local prompt_length=${#${(S%%)prom//$~zlchars/}}
# If the prompt is over half the terminal, we go into multiline-mode.
if (( prompt_length > ( COLUMNS / 2 ) )); then
prom=${PROMPT_ADAM3_PS1} # reset prompt
prom+=$PROMPT_ADAM3_DLM_DIR # double-line-mode dir
# We need to recalculate prompt_length to know where to put time
prompt_length=${#${(S%%)prom//$~zlchars/}}
local rprompt_length=${#${(S%%)PROMPT_ADAM3_RPS1//$~zlchars/}}
local timepadding=$((COLUMNS - prompt_length + $#PROMPT_ADAM3_RPS1 - rprompt_length - 3 ))
# Add time. Left-pad with spaces.
prom+=${(l:$timepadding:)PROMPT_ADAM3_RPS1}
prom+=$'\n' # newline
prom+=$venv_info # virtualenv info
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
PS2="${PS1}%_> %b%f%k"
PS3="${PS1}?# %b%f%k"
}
prompt_adam3_setup "${@}"
# END OF FILE #################################################################
# vim: filetype=zsh