From 788e1819d413c1a133c97ec6e892ac61787e67dd Mon Sep 17 00:00:00 2001 From: James Seward Date: Tue, 3 Dec 2013 15:41:57 +0000 Subject: [PATCH] Per-host prompt colouring Hashes the hostname to get a value 1-15 and uses that as the colour for the hostname in the prompt. Includes example theme (jms) which uses it --- lib/functions.zsh | 14 ++++++++++++++ themes/jms.zsh-theme | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 themes/jms.zsh-theme diff --git a/lib/functions.zsh b/lib/functions.zsh index 63ab755cf..1e15e20e5 100644 --- a/lib/functions.zsh +++ b/lib/functions.zsh @@ -15,3 +15,17 @@ function take() { cd $1 } +function string_hash() { + HASHSTR=$1 + HASHSIZE=$2 + + HASHVAL=0 + for i in {1..${#HASHSTR}}; do; + THISCHAR=$HASHSTR[$i] + HASHVAL=$(( $HASHVAL + $((#THISCHAR)) )) + done + HASHSIZE=$(( $HASHSIZE - 1 )) + HASHVAL=$(( $HASHVAL % $HASHSIZE )) + HASHVAL=$(( $HASHVAL + 1 )) + echo $HASHVAL +} diff --git a/themes/jms.zsh-theme b/themes/jms.zsh-theme new file mode 100644 index 000000000..fd93d68f0 --- /dev/null +++ b/themes/jms.zsh-theme @@ -0,0 +1,42 @@ +# check if 'screen' is in TERM +SCR_COLOR=%{$fg[white]%} + +if [ "$TERM" = "screen" ]; then + SCR_COLOR=%{$fg[yellow]%} +fi + +if [ "$TERM" = "screen-bce" ]; then + SCR_COLOR=%{$fg[yellow]%} +fi + +if [ "x$WINDOW" != "x" ]; then + SCR_WINDOW="%{$fg[yellow]%}#$WINDOW" +elif [ "x$TMUX" != "x" ]; then + SCR_WINDOW="%{$fg[yellow]%}#$(tmux display-message -p '#I')" +else + SCR_WINDOW="" +fi + +if [ "x$(battery_pct_prompt)" = "x" ]; then + BATTERY="" +else + BATTERY="$(battery_pct_prompt) " +fi +BATTERY="" + +HOSTNAME=$(hostname) +HOSTCOLOUR=$(string_hash $HOSTNAME:l 15) + +PROMPT='%{$reset_color%}%n%{$fg[white]%}@%{%F{$HOSTCOLOUR}%}%m$SCR_WINDOW$SCR_COLOR:%{$fg[blue]%}%~%{$reset_color%}$(git_prompt_info) %(0?,,%{$fg[red]%}%?!%{$reset_color%} )%(!.%{$fg[red]%}.%{$fg[green]%})%#%{$reset_color%} ' + +RPROMPT='$BATTERY%{$fg_bold[black]%}%*%{$reset_color%}' +# include aws_prompt if the ec2 plugin is loaded +if [ "x$AWS_PLUGIN" = "x1" ]; then + RPROMPT='$(aws_prompt) '$RPROMPT +fi + +ZSH_THEME_GIT_PROMPT_PREFIX=":%{$fg[yellow]%}" +ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%}" +ZSH_THEME_GIT_PROMPT_CLEAN="" +ZSH_THEME_GIT_PROMPT_DIRTY="%F{161}!%{$reset_color%}" +