refactor: deduplicate safeSliceEnd into shared export from text-measure

This commit is contained in:
Yunfei He
2026-05-28 09:55:10 +08:00
parent 2c994314e1
commit 6628dde35e
2 changed files with 7 additions and 19 deletions
+6 -5
View File
@@ -37,13 +37,14 @@ export type WrapMode = NonNullable<TextProps["wrap"]>;
* `slice-ansi` can overshoot when a wide character straddles the boundary, so * `slice-ansi` can overshoot when a wide character straddles the boundary, so
* we reduce the slice position until the result fits. * we reduce the slice position until the result fits.
*/ */
function safeSliceEnd(text: string, maxCols: number): string { export function safeSliceEnd(text: string, maxCols: number): string {
if (maxCols <= 0) return ""; if (maxCols <= 0) return "";
let sliced = sliceAnsi(text, 0, maxCols); let end = maxCols;
let sliced = sliceAnsi(text, 0, end);
let w = stringWidth(sliced); let w = stringWidth(sliced);
while (w > maxCols && maxCols > 0) { while (w > maxCols && end > 0) {
maxCols--; end--;
sliced = sliceAnsi(text, 0, maxCols); sliced = sliceAnsi(text, 0, end);
w = stringWidth(sliced); w = stringWidth(sliced);
} }
return sliced; return sliced;
+1 -14
View File
@@ -19,7 +19,7 @@ import type {
BoxProps, BoxProps,
} from "../host/nodes.ts"; } from "../host/nodes.ts";
import { createRoot as createIsoRoot } from "../host/nodes.ts"; import { createRoot as createIsoRoot } from "../host/nodes.ts";
import { wrapText } from "../host/text-measure.ts"; import { wrapText, safeSliceEnd } from "../host/text-measure.ts";
import { import {
attachYoga, attachYoga,
detachYoga, detachYoga,
@@ -29,19 +29,6 @@ import {
export type Transformer = (line: string, lineIndex: number) => string; export type Transformer = (line: string, lineIndex: number) => string;
function safeSliceEnd(text: string, maxCols: number): string {
if (maxCols <= 0) return "";
let end = maxCols;
let sliced = sliceAnsi(text, 0, end);
let w = stringWidth(sliced);
while (w > maxCols && end > 0) {
end--;
sliced = sliceAnsi(text, 0, end);
w = stringWidth(sliced);
}
return sliced;
}
interface ClipRect { interface ClipRect {
x1: number | undefined; x1: number | undefined;
x2: number | undefined; x2: number | undefined;