fix(tui): guard nullish args in truncateToWidth wrapper

renderContentPreview crashed on the Windows prebuilt with "Failed to
convert napi value Null into rust type `u8`" (and the related `bool`
variant) because the wrapper forwarded `null` to napi-rs 3's
`Option<Ellipsis>` / `Option<bool>` parameters; the Windows binding
rejects null where Linux/macOS tolerates it. `maxWidth` was also passed
through unchecked, so a stray null/undefined upstream blew up the
required `u32` everywhere. Pass concrete defaults that mirror the Rust
`unwrap_or`s and clamp `maxWidth` to a sane non-negative integer.

Fixes #848
This commit is contained in:
can1357
2026-04-30 04:36:33 +02:00
parent d9376c5866
commit d731e57513
2 changed files with 56 additions and 2 deletions
+9 -2
View File
@@ -1,9 +1,11 @@
import type { Ellipsis, ExtractSegmentsResult, SliceResult } from "@oh-my-pi/pi-natives";
import {
Ellipsis,
type ExtractSegmentsResult,
extractSegments as nativeExtractSegments,
sliceWithWidth as nativeSliceWithWidth,
truncateToWidth as nativeTruncateToWidth,
wrapTextWithAnsi as nativeWrapTextWithAnsi,
type SliceResult,
} from "@oh-my-pi/pi-natives";
import { getDefaultTabWidth, getIndentation } from "@oh-my-pi/pi-utils";
@@ -21,7 +23,12 @@ export function truncateToWidth(
ellipsisKind?: Ellipsis | null,
pad?: boolean | null,
): string {
return nativeTruncateToWidth(text, maxWidth, ellipsisKind ?? null, pad ?? null, getDefaultTabWidth());
// Guard nullish napi inputs: napi-rs 3 on the Windows prebuilt rejects
// `null` for `Option<u8>` (Ellipsis) / `Option<bool>` (pad) (issue #848),
// and `maxWidth` is a required `u32` that throws on `null`/`undefined`
// everywhere. Pass concrete defaults that mirror the Rust `unwrap_or`s.
const safeWidth = Number.isFinite(maxWidth) ? Math.max(0, Math.trunc(maxWidth)) : 0;
return nativeTruncateToWidth(text, safeWidth, ellipsisKind ?? Ellipsis.Unicode, pad ?? false, getDefaultTabWidth());
}
export function wrapTextWithAnsi(text: string, width: number): string[] {