libsmartcols: allow to reduce tree columns

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2015-11-30 11:43:05 +01:00
parent 9dfd601976
commit 440f73b7bc
2 changed files with 12 additions and 5 deletions

View File

@ -75,6 +75,7 @@ struct libscols_column {
size_t width_min; /* minimal width (usually header width) */
size_t width_max; /* maximal width */
size_t width_avg; /* average width, used to detect extreme fields */
size_t width_treeart; /* size of the tree ascii art */
double width_hint; /* hint (N < 1 is in percent of termwidth) */
int flags;

View File

@ -703,8 +703,7 @@ static int count_column_width(struct libscols_table *tb,
if (len == (size_t) -1) /* ignore broken multibyte strings */
len = 0;
if (len > cl->width_max)
cl->width_max = len;
cl->width_max = max(len, cl->width_max);
if (cl->is_extreme && len > cl->width_avg * 2)
continue;
@ -712,8 +711,11 @@ static int count_column_width(struct libscols_table *tb,
sum += len;
count++;
}
if (len > cl->width)
cl->width = len;
cl->width = max(len, cl->width);
if (scols_column_is_tree(cl)) {
size_t treewidth = buffer_get_safe_art_size(buf);
cl->width_treeart = max(cl->width_treeart, treewidth);
}
}
if (count && cl->width_avg == 0) {
@ -866,11 +868,15 @@ static int recount_widths(struct libscols_table *tb, struct libscols_buffer *buf
scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
while (scols_table_next_column(tb, &itr, &cl) == 0) {
DBG(TAB, ul_debugobj(cl, " checking %s (width=%zu, treeart=%zu)",
cl->header.data, cl->width, cl->width_treeart));
if (width <= tb->termwidth)
break;
if (cl->width_hint > 1 && !scols_column_is_trunc(cl))
continue; /* never truncate columns with absolute sizes */
if (scols_column_is_tree(cl))
if (scols_column_is_tree(cl) && width <= cl->width_treeart)
continue; /* never truncate the tree */
if (trunc_only && !scols_column_is_trunc(cl))
continue;