Files
oh-my-pi/packages/tui/test/text-utils.test.ts
T
can1357 8ef11355e7 perf(tui,natives): added native text utilities with JS fast path for short strings
- Added native text utilities in pi-natives (visible width, truncation, slicing, segment extraction)
- Wired pi-tui to use native implementations with short-string JS fast path
- Optimized key matching via cached parsed key ids
- Refactored visual truncation to reuse Text instances
- Reduced theme color lookups by switching from Map to plain records
- Regenerated WASM artifacts

Benchmarks (after vs before, lower is better):
- truncateToWidth/ansi: 20.37ms vs 38.13ms (2000 iters)
- sliceWithWidth/ansi: 5.12ms vs 20.39ms
- extractSegments/ansi: 8.92ms vs 27.60ms
- truncateToVisualLines: 3.51ms vs 411.55ms (500 iters)
- visibleWidth/ansi: 0.24ms vs 0.31ms
2026-01-29 10:47:55 +01:00

37 lines
1.3 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import { extractSegments, sliceWithWidth, truncateToWidth, visibleWidth } from "@oh-my-pi/pi-tui/utils";
describe("text utils", () => {
it("computes visible width for ANSI and tabs", () => {
const text = `\x1b[31mhi\tthere\x1b[0m`;
expect(visibleWidth(text)).toBe(2 + 3 + 5);
});
it("ignores OSC hyperlinks in visible width", () => {
const text = "\x1b]8;;https://example.com\x07link\x1b]8;;\x07";
expect(visibleWidth(text)).toBe(4);
});
it("truncates ANSI text with ellipsis", () => {
const text = "\x1b[31mhello world\x1b[0m";
const result = truncateToWidth(text, 6, "…", false);
expect(result.includes("\x1b[0m…")).toBe(true);
expect(visibleWidth(result)).toBe(6);
});
it("slices visible columns while preserving ANSI", () => {
const text = "\x1b[31mhello\x1b[0m world";
const result = sliceWithWidth(text, 1, 4, true);
expect(result.text.startsWith("\x1b[31mello")).toBe(true);
expect(result.width).toBe(4);
});
it("extracts segments with inherited styling", () => {
const text = "\x1b[31mhello world\x1b[0m";
const result = extractSegments(text, 3, 6, 5, true);
expect(result.before).toContain("hel");
expect(result.after.startsWith("\x1b[31m")).toBe(true);
expect(result.afterWidth).toBeGreaterThan(0);
});
});