Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Even more fun with cal and GeekTool' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Even more fun with cal and GeekTool
Authored by: dILVISH on Jul 05, '04 09:19:51PM
Well, no one volunteered to improve upon my code, which is too bad. But I *did* make a couple of small improvements that are worth mentioning. This will display the previous, current and next month vertically, with the current date highlighted with parenthesis.

I'm pretty sure the original post from wallybear would have had a problem highlighting the first or last days of the week, so that's been fixed here. I did NOT include the change so the week starts on Monday, I like Sunday.

Here is the code:


#!/bin/zsh

month=`date +%m`
year=`date +%Y`

nextMonth=$(($month+1))
lastMonth=$(($month-1))

if [[ $nextMonth -gt 12 ]]
then
        nextMonth=1
        year=$(($year+1))
fi

if [[ $lastMonth -eq 0 ]]
then
        lastMonth=12
        year=$(($year-1))
fi

cal $lastMonth $year | awk '{ print $0; while (getline != 0 ) { print " "$0 }}'
cal | awk -v cday=`date "+%d"` '{ fill=(int(cday)>9?"":" "); print " "$0; getline; print " "$0; while (getline != 0) {a=" "$0" "; sub(" "fill int(cday)" ",fill "(" int(cday)")", a); print a }}'
cal $nextMonth $year | awk '{ print $0; while (getline != 0 ) { print " "$0 }}'
and here is the output:

     June 2004
  S  M Tu  W Th  F  S
        1  2  3  4  5
  6  7  8  9 10 11 12
 13 14 15 16 17 18 19
 20 21 22 23 24 25 26
 27 28 29 30
 
      July 2004
  S  M Tu  W Th  F  S
              1  2  3 
  4 (5) 6  7  8  9 10 
 11 12 13 14 15 16 17 
 18 19 20 21 22 23 24 
 25 26 27 28 29 30 31 
  
    August 2004
  S  M Tu  W Th  F  S
  1  2  3  4  5  6  7
  8  9 10 11 12 13 14
 15 16 17 18 19 20 21
 22 23 24 25 26 27 28
 29 30 31

Last word on the subject, I promise!

[ Reply to This | # ]

Even more fun with cal and GeekTool
Authored by: wallybear on Jul 07, '04 04:56:32AM

No, my code has no problem marking first or last day of the week, as I added a space before and after each line, in order to make the replace string working everywhere.
Just give it a try...



[ Reply to This | # ]
Even more fun with cal and GeekTool
Authored by: mrchaotica on Jul 09, '04 01:09:56AM
there's one serious issue with your script: When the month is december or january, it wil display the wrong year, since you only calculate it once. If it's december, you have to call only the next month with "year + 1", and if it's january, you have to call only the previous month with "year - 1" Here's the fix:

#!/bin/zsh

month=`date +%m`
year=`date +%Y`

nextMonth=$(($month+1))
lastMonth=$(($month-1))

if [[ $month -eq 1 ]]
then
        lastMonth=12
        cal $lastMonth $(($year-1)) | awk '{ print $0; while (getline != 0 ) { print " "$0 }}'
else
        cal $lastMonth $year | awk '{ print $0; while (getline != 0 ) { print " "$0 }}'
fi

cal | awk -v cday=`date "+%d"` '{ fill=(int(cday)>9?"":" "); print " "$0; getline; print " "$0; while (getline != 0) {a=" "$0" "; sub(" "fill int(cday)" ",fill "(" int(cday)")", a); print a }}'

if [[ $month -eq 12 ]]
then
        nextMonth=1
        cal $nextMonth $(($year+1)) | awk '{ print $0; while (getline != 0 ) { print " "$0 }}'
else
        cal $nextMonth $year | awk '{ print $0; while (getline != 0 ) { print " "$0 }}'
fi


[ Reply to This | # ]