libtabular-c

C tabular formatting library
git clone https://git.sinitax.com/sinitax/libtabular-c
Log | Files | Refs | Submodules | sfeed.txt

commit 0367626d37f9352378cab11f8b76cc42c78060b5
parent cf78bbc8205688626487cb15f83aa351a6f07864
Author: Louis Burda <quent.burda@gmail.com>
Date:   Wed,  7 Jun 2023 01:25:05 +0200

Fix column 'truncate' strategy behaviour

Diffstat:
Msrc/tabular.c | 28+++++++++++++---------------
1 file changed, 13 insertions(+), 15 deletions(-)

diff --git a/src/tabular.c b/src/tabular.c @@ -453,9 +453,21 @@ output_row(FILE *file, const struct tabular_cfg *cfg, bool first, done, dirty; char *entry; - for (i = 0; i < cfg->column_count; i++) + for (i = 0; i < cfg->column_count; i++) { fmt->columns[i].written = 0; + if (cfg->columns[i].strategy == TABULAR_TRUNC) { + width = fmt->columns[i].width + - fmt->columns[i].lpad - fmt->columns[i].rpad; + if (row->entries[i].ulen > width) { + width = u8rawlen(row->entries[i].str, width - 2); + row->entries[i].str[width] = '.'; + row->entries[i].str[width+1] = '.'; + row->entries[i].str[width+2] = '\0'; + } + } + } + do { done = true; first = true; @@ -476,15 +488,6 @@ output_row(FILE *file, const struct tabular_cfg *cfg, entry = row->entries[i].str + fmt->columns[i].written; - if (cfg->columns[i].strategy == TABULAR_TRUNC) { - if (u8strlen(entry) > fmt->columns[i].width) { - width = fmt->columns[i].width; - entry[MAX(width - 2, 0)] = '.'; - entry[MAX(width - 1, 0)] = '.'; - entry[MAX(width - 0, 0)] = '\0'; - } - } - dirty = cfg->print_style(file, cfg, row, &cfg->columns[i]); off = 0; @@ -509,11 +512,6 @@ output_row(FILE *file, const struct tabular_cfg *cfg, if (dirty) fprintf(file, "\x1b[0m"); - if (cfg->columns[i].strategy == TABULAR_TRUNC) { - fmt->columns[i].written += u8strlen(entry); - continue; - } - outlen = MIN(u8strlen(entry), width + off); fmt->columns[i].written += u8rawlen(entry, outlen);