skyloom 1.13.0 → 1.13.1
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 +17 -22
- package/dist/cli/main.js +79 -78
- package/dist/cli/main.js.map +1 -1
- package/dist/cli/tui.d.ts +52 -19
- package/dist/cli/tui.d.ts.map +1 -1
- package/dist/cli/tui.js +192 -266
- package/dist/cli/tui.js.map +1 -1
- package/package.json +1 -1
- package/src/cli/main.ts +58 -66
- package/src/cli/tui.ts +220 -272
- package/tests/tui.test.ts +67 -0
package/src/cli/tui.ts
CHANGED
|
@@ -1,272 +1,220 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 天空织机 TUI —
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
*
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
["
|
|
41
|
-
["
|
|
42
|
-
["
|
|
43
|
-
["
|
|
44
|
-
["
|
|
45
|
-
["
|
|
46
|
-
]
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
return
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
if (!stdin.isTTY) {
|
|
224
|
-
const rl = readline.createInterface({ input: stdin });
|
|
225
|
-
rl.on("line", (line) => { rl.close(); resolve(line.trim()); });
|
|
226
|
-
return;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
stdin.setRawMode(true);
|
|
230
|
-
stdin.resume();
|
|
231
|
-
render();
|
|
232
|
-
|
|
233
|
-
let escBuf = "";
|
|
234
|
-
stdin.on("data", (data: Buffer) => {
|
|
235
|
-
const str = data.toString();
|
|
236
|
-
escBuf += str;
|
|
237
|
-
|
|
238
|
-
if (escBuf.startsWith("\x1b[") && escBuf.length >= 3) {
|
|
239
|
-
const code = escBuf[2]; escBuf = "";
|
|
240
|
-
if (code === "A") { if (palette) selIdx = Math.max(0, selIdx - 1); render(); return; }
|
|
241
|
-
if (code === "B") { if (palette) { const all = [...AGENT_CMDS.map(c => c[1]), ...ACTION_CMDS.map(c => c[0])]; selIdx = Math.min(all.filter(a => a.includes(buf)).length - 1, selIdx + 1); } render(); return; }
|
|
242
|
-
if (code === "C") { if (cursor < buf.length) cursor++; render(); return; }
|
|
243
|
-
if (code === "D") { if (cursor > 0) cursor--; render(); return; }
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
for (const ch of escBuf) {
|
|
247
|
-
escBuf = "";
|
|
248
|
-
if (ch === "\x1b") { palette = false; render(); return; }
|
|
249
|
-
if (ch === "\r" || ch === "\n") {
|
|
250
|
-
if (palette) {
|
|
251
|
-
const all = [...AGENT_CMDS.map(c => c[1]), ...ACTION_CMDS.map(c => c[0])];
|
|
252
|
-
const filtered = all.filter(a => a.includes(buf));
|
|
253
|
-
if (filtered[selIdx]) buf = filtered[selIdx];
|
|
254
|
-
palette = false;
|
|
255
|
-
render();
|
|
256
|
-
stdin.setRawMode(false); stdin.pause(); resolve(buf.trim()); return;
|
|
257
|
-
}
|
|
258
|
-
stdin.setRawMode(false); stdin.pause(); resolve(buf.trim()); return;
|
|
259
|
-
}
|
|
260
|
-
if (ch === "\t") { /* ignore */ return; }
|
|
261
|
-
if (ch === "\x7f" || ch === "\b") { if (cursor > 0) { buf = buf.slice(0, cursor - 1) + buf.slice(cursor); cursor--; } if (!buf) palette = false; render(); return; }
|
|
262
|
-
if (ch === "\x03") { stdin.setRawMode(false); stdin.pause(); resolve("/quit"); return; }
|
|
263
|
-
if (ch >= " ") {
|
|
264
|
-
buf = buf.slice(0, cursor) + ch + buf.slice(cursor); cursor++;
|
|
265
|
-
if (ch === "/") { palette = true; selIdx = 0; }
|
|
266
|
-
else if (palette) selIdx = 0;
|
|
267
|
-
render(); return;
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
});
|
|
271
|
-
});
|
|
272
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 天空织机 TUI — a polished *linear* terminal interface.
|
|
3
|
+
*
|
|
4
|
+
* Design note: the previous version tried to be a full-screen app, redrawing
|
|
5
|
+
* the whole screen on every keystroke while the reply streamed linearly below
|
|
6
|
+
* it — the two fought, the conversation never persisted, and hand-rolled
|
|
7
|
+
* raw-mode editing mangled CJK width. This rewrite is linear (like Claude Code
|
|
8
|
+
* / opencode): real readline line-editing + a CJK-aware wrapping stream
|
|
9
|
+
* renderer. Robust, flicker-free, and it actually reads like a conversation.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import * as readline from "readline";
|
|
13
|
+
import chalk from "chalk";
|
|
14
|
+
import { agentTheme, PALETTE } from "../core/theme";
|
|
15
|
+
|
|
16
|
+
const TUI_VERSION = (() => { try { return require("../../package.json").version; } catch { return ""; } })();
|
|
17
|
+
|
|
18
|
+
export interface TUIContext {
|
|
19
|
+
agent: any;
|
|
20
|
+
agents: Map<string, any>;
|
|
21
|
+
model: string;
|
|
22
|
+
cost: string;
|
|
23
|
+
width: number;
|
|
24
|
+
height: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/* ── Slash commands (for tab-completion + the inline palette) ── */
|
|
28
|
+
export const SLASH_COMMANDS: [string, string][] = [
|
|
29
|
+
["/fog", "≋ 雾 · 探索洞察"],
|
|
30
|
+
["/rain", "⸽ 雨 · 创造产出"],
|
|
31
|
+
["/frost", "✱ 霜 · 精炼品质"],
|
|
32
|
+
["/snow", "❉ 雪 · 架构规划"],
|
|
33
|
+
["/dew", "∘ 露 · 可靠守护"],
|
|
34
|
+
["/fair", "☼ 晴 · 情感陪伴"],
|
|
35
|
+
["/help", "查看所有命令"],
|
|
36
|
+
["/setup", "配置向导"],
|
|
37
|
+
["/model", "模型信息"],
|
|
38
|
+
["/cost", "费用统计"],
|
|
39
|
+
["/status", "状态总览"],
|
|
40
|
+
["/memory", "记忆状态"],
|
|
41
|
+
["/sessions", "会话列表"],
|
|
42
|
+
["/workspace", "工作空间"],
|
|
43
|
+
["/compact", "压缩上下文"],
|
|
44
|
+
["/clear", "清屏"],
|
|
45
|
+
["/task ", "多 Agent 编排"],
|
|
46
|
+
["/mcp", "MCP 服务器"],
|
|
47
|
+
["/version", "版本信息"],
|
|
48
|
+
["/quit", "退出"],
|
|
49
|
+
];
|
|
50
|
+
|
|
51
|
+
/* ════════════════════════════════════════
|
|
52
|
+
CJK-aware display width
|
|
53
|
+
════════════════════════════════════════ */
|
|
54
|
+
/** Visual columns occupied by a single code point (CJK / fullwidth = 2). */
|
|
55
|
+
export function charWidth(cp: number): number {
|
|
56
|
+
if (cp === 0) return 0;
|
|
57
|
+
if (cp < 32 || (cp >= 0x7f && cp < 0xa0)) return 0; // control
|
|
58
|
+
// East-Asian wide / fullwidth ranges
|
|
59
|
+
if (
|
|
60
|
+
(cp >= 0x1100 && cp <= 0x115f) || // Hangul Jamo
|
|
61
|
+
(cp >= 0x2e80 && cp <= 0x303e) || // CJK radicals, Kangxi, punctuation
|
|
62
|
+
(cp >= 0x3041 && cp <= 0x33ff) || // Hiragana…CJK symbols
|
|
63
|
+
(cp >= 0x3400 && cp <= 0x4dbf) || // CJK Ext A
|
|
64
|
+
(cp >= 0x4e00 && cp <= 0x9fff) || // CJK Unified
|
|
65
|
+
(cp >= 0xa000 && cp <= 0xa4cf) || // Yi
|
|
66
|
+
(cp >= 0xac00 && cp <= 0xd7a3) || // Hangul syllables
|
|
67
|
+
(cp >= 0xf900 && cp <= 0xfaff) || // CJK compat
|
|
68
|
+
(cp >= 0xfe10 && cp <= 0xfe19) ||
|
|
69
|
+
(cp >= 0xfe30 && cp <= 0xfe6f) || // CJK compat forms
|
|
70
|
+
(cp >= 0xff00 && cp <= 0xff60) || // Fullwidth forms
|
|
71
|
+
(cp >= 0xffe0 && cp <= 0xffe6) ||
|
|
72
|
+
(cp >= 0x1f300 && cp <= 0x1faff) // emoji / pictographs
|
|
73
|
+
) return 2;
|
|
74
|
+
return 1;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const ANSI_RE = /\x1b\[[0-9;]*m/g;
|
|
78
|
+
|
|
79
|
+
/** Visual width of a string, ignoring ANSI color codes. */
|
|
80
|
+
export function visualWidth(s: string): number {
|
|
81
|
+
let w = 0;
|
|
82
|
+
for (const ch of s.replace(ANSI_RE, "")) w += charWidth(ch.codePointAt(0) || 0);
|
|
83
|
+
return w;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Pad a string (containing ANSI) to a visual width. */
|
|
87
|
+
export function padVisual(s: string, width: number): string {
|
|
88
|
+
const diff = width - visualWidth(s);
|
|
89
|
+
return diff > 0 ? s + " ".repeat(diff) : s;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/* ════════════════════════════════════════
|
|
93
|
+
Streaming renderer — word-wrap aware, CJK aware
|
|
94
|
+
════════════════════════════════════════ */
|
|
95
|
+
/**
|
|
96
|
+
* Writes streamed text with a fixed left gutter, wrapping at the terminal
|
|
97
|
+
* width. English wraps on word boundaries; CJK wraps per glyph. Color is
|
|
98
|
+
* applied per flushed chunk so styling survives wrapping.
|
|
99
|
+
*/
|
|
100
|
+
export class StreamRenderer {
|
|
101
|
+
private col = 0;
|
|
102
|
+
private word = "";
|
|
103
|
+
private atLineStart = true;
|
|
104
|
+
private out: NodeJS.WriteStream;
|
|
105
|
+
private gutter: string;
|
|
106
|
+
private maxCols: number;
|
|
107
|
+
private color: (s: string) => string;
|
|
108
|
+
|
|
109
|
+
constructor(out: NodeJS.WriteStream, opts?: { gutter?: string; color?: (s: string) => string }) {
|
|
110
|
+
this.out = out;
|
|
111
|
+
this.gutter = opts?.gutter ?? " ";
|
|
112
|
+
this.color = opts?.color ?? ((s) => s);
|
|
113
|
+
const cols = out.columns || 80;
|
|
114
|
+
// content width excludes the gutter; clamp for readability
|
|
115
|
+
this.maxCols = Math.max(32, Math.min(cols - visualWidth(this.gutter) - 1, 96));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Lazily emit the left gutter at the start of each visual line. */
|
|
119
|
+
private startLine() { if (this.atLineStart) { this.out.write(this.gutter); this.atLineStart = false; } }
|
|
120
|
+
private newline() { this.out.write("\n"); this.atLineStart = true; this.col = 0; }
|
|
121
|
+
|
|
122
|
+
private flushWord() {
|
|
123
|
+
if (!this.word) return;
|
|
124
|
+
const w = visualWidth(this.word);
|
|
125
|
+
if (this.col > 0 && this.col + w > this.maxCols) this.newline();
|
|
126
|
+
this.startLine();
|
|
127
|
+
this.out.write(this.color(this.word));
|
|
128
|
+
this.col += w;
|
|
129
|
+
this.word = "";
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Feed a chunk of streamed text. */
|
|
133
|
+
write(text: string) {
|
|
134
|
+
for (const ch of text) {
|
|
135
|
+
if (ch === "\r") continue; // normalize CRLF / stray CR from providers
|
|
136
|
+
if (ch === "\n") { this.flushWord(); this.newline(); continue; }
|
|
137
|
+
if (ch === " " || ch === "\t") {
|
|
138
|
+
this.flushWord();
|
|
139
|
+
if (this.col > 0 && this.col < this.maxCols) { this.startLine(); this.out.write(" "); this.col += 1; }
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
const cp = ch.codePointAt(0) || 0;
|
|
143
|
+
if (charWidth(cp) === 2) {
|
|
144
|
+
// CJK / wide: flush any pending latin word, then place this glyph
|
|
145
|
+
this.flushWord();
|
|
146
|
+
if (this.col > 0 && this.col + 2 > this.maxCols) this.newline();
|
|
147
|
+
this.startLine();
|
|
148
|
+
this.out.write(this.color(ch));
|
|
149
|
+
this.col += 2;
|
|
150
|
+
} else {
|
|
151
|
+
this.word += ch;
|
|
152
|
+
// very long unbroken token: hard-break to avoid overflow
|
|
153
|
+
if (visualWidth(this.word) >= this.maxCols) this.flushWord();
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Flush any buffered word (call before switching styles / ending). */
|
|
159
|
+
flush() { this.flushWord(); }
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/* ════════════════════════════════════════
|
|
163
|
+
Input — readline-based, robust line editing
|
|
164
|
+
════════════════════════════════════════ */
|
|
165
|
+
/** Tab-completer for slash commands. */
|
|
166
|
+
function slashCompleter(line: string): [string[], string] {
|
|
167
|
+
if (!line.startsWith("/")) return [[], line];
|
|
168
|
+
const names = SLASH_COMMANDS.map(([c]) => c.trimEnd());
|
|
169
|
+
const hits = names.filter((c) => c.startsWith(line));
|
|
170
|
+
return [hits.length ? hits : names, line];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** The prompt string for an agent: a small mineral seal + chevron. */
|
|
174
|
+
export function promptFor(agentName: string): string {
|
|
175
|
+
const t = agentTheme(agentName);
|
|
176
|
+
return chalk.hex(t.hex)(` ${t.symbol} ${t.kanji} `) + chalk.hex(PALETTE.inkLight)("❯ ");
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Cross-turn input history (↑/↓), shared by every per-turn reader. */
|
|
180
|
+
const inputHistory: string[] = [];
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Read one line with the agent-themed prompt. A fresh readline interface is
|
|
184
|
+
* created and closed per call — this deliberately avoids clashing with the
|
|
185
|
+
* separate readline prompts used by the setup wizard and tool-approval flow
|
|
186
|
+
* (two live interfaces on one stdin corrupt input). History is preserved
|
|
187
|
+
* manually across turns.
|
|
188
|
+
*/
|
|
189
|
+
export function readLine(agentName: string, out: NodeJS.WriteStream = process.stdout): Promise<string> {
|
|
190
|
+
return new Promise((resolve) => {
|
|
191
|
+
const rl = readline.createInterface({
|
|
192
|
+
input: process.stdin,
|
|
193
|
+
output: out,
|
|
194
|
+
completer: slashCompleter,
|
|
195
|
+
terminal: process.stdin.isTTY ?? false,
|
|
196
|
+
history: [...inputHistory],
|
|
197
|
+
historySize: 200,
|
|
198
|
+
} as any);
|
|
199
|
+
rl.on("SIGINT", () => { out.write("\n" + chalk.dim(" 再会。\n")); rl.close(); process.exit(0); });
|
|
200
|
+
rl.question(promptFor(agentName), (answer) => {
|
|
201
|
+
const trimmed = answer.trim();
|
|
202
|
+
if (trimmed) inputHistory.unshift(trimmed);
|
|
203
|
+
rl.close();
|
|
204
|
+
resolve(trimmed);
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** Render the inline slash-command palette (printed, not full-screen). */
|
|
210
|
+
export function renderPalette(filter: string): string {
|
|
211
|
+
const f = filter.toLowerCase();
|
|
212
|
+
const matches = SLASH_COMMANDS.filter(([c]) => c.toLowerCase().startsWith(f));
|
|
213
|
+
const list = matches.length ? matches : SLASH_COMMANDS;
|
|
214
|
+
const lines = list.slice(0, 12).map(([cmd, desc]) => {
|
|
215
|
+
const isAgent = ["/fog", "/rain", "/frost", "/snow", "/dew", "/fair"].includes(cmd.trim());
|
|
216
|
+
const name = isAgent ? chalk.hex(agentTheme(cmd.trim().slice(1)).hex)(cmd.padEnd(12)) : chalk.hex(PALETTE.inkMid)(cmd.padEnd(12));
|
|
217
|
+
return " " + name + chalk.hex(PALETTE.inkLight)(desc);
|
|
218
|
+
});
|
|
219
|
+
return chalk.dim(" 命令 · Tab 补全\n") + lines.join("\n") + "\n";
|
|
220
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { charWidth, visualWidth, padVisual, StreamRenderer } from "../src/cli/tui";
|
|
3
|
+
|
|
4
|
+
describe("CJK-aware width", () => {
|
|
5
|
+
it("counts ascii as 1, CJK as 2", () => {
|
|
6
|
+
expect(charWidth("a".codePointAt(0)!)).toBe(1);
|
|
7
|
+
expect(charWidth("雾".codePointAt(0)!)).toBe(2);
|
|
8
|
+
expect(charWidth(",".codePointAt(0)!)).toBe(2); // fullwidth comma
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it("treats control chars as width 0", () => {
|
|
12
|
+
expect(charWidth("\r".codePointAt(0)!)).toBe(0);
|
|
13
|
+
expect(charWidth("\n".codePointAt(0)!)).toBe(0);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("visualWidth sums correctly and ignores ANSI", () => {
|
|
17
|
+
expect(visualWidth("abc")).toBe(3);
|
|
18
|
+
expect(visualWidth("雾雨")).toBe(4);
|
|
19
|
+
expect(visualWidth("a雾b")).toBe(4);
|
|
20
|
+
expect(visualWidth("\x1b[36m雾\x1b[39m")).toBe(2); // color codes don't count
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("padVisual pads to a visual column count", () => {
|
|
24
|
+
expect(visualWidth(padVisual("雾", 6))).toBe(6);
|
|
25
|
+
expect(padVisual("abc", 2)).toBe("abc"); // never truncates
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
/** Capture writes from a StreamRenderer into a string. */
|
|
30
|
+
function render(text: string, columns = 40, chunk = 3): string {
|
|
31
|
+
let buf = "";
|
|
32
|
+
const fakeOut = { columns, write: (s: string) => { buf += s; return true; } } as any;
|
|
33
|
+
const r = new StreamRenderer(fakeOut, { gutter: " " });
|
|
34
|
+
for (let i = 0; i < text.length; i += chunk) r.write(text.slice(i, i + chunk));
|
|
35
|
+
r.flush();
|
|
36
|
+
return buf;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
describe("StreamRenderer", () => {
|
|
40
|
+
it("prefixes every line with the gutter", () => {
|
|
41
|
+
const out = render("hello world", 80);
|
|
42
|
+
expect(out.startsWith(" ")).toBe(true);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("never exceeds the content width per visual line", () => {
|
|
46
|
+
const out = render("天空织机是一个本地优先的多智能体终端框架用于验证换行宽度限制是否生效啊", 40);
|
|
47
|
+
const maxContent = Math.min(40 - 2 - 1, 96);
|
|
48
|
+
for (const line of out.split("\n")) {
|
|
49
|
+
expect(visualWidth(line)).toBeLessThanOrEqual(2 + maxContent); // gutter + content
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("strips stray carriage returns (CRLF from providers)", () => {
|
|
54
|
+
const out = render("line one\r\nline two", 80);
|
|
55
|
+
expect(out.includes("\r")).toBe(false);
|
|
56
|
+
expect(out).toContain("line one");
|
|
57
|
+
expect(out).toContain("line two");
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("wraps English on word boundaries without splitting short words", () => {
|
|
61
|
+
// maxCols floors at 32, so use text long enough to exceed it.
|
|
62
|
+
const out = render("alpha beta gamma delta epsilon zeta eta theta iota kappa", 40);
|
|
63
|
+
expect(out.split("\n").length).toBeGreaterThan(1);
|
|
64
|
+
// no whole word should be broken across a wrap (each appears intact)
|
|
65
|
+
for (const w of ["alpha", "epsilon", "kappa"]) expect(out).toContain(w);
|
|
66
|
+
});
|
|
67
|
+
});
|