Thursday, March 3, 2011

New zsh Prompt

Spring's coming and it's time for a change. I have always liked the idea of colours in the prompt to make you aware of what kind of platform you are working on.  Stumbling upon this post inspired me and now here is my version for the zsh:

function cnprompt {
  set -A colours red green yellow blue magenta cyan #black white
  local col=${colours[$(hostname | cksum | cut -c2-6) % 6]}
  [[ -n "$SSH_CLIENT" ]] && local sig="%#%#" || local sig="%#"
  # i am thick at times; i need more warnings :P
  PS1="%B%(!.%F{red}root@.)%F{$col}%m%f%(?.. %??)%(1j. %j&.):%b %2~%B%(!.%F{red}.%F{yellow})$sig%f%b"
} 
Quick rundown:
  • first line sets up an array with the colours that make sense
  • second line computes an index into the colours array and sets col accordingly 
  • I check for whether I am on a local machine or logged in via ssh and double up the last sign
  • The main line sets a bold, coloured hostname preceded by a red root@. Then i print the exit code of the last command followed by number of backgrounded processes if any, both of which are a lot more helpful than i first thought. The whole thing ends with the last two dirs in pwd and the var set up in line 3
As for the formula, i ran

for host in list; do $((1+$(echo $host | cksum| cut -c1-2) % 6)); done 
to find the number combination that would cause the least overlap and gave the colours I preferred to the hosts I use most. I also tried using md5 instead or in addition to cksum but the spectrum it created seemed narrower.

No comments:

Post a Comment