cal: Highlight current day

Thanks cls!
This commit is contained in:
sin 2015-11-21 10:19:19 +00:00
parent 211c565b3d
commit 41a9742037

13
cal.c
View File

@ -4,6 +4,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <unistd.h>
#include "util.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 caltype { JULIAN, GREGORIAN };
enum { TRANS_YEAR = 1752, TRANS_MONTH = SEP, TRANS_DAY = 2 }; enum { TRANS_YEAR = 1752, TRANS_MONTH = SEP, TRANS_DAY = 2 };
static struct tm *ltime;
static int static int
isleap(size_t year, enum caltype cal) isleap(size_t year, enum caltype cal)
{ {
@ -56,6 +59,7 @@ printgrid(size_t year, int month, int fday, int line)
{ {
enum caltype cal; enum caltype cal;
int offset, dom, d = 0, trans; /* are we in the transition from Julian to Gregorian? */ 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; cal = (year < TRANS_YEAR || (year == TRANS_YEAR && month <= TRANS_MONTH)) ? JULIAN : GREGORIAN;
trans = (year == TRANS_YEAR && month == TRANS_MONTH); trans = (year == TRANS_YEAR && month == TRANS_MONTH);
@ -72,7 +76,12 @@ printgrid(size_t year, int month, int fday, int line)
if (trans && !(line == 2 && fday == 3)) if (trans && !(line == 2 && fday == 3))
dom += 11; 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) { for (; d < 7 && dom <= monthlength(year, month, cal); ++d, ++dom) {
if (dom == today)
printf("\x1b[7m%2d\x1b[0m ", dom); /* highlight today's date */
else
printf("%2d ", dom); printf("%2d ", dom);
if (trans && dom == TRANS_DAY) if (trans && dom == TRANS_DAY)
dom += 11; dom += 11;
@ -136,7 +145,6 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct tm *ltime;
time_t now; time_t now;
size_t year, ncols, nmons; size_t year, ncols, nmons;
int fday, month; int fday, month;
@ -147,6 +155,9 @@ main(int argc, char *argv[])
month = ltime->tm_mon + 1; month = ltime->tm_mon + 1;
fday = 0; fday = 0;
if (!isatty(STDOUT_FILENO))
ltime = NULL; /* don't highlight today's date */
ncols = 3; ncols = 3;
nmons = 0; nmons = 0;