From: Karel Zak Date: Wed, 11 Mar 2026 09:55:11 +0000 (+0100) Subject: libsmartcols: fix column width calculation X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a9ac4744fa31fe84a4e43c88290ae7af7a41a8d0;p=thirdparty%2Futil-linux.git libsmartcols: fix column width calculation 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 --- diff --git a/libsmartcols/src/calculate.c b/libsmartcols/src/calculate.c index dbf04af15..2157454c2 100644 --- a/libsmartcols/src/calculate.c +++ b/libsmartcols/src/calculate.c @@ -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 index 000000000..be4c1625b --- /dev/null +++ b/tests/expected/column/table-min-width @@ -0,0 +1,3 @@ +X|YY|Z +a|b |c +a|b |c diff --git a/tests/ts/column/table b/tests/ts/column/table index 01127bbc6..035ee8fe3 100755 --- a/tests/ts/column/table +++ b/tests/ts/column/table @@ -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