Tuesday·30·November·2010
Useful but Unknown Unix Tools: colored cal(endar) //at 02:09 //by abe
Another thing I regard as useful on the commandline are colors. (Others commonly refer to my color preferences as eye cancer, but that’s not relevant here. ;-) Colors help to easily distinguish between relevant and non-relevant things or to separate different things.
One colored command line program I use nearly everyday is the colored fork of bsdmainutils’s well know (or at least better known) “cal” program whose output looks like this:
$ cal
November 2010
Su Mo Tu We Th Fr Sa
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
$
Since the colored fork calls itself still “cal” (as the Debian source package is called), to avoid conflicts with bsdmainutils’s cal, the binary package and the binary itself are called ccal (short for colored cal) and its output looks like this:
$ ccal November 2010 Mo Tu We Th Fr Sa Su 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> $
ccal also use the locale definitions (LC_TIME), if available, that’s why the one “screenshot” is Sun-Sat and the other one is Mon-Sun.
According to the man page, ccal has some more improvements over the classic cal:
If displaying the single-month format, ccal will look for a date file (the default file or whatever you specify with the -d option). If found, ccal will read the file, looking for special date descriptions for that month which will be displayed to the right of the calendar. By default, up to 24 appointments (number may be changed with -m) may be displayed per month. If the current date happens to fall on one of these special dates, it will be flagged by an asterisk. If there is room, appointments for the next month may also be displayed (next monthâs dates having definitions like “2nd Thursday” will be skipped).
But I must admit, I don’t use that feature. I just use it as on-demand calendar sheet.
The colored “screenshot” above is btw. generated with Adam Borowski’s ansi2html which popped up after I published my Intent to package “aha” (for Ansi HTML Adapter), the first ANSI colors to HTML converter I found on the net. (And yes I searched for it because of this and some yet to come blog postings. No spoilers here though. ;-)
While capturing colored output of “ls” and some other tools was easy, it needed a little hack to capture ccal’s output, because if you pipe ccal output to anything, it drops its colors. Always. The solution was to run it in GNU Screen, log the output and then pipe the log file through “aha” or “ansi2html”:
$ screen -c /dev/null -L ccal; cat screenlog.0 | ansi2html > ccal.html
Probably the output of every program which drops colors when writing to a pipe instead of a terminal can be fetched that way.
Update, 2:04 CET: As Adam Borowski points out in message 62 of #605380, “script” is the better tool for making tools believe they talk to a terminal:
$ script -q /dev/null -c "ccal" | ansi2html
Catching the output of a whole year’s calender (“ccal 2011”) failed
with GNU screen though, as it was truncated on the 24th of September
by GNU screen (as it was already truncated in the screenlog.0), so no
“screenshot” of that for the moment. And with script, I’d get the
whole output, but would have to manually fix the styles so they don’t
get posted literally to Planet Debian. So look for yourself how the
output of “ccal 2011” looks like by installing ccal. ;-)
Tagged as: aha, ANSI, ansi2html, cal, calendar, ccal, Colorful Console, Eye Cancer, GNU Screen, HTML, ITP, LC_TIME, locales, nuggets, Planet Debian, screen, UUUT
// show without comments // write a comment