From e2e94816c8456557975edc1bfe8ab9e8701f34a1 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 19 Oct 2023 11:23:20 +0200 Subject: [PATCH] libsmartcols: make calculation more robust * add debug messages * make columns stat debug more compact * default to zero if data undefined * fix "extreme" column enlarging Signed-off-by: Karel Zak --- libsmartcols/src/calculate.c | 50 +++++++++++++++++++++--------------- libsmartcols/src/print.c | 2 +- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/libsmartcols/src/calculate.c b/libsmartcols/src/calculate.c index ebb61344db..e9db3b2998 100644 --- a/libsmartcols/src/calculate.c +++ b/libsmartcols/src/calculate.c @@ -12,19 +12,19 @@ static void dbg_column(struct libscols_table *tb, struct libscols_column *cl) st = &cl->wstat; - DBG(COL, ul_debugobj(cl, "%15s seq=%zu, width=%zd, " - "hint=%d, max=%zu, min=%zu, " + DBG(COL, ul_debugobj(cl, "#%zu %12s: width=%zd " + "hint=%d max=%zu min=%zu " "0x04%x [%s%s%s]", - cl->header.data, cl->seqnum, cl->width, + cl->seqnum, cl->header.data, cl->width, cl->width_hint >= 1.0 ? (int) cl->width_hint : (int) (cl->width_hint * tb->termwidth), st->width_max, st->width_min, cl->flags, - cl->flags & SCOLS_FL_TRUNC ? "trunc" : "", - scols_column_is_right(cl) ? " right" : "", - scols_column_is_noextremes(cl) ? " noextrem" : "")); + cl->flags & SCOLS_FL_TRUNC ? "trunc " : "", + scols_column_is_right(cl) ? "right " : "", + scols_column_is_noextremes(cl) ? "noextrem " : "")); } static void dbg_columns(struct libscols_table *tb) @@ -53,16 +53,16 @@ static int count_cell_width(struct libscols_table *tb, rc = __cursor_to_buffer(tb, buf, 1); if (rc) goto done; - data = ul_buffer_get_data(buf, NULL, NULL); - if (!data) - goto done; - len = scols_table_is_noencoding(tb) ? - mbs_width(data) : - mbs_safe_width(data); + data = ul_buffer_get_data(buf, NULL, NULL); + if (data) { + len = scols_table_is_noencoding(tb) ? + mbs_width(data) : + mbs_safe_width(data); - if (len == (size_t) -1) /* ignore broken multibyte strings */ - len = 0; + if (len == (size_t) -1) /* ignore broken multibyte strings */ + len = 0; + } if (scols_column_is_tree(cl)) { size_t treewidth = ul_buffer_get_safe_pointer_width(buf, SCOLS_BUFPTR_TREEEND); @@ -507,6 +507,8 @@ int __scols_calculate(struct libscols_table *tb, struct ul_buffer *buf) /* enlarge */ if (width < tb->termwidth) { + DBG(TAB, ul_debugobj(tb, " enlarge (extreme, avalable %zu)", + tb->termwidth - width)); if (ignore_extremes) { if (!sorted) { sort_columns(tb, cmp_deviation); @@ -521,14 +523,17 @@ int __scols_calculate(struct libscols_table *tb, struct ul_buffer *buf) continue; if (cl->wstat.width_min == 0 && cl->width == 0) continue; + if (cl->width >= cl->wstat.width_max) + continue; add = tb->termwidth - width; - if (add && cl->wstat.width_max && - cl->width + add > cl->wstat.width_max) + if (add && cl->wstat.width_max + && cl->width + add > cl->wstat.width_max) add = cl->wstat.width_max - cl->width; + if (!add) continue; - DBG(TAB, ul_debugobj(tb, " add +%zd (extreme %s)", + DBG(COL, ul_debugobj(cl, " add +%zd (%s)", add, cl->header.data)); cl->width += add; width += add; @@ -539,7 +544,8 @@ int __scols_calculate(struct libscols_table *tb, struct ul_buffer *buf) } if (width < tb->termwidth && scols_table_is_maxout(tb)) { - DBG(TAB, ul_debugobj(tb, " enlarge width (max-out)")); + DBG(TAB, ul_debugobj(tb, " enlarge (max-out, avalable %zu)", + tb->termwidth - width)); /* try enlarging all columns */ while (width < tb->termwidth) { @@ -547,7 +553,7 @@ int __scols_calculate(struct libscols_table *tb, struct ul_buffer *buf) while (scols_table_next_column(tb, &itr, &cl) == 0) { if (scols_column_is_hidden(cl)) continue; - DBG(TAB, ul_debugobj(tb, " enlarge (max-out %s)", + DBG(COL, ul_debugobj(cl, " add +1 (%s)", cl->header.data)); cl->width++; width++; @@ -557,9 +563,13 @@ int __scols_calculate(struct libscols_table *tb, struct ul_buffer *buf) } } else if (width < tb->termwidth) { /* enlarge the last column */ - DBG(TAB, ul_debugobj(tb, " enlarge width (last column)")); + DBG(TAB, ul_debugobj(tb, " enlarge (last column, avalable %zu)", + tb->termwidth - width)); if (!scols_column_is_right(last_cl)) { + DBG(COL, ul_debugobj(last_cl, " add +%zu (%s)", + tb->termwidth - width, + last_cl->header.data)); last_cl->width += tb->termwidth - width; width = tb->termwidth; } diff --git a/libsmartcols/src/print.c b/libsmartcols/src/print.c index d6a2753dbb..6cff5482a2 100644 --- a/libsmartcols/src/print.c +++ b/libsmartcols/src/print.c @@ -689,7 +689,7 @@ int __cursor_to_buffer(struct libscols_table *tb, data = scols_cell_get_data(ce); datasiz = scols_cell_get_datasiz(ce); } - DBG(CELL, ul_debugobj(cl, "cursor data: '%s' [%zu]", data, datasiz)); + /*DBG(CELL, ul_debugobj(ce, "cursor data: '%s' [%zu]", data, datasiz));*/ } if (!scols_column_is_tree(cl)) -- 2.47.3