[hackers] [sbase] cal: Highlight current day || sin
commit 41a974203782cf6282c203fd7c16279afc0a10b3
Author: sin <sin_AT_2f30.org>
AuthorDate: Sat Nov 21 10:19:19 2015 +0000
Commit: sin <sin_AT_2f30.org>
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
_AT_@ -4,6 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
+#include <unistd.h>
#include "util.h"
_AT_@ -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)
{
_AT_@ -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);
_AT_@ -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;
}
_AT_@ -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;
_AT_@ -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;
Received on Sat Nov 21 2015 - 11:20:14 CET
This archive was generated by hypermail 2.3.0
: Sat Nov 21 2015 - 11:24:18 CET