]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: allow to reduce tree columns
authorKarel Zak <kzak@redhat.com>
Mon, 30 Nov 2015 10:43:05 +0000 (11:43 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 30 Nov 2015 10:43:05 +0000 (11:43 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/src/smartcolsP.h
libsmartcols/src/table_print.c

index 2cf8a9d6df9a9f2c7792e2e35af018c4c8fc3d13..c4df79b534bd933cdf183d8fe3bdb7cedd338a75 100644 (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;
index 6cef68f769c07f127e67695fe6c17e8d61a691f2..316db00fa4c0720533ddb52995ceb4bd681bec61 100644 (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;