tsv2agenda: add line wrapping - ics2txt - convert icalendar .ics file to plain text HTML git clone git://bitreich.org/ics2txt git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/ics2txt DIR Log DIR Files DIR Refs DIR Tags DIR README --- DIR commit d1604d44d1926ce38a94073ad8d55b2c8a99d172 DIR parent 3b9f2bbdc0b786d3732555f8e7a001f40eaa59c6 HTML Author: Josuah Demangeon <me@josuah.net> Date: Fri, 25 Jun 2021 20:18:33 +0200 tsv2agenda: add line wrapping Diffstat: M tsv2agenda.c | 50 ++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 11 deletions(-) --- DIR diff --git a/tsv2agenda.c b/tsv2agenda.c @@ -106,20 +106,48 @@ print_header(AgendaCtx *ctx, struct tm *beg, struct tm *end, size_t *num) } static void -print_row(AgendaCtx *ctx, char *line, struct tm *beg, struct tm *end, size_t *num) +unescape(char const *s, char *d) { + for (; *s != '\0'; s++) { + if (*s == '\\') { + s++; + *d++ = (*s == 'n') ? '\n' : (*s == 't') ? ' ' : *s; + } else { + if (*s == '\\') + debug("s='%c'", *s); + *d++ = *s; + } + } + *d = '\0'; +} + +static void +print_row(AgendaCtx *ctx, char *s, struct tm *beg, struct tm *end, size_t *num) +{ + unescape(s, s); + print_header(ctx, beg, end, num); - for (char *cp = line; *cp != '\0'; cp++) { - if (*cp == '\\') { - switch (*++cp) { - case 'n': - fputc('\n', stdout); - print_header(ctx, beg, end, num); - fputs(": ", stdout); - continue; - } + for (size_t i, n = 0; *s != '\0'; s++) { + switch (*s) { + case '\n': +newline: + fputc('\n', stdout); + print_header(ctx, beg, end, num); + fputs(": ", stdout); + n = 0; + break; + case ' ': + case '\t': + i = strcspn(s + 1, " \t\n"); + if (n + i > 70) + goto newline; + fputc(' ', stdout); + n++; + break; + default: + fputc(*s, stdout); + n++; } - fputc(*cp, stdout); } fputc('\n', stdout); }