]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: fix column width calculation
authorKarel Zak <kzak@redhat.com>
Wed, 11 Mar 2026 09:55:11 +0000 (10:55 +0100)
committerKarel Zak <kzak@redhat.com>
Wed, 11 Mar 2026 10:29:49 +0000 (11:29 +0100)
echo -e "a b c\na b c" | ./column --table --table-column "name=X,width=5" -C "name=YY,width=5" -C "name=Z" --output-separator '|'
X    |YY|Z
a    |b |c
a    |b |c

The only difference between the X and YY columns is the column name.
The column header width affects the use of the width property. This
occurs when the minimum and maximum column widths are the same ("YY").

Fixed version:

echo -e "a b c\na b c" | ./column --table --table-column "name=X,width=5" -C "name=YY,width=5" -C "name=Z" --output-separator '|'
X|YY|Z
a|b |c
a|b |c

Note that width= is just a hint and should not be used when the
'strictwidth' property is not specified.

Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/src/calculate.c
tests/expected/column/table-min-width [new file with mode: 0644]
tests/ts/column/table

index dbf04af15057fc584ae474030313ef03847793c9..2157454c219273a4e87243488e39d7f7174d6575 100644 (file)
@@ -215,7 +215,7 @@ static int count_column_width(struct libscols_table *tb,
        cl->width = st->width_max;
 
        /* enlarge to minimal width */
-       if (cl->width < st->width_min && !scols_column_is_strict_width(cl))
+       if (cl->width <= st->width_min && !scols_column_is_strict_width(cl))
                cl->width = st->width_min;
 
        /* use absolute size for large columns */
diff --git a/tests/expected/column/table-min-width b/tests/expected/column/table-min-width
new file mode 100644 (file)
index 0000000..be4c162
--- /dev/null
@@ -0,0 +1,3 @@
+X|YY|Z
+a|b |c
+a|b |c
index 01127bbc6b5dcd9af69e46b87c6b3c5dce5ae667..035ee8fe3516ca644b66a510d72c42c4c1bff754 100755 (executable)
@@ -239,4 +239,8 @@ ts_init_subtest "header-as-columns-empty"
 echo -e 'COL1\t\tCOL3\nData1\tData2\tData3' | $TS_CMD_COLUMN --table -K >> $TS_OUTPUT 2>> $TS_ERRLOG
 ts_finalize_subtest
 
+ts_init_subtest "min-width"
+echo -e "a b c\na b c" | $TS_CMD_COLUMN --table --table-column "name=X,width=5" -C "name=YY,width=5" -C "name=Z" --output-separator '|' --output-width unlimited >> $TS_OUTPUT 2>> $TS_ERRLOG
+ts_finalize_subtest
+
 ts_finalize