~sircmpwn/ctools

Added highlighting for current day v2 PROPOSED

Lo: 1
 Added highlighting for current day

 1 files changed, 53 insertions(+), 22 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/ctools/patches/9833/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH v2] Added highlighting for current day Export this patch

---
 src/cal.c | 75 +++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 53 insertions(+), 22 deletions(-)

diff --git a/src/cal.c b/src/cal.c
index 4f21db7..4fe4ce9 100644
--- a/src/cal.c
+++ b/src/cal.c
@@ -1,3 +1,4 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -9,12 +10,14 @@ enum config {
	INNER_SPACE = 1,
	COL_ROW_SPACES = 4,
	MONTH_WIDTH = 2 * 7 + 6 * INNER_SPACE + 1
	/*2 chars per day, 6 INNER_SPACE between days and 1 for '\0'*/
};

const char *days_of_the_week[7] = {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"};

const int days_in_months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

const int first_week_1752[7] = {-1, -1, 1, 2, 14, 15, 16};
const int first_week_sep_1752[7] = {-1, -1, 1, 2, 14, 15, 16};

const char *months[12] = {"January",
		"February",
@@ -65,7 +68,7 @@ first_day_of_month(int year, int month) {

	h = h % 7;

	/*Zeller's formula starts from h0 = Saturday*/
	/*Zeller's formula starts from 0 = Saturday, adjust it to get 0=Sunday*/
	return h - 1 >= 0 ? h - 1 : 6;
}

@@ -90,8 +93,7 @@ center_print(char *dest, const char *src, size_t dest_size) {
struct month_str
create_month_str(int year, int month) {
	struct month_str m;

	size_t year_digits = n_digits(year);
	int year_digits = n_digits(year);
	size_t l = strlen(months[month]) + year_digits + 1;
	char s[l + 1];
	sprintf(s, "%s %d", months[month], year);
@@ -112,7 +114,9 @@ create_month_str(int year, int month) {
	*  24 25 26 27 28 29 30
 	*/
	if (year == 1752 && month == 8) {
		memcpy(&m.days_numbers[0], first_week_1752, sizeof(first_week_1752));
		memcpy(&m.days_numbers[0],
				first_week_sep_1752,
				sizeof(first_week_sep_1752));
		int k = 17;
		for (int i = 7; i < 21; i++) {
			m.days_numbers[i / 7][i % 7] = k++;
@@ -142,16 +146,22 @@ create_month_str(int year, int month) {
}

void
print_day(int d, int col_index) {
print_day(int d, int col_index, bool highlight) {
	/*If d == -1, then the we should print 2 + INNER_SPACE spaces. 
	 * in case the current position is at the left (col_index ==0) 
	 * then we print only 2 spaces*/

	if (d == -1) {
		printf("%*s", col_index == 0 ? 2 : 2 + INNER_SPACE, "");
	} else {
		if (col_index != 0) {
			printf("%*s", INNER_SPACE, "");
			printf("%2d", d);

			if (highlight) {
				printf("%s%2d%s", "\033[7m", d, "\033[27m");
			} else {
				printf("%2d", d);
			}
		} else {
			printf("%2d", d);
		}
@@ -159,14 +169,27 @@ print_day(int d, int col_index) {
}

void
print_cal(int year, int month) {
	int n_rows = 4;
	int n_cols = 3;
	/*TODO: find a POSIX way to get the width of the terminal for printing
	 * and avoid setting fixed values for n_rows and n_cols
	 * */
	/*if month is specified, print a single month
	 * */
set_rows_cols(int *n_rows, int *n_cols) {
	/*n_cols represent the number of months per row to print.
	 * We set an hard limit to the number of chars per line to 80.
	 * TODO: find, if any exists, a POSIX way to get the size of the current 
	 * terminal instead of hard coding it to 80 chars.*/
	int ws_cols = 80;
	if (ws_cols >= 2 * COL_ROW_SPACES + 3 * MONTH_WIDTH) {
		*n_cols = 3;
	} else if (ws_cols >= COL_ROW_SPACES + 2 * MONTH_WIDTH) {
		*n_cols = 2;
	} else {
		*n_cols = 1;
	}
	*n_rows = 12 / *n_cols;
}

void
print_cal(int year, int month, int today_year, int today_month, int today_day) {
	int n_rows, n_cols;
	set_rows_cols(&n_rows, &n_cols);
	/*if month is specified, print a single month */
	if (month != -1) {
		struct month_str m = create_month_str(year, month);
		printf("%s\n", m.month_year_header);
@@ -181,7 +204,9 @@ print_cal(int year, int month) {

		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 7; j++) {
				print_day(m.days_numbers[i][j], j);
				print_day(m.days_numbers[i][j],
						j,
						m.days_numbers[i][j] == today_day);
			}
			printf("\n");
		}
@@ -223,7 +248,11 @@ print_cal(int year, int month) {
			for (int i = 0; i < 6; i++) {
				for (int c = 0, p = r * n_cols; c < n_cols; c++, p++) {
					for (int j = 0; j < 7; j++) {
						print_day(months_arr[p].days_numbers[i][j], j);
						print_day(months_arr[p].days_numbers[i][j],
								j,
								year == today_year && p == today_month &&
										months_arr[p].days_numbers[i][j] ==
												today_day);
					}
					printf("%*s", COL_ROW_SPACES, "");
				}
@@ -247,14 +276,16 @@ main(int argc, char *argv[]) {

	int month = -1;
	int year = -1;

	time_t now = time(0);
	struct tm *ct;
	ct = localtime(&now);

	if (argc == 1) {

		/* If no argument is passed consider current date */
		time_t now = time(0);
		struct tm *ct;
		ct = localtime(&now);
		month = ct->tm_mon;		   /* tm_mon in [0,11] as for the other inputs*/
		year = ct->tm_year + 1900; /* years since 1900*/
		month = ct->tm_mon;		   /* tm_mon in [0,11] as for the other inputs*/
	} else if (argc == 2) {
		/* only year specified */
		char *endptr;
@@ -282,6 +313,6 @@ main(int argc, char *argv[]) {
		}
	}
	/*month in [0-11] */
	print_cal(year, month);
	print_cal(year, month, ct->tm_year + 1900, ct->tm_mon, ct->tm_mday);
	return 0;
}
-- 
2.25.1
Can you gate this feature behind an #ifdef ANSI_COMPATIBLE, and add an
option to meson_options.txt to enable it? POSIX doesn't define these
escape sequences.

This also applies to the earlier patch: some of these indents are pretty
extreme. See if you can refactor these deeper branches into separate
functions.

I also don't see a way to enable highlighting? Maybe just do it by
default if isatty(stdout) is true.