aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLukasz Kasprzak <lukasz@arcofasiagroup.com>2026-08-21 09:51:36 +0200
committerLukasz Kasprzak <lukasz@arcofasiagroup.com>2026-08-21 09:51:36 +0200
commit147932ae2da02b5eceb6be6e1d704e1199bcb72e (patch)
tree8d6d622551e012ac42598c2d9d96006417496d33
parent7459e218fa8b5b86c059762519447cb8863d3e75 (diff)
downloadcolitur-x-147932ae2da02b5eceb6be6e1d704e1199bcb72e.tar.gz
colitur-x-147932ae2da02b5eceb6be6e1d704e1199bcb72e.zip
fix(colitur-x): detect the CSV header instead of assuming it
Skipping the first line unconditionally punished the obvious way to view one month -- colitur emit --format csv ... | grep '^2027-04' | colitur-x -- by silently eating 1 April, because grep had already removed the header the program still expected. A filter is exactly what this program should compose with. A header is the line beginning 'date,'; anything else is a day. Verified all three ways: full year with header 365 rows from 01-01, a grepped month without header 30 rows from 04-01, and the old head -1 incantation still 30 rows.
-rw-r--r--README.md8
-rwxr-xr-xcolitur-xbin17872 -> 17968 bytes
-rw-r--r--colitur-x.c17
3 files changed, 21 insertions, 4 deletions
diff --git a/README.md b/README.md
index b0872cf..9df7b8e 100644
--- a/README.md
+++ b/README.md
@@ -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 b/colitur-x
index f2b36f7..b5db32a 100755
--- a/colitur-x
+++ b/colitur-x
Binary files differ
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);
}
}