feat(width): add is_ok() success indicator to Wrap and Truncate (#186)#576
feat(width): add is_ok() success indicator to Wrap and Truncate (#186)#576mvanhorn wants to merge 1 commit into
Conversation
…urt#186) Closes zhiburt#186 Adds a way for callers to detect whether `Width::wrap` or `Width::truncate` actually had to do anything, without manually checking `grid.total_width` after the fact. The new pattern follows the maintainer's hint in the issue: let mut wrap = Width::wrap(80); table.with(&mut wrap); assert!(wrap.is_ok()); // true if wrapping was actually required - Add a `success: bool` field to both `Wrap` and `Truncate` (default false, set true on the wrapping/truncating branch). - Add an `is_ok()` accessor on both structs. - Add a new `TableOption` impl for `&mut Wrap<W,P>` and `&mut Truncate<'_, W, P>` that mirrors the existing owned impl and writes `self.success = true` when actual work occurs. - 4 new tests in `tabled/tests/settings/width_test.rs` cover both the work-needed and already-fits paths for each struct. Existing owned-value impls and call sites are untouched -- the new `&mut` impls are purely additive, so no existing code path changes behavior.
|
Hi @mvanhorn I'll read it through in the morning. But this idea:
Is very very very very valuable. There's more options which can fail and produce no actual change to the table. Just wonder if there's more and if we could produce some interface for it. Also what about name Just saying things out loud (a little bit exited about this feature). Maybe a decorator like the next. let mut result = tabled::SettingsResult::new(Width::wrap(80));
table.with(&mut result);
if result.is_ok() {
}Once again great proposal @mvanhorn will check it tomorrow. |
|
Glad it resonates. No rush on the read-through, and happy to iterate on the implementation shape if you have a different signature in mind once you've had time to look. |
Summary
Adds a way for callers to detect whether
Width::wraporWidth::truncateactually had to do anything, without manually checkinggrid.total_widthafter the fact.Closes #186
Why this matters
Issue #186 noted that today users have to compare
grid.total_widthbefore and after to know whether wrapping/truncation actually happened. The maintainer's suggestion in the issue body was to add a flag to the struct andimpl TableOption on &mut, exposing the result via something likewrap.is_ok(). This PR implements that pattern for bothWrapandTruncate.Changes
tabled/src/settings/width/wrap.rs: addedsuccess: boolfield toWrap,is_ok()accessor, and a newTableOptionimpl for&mut Wrap<W, P>that mirrors the existing owned impl.tabled/src/settings/width/truncate.rs: same shape forTruncate.tabled/tests/settings/width_test.rs: 6 tests covering the work-needed path, the already-fits path, and the no-op edge case where the request is below the table's minimum (borders/padding floor) and no cell content actually changes.The existing owned-value
TableOptionimpls and call sites are untouched. The new&mutimpls are purely additive.Self-review notes
Two things from a self-review pass that aren't obvious from the diff:
Clonebounds on the new impls. The new&mutimpls cloneself.width.clone()andself.priority.clone()so they can construct a one-shot ownedWrap/Truncateto feed into the existingwrap_total_width/truncate_total_widthhelpers. TheMeasurementandPeakertraits are alreadyClonefor the standard implementations (PriorityNone,PriorityMax,PriorityMin), so this didn't tighten the bound for any in-tree call site.is_ok()only reflects real work, not "below total width". A naive implementation could setsuccess = truewhenever the requested width is below the rendered total, but that misclassifies the case where the table's minimum is determined by borders/padding rather than cell content -- in which case no cell text changes. The implementation now compares column widths before and afterwrap_total_width/truncate_total_widthand only marks success when they actually differ. The two*_below_minimum_borders_paddingtests assert this with aBuilder::from_iter([[""]]).build().with(Style::modern())table andWidth::wrap(1)/Width::truncate(1)-- the rendered output is unchanged andis_ok()correctly returnsfalse.Testing
This contribution was developed with AI assistance (Codex).