feat(runtime): add wrap='hard' mode to Text component

Hard wrap breaks text at exact width boundaries without respecting
word boundaries. Uses wrap-ansi with { hard: true, trim: false }.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Yunfei He
2026-05-25 20:45:21 +08:00
parent f4ef12f484
commit 1503c18a66
3 changed files with 12 additions and 2 deletions
+7 -1
View File
@@ -1,7 +1,13 @@
import { defineComponent, getCurrentInstance, h, type PropType } from "vue";
type Color = string | [number, number, number];
type WrapMode = "wrap" | "truncate" | "truncate-end" | "truncate-middle" | "truncate-start";
type WrapMode =
| "wrap"
| "hard"
| "truncate"
| "truncate-end"
| "truncate-middle"
| "truncate-start";
export const Text = defineComponent({
name: "Text",
+1 -1
View File
@@ -16,7 +16,7 @@ export interface TextProps {
underline?: boolean;
strikethrough?: boolean;
inverse?: boolean;
wrap?: "wrap" | "truncate" | "truncate-end" | "truncate-middle" | "truncate-start";
wrap?: "wrap" | "hard" | "truncate" | "truncate-end" | "truncate-middle" | "truncate-start";
}
interface NodeBase {
@@ -24,6 +24,10 @@ export function wrapText(text: string, width: number, mode: WrapMode = "wrap"):
return wrapAnsi(text, width, { hard: true, trim: true, wordWrap: true }).split("\n");
}
if (mode === "hard") {
return wrapAnsi(text, width, { hard: true, trim: false }).split("\n");
}
// truncate variants: collapse newlines, then slice from the appropriate side.
const single = text.replace(/\n/g, " ");
if (stringWidth(single) <= width) return [single];