]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: enlarge columns with absolute width hint
authorKarel Zak <kzak@redhat.com>
Thu, 12 Mar 2026 09:40:45 +0000 (10:40 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 12 Mar 2026 09:40:45 +0000 (10:40 +0100)
The width hint is strictly followed only if SCOLS_FL_STRICTWIDTH is
specified. The previous patch ensures minimal space usage for all
other columns. This patch enlarges columns with an absolute hint (>
1.0) until the hints are satisfied and there is free space on screen.

Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/src/calculate.c

index 91c831cea218b2ec78c11570f826df09a7bf2c10..efcb5b6afd2dfd4973edba7851cf0bab305e18e3 100644 (file)
@@ -214,12 +214,13 @@ static int count_column_width(struct libscols_table *tb,
        /* this is default, may be later reduced */
        cl->width = st->width_max;
 
-       /* enlarge to minimal width */
+       /* enlarge to minimal width (usually header width) */
        if (cl->width <= st->width_min && !scols_column_is_strict_width(cl))
                cl->width = st->width_min;
 
-       /* use absolute size for large columns */
-       else if (cl->width_hint >= 1 && cl->width < (size_t) cl->width_hint
+       /* use absolute width if specified (usually "strictwidth" columns) */
+       else if (cl->width_hint >= 1
+                && cl->width     < (size_t) cl->width_hint
                 && st->width_min < (size_t) cl->width_hint)
 
                cl->width = (size_t) cl->width_hint;
@@ -560,6 +561,35 @@ int __scols_calculate(struct libscols_table *tb, struct ul_buffer *buf)
                        }
                }
 
+               if (width < tb->outwidth) {
+                       DBG(TAB, ul_debugobj(tb, " enlarge (absolute, available %zu)",
+                                               tb->outwidth - width));
+
+                       /* try enlarging all columns with absolute hint */
+                       while (width < tb->outwidth) {
+                               int changed = 0;
+
+                               scols_reset_iter(&itr, SCOLS_ITER_BACKWARD);
+                               while (scols_table_next_column(tb, &itr, &cl) == 0) {
+                                       if (scols_column_is_hidden(cl))
+                                               continue;
+                                       if (cl->width_hint < 1.0)
+                                               continue;
+                                       if (cl->width >= (size_t) cl->width_hint)
+                                               continue;
+                                       DBG(COL, ul_debugobj(cl, "  add +1 (%s)",
+                                                               cl->header.data));
+                                       cl->width++;
+                                       width++;
+                                       changed++;
+                                       if (width == tb->outwidth)
+                                               break;
+                               }
+                               if (!changed)
+                                       break;
+                       }
+
+               }
                if (width < tb->outwidth && scols_table_is_maxout(tb)) {
                        DBG(TAB, ul_debugobj(tb, " enlarge (max-out, available %zu)",
                                                tb->outwidth - width));
@@ -578,6 +608,7 @@ int __scols_calculate(struct libscols_table *tb, struct ul_buffer *buf)
                                                break;
                                }
                        }
+
                } else if (width < tb->outwidth) {
                        /* enlarge the last column */
                        DBG(TAB, ul_debugobj(tb, " enlarge (last column, available %zu)",