agent-whisker 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -3
- package/README.zh-CN.md +12 -3
- package/package.json +3 -2
- package/packages/antigravity-plugin/package.json +1 -1
- package/packages/antigravity-plugin/plugin.json +1 -1
- package/packages/claude-plugin/.claude-plugin/plugin.json +1 -1
- package/packages/claude-plugin/.codex-plugin/plugin.json +1 -1
- package/packages/claude-plugin/README.md +11 -1
- package/packages/claude-plugin/README.zh-CN.md +4 -1
- package/packages/claude-plugin/bin/taskstate +145 -4
- package/packages/claude-plugin/hooks/codex.json +8 -8
- package/packages/claude-plugin/package.json +1 -1
- package/packages/cli/README.md +76 -3
- package/packages/cli/README.zh-CN.md +76 -3
- package/packages/cli/package.json +1 -1
- package/packages/cli/src/command.js +20 -3
- package/packages/cli/src/commands/catalog.js +26 -7
- package/packages/cli/src/commands/doctor.js +31 -27
- package/packages/cli/src/commands/exclude.js +156 -0
- package/packages/cli/src/commands/extract.js +4 -2
- package/packages/cli/src/commands/index-command.js +1 -1
- package/packages/cli/src/commands/index.js +4 -0
- package/packages/cli/src/commands/install.js +12 -1
- package/packages/cli/src/commands/nudge.js +39 -37
- package/packages/cli/src/commands/read.js +2 -1
- package/packages/cli/src/commands/search.js +33 -3
- package/packages/cli/src/commands/sessions.js +20 -12
- package/packages/cli/src/commands/status.js +6 -3
- package/packages/cli/src/commands/tui.js +62 -0
- package/packages/cli/src/commands/version.js +4 -4
- package/packages/cli/src/exclusion.js +88 -0
- package/packages/cli/src/install/codex-plugin.js +6 -5
- package/packages/cli/src/install/external.js +48 -37
- package/packages/cli/src/install/handlers.js +4 -3
- package/packages/cli/src/install/mcp.js +3 -3
- package/packages/cli/src/install/steps.js +43 -15
- package/packages/cli/src/install/wrapper.js +28 -5
- package/packages/cli/src/main.js +9 -12
- package/packages/cli/src/node-version.js +25 -0
- package/packages/cli/src/run.js +25 -5
- package/packages/cli/src/tui/app.js +1430 -0
- package/packages/cli/src/tui/data/call.js +83 -0
- package/packages/cli/src/tui/data/poll.js +24 -0
- package/packages/cli/src/tui/glyphs.js +93 -0
- package/packages/cli/src/tui/i18n.js +342 -0
- package/packages/cli/src/tui/model.js +251 -0
- package/packages/cli/src/tui/pages/common.js +71 -0
- package/packages/cli/src/tui/pages/library.js +154 -0
- package/packages/cli/src/tui/pages/sessions.js +96 -0
- package/packages/cli/src/tui/pages/system.js +124 -0
- package/packages/cli/src/tui/pages/tasks.js +297 -0
- package/packages/cli/src/tui/run.js +216 -0
- package/packages/cli/src/tui/term/keys.js +152 -0
- package/packages/cli/src/tui/term/screen.js +88 -0
- package/packages/cli/src/tui/term/term.js +157 -0
- package/packages/cli/src/tui/term/width.js +290 -0
- package/packages/cli/src/tui/ui/widgets.js +247 -0
- package/packages/core/README.md +1 -1
- package/packages/core/README.zh-CN.md +2 -2
- package/packages/core/package.json +1 -1
- package/packages/core/src/catalog.js +13 -1
- package/packages/core/src/clairic.js +1 -1
- package/packages/core/src/convert.js +1 -1
- package/packages/core/src/db.js +6 -1
- package/packages/core/src/host.js +104 -0
- package/packages/core/src/index.js +2 -2
- package/packages/core/src/library-read.js +12 -8
- package/packages/core/src/nudge.js +34 -1
- package/packages/core/src/schema.js +17 -0
- package/packages/core/src/sessions-read.js +23 -11
- package/packages/dsh-plugin/package.json +1 -1
- package/packages/herdr-plugin/herdr-plugin.toml +1 -1
- package/packages/herdr-plugin/package.json +1 -1
- package/packages/mcp/README.md +5 -5
- package/packages/mcp/README.zh-CN.md +5 -5
- package/packages/mcp/package.json +1 -1
- package/packages/pi-extension/README.md +1 -1
- package/packages/pi-extension/README.zh-CN.md +1 -1
- package/packages/pi-extension/package.json +1 -1
- package/packages/pi-extension/src/extension.ts +3 -3
- package/packages/service/package.json +1 -1
- package/packages/service/src/index.js +1 -1
- package/packages/service/src/runtime.js +4 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Frames and their output. A frame is one encoded string per terminal row, each exactly `cols` columns wide, built
|
|
2
|
+
// from styled spans; flush turns the difference between two frames into the bytes to write: only changed rows, each
|
|
3
|
+
// placed with an absolute cursor move (CUP) and ended with erase-to-end-of-line (EL), inside a synchronized update
|
|
4
|
+
// (`?2026h` … `?2026l`, ignored by terminals without it). A row is drawn whole, so a cluster whose width a terminal
|
|
5
|
+
// draws differently can disturb only its own row.
|
|
6
|
+
import { clusters } from "./width.js";
|
|
7
|
+
|
|
8
|
+
/** The 16-colour foregrounds this interface uses: 31 red (fault), 32 green (ok), 33 yellow (warn). */
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
/** A span's style: bold (titles), dim (secondary text), inverse (selection, current tab) and a foreground. */
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
/** A run of text in one style. Its text must already be sanitized (width.ts sanitize). */
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
/** One row: spans drawn left to right, cut or padded to the row's width when encoded. */
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
const FG = { red: "31", green: "32", yellow: "33" };
|
|
21
|
+
|
|
22
|
+
/** How spans become bytes: the measured ambiguous width, and whether colours are drawn (NO_COLOR drops them). */
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
/** The SGR sequence for a style, or "" for the default style. */
|
|
26
|
+
function sgr(style , paint ) {
|
|
27
|
+
if (style === undefined) return "";
|
|
28
|
+
const codes = [];
|
|
29
|
+
if (style.bold) codes.push("1");
|
|
30
|
+
if (style.dim) codes.push("2");
|
|
31
|
+
if (style.inverse) codes.push("7");
|
|
32
|
+
if (style.fg !== undefined && paint.colour) codes.push(FG[style.fg]);
|
|
33
|
+
return codes.length === 0 ? "" : `\x1b[${codes.join(";")}m`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* A row encoded to exactly cols columns: spans in order, each in its style followed by a reset; cut at cols (a wide
|
|
38
|
+
* cluster that would cross it becomes a space) and padded with plain spaces. Returns the bytes.
|
|
39
|
+
*/
|
|
40
|
+
export function encodeRow(row , cols , paint ) {
|
|
41
|
+
let out = "";
|
|
42
|
+
let col = 0;
|
|
43
|
+
for (const span of row) {
|
|
44
|
+
if (col >= cols) break;
|
|
45
|
+
let text = "";
|
|
46
|
+
for (const found of clusters(span.text, paint.ambiguous)) {
|
|
47
|
+
// A control character never reaches the terminal: a newline left in one-line text is a space, anything else `?`.
|
|
48
|
+
const code = found.text.codePointAt(0) ?? 0;
|
|
49
|
+
const control = code < 0x20 || (code >= 0x7f && code < 0xa0);
|
|
50
|
+
const cluster = control ? { text: found.text === "\n" ? " " : "?", width: 1 } : found;
|
|
51
|
+
if (col + cluster.width > cols) {
|
|
52
|
+
text += " ".repeat(cols - col);
|
|
53
|
+
col = cols;
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
text += cluster.text;
|
|
57
|
+
col += cluster.width;
|
|
58
|
+
}
|
|
59
|
+
if (text === "") continue;
|
|
60
|
+
const open = sgr(span.style, paint);
|
|
61
|
+
out += open === "" ? text : `${open}${text}\x1b[0m`;
|
|
62
|
+
}
|
|
63
|
+
if (col < cols) out += " ".repeat(cols - col);
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** A frame: the encoded rows, one per terminal row. */
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
/** Trailing plain spaces of an encoded row, which EL clears anyway. */
|
|
71
|
+
function trimEnd(row ) {
|
|
72
|
+
return row.replace(/ +$/u, "");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The bytes that turn the screen showing prev into next: every row when prev is null (after a resize or a resume, with
|
|
77
|
+
* a clear first), otherwise only the rows that differ. Each row: CUP to its start, its bytes, SGR reset, EL.
|
|
78
|
+
*/
|
|
79
|
+
export function flush(next , prev ) {
|
|
80
|
+
let out = "";
|
|
81
|
+
if (prev === null || prev.length !== next.length) out += "\x1b[0m\x1b[2J";
|
|
82
|
+
const full = prev === null || prev.length !== next.length;
|
|
83
|
+
for (let r = 0; r < next.length; r++) {
|
|
84
|
+
if (!full && prev?.[r] === next[r]) continue;
|
|
85
|
+
out += `\x1b[${r + 1};1H${trimEnd(next[r] ?? "")}\x1b[0m\x1b[K`;
|
|
86
|
+
}
|
|
87
|
+
return out === "" ? "" : `\x1b[?2026h${out}\x1b[?2026l`;
|
|
88
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// The terminal's lifetime: entering the alternate screen in raw mode, measuring how wide it draws East Asian
|
|
2
|
+
// Ambiguous characters, and restoring it on every way out (a normal exit, Ctrl+C, SIGTERM, SIGHUP, an uncaught error,
|
|
3
|
+
// a hang-up). restore is idempotent, so every exit path may call it. Ctrl+Z restores first, stops the process, and on
|
|
4
|
+
// SIGCONT enters again and asks for a full redraw. Resizes are debounced on the trailing edge (40 ms after the last
|
|
5
|
+
// event, at most 120 ms after the first), so dragging a split pane redraws as it goes without a flood of frames.
|
|
6
|
+
// The terminal is reached only through the HostTty interface given to it (core's Host.tty), never process directly.
|
|
7
|
+
|
|
8
|
+
/** What term.ts needs from the terminal: the HostTty methods (structurally the same type as core's). */
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
/** Alternate screen, cursor hidden, autowrap off (a full last column never scrolls), bracketed paste on, cleared. */
|
|
22
|
+
export const ENTER = "\x1b[?1049h\x1b[?25l\x1b[?7l\x1b[?2004h\x1b[2J\x1b[H";
|
|
23
|
+
/** The reverse of ENTER, in reverse order: paste off, autowrap on, style reset, cursor shown, main screen. */
|
|
24
|
+
export const LEAVE = "\x1b[?2004l\x1b[?7h\x1b[0m\x1b[?25h\x1b[?1049l";
|
|
25
|
+
|
|
26
|
+
/** The trailing-edge resize debounce: 40 ms quiet, at most 120 ms from the first event. */
|
|
27
|
+
const RESIZE_QUIET_MS = 40;
|
|
28
|
+
const RESIZE_MAX_MS = 120;
|
|
29
|
+
|
|
30
|
+
/** Timers term.ts uses, injected so tests control time. */
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
/** A terminal session: enter() once, then restore() on any exit; events go to the handlers given. */
|
|
37
|
+
export class Terminal {
|
|
38
|
+
restored = true;
|
|
39
|
+
offs = [];
|
|
40
|
+
resizeTimer = null;
|
|
41
|
+
resizeFirst = 0;
|
|
42
|
+
|
|
43
|
+
tty ;
|
|
44
|
+
timers ;
|
|
45
|
+
now ;
|
|
46
|
+
|
|
47
|
+
constructor(tty , timers , now ) {
|
|
48
|
+
this.tty = tty;
|
|
49
|
+
this.timers = timers;
|
|
50
|
+
this.now = now;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Whether the terminal is entered and not yet restored. */
|
|
54
|
+
get active() {
|
|
55
|
+
return !this.restored;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Enters the alternate screen in raw mode and subscribes the handlers. onExit receives why the session must end
|
|
60
|
+
* (a signal or a fatal error); the caller restores and ends its loop.
|
|
61
|
+
*/
|
|
62
|
+
enter(handlers
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
) {
|
|
68
|
+
this.tty.setRaw(true);
|
|
69
|
+
this.tty.write(ENTER);
|
|
70
|
+
this.restored = false;
|
|
71
|
+
this.resumed = handlers.resume;
|
|
72
|
+
this.offs.push(
|
|
73
|
+
this.tty.onInput(handlers.input),
|
|
74
|
+
this.tty.onResize(() => this.debounceResize(handlers.resize)),
|
|
75
|
+
this.tty.onSignal("SIGTERM", () => handlers.exit("SIGTERM")),
|
|
76
|
+
this.tty.onSignal("SIGHUP", () => handlers.exit("SIGHUP")),
|
|
77
|
+
this.tty.onSignal("SIGINT", () => handlers.exit("SIGINT")),
|
|
78
|
+
this.tty.onSignal("SIGCONT", () => {
|
|
79
|
+
if (!this.restored) return;
|
|
80
|
+
this.tty.setRaw(true);
|
|
81
|
+
this.tty.write(ENTER);
|
|
82
|
+
this.restored = false;
|
|
83
|
+
handlers.resume();
|
|
84
|
+
}),
|
|
85
|
+
this.tty.onFatal((error) => handlers.exit(error === null ? "exit" : "fatal", error)),
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Writes bytes while entered; a write after restore is dropped, so nothing lands on the main screen. */
|
|
90
|
+
write(bytes ) {
|
|
91
|
+
if (!this.restored && bytes !== "") this.tty.write(bytes);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Leaves raw mode and the alternate screen. Idempotent; errors from a hung-up terminal are swallowed. */
|
|
95
|
+
restore() {
|
|
96
|
+
if (this.restored) return;
|
|
97
|
+
this.restored = true;
|
|
98
|
+
try {
|
|
99
|
+
this.tty.write(LEAVE);
|
|
100
|
+
} catch {
|
|
101
|
+
// EIO or EPIPE: the terminal is gone.
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
this.tty.setRaw(false);
|
|
105
|
+
} catch {
|
|
106
|
+
// As above.
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** restore, and unsubscribe everything, cancelling a pending resize: the end of the session. */
|
|
111
|
+
close() {
|
|
112
|
+
this.restore();
|
|
113
|
+
if (this.resizeTimer !== null) this.timers.clearTimeout(this.resizeTimer);
|
|
114
|
+
this.resizeTimer = null;
|
|
115
|
+
for (const off of this.offs.splice(0)) off();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Ctrl+Z: restore the terminal, then stop; SIGCONT enters again (see enter). Where a process cannot stop itself
|
|
120
|
+
* (Windows has no SIGTSTP), the terminal is entered again at once and the session goes on.
|
|
121
|
+
*/
|
|
122
|
+
suspend() {
|
|
123
|
+
this.restore();
|
|
124
|
+
try {
|
|
125
|
+
this.tty.suspend();
|
|
126
|
+
} catch {
|
|
127
|
+
this.tty.setRaw(true);
|
|
128
|
+
this.tty.write(ENTER);
|
|
129
|
+
this.restored = false;
|
|
130
|
+
this.resumed?.();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
resumed = null;
|
|
135
|
+
|
|
136
|
+
debounceResize(fire ) {
|
|
137
|
+
const now = this.now();
|
|
138
|
+
if (this.resizeTimer === null) this.resizeFirst = now;
|
|
139
|
+
else this.timers.clearTimeout(this.resizeTimer);
|
|
140
|
+
const wait = Math.max(0, Math.min(RESIZE_QUIET_MS, this.resizeFirst + RESIZE_MAX_MS - now));
|
|
141
|
+
this.resizeTimer = this.timers.setTimeout(() => {
|
|
142
|
+
this.resizeTimer = null;
|
|
143
|
+
fire();
|
|
144
|
+
}, wait);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* The probe that measures the ambiguous width: draw `◆` at the top-left of the (cleared) alternate screen, then ask
|
|
150
|
+
* for the cursor position; a terminal that draws it in one column answers column 2, a wide one column 3.
|
|
151
|
+
*/
|
|
152
|
+
export const AMBIGUOUS_PROBE = "\x1b[1;1H◆\x1b[6n";
|
|
153
|
+
|
|
154
|
+
/** The ambiguous width a cursor position report gives after AMBIGUOUS_PROBE. */
|
|
155
|
+
export function ambiguousFromReport(col ) {
|
|
156
|
+
return col >= 3 ? 2 : 1;
|
|
157
|
+
}
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
// Display width of terminal text: grapheme clusters (Intl.Segmenter), East Asian Wide and Fullwidth as two columns,
|
|
2
|
+
// emoji sequences as two, combining marks and format characters as none, and East Asian Ambiguous characters
|
|
3
|
+
// (◆◇◎●◯ ─│ and the like) as the width the terminal was measured to draw them (term.ts asks it with a cursor
|
|
4
|
+
// position report). sliceColumns never cuts a wide character in half: a cluster crossing an edge becomes a space.
|
|
5
|
+
// Imports nothing but the language itself, so the kernel under term/ stays free of the product.
|
|
6
|
+
|
|
7
|
+
/** How wide this terminal draws East Asian Ambiguous characters: 1 (most terminals) or 2 (CJK-configured ones). */
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
const segmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
11
|
+
const ZERO = /^[\p{Mn}\p{Me}\p{Cf}\u200B]+$/u;
|
|
12
|
+
const EMOJI = /\p{Extended_Pictographic}|\p{Regional_Indicator}/u;
|
|
13
|
+
const EMOJI_PRESENTATION = /\p{Emoji_Presentation}/u;
|
|
14
|
+
|
|
15
|
+
/** East Asian Wide and Fullwidth ranges (Unicode 16, merged), sorted, as [first, last] code points. */
|
|
16
|
+
const WIDE = [
|
|
17
|
+
[0x1100, 0x115f],
|
|
18
|
+
[0x231a, 0x231b],
|
|
19
|
+
[0x2329, 0x232a],
|
|
20
|
+
[0x23e9, 0x23ec],
|
|
21
|
+
[0x23f0, 0x23f0],
|
|
22
|
+
[0x23f3, 0x23f3],
|
|
23
|
+
[0x25fd, 0x25fe],
|
|
24
|
+
[0x2614, 0x2615],
|
|
25
|
+
[0x2648, 0x2653],
|
|
26
|
+
[0x267f, 0x267f],
|
|
27
|
+
[0x2693, 0x2693],
|
|
28
|
+
[0x26a1, 0x26a1],
|
|
29
|
+
[0x26aa, 0x26ab],
|
|
30
|
+
[0x26bd, 0x26be],
|
|
31
|
+
[0x26c4, 0x26c5],
|
|
32
|
+
[0x26ce, 0x26ce],
|
|
33
|
+
[0x26d4, 0x26d4],
|
|
34
|
+
[0x26ea, 0x26ea],
|
|
35
|
+
[0x26f2, 0x26f3],
|
|
36
|
+
[0x26f5, 0x26f5],
|
|
37
|
+
[0x26fa, 0x26fa],
|
|
38
|
+
[0x26fd, 0x26fd],
|
|
39
|
+
[0x2705, 0x2705],
|
|
40
|
+
[0x270a, 0x270b],
|
|
41
|
+
[0x2728, 0x2728],
|
|
42
|
+
[0x274c, 0x274c],
|
|
43
|
+
[0x274e, 0x274e],
|
|
44
|
+
[0x2753, 0x2755],
|
|
45
|
+
[0x2757, 0x2757],
|
|
46
|
+
[0x2795, 0x2797],
|
|
47
|
+
[0x27b0, 0x27b0],
|
|
48
|
+
[0x27bf, 0x27bf],
|
|
49
|
+
[0x2b1b, 0x2b1c],
|
|
50
|
+
[0x2b50, 0x2b50],
|
|
51
|
+
[0x2b55, 0x2b55],
|
|
52
|
+
[0x2e80, 0x303e],
|
|
53
|
+
[0x3041, 0x33ff],
|
|
54
|
+
[0x3400, 0x4dbf],
|
|
55
|
+
[0x4e00, 0x9fff],
|
|
56
|
+
[0xa000, 0xa4cf],
|
|
57
|
+
[0xa960, 0xa97f],
|
|
58
|
+
[0xac00, 0xd7a3],
|
|
59
|
+
[0xf900, 0xfaff],
|
|
60
|
+
[0xfe10, 0xfe19],
|
|
61
|
+
[0xfe30, 0xfe6f],
|
|
62
|
+
[0xff00, 0xff60],
|
|
63
|
+
[0xffe0, 0xffe6],
|
|
64
|
+
[0x16fe0, 0x16fe4],
|
|
65
|
+
[0x17000, 0x18cff],
|
|
66
|
+
[0x1b000, 0x1b2ff],
|
|
67
|
+
[0x1f004, 0x1f004],
|
|
68
|
+
[0x1f0cf, 0x1f0cf],
|
|
69
|
+
[0x1f18e, 0x1f18e],
|
|
70
|
+
[0x1f191, 0x1f19a],
|
|
71
|
+
[0x1f200, 0x1f251],
|
|
72
|
+
[0x1f300, 0x1f64f],
|
|
73
|
+
[0x1f680, 0x1f6ff],
|
|
74
|
+
[0x1f7e0, 0x1f7eb],
|
|
75
|
+
[0x1f90c, 0x1f9ff],
|
|
76
|
+
[0x1fa70, 0x1faff],
|
|
77
|
+
[0x20000, 0x2fffd],
|
|
78
|
+
[0x30000, 0x3fffd],
|
|
79
|
+
];
|
|
80
|
+
|
|
81
|
+
/** East Asian Ambiguous ranges that this interface draws or that commonly appear in text, as [first, last]. */
|
|
82
|
+
const AMBIGUOUS = [
|
|
83
|
+
[0x00a1, 0x00a1],
|
|
84
|
+
[0x00a7, 0x00a8],
|
|
85
|
+
[0x00b0, 0x00b4],
|
|
86
|
+
[0x00b6, 0x00ba],
|
|
87
|
+
[0x00bc, 0x00bf],
|
|
88
|
+
[0x00d7, 0x00d7],
|
|
89
|
+
[0x00f7, 0x00f7],
|
|
90
|
+
[0x0391, 0x03a9],
|
|
91
|
+
[0x03b1, 0x03c9],
|
|
92
|
+
[0x0401, 0x0451],
|
|
93
|
+
[0x2010, 0x2010],
|
|
94
|
+
[0x2013, 0x2016],
|
|
95
|
+
[0x2018, 0x2019],
|
|
96
|
+
[0x201c, 0x201d],
|
|
97
|
+
[0x2020, 0x2022],
|
|
98
|
+
[0x2024, 0x2027],
|
|
99
|
+
[0x2030, 0x2030],
|
|
100
|
+
[0x2032, 0x2033],
|
|
101
|
+
[0x2035, 0x2035],
|
|
102
|
+
[0x203b, 0x203b],
|
|
103
|
+
[0x2103, 0x2103],
|
|
104
|
+
[0x2116, 0x2116],
|
|
105
|
+
[0x2121, 0x2122],
|
|
106
|
+
[0x2160, 0x216b],
|
|
107
|
+
[0x2190, 0x2199],
|
|
108
|
+
[0x21d2, 0x21d2],
|
|
109
|
+
[0x21d4, 0x21d4],
|
|
110
|
+
[0x2200, 0x22ff],
|
|
111
|
+
[0x2460, 0x24e9],
|
|
112
|
+
[0x24eb, 0x254b],
|
|
113
|
+
[0x2550, 0x2573],
|
|
114
|
+
[0x2580, 0x258f],
|
|
115
|
+
[0x2592, 0x2595],
|
|
116
|
+
[0x25a0, 0x25a1],
|
|
117
|
+
[0x25a3, 0x25a9],
|
|
118
|
+
[0x25b2, 0x25b3],
|
|
119
|
+
[0x25b6, 0x25b7],
|
|
120
|
+
[0x25bc, 0x25bd],
|
|
121
|
+
[0x25c0, 0x25c1],
|
|
122
|
+
[0x25c6, 0x25c8],
|
|
123
|
+
[0x25cb, 0x25cb],
|
|
124
|
+
[0x25ce, 0x25d1],
|
|
125
|
+
[0x25e2, 0x25e5],
|
|
126
|
+
[0x25ef, 0x25ef],
|
|
127
|
+
[0x2605, 0x2606],
|
|
128
|
+
[0x2609, 0x2609],
|
|
129
|
+
[0x260e, 0x260f],
|
|
130
|
+
[0x261c, 0x261c],
|
|
131
|
+
[0x261e, 0x261e],
|
|
132
|
+
[0x2640, 0x2640],
|
|
133
|
+
[0x2642, 0x2642],
|
|
134
|
+
[0x2660, 0x2661],
|
|
135
|
+
[0x2663, 0x2665],
|
|
136
|
+
[0x2667, 0x266a],
|
|
137
|
+
[0x266c, 0x266d],
|
|
138
|
+
[0x266f, 0x266f],
|
|
139
|
+
[0x2713, 0x2713],
|
|
140
|
+
[0x2715, 0x2715],
|
|
141
|
+
[0x273d, 0x273d],
|
|
142
|
+
[0x2776, 0x277f],
|
|
143
|
+
[0x2b56, 0x2b59],
|
|
144
|
+
[0x3248, 0x324f],
|
|
145
|
+
[0xfffd, 0xfffd],
|
|
146
|
+
];
|
|
147
|
+
|
|
148
|
+
function inRanges(cp , ranges ) {
|
|
149
|
+
let lo = 0;
|
|
150
|
+
let hi = ranges.length - 1;
|
|
151
|
+
while (lo <= hi) {
|
|
152
|
+
const mid = (lo + hi) >> 1;
|
|
153
|
+
const range = ranges[mid] ;
|
|
154
|
+
if (cp < range[0]) hi = mid - 1;
|
|
155
|
+
else if (cp > range[1]) lo = mid + 1;
|
|
156
|
+
else return true;
|
|
157
|
+
}
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** The columns one grapheme cluster takes: 0, 1 or 2. Control characters count 0 (sanitize removes them first). */
|
|
162
|
+
function clusterWidth(cluster , ambiguous ) {
|
|
163
|
+
const cp = cluster.codePointAt(0) ?? 0;
|
|
164
|
+
if (cp < 0x20 || (cp >= 0x7f && cp < 0xa0)) return 0;
|
|
165
|
+
if (cp < 0x7f) return 1;
|
|
166
|
+
if (ZERO.test(cluster)) return 0;
|
|
167
|
+
if (cluster.includes("\uFE0F") && EMOJI.test(cluster)) return 2;
|
|
168
|
+
if (cluster.length > 1 && EMOJI.test(cluster) && (EMOJI_PRESENTATION.test(cluster) || cluster.includes("\u200D")))
|
|
169
|
+
return 2;
|
|
170
|
+
if (inRanges(cp, WIDE)) return 2;
|
|
171
|
+
if (inRanges(cp, AMBIGUOUS)) return ambiguous;
|
|
172
|
+
return 1;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** The grapheme clusters of text with their widths. */
|
|
176
|
+
export function clusters(text , ambiguous ) {
|
|
177
|
+
const out = [];
|
|
178
|
+
for (const { segment } of segmenter.segment(text))
|
|
179
|
+
out.push({ text: segment, width: clusterWidth(segment, ambiguous) });
|
|
180
|
+
return out;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** The columns text takes on this terminal. */
|
|
184
|
+
export function displayWidth(text , ambiguous ) {
|
|
185
|
+
// ASCII fast path: most of what an interface draws.
|
|
186
|
+
if (/^[ -~]*$/u.test(text)) return text.length;
|
|
187
|
+
let width = 0;
|
|
188
|
+
for (const { segment } of segmenter.segment(text)) width += clusterWidth(segment, ambiguous);
|
|
189
|
+
return width;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* The part of text from column start, width columns wide at most. A wide cluster crossing either edge becomes a
|
|
194
|
+
* space, so the result never holds half a character; it may be narrower than width when text ends first.
|
|
195
|
+
*/
|
|
196
|
+
export function sliceColumns(text , start , width , ambiguous ) {
|
|
197
|
+
let col = 0;
|
|
198
|
+
let out = "";
|
|
199
|
+
const end = start + width;
|
|
200
|
+
for (const { segment } of segmenter.segment(text)) {
|
|
201
|
+
const w = clusterWidth(segment, ambiguous);
|
|
202
|
+
const next = col + w;
|
|
203
|
+
if (next <= start) {
|
|
204
|
+
col = next;
|
|
205
|
+
if (w === 0 && col > start) out += segment;
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
if (col < start) {
|
|
209
|
+
out += " ".repeat(next - start);
|
|
210
|
+
col = next;
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
if (next > end) {
|
|
214
|
+
if (col < end) out += " ".repeat(end - col);
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
out += segment;
|
|
218
|
+
col = next;
|
|
219
|
+
}
|
|
220
|
+
return out;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** text cut or padded with spaces to exactly width columns; a cut text ends in `…` when ellipsis is true. */
|
|
224
|
+
export function fit(text , width , ambiguous , ellipsis = true) {
|
|
225
|
+
if (width <= 0) return "";
|
|
226
|
+
const have = displayWidth(text, ambiguous);
|
|
227
|
+
if (have <= width) return text + " ".repeat(width - have);
|
|
228
|
+
// `…` is itself East Asian Ambiguous: two columns on a terminal that draws those wide.
|
|
229
|
+
const mark = displayWidth("…", ambiguous);
|
|
230
|
+
if (!ellipsis || width <= mark) return pad(sliceColumns(text, 0, width, ambiguous), width, ambiguous);
|
|
231
|
+
const cut = sliceColumns(text, 0, width - mark, ambiguous);
|
|
232
|
+
return pad(`${cut}…`, width, ambiguous);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** text padded with spaces on the right to width columns (unchanged when it is already as wide or wider). */
|
|
236
|
+
function pad(text , width , ambiguous ) {
|
|
237
|
+
const have = displayWidth(text, ambiguous);
|
|
238
|
+
return have >= width ? text : text + " ".repeat(width - have);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* text broken into lines of at most width columns: at spaces when a word fits, inside a word (between clusters,
|
|
243
|
+
* never inside one) when it does not; CJK text breaks between any two characters. Newlines in text start new lines.
|
|
244
|
+
*/
|
|
245
|
+
export function wrap(text , width , ambiguous ) {
|
|
246
|
+
if (width <= 0) return [];
|
|
247
|
+
const lines = [];
|
|
248
|
+
for (const paragraph of text.split("\n")) {
|
|
249
|
+
let line = "";
|
|
250
|
+
let lineWidth = 0;
|
|
251
|
+
let lastSpace = -1;
|
|
252
|
+
for (const { segment } of segmenter.segment(paragraph)) {
|
|
253
|
+
const w = clusterWidth(segment, ambiguous);
|
|
254
|
+
if (lineWidth + w > width) {
|
|
255
|
+
if (segment !== " " && lastSpace > 0) {
|
|
256
|
+
const head = line.slice(0, lastSpace);
|
|
257
|
+
const tail = line.slice(lastSpace + 1);
|
|
258
|
+
lines.push(head);
|
|
259
|
+
line = tail;
|
|
260
|
+
lineWidth = displayWidth(tail, ambiguous);
|
|
261
|
+
} else {
|
|
262
|
+
lines.push(line);
|
|
263
|
+
line = "";
|
|
264
|
+
lineWidth = 0;
|
|
265
|
+
}
|
|
266
|
+
lastSpace = -1;
|
|
267
|
+
if (segment === " ") continue;
|
|
268
|
+
}
|
|
269
|
+
if (segment === " ") lastSpace = line.length;
|
|
270
|
+
line += segment;
|
|
271
|
+
lineWidth += w;
|
|
272
|
+
}
|
|
273
|
+
lines.push(line);
|
|
274
|
+
}
|
|
275
|
+
return lines;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: control characters are exactly what sanitize replaces.
|
|
279
|
+
const CONTROL = /[\u0000-\u0009\u000b-\u001f\u007f-\u009f\u202a-\u202e\u2066-\u2069]/gu;
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Untrusted text made safe to draw: C0, C1 and DEL control characters and the bidirectional overrides and isolates
|
|
283
|
+
* become `?`, a tab a space, CR LF and a lone CR a newline. Newlines are kept for the wrapping views (a session's
|
|
284
|
+
* entries, a claim) to break lines at; a row that draws text on one line shows each as a space (screen.ts). So a
|
|
285
|
+
* session's text, a claim, a gap's command or a file name can never move the cursor, set the clipboard (OSC 52),
|
|
286
|
+
* retitle the window, reset the terminal or reverse how a path reads. Lone surrogates become U+FFFD. Pure.
|
|
287
|
+
*/
|
|
288
|
+
export function sanitize(text ) {
|
|
289
|
+
return String(text).toWellFormed().replace(/\r\n?/gu, "\n").replace(/\t/gu, " ").replace(CONTROL, "?");
|
|
290
|
+
}
|