e1bcf492a1
- Renamed `EllipsisKind` enum to `Ellipsis` across all packages for clearer API naming. - Refactored Rust text module from 356 to 155 lines with simplified state tracking using u32 ColorVal instead of enum ColorCode and named attribute constants. - Removed `Env` parameter from public NAPI functions `truncate_to_width` and `extract_segments` to simplify API surface. - Changed return type of `truncate_to_width` from `JsString` to `Either<JsString, Utf16String>` to support zero-copy returns. - Removed early return optimization in `truncateToWidth()` function. - Reordered native module search paths to prioritize repository build artifacts over system paths.
37 lines
1.3 KiB
TypeScript
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);
|
|
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);
|
|
});
|
|
});
|