commit 41a974203782cf6282c203fd7c16279afc0a10b3
Author:     sin <[email protected]>
AuthorDate: Sat Nov 21 10:19:19 2015 +0000
Commit:     sin <[email protected]>
CommitDate: Sat Nov 21 10:20:07 2015 +0000

    cal: Highlight current day
    
    Thanks cls!

diff --git a/cal.c b/cal.c
index 740dc02..b403d00 100644
--- a/cal.c
+++ b/cal.c
@@ -4,6 +4,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
+#include <unistd.h>
 
 #include "util.h"
 
@@ -11,6 +12,8 @@ enum { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, 
DEC };
 enum caltype { JULIAN, GREGORIAN };
 enum { TRANS_YEAR = 1752, TRANS_MONTH = SEP, TRANS_DAY = 2 };
 
+static struct tm *ltime;
+
 static int
 isleap(size_t year, enum caltype cal)
 {
@@ -56,6 +59,7 @@ printgrid(size_t year, int month, int fday, int line)
 {
        enum caltype cal;
        int offset, dom, d = 0, trans; /* are we in the transition from Julian 
to Gregorian? */
+       int today = 0;
 
        cal = (year < TRANS_YEAR || (year == TRANS_YEAR && month <= 
TRANS_MONTH)) ? JULIAN : GREGORIAN;
        trans = (year == TRANS_YEAR && month == TRANS_MONTH);
@@ -72,8 +76,13 @@ printgrid(size_t year, int month, int fday, int line)
                if (trans && !(line == 2 && fday == 3))
                        dom += 11;
        }
+       if (ltime && year == ltime->tm_year + 1900 && month == ltime->tm_mon)
+               today = ltime->tm_mday;
        for (; d < 7 && dom <= monthlength(year, month, cal); ++d, ++dom) {
-               printf("%2d ", dom);
+               if (dom == today)
+                       printf("\x1b[7m%2d\x1b[0m ", dom); /* highlight today's 
date */
+               else
+                       printf("%2d ", dom);
                if (trans && dom == TRANS_DAY)
                        dom += 11;
        }
@@ -136,7 +145,6 @@ usage(void)
 int
 main(int argc, char *argv[])
 {
-       struct tm *ltime;
        time_t now;
        size_t year, ncols, nmons;
        int fday, month;
@@ -147,6 +155,9 @@ main(int argc, char *argv[])
        month = ltime->tm_mon + 1;
        fday  = 0;
 
+       if (!isatty(STDOUT_FILENO))
+               ltime = NULL; /* don't highlight today's date */
+
        ncols = 3;
        nmons = 0;
 

Reply via email to