[PATCH] cal: add an option to control highlight date

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> - add an option to the cal command to control highlight date
>  (cal -v YYYY/MM/DD)

I've done this TODO.

If the specified date is within the displayed calendar(s), this date will
be highlighted.

This option can be used with other options such as option -3.

The manpage has been updated accordingly.

Signed-off-by: Li Zefan <lizf@xxxxxxxxxxxxxx>
---
 cal.1 |    3 +++
 cal.c |   50 +++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 50 insertions(+), 3 deletions(-)

diff --git a/misc-utils/cal.1 b/misc-utils/cal.1
index 0f6af65..db1f89a 100644
--- a/misc-utils/cal.1
+++ b/misc-utils/cal.1
@@ -42,6 +42,7 @@
 .Nd displays a calendar
 .Sh SYNOPSIS
 .Nm cal
+.Op Fl v Ar date
 .Op Fl smjy13
 .Op [ Ar month ] Ar year
 .Sh DESCRIPTION
@@ -63,6 +64,8 @@ Display Sunday as the first day of the week.
 Display Monday as the first day of the week.
 .It Fl j
 Display Julian dates (days one-based, numbered from January 1).
+.It Fl v Ar date
+Highlight the specified date(YYYY/MM/DD).
 .It Fl y
 Display a calendar for the current year.
 .El
diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index f5ede93..818f90b 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -264,6 +264,7 @@ main(int argc, char **argv) {
 	int ch, day, month, year, yflag;
 	char *progname, *p;
 	int num_months = NUM_MONTHS;
+	char *hl_date = NULL;
 
 	progname = argv[0];
 	if ((p = strrchr(progname, '/')) != NULL)
@@ -309,7 +310,7 @@ main(int argc, char **argv) {
 #endif
 
 	yflag = 0;
-	while ((ch = getopt(argc, argv, "13mjsyV")) != -1)
+	while ((ch = getopt(argc, argv, "13mjsv:yV")) != -1)
 		switch(ch) {
 		case '1':
 			num_months = 1;		/* default */
@@ -326,6 +327,9 @@ main(int argc, char **argv) {
 		case 'j':
 			julian = 1;
 			break;
+		case 'v':
+			hl_date = optarg;
+			break;
 		case 'y':
 			yflag = 1;
 			break;
@@ -371,6 +375,47 @@ main(int argc, char **argv) {
 	default:
 		usage();
 	}
+
+	/* control highlight date */
+	if (isatty(1) && hl_date != NULL) {
+		int hl_day, hl_month, hl_year;
+		int mday;
+		int highlight;
+
+		if (sscanf(hl_date, "%d/%d/%d", &hl_year, &hl_month, &hl_day) != 3)
+			errx(1, _("illegal date value: use YYYY/MM/DD"));
+		if (hl_year < 1 || hl_year > 9999)
+			errx(1, _("illegal year value: use 1-9999"));
+		if (hl_month < 1 || hl_month > 12)
+			errx(1, _("illegal month value: use 1-12"));
+		mday = days_in_month[leap_year(hl_year)][hl_month];
+		if (hl_day < 1 || hl_day > mday)
+			errx(1, _("illegal day value: use 1-%d"), mday);
+
+		highlight = 0;
+		if (!month) {
+			if (hl_year == year)
+				highlight = 1;
+		}
+		else if (month && num_months == 1) {
+			if (hl_year == year && hl_month == month)
+				highlight = 1;
+		}
+		else if (month && num_months == 3) {
+			if (hl_year == year && abs(hl_month - month) <= 1)
+				highlight = 1;
+			else if (hl_year == year - 1 && month == 1 && hl_month == 12)
+				highlight = 1;
+			else if (hl_year == year + 1 && month == 12 && hl_month == 1)
+				highlight = 1;
+		}
+
+		if (highlight)
+			day = day_in_year(hl_day, hl_month, hl_year);
+		else
+			day = 0;
+	}
+
 	headers_init();
 
 	if (month && num_months == 1)
@@ -806,7 +851,6 @@ center(str, len, separate)
 void
 usage()
 {
-
-	(void)fprintf(stderr, _("usage: cal [-13smjyV] [[month] year]\n"));
+	fprintf(stderr, _("usage: cal [-v YYYY/MM/DD] [-13smjyV] [[month] year]\n"));
 	exit(1);
 }
---

-
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux