19

The following shell script formats the output of /usr/bin/cal, changing the current DAY and DATE to red. Point geektool to an executable file with the following.

#!/bin/sh
#
# colorcal - change current day and date via ANSI color escape sequences
#
# developed/tested on 10.6.3 with:
# /bin/sh --version GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0)
# geektool 3, 3.0 (12A)
# Terminal Version 2.1.1 (273)
# see http://www.termsys.demon.co.uk/vtansi.htm for color codes.
#
# sed (1) is your friend.
#

# pick color, clear color
color="33[1;31m"
nocolor="33[0m"

# get day & date
current_day=`/bin/date "+%a" | cut -b 1,2`
current_date=`date | awk '{print $3}'`

#color em.
color_day=`echo  "${color}${current_day}${nocolor}"`
color_date=`echo  "${color}${current_date}${nocolor}"`

# format cal output so the sed replacements work at begining and end of cal output.
function calendar {
        /usr/bin/cal | sed 's/ /_/g' | sed -e 's/^/ /' -e 's/$/_/' | sed 's/_/ /g'
}

# run calendar & substitute colored day & date.
calendar | /usr/bin/sed -e "s/ ${current_day} / ${color_day} /" -e "s/ ${current_date} / ${color_date} /"
# END of colorcal

Comments

User Avatar
djholman 619 days ago
fixed a bug in variablecurrent_date as (date "+%d") outputs a two digit day e.g. 01 instead of 1. This breaks the sed replacement of cal output. Replaced with `date | awk '{print $3}'` which provides single character for days 1-9 of the month
User Avatar
zaclondon 600 days ago
I am having trouble displaying the calendar in colour. The colour codes being displayed as text.

Do I need to change a setting somewhere?

I'm using GeekTool 3 on Mac OS X 10.6.4.

Thanks
User Avatar
djholman 598 days ago
Try this.

Replace after the #color em comment the echo with `echo -e` e.g.

#color em.

color_day=`echo -e "${color}${current_day}${nocolor}"`

color_date=`echo -e "${color}${current_date}${nocolor}"`

Odd that you see this as I am running 10.6.4 as well now.
User Avatar
djholman 598 days ago
BTW, /bin/bash requires -e after the echo while /bin/sh apparently does not.
User Avatar
wontolla 573 days ago
I'm having the same problem under 10.6.4
User Avatar
un1xt3r 559 days ago
I also encountered issues under 10.6.4, but all I had to do was escape the color codes. Then it worked lovely.

So, lines 15 and 16 should read more like this:

color="33[1;31m"

nocolor="33[0m"
User Avatar
un1xt3r 559 days ago
Okay. The problem is in the code posted above. It must have had it's escape sequences removed.

Just for giggles, here's lines 15 and 16 again using html entities:

color="\033[1;31m"

nocolor="\033[0m"
User Avatar
ifuxusux 268 days ago
There is no need for that long of a code. Just use cal | sed -E '1,$'"s/ ($(date +%e))( |$)/ $(echo '33[1;31m')1$(echo  '33[0m')2/" 

Log in to comment or register here.