Skip to content

fix(nu-term-grid): accept a grid that is exactly as wide as the limit - #19409

Merged
fdncred merged 2 commits into
nushell:mainfrom
Huang-404-Q:fix/nu-term-grid-exact-width
Nov 3, 2026
Merged

fdncred merged 2 commits into
nushell:mainfrom
Huang-404-Q:fix/nu-term-grid-exact-width

Conversation

@Huang-404-Q

@Huang-404-Q Huang-404-Q commented Nov 2, 2026 •

Copy link
Copy Markdown

Description

Grid::width_dimensions rejected a layout that was exactly as wide as the limit (< instead of <=), and it could also fall through to None after the loop even when it had already stored a fitting layout in smallest_dimensions_yet. That second path is hit when every smaller layout aborts on the separator width.

Both show up through grid with the default │ separator:

> [aa bb cc] | grid --width 7
Error: nu::shell::cant_fit_grid
  × Couldn't fit grid into 7 columns

> [1 2 3 4 5] | grid --width 5
Error: nu::shell::cant_fit_grid
  × Couldn't fit grid into 5 columns

aa │ cc is exactly 7 wide, and 1 │ 4 is exactly 5 wide (the two- and five-column layouts are too wide once the separators are counted). Widths 9, 10 and 11 fail the same way as width 5.

The fix:

  • compare the summed column widths with <=, so an exact fit is accepted;
  • after the loop, return smallest_dimensions_yet instead of None.

Tests

  • multi_item_exact_width in crates/nu-term-grid/src/grid.rs pins the exact-width case.
  • test_output_at_exact_width in crates/nu-command/tests/commands/griddle.rs covers the command level for [aa bb cc] | grid --width 7 and [1 2 3 4 5] | grid --width 5.
  • huge_separator now expects the single-column layout that fits, and grid_errors_with_few_columns uses [123456] | grid --width 5 (a cell wider than the limit) so it still tests the error path.

cargo test -p nu-term-grid passes (14 tests plus the doctest) and cargo fmt --all -- --check is clean. This machine has no MSVC linker, and the kitest harness links with 0 registered tests under MinGW, so the command-level cases were additionally checked by running a freshly built nu: [aa bb cc] | grid --width 7 prints aa │ cc / bb │, [1 2 3 4 5] | grid --width 5 prints three rows, and [123456] | grid --width 5 still errors.

User-facing changes (Release notes)

grid now uses layouts that fill the full width, so it no longer errors on a grid that fits exactly and can print fewer rows.

@fdncred fdncred left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review overview

🟡 Changes recommended

The <= fixes grids whose tallest layout is exactly the limit, so [aa bb cc] | grid --width 7 renders now instead of erroring. One path still throws away a layout that fits: [1 2 3 4 5] | grid --width 5 errors on the branch the same as on main.

Findings: 0 High severity · 1 Medium severity · 2 Low severity

Open (3)

Notes from my clanker, checked on b5a396a. CI only ran triage on this head, so I built the branch and ran cargo test -p nu-term-grid (14 passed plus the doctest), cargo test -p nu-command --test tests -- commands::griddle (5 passed), clippy with CI's flags on nu-term-grid (clean), cargo fmt --all --check and typos (clean). Each grid case ran on the branch binary and on 0.116.2, plus a sweep of --width 1..40 over four lists. The fix in the first thread is from a trial patch on the branch, since reverted.

Comment thread crates/nu-term-grid/src/grid.rs Resolved
Comment thread crates/nu-term-grid/src/grid.rs Resolved
Comment thread crates/nu-term-grid/src/grid.rs Resolved
`width_dimensions` could fall through to `None` after the loop even
though it had stored a fitting layout in `smallest_dimensions_yet`:
every smaller `num_lines` aborts on the separator width. Return the
stored layout instead, so `grid --width 5` (and 9/10/11) render
instead of erroring.

Update `huge_separator` for the single-column layout that now fits,
keep `grid_errors_with_few_columns` on the error path with a cell wider
than the limit, and add command-level exact-width cases to
`crates/nu-command/tests/commands/griddle.rs`.
@Huang-404-Q

Copy link
Copy Markdown
Author

Thanks! Addressed all three threads in 3acea8c:

  • width_dimensions now returns smallest_dimensions_yet after the loop instead of None. grid --width 5 now prints 1 │ 4 / 2 │ 5 / 3 │, and 9/10/11 print 1 │ 3 │ 5 / 2 │ 4 │. huge_separator now expects the single-column layout that fits (num_lines: 2, widths: [1]).
  • grid_errors_with_few_columns now uses [123456] | grid --width 5, so it still tests a cell wider than the limit.
  • Added test_output_at_exact_width in crates/nu-command/tests/commands/griddle.rs with cases for [aa bb cc] --width 7 and [1 2 3 4 5] --width 5.
  • Updated the description: the --width 7 example and the "User-facing changes (Release notes)" section.

Tests:

  • cargo fmt --all -- --check → clean.
  • cargo test -p nu-term-grid → 14 passed, 1 doctest passed.
  • The nu-command integration test binary compiles, but this machine has no MSVC linker; with the GNU toolchain the kitest/linkme harness registers 0 tests locally, so I checked the command-level cases with a freshly built nu instead: [aa bb cc] | grid --width 7 → aa │ cc / bb │, [1 2 3 4 5] | grid --width 5 → three rows, [123456] | grid --width 5 → Couldn't fit grid into 5 columns (exit 1).

@fdncred

fdncred commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

please respond to each review finding so we can make sure nothing is missed

@fdncred fdncred added notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes labels Nov 3, 2026
@fdncred
fdncred merged commit adb5708 into nushell:main Nov 3, 2026
11 checks passed
@fdncred

fdncred commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Thanks

@github-actions github-actions Bot added this to the v0.117.0 milestone Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants