calendar

BSD calendar reimplementation
git clone https://git.ce9e.org/calendar.git

commit
6c5cc94ae327a1e17b15b9ef5b0aa1d0a541d475
parent
bda1f90742053c2c71866c5678cdac0bc6842c2b
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2023-06-04 13:29
add support for colors

Diffstat

M calendar.c 37 ++++++++++++++++++++++++++++++-------

1 files changed, 30 insertions, 7 deletions


diff --git a/calendar.c b/calendar.c

@@ -36,6 +36,10 @@
   36    36  *
   37    37  *     May Sun+2	05/Sun+2
   38    38  *     04/SunLast	04/Sun-1
   -1    39  *
   -1    40  * This implementation also adds some support for metadata:
   -1    41  *
   -1    42  *     1999/06/15	my description	color:2
   39    43  */
   40    44 
   41    45 #define _XOPEN_SOURCE 500
@@ -67,6 +71,7 @@ struct tpl {
   67    71 struct line {
   68    72 	struct line *next;
   69    73 	struct tpl tpl;
   -1    74 	int color;
   70    75 	char *desc;
   71    76 };
   72    77 
@@ -342,12 +347,21 @@ struct tpl parse_date(char *s) {
  342   347 }
  343   348 
  344   349 struct line *parse_line(char *s_line) {
   -1   350 	s_line[strlen(s_line) - 1] = '\0';  // remove newline
  345   351 	char *s_date = strtok(s_line, "\t");
  346    -1 	const char *desc = strtok(NULL, "\n");
   -1   352 
  347   353 	struct line *line = (struct line *)malloc(sizeof(struct line));
  348    -1 	line->tpl = parse_date(s_date);
  349    -1 	line->desc = strdup(desc);
   -1   354 	line->desc = strdup(strtok(NULL, "\t"));
  350   355 	line->next = NULL;
   -1   356 	line->color = 0;
   -1   357 
   -1   358 	char *token;
   -1   359 	while ((token = strtok(NULL, "\t"))) {
   -1   360 		sscanf(token, "color:%i", &line->color);
   -1   361 	}
   -1   362 
   -1   363 	line->tpl = parse_date(s_date);
   -1   364 
  351   365 	return line;
  352   366 }
  353   367 
@@ -399,7 +413,7 @@ void free_lines(struct line *line) {
  399   413 	}
  400   414 }
  401   415 
  402    -1 void print_matches(struct tm date, struct line *first) {
   -1   416 void print_matches(struct tm date, struct line *first, bool use_color) {
  403   417 	char ds[11];
  404   418 	strftime(ds, 11, "%a %b %d", &date);
  405   419 
@@ -411,7 +425,11 @@ void print_matches(struct tm date, struct line *first) {
  411   425 		if (is_match(line->tpl, date)) {
  412   426 			if (!is_first) printf("; ");
  413   427 			is_first = false;
  414    -1 			printf("%s", line->desc);
   -1   428 			if (use_color && line->color) {
   -1   429 				printf("\033[0;3%im%s\033[0m", line->color, line->desc);
   -1   430 			} else {
   -1   431 				printf("%s", line->desc);
   -1   432 			}
  415   433 		}
  416   434 		line = line->next;
  417   435 	}
@@ -424,7 +442,11 @@ void print_matches(struct tm date, struct line *first) {
  424   442 			if (!line->tpl.year) {
  425   443 				printf("*");
  426   444 			}
  427    -1 			printf("\t%s\n", line->desc);
   -1   445 			if (use_color && line->color) {
   -1   446 				printf("\t\033[0;3%im%s\033[0m\n", line->color, line->desc);
   -1   447 			} else {
   -1   448 				printf("\t%s\n", line->desc);
   -1   449 			}
  428   450 		}
  429   451 		line = line->next;
  430   452 	}
@@ -442,6 +464,7 @@ void help() {
  442   464 int main(int argc, char *argv[]) {
  443   465 	int days_before = 0;
  444   466 	int days_after = 3;
   -1   467 	bool use_color = isatty(1);
  445   468 
  446   469 	int c;
  447   470 	while ((c = getopt(argc, argv, "hB:A:")) != -1) {
@@ -472,7 +495,7 @@ int main(int argc, char *argv[]) {
  472   495 	struct tm day;
  473   496 	for (int i = -days_before; i < days_after; i++) {
  474   497 		day = add_days(*TODAY, i);
  475    -1 		print_matches(day, first);
   -1   498 		print_matches(day, first, use_color);
  476   499 	}
  477   500 
  478   501 	free_lines(first);