diff options
| -rw-r--r-- | README.md | 8 | ||||
| -rwxr-xr-x | colitur-x | bin | 17872 -> 17968 bytes | |||
| -rw-r--r-- | colitur-x.c | 17 |
3 files changed, 21 insertions, 4 deletions
@@ -12,8 +12,12 @@ Keys: arrows or `hjkl` move, `n`/`p` change month, `q` quits. It reads CSV on **stdin** and nothing else, so the shell decides what it shows. One month: - colitur emit --format csv --from 2027 --to 2027 \ - | { head -1; grep "^2027-04"; } | colitur-x + colitur emit --format csv --from 2027 --to 2027 | grep "^2027-04" | colitur-x + +The header line is detected rather than assumed, so filtering it away is +fine. An earlier version skipped the first line unconditionally, which meant +that pipeline silently dropped 1 April -- the program fought the obvious +filter instead of composing with it. Another language, or a local calendar: diff --git a/colitur-x.c b/colitur-x.c index def42fa..06f2f1a 100644 --- a/colitur-x.c +++ b/colitur-x.c @@ -55,15 +55,28 @@ static int split(char *line, char **out) return n; } +/* The header line is DETECTED, not assumed. Skipping the first line + unconditionally punishes the obvious way to look at one month -- + `... | grep "^2027-04" | colitur-x` -- by silently eating the 1st, + which is a filter this program should compose with rather than fight. + A header is exactly the line beginning "date,"; anything else is a day. */ +static int is_header(const char *line) +{ + return strncmp(line, "date,", 5) == 0; +} + static void load(void) { char buf[4096]; - if (!fgets(buf, sizeof buf, stdin)) return; /* header */ + int first = 1; while (nrows < MAXDAYS && fgets(buf, sizeof buf, stdin)) { buf[strcspn(buf, "\n")] = 0; + if (first && is_header(buf)) { first = 0; continue; } + first = 0; + if (buf[0] == 0) continue; char *dup = strdup(buf); if (!dup) break; - if (split(dup, rows[nrows].f) >= 12) nrows++; + if (split(dup, rows[nrows].f) >= 12) nrows++; else free(dup); } } |
