On Sun, Sep 18, 2005 at 08:52:42PM +1000, Tim Connors wrote:
> I have a patch to output the current date in red (by default; configurable
> via environment variable) for both ncal and cal.

I've gone with a simpler patch, which I've included below. I think the
important thing to have the date stand out, so I've used reverse video
instead of color, and I've made it non-configurable. If stdout is a TTY,
then the current date is shown in reverse.

> If there is sufficient interest, I may try to split the patch into more
> logical units, but this could be difficult, and if there is no interest, I
> won't bother :)

Yes, I'd prefer split patches in the future, as it makes it much easier
for me to review them. I'm interested in -3 support for ncal, and other
improvements you have as well.

Thanks for your interest.

diff --git a/usr.bin/ncal/ncal.c b/usr.bin/ncal/ncal.c
index b2f16e2..f33205b 100644
--- a/usr.bin/ncal/ncal.c
+++ b/usr.bin/ncal/ncal.c
@@ -50,7 +50,7 @@
 #define MONTH_WIDTH_J 24
 #define MONTH_WIDTH 18
 
-#define MAX_WIDTH 28
+#define MAX_WIDTH 36
 
 typedef struct date date;
 
@@ -162,6 +162,8 @@ int nswitchb;               /* switch date for backwa
 
 int    weekstart;              /* day the week starts on (Sun [0] - Sat [6]) */
 
+date   today;                  /* current date (for highlighting) */
+
 char   *align(char *s, char *t, int w, int l);
 #define center(s, t, w) align((s), (t), (w), 0)
 void   mkmonth(int year, int month, int jd_flag, struct monthlines * monthl);
@@ -186,6 +188,8 @@ int     weekdayb(int nd);
 int
 main(int argc, char *argv[])
 {
+       time_t  t;                      /* For obtaining current day */
+       struct  tm *tm;                 /* "                     "   */
        struct  djswitch *p, *q;        /* to search user defined switch date */
        date    never = {5875706, 1, 1};/* outside valid range of dates */
        date    ukswitch = {1752, 9, 2};/* switch date for Great Britain */
@@ -310,14 +314,16 @@ main(int argc, char *argv[])
        argc -= optind;
        argv += optind;
 
-       if (argc == 0) {
-               time_t t;
-               struct tm *tm;
+       t = time(NULL);
+       tm = localtime(&t);
 
-               t = time(NULL);
-               tm = localtime(&t);
-               y = tm->tm_year + 1900;
-               m = tm->tm_mon + 1;
+       today.y = tm->tm_year + 1900;
+       today.m = tm->tm_mon + 1;
+       today.d = tm->tm_mday;
+
+       if (argc == 0) {
+               y = today.y;
+               m = today.m;
        }
 
        switch (argc) {
@@ -698,8 +704,24 @@ mkmonth(int y, int m, int jd_flag, struc
                                        dt.d = j - jan1 + 1;
                                else
                                        sdate(j, &dt);
-                               memcpy(mlines->lines[i] + k,
-                                      ds + dt.d * dw, dw);
+                               if (isatty(1) && j == sndays(&today)) {
+                                       int n = 0;
+                                       while (ds[dt.d*dw+n] == ' ') {
+                                               mlines->lines[i][k+n] = ' ';
+                                               n++;
+                                       }
+
+                                       memcpy(&mlines->lines[i][k+n],
+                                              "\033[7m", 4);
+                                       memcpy(&mlines->lines[i][k+n+4],
+                                              ds + dt.d * dw + n, dw - n);
+                                       memcpy(&mlines->lines[i][dw+k+4],
+                                              "\033[0m", 4);
+
+                                       k += 8;
+                               } else
+                                       memcpy(mlines->lines[i] + k,
+                                              ds + dt.d * dw, dw);
                        } else
                                memcpy(mlines->lines[i] + k, "    ", dw);
                mlines->lines[i][k] = '\0';
@@ -787,15 +809,31 @@ mkmonthb(int y, int m, int jd_flag, stru
         * column is one day number. print column index: k.
         */
        for (i = 0; i != 6; i++) {
-               for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7;
+               for (j = firsts + 7 * i, k = 0; j < last && k < dw * 7;
                     j++, k += dw)
                        if (j >= first) {
                                if (jd_flag)
                                        dt.d = j - jan1 + 1;
                                else
                                        sdateb(j, &dt);
-                               memcpy(mlines->lines[i] + k,
-                                      ds + dt.d * dw, dw);
+                               if (isatty(1) && j == sndaysb(&today)) {
+                                       int n = 0;
+                                       while (ds[dt.d*dw+n] == ' ') {
+                                               mlines->lines[i][k+n] = ' ';
+                                               n++;
+                                       }
+
+                                       memcpy(&mlines->lines[i][k+n],
+                                              "\033[7m", 4);
+                                       memcpy(&mlines->lines[i][k+n+4],
+                                              ds + dt.d * dw + n, dw - n);
+                                       memcpy(&mlines->lines[i][dw+k+4],
+                                              "\033[0m", 4);
+
+                                       k += 8;
+                               } else
+                                       memcpy(mlines->lines[i] + k,
+                                              ds + dt.d * dw, dw);
                        } else
                                memcpy(mlines->lines[i] + k, "    ", dw);
                if (k == 0)

-- 
gram


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to