summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLouis Burda <quent.burda@gmail.com>2023-06-11 13:59:03 +0200
committerLouis Burda <quent.burda@gmail.com>2023-06-11 13:59:03 +0200
commita019364e51a8831fc2e7ea3ac57dbc56df5e2a5b (patch)
treefedebf04a8280da511683466100c4ee096e2f47b
parent5ecf4f581ef42a7c6320b29f3e7173944c3cabf6 (diff)
downloadtabular-a019364e51a8831fc2e7ea3ac57dbc56df5e2a5b.tar.gz
tabular-a019364e51a8831fc2e7ea3ac57dbc56df5e2a5b.zip
Add col entries matching string
-rw-r--r--main.c106
1 files changed, 62 insertions, 44 deletions
diff --git a/main.c b/main.c
index 3ef09d1..8327572 100644
--- a/main.c
+++ b/main.c
@@ -13,6 +13,11 @@
#define ARRLEN(x) (sizeof(x)/sizeof(*(x)))
+struct col {
+ struct tabular_col tabular;
+ const char *hide_out;
+};
+
static bool print_style(FILE *file, const struct tabular_cfg *cfg,
const struct tabular_row *row, const struct tabular_col *col);
@@ -24,7 +29,7 @@ static bool col_hidden(const struct tabular_user *user_row,
static const struct allocator *ga = &stdlib_strict_heap_allocator;
-static bool hide_empty = false;
+static bool hide_empty = true;
static bool skip_empty_lines = true;
static bool skip_empty_entries = false;
static bool header_underline = false;
@@ -186,13 +191,14 @@ row_gen(const struct tabular_user *user_cfg)
static char *
col_str(const struct tabular_user *user_row, const struct tabular_user *user_col)
{
+ struct col *col = user_col->ptr;
char *str;
size_t *off;
- if (user_col->id >= dvec_len(&entries))
+ if (col->tabular.user.id >= dvec_len(&entries))
return NULL;
- off = dvec_at(&entries, user_col->id);
+ off = dvec_at(&entries, col->tabular.user.id);
str = strdup(line.data + *off);
if (!str) die("strdup:");
@@ -202,14 +208,18 @@ col_str(const struct tabular_user *user_row, const struct tabular_user *user_col
static bool
col_hidden(const struct tabular_user *user, const struct tabular_user *user_col)
{
+ struct col *col = user_col->ptr;
size_t *off;
- if (user_col->id >= dvec_len(&entries))
- return true;
+ if (col->tabular.user.id >= dvec_len(&entries))
+ return hide_empty;
- off = dvec_at(&entries, user_col->id);
+ off = dvec_at(&entries, col->tabular.user.id);
- return !strcmp(line.data + *off, "");
+ if (col->hide_out && !strcmp(line.data + *off, col->hide_out))
+ return true;
+
+ return hide_empty && !strcmp(line.data + *off, "");
}
static bool
@@ -226,7 +236,8 @@ bool_arg(const char *arg, const char *name)
static void
parse(int argc, const char **argv)
{
- struct tabular_col *col;
+ struct col *col;
+ struct tabular_col *tcol;
const char **arg, **dst;
struct hmap_link *link;
struct hmap_iter iter;
@@ -323,7 +334,8 @@ parse(int argc, const char **argv)
for (dst = arg = argv + 1; *arg; arg++) {
if (!strcmp(*arg, "--col")) {
if (!*++arg) die("missing args");
- col = ga->alloc(ga, sizeof(struct tabular_col), NULL);
+ col = ga->alloc(ga, sizeof(struct col), NULL);
+ tcol = &col->tabular;
if (strlen(*arg) > 63) die("col name too long");
strncpy(namebuf, *arg, 64);
@@ -332,22 +344,23 @@ parse(int argc, const char **argv)
upper = strdup(namebuf);
if (!upper) die("strdup:");
- col->name = *arg;
- col->align = TABULAR_ALIGN_LEFT;
- col->essential = true;
- col->is_hidden = col_hidden;
- col->to_str = col_str;
- col->lpad = 0;
- col->rpad = 0;
- col->minwidth = strlen(col->name);
- col->maxwidth = cfg.outw;
- col->strategy = TABULAR_WRAP_WORDAWARE;
- col->squashable = false;
- col->user.id = colcnt++;
+ col->hide_out = NULL;
+ tcol->name = *arg;
+ tcol->align = TABULAR_ALIGN_LEFT;
+ tcol->essential = true;
+ tcol->is_hidden = col_hidden;
+ tcol->to_str = col_str;
+ tcol->lpad = 0;
+ tcol->rpad = 0;
+ tcol->minwidth = strlen(tcol->name);
+ tcol->maxwidth = cfg.outw;
+ tcol->strategy = TABULAR_WRAP_WORDAWARE;
+ tcol->squashable = false;
+ tcol->user.id = colcnt++;
rc = hmap_add(&colmap,
(struct hmap_key) { .p = upper },
(struct hmap_val) { .p = col });
- if (rc) die("duplicate col %s", *col->name);
+ if (rc) die("duplicate col %s", tcol->name);
} else {
*dst++ = *arg;
}
@@ -363,59 +376,63 @@ parse(int argc, const char **argv)
(struct hmap_key) { .p = namebuf });
if (!link) goto skip;
col = link->value._p;
+ tcol = &col->tabular;
if (!strcmp(*arg + n, "lpad")) {
if (!*++arg) die("missing args");
- col->lpad = strtoul(*arg, &end, 10);
+ tcol->lpad = strtoul(*arg, &end, 10);
if (end && *end) die("bad %s", arg[-1]);
- len = col->lpad + strlen(col->name) + col->rpad;
- if (col->minwidth < len)
- col->minwidth = len;
+ len = tcol->lpad + strlen(tcol->name) + tcol->rpad;
+ if (tcol->minwidth < len)
+ tcol->minwidth = len;
} else if (!strcmp(*arg + n, "rpad")) {
if (!*++arg) die("missing args");
- col->rpad = strtoul(*arg, &end, 10);
+ tcol->rpad = strtoul(*arg, &end, 10);
if (end && *end) die("bad %s", arg[-1]);
- len = col->lpad + strlen(col->name) + col->rpad;
- if (col->minwidth < len)
- col->minwidth = len;
+ len = tcol->lpad + strlen(tcol->name) + tcol->rpad;
+ if (tcol->minwidth < len)
+ tcol->minwidth = len;
} else if (!strcmp(*arg + n, "align")) {
if (!*++arg) die("missing args");
if (!strcmp(*arg, "left")) {
- col->align = TABULAR_ALIGN_LEFT;
+ tcol->align = TABULAR_ALIGN_LEFT;
} else if (!strcmp(*arg, "right")) {
- col->align = TABULAR_ALIGN_RIGHT;
+ tcol->align = TABULAR_ALIGN_RIGHT;
} else if (!strcmp(*arg, "center")) {
- col->align = TABULAR_ALIGN_CENTER;
+ tcol->align = TABULAR_ALIGN_CENTER;
} else {
die("bad %s", arg[-1]);
}
} else if (!strcmp(*arg + n, "squashable")) {
if (!*++arg) die("missing args");
- col->squashable = bool_arg(*arg, arg[-1]);
+ tcol->squashable = bool_arg(*arg, arg[-1]);
} else if (!strcmp(*arg + n, "essential")) {
if (!*++arg) die("missing args");
- col->essential = bool_arg(*arg, arg[-1]);
+ tcol->essential = bool_arg(*arg, arg[-1]);
} else if (!strcmp(*arg + n, "minwidth")) {
if (!*++arg) die("missing args");
- col->minwidth = strtoul(*arg, &end, 10);
- if (col->minwidth > col->maxwidth)
- col->maxwidth = col->minwidth;
+ tcol->minwidth = strtoul(*arg, &end, 10);
+ if (tcol->minwidth > tcol->maxwidth)
+ tcol->maxwidth = tcol->minwidth;
if (end && *end) die("bad %s", arg[-1]);
} else if (!strcmp(*arg + n, "maxwidth")) {
if (!*++arg) die("missing args");
- col->maxwidth = strtoul(*arg, &end, 10);
+ tcol->maxwidth = strtoul(*arg, &end, 10);
if (end && *end) die("bad %s", arg[-1]);
} else if (!strcmp(*arg + n, "strategy")) {
if (!*++arg) die("missing args");
if (!strcmp(*arg, "word-aware")) {
- col->strategy = TABULAR_WRAP_WORDAWARE;
+ tcol->strategy = TABULAR_WRAP_WORDAWARE;
} else if (!strcmp(*arg, "wrap")) {
- col->strategy = TABULAR_WRAP;
+ tcol->strategy = TABULAR_WRAP;
} else if (!strcmp(*arg, "trunc")) {
- col->strategy = TABULAR_TRUNC;
+ tcol->strategy = TABULAR_TRUNC;
} else {
die("bad %s", arg[-1]);
}
+ } else if (!strcmp(*arg + n, "hide")) {
+ if (!*++arg) die("missing args");
+ col->hide_out = *arg;
} else {
goto skip;
}
@@ -431,8 +448,9 @@ skip:
dvec_add_back(&cols, colcnt);
for (HMAP_ITER(&colmap, iter)) {
col = iter.link->value._p;
- memcpy(dvec_at(&cols, col->user.id), col,
- sizeof(struct tabular_col));
+ tcol = dvec_at(&cols, col->tabular.user.id);
+ memcpy(tcol, &col->tabular, sizeof(struct tabular_col));
+ tcol->user.ptr = col;
}
cfg.columns = cols.data;
cfg.column_count = cols.len;