Tuesday, March 29, 2011

HIbernate mac from cli

I forgot to turn off my mac and left the house, so what's a guy to do? Right log in via ssh (having dyndns working is a real plus here) and hibernate it. To do this use the following snippet in a script.
osascript << EOT
tell application "System Events"

Control apple network profile from the command line

I have to change between a network with a fixed address setup and some others with dhcp quite often, so i was really glad i found out about networksetup. The following snippet switches from the static to the dhcp profile, both of which i defined earlier in the network settings.
networksetup -setnetworkserviceenabled static off
networksetup -setnetworkserviceenabled DHCP on
This is a real time and sanity saver!

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.