@uncarved/jade 0.1.0 → 0.1.2
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 +198 -188
- package/dist/distro.d.ts +12 -1
- package/dist/distro.js +37 -2
- package/dist/distro.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/main.js +159 -29
- package/dist/main.js.map +1 -1
- package/dist/mcp.js +1 -1
- package/dist/mcp.js.map +1 -1
- package/dist/plugins/policy.d.ts +41 -9
- package/dist/plugins/policy.js +367 -58
- package/dist/plugins/policy.js.map +1 -1
- package/dist/plugins/prompts.d.ts +10 -0
- package/dist/plugins/prompts.js +29 -2
- package/dist/plugins/prompts.js.map +1 -1
- package/dist/plugins/repeats.js +42 -7
- package/dist/plugins/repeats.js.map +1 -1
- package/dist/plugins/review.d.ts +16 -0
- package/dist/plugins/review.js +92 -0
- package/dist/plugins/review.js.map +1 -0
- package/dist/provider.d.ts +62 -3
- package/dist/provider.js +150 -7
- package/dist/provider.js.map +1 -1
- package/dist/sessions.d.ts +9 -0
- package/dist/sessions.js +14 -1
- package/dist/sessions.js.map +1 -1
- package/dist/sys.d.ts +5 -1
- package/dist/sys.js +3 -2
- package/dist/sys.js.map +1 -1
- package/dist/tools/screenshot.d.ts +15 -0
- package/dist/tools/screenshot.js +162 -0
- package/dist/tools/screenshot.js.map +1 -0
- package/dist/tools/shell.d.ts +6 -0
- package/dist/tools/shell.js +26 -7
- package/dist/tools/shell.js.map +1 -1
- package/dist/tools/task.d.ts +4 -0
- package/dist/tools/task.js +4 -1
- package/dist/tools/task.js.map +1 -1
- package/dist/ui/configure.js +121 -28
- package/dist/ui/configure.js.map +1 -1
- package/dist/ui/markdown.d.ts +17 -0
- package/dist/ui/markdown.js +204 -0
- package/dist/ui/markdown.js.map +1 -0
- package/dist/ui/render.d.ts +2 -0
- package/dist/ui/render.js +10 -0
- package/dist/ui/render.js.map +1 -1
- package/dist/ui/screen.d.ts +8 -0
- package/dist/ui/screen.js +3 -0
- package/dist/ui/screen.js.map +1 -1
- package/dist/ui/transcript.js +85 -13
- package/dist/ui/transcript.js.map +1 -1
- package/dist/ui/tui.d.ts +17 -0
- package/dist/ui/tui.js +367 -93
- package/dist/ui/tui.js.map +1 -1
- package/package.json +5 -5
- package/src/distro.ts +46 -4
- package/src/index.ts +2 -0
- package/src/main.ts +156 -31
- package/src/mcp.ts +1 -1
- package/src/plugins/policy.ts +366 -51
- package/src/plugins/prompts.ts +36 -2
- package/src/plugins/repeats.ts +42 -8
- package/src/plugins/review.ts +109 -0
- package/src/provider.ts +170 -11
- package/src/sessions.ts +14 -1
- package/src/sys.ts +5 -2
- package/src/tools/screenshot.ts +171 -0
- package/src/tools/shell.ts +25 -7
- package/src/tools/task.ts +5 -1
- package/src/ui/configure.ts +127 -29
- package/src/ui/markdown.ts +219 -0
- package/src/ui/render.ts +9 -0
- package/src/ui/screen.ts +5 -0
- package/src/ui/transcript.ts +84 -16
- package/src/ui/tui.tsx +480 -142
package/src/ui/tui.tsx
CHANGED
|
@@ -22,9 +22,11 @@ import {
|
|
|
22
22
|
useState,
|
|
23
23
|
useSyncExternalStore,
|
|
24
24
|
} from "react";
|
|
25
|
+
import stringWidth from "string-width";
|
|
25
26
|
import wrapAnsi from "wrap-ansi";
|
|
26
27
|
import { mentionAt, mentionText, rankPaths } from "../mentions.ts";
|
|
27
28
|
import { edit, empty, layout, type EditState } from "./editor.ts";
|
|
29
|
+
import { markdownStream, type MdLine } from "./markdown.ts";
|
|
28
30
|
import {
|
|
29
31
|
dim,
|
|
30
32
|
type Choice,
|
|
@@ -33,6 +35,16 @@ import {
|
|
|
33
35
|
type StatusInfo,
|
|
34
36
|
} from "./screen.ts";
|
|
35
37
|
|
|
38
|
+
/**
|
|
39
|
+
* 界面可用的尺寸:宽度比终端少一列。
|
|
40
|
+
* Ink 增量重画时每行写成「内容 + ESC[K」;一行正好占满终端宽度时光标停在最后一列(等待换行),
|
|
41
|
+
* 紧跟的 ESC[K 会把最后一个字符擦掉,输入框之类的右边框就缺了一块。留出最后一列就不会这样
|
|
42
|
+
*/
|
|
43
|
+
function useScreenSize(): { columns: number; rows: number } {
|
|
44
|
+
const { columns, rows } = useWindowSize();
|
|
45
|
+
return { columns: Math.max(1, columns - 1), rows };
|
|
46
|
+
}
|
|
47
|
+
|
|
36
48
|
export interface TuiHandlers {
|
|
37
49
|
/** 用户在输入框里提交了一行(消息或命令) */
|
|
38
50
|
submit(text: string): void;
|
|
@@ -66,7 +78,8 @@ type Dialog =
|
|
|
66
78
|
interface UiState {
|
|
67
79
|
/** 对话内容变化时递增,让界面重画 */
|
|
68
80
|
version: number;
|
|
69
|
-
|
|
81
|
+
/** 还没结束的正文:完整的行已经移进对话,这里只有最后不完整的一行(思考是对话里的一项,见 Thinking) */
|
|
82
|
+
live?: { text: string; /** Markdown 里攒着的部分(还没结束的表格),显示在 text 前面 */ pending?: MdLine[] };
|
|
70
83
|
/** 对话区离底部滚上去了多少行;0 表示跟着最新的输出 */
|
|
71
84
|
scroll: number;
|
|
72
85
|
busy?: { label: string; since: number };
|
|
@@ -88,18 +101,26 @@ const wrap = (text: string, width: number) =>
|
|
|
88
101
|
* 对话里的一项:普通的一行,或者可以点击展开、收起的一段。
|
|
89
102
|
* - details:折叠起来的完整内容
|
|
90
103
|
* - oneRow:收起时只占一行,放不下的部分截掉,展开时完整显示
|
|
91
|
-
* - title:展开时代替 text
|
|
104
|
+
* - title:展开时代替 text 显示的标题
|
|
105
|
+
* - think:思考;收起时按宽度现排一行(思考中显示最新的进展,结束后显示用了多久),展开时显示全文
|
|
92
106
|
*/
|
|
93
107
|
interface Line {
|
|
94
108
|
text: string;
|
|
109
|
+
think?: Thinking;
|
|
95
110
|
details?: string;
|
|
96
111
|
oneRow?: boolean;
|
|
97
112
|
title?: string;
|
|
98
113
|
open?: boolean;
|
|
99
114
|
/** 之后用 amend 补充内容时用的名字 */
|
|
100
115
|
id?: string;
|
|
116
|
+
/** 按宽度现排的内容(Markdown 表格、分隔线),代替 text */
|
|
117
|
+
render?(width: number): string[];
|
|
101
118
|
}
|
|
102
119
|
|
|
120
|
+
/** Markdown 排好的一行折成屏幕上的行 */
|
|
121
|
+
const mdRows = (l: MdLine, width: number) =>
|
|
122
|
+
typeof l === "string" ? wrap(l, width) : l.render(width);
|
|
123
|
+
|
|
103
124
|
/** 按宽度截成一行,截掉了内容时以 … 结尾;留出两列给 … 和展开标记 */
|
|
104
125
|
function oneRow(text: string, width: number): { row: string; cut: boolean } {
|
|
105
126
|
const rows = wrap(text, Math.max(1, width - 3));
|
|
@@ -109,14 +130,66 @@ function oneRow(text: string, width: number): { row: string; cut: boolean } {
|
|
|
109
130
|
|
|
110
131
|
/** 这一项能不能展开:有折叠的内容,或者收起时被截掉了一部分 */
|
|
111
132
|
function foldable(l: Line, width: number): boolean {
|
|
133
|
+
if (l.think) return l.think.text.trim() !== "";
|
|
112
134
|
return l.details !== undefined || (Boolean(l.oneRow) && oneRow(l.text, width).cut);
|
|
113
135
|
}
|
|
114
136
|
|
|
115
|
-
/**
|
|
116
|
-
|
|
117
|
-
|
|
137
|
+
/** 一段思考:start、end 是开始和结束的时间(毫秒),没有 end 表示还在思考 */
|
|
138
|
+
interface Thinking {
|
|
139
|
+
text: string;
|
|
140
|
+
start: number;
|
|
141
|
+
end?: number;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const elapsed = (t: Thinking) =>
|
|
145
|
+
`${Math.max(0, Math.round(((t.end ?? Date.now()) - t.start) / 1000))}s`;
|
|
146
|
+
|
|
147
|
+
/** 按显示宽度取 text 末尾最多 cols 列;截掉了开头时以 … 开头 */
|
|
148
|
+
export function tail(text: string, cols: number): string {
|
|
149
|
+
if (stringWidth(text) <= cols) return text;
|
|
150
|
+
const chars = [...text];
|
|
151
|
+
let out = "";
|
|
152
|
+
let w = 1;
|
|
153
|
+
for (let i = chars.length - 1; i >= 0; i--) {
|
|
154
|
+
const cw = stringWidth(chars[i]!);
|
|
155
|
+
if (w + cw > cols) break;
|
|
156
|
+
out = chars[i] + out;
|
|
157
|
+
w += cw;
|
|
158
|
+
}
|
|
159
|
+
return `…${out}`;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* 思考收起时的一行(不含展开标记):思考中显示"✻ Thinking 12s · "加上最新一行的末尾,跟着模型实时往前走;
|
|
164
|
+
* 结束后显示"✻ Thought for 12s"
|
|
165
|
+
*/
|
|
166
|
+
function thinkingRow(t: Thinking, width: number): string {
|
|
167
|
+
if (t.end !== undefined) return dim(`✻ Thought for ${elapsed(t)}`);
|
|
168
|
+
const label = `✻ Thinking ${elapsed(t)}`;
|
|
169
|
+
const latest = t.text
|
|
170
|
+
.split("\n")
|
|
171
|
+
.findLast((l) => l.trim())
|
|
172
|
+
?.trim()
|
|
173
|
+
.replace(/\s+/g, " ");
|
|
174
|
+
// 留出 " · "、" ▸" 和一列余量
|
|
175
|
+
const room = width - stringWidth(label) - 6;
|
|
176
|
+
return dim(latest && room >= 8 ? `${label} · ${tail(latest, room)}` : `${label}…`);
|
|
177
|
+
}
|
|
118
178
|
|
|
119
179
|
function wrapLine(l: Line, width: number): string[] {
|
|
180
|
+
if (l.render) return l.render(width);
|
|
181
|
+
if (l.think) {
|
|
182
|
+
if (!foldable(l, width)) return [thinkingRow(l.think, width)];
|
|
183
|
+
if (!l.open) return [`${thinkingRow(l.think, width)}${dim(" ▸")}`];
|
|
184
|
+
const t = l.think;
|
|
185
|
+
const title = dim(t.end === undefined ? `✻ Thinking ${elapsed(t)}` : `✻ Thought for ${elapsed(t)}`);
|
|
186
|
+
const body = t.text
|
|
187
|
+
.replace(/\s+$/, "")
|
|
188
|
+
.split("\n")
|
|
189
|
+
.map((r) => (r ? dim(` ${r}`) : r))
|
|
190
|
+
.join("\n");
|
|
191
|
+
return [...wrap(`${title}${dim(" ▾")}`, width), ...wrap(body, width)];
|
|
192
|
+
}
|
|
120
193
|
if (!foldable(l, width)) return wrap(l.text, width);
|
|
121
194
|
if (!l.open) {
|
|
122
195
|
const head = l.oneRow ? oneRow(l.text, width).row : l.text;
|
|
@@ -290,40 +363,61 @@ export interface TuiOptions {
|
|
|
290
363
|
/** 开关鼠标事件上报(SGR 格式),用来接收滚轮和点击;开着时终端里选中文字要按住 Shift */
|
|
291
364
|
const MOUSE_ON = "\x1b[?1000h\x1b[?1006h";
|
|
292
365
|
const MOUSE_OFF = "\x1b[?1006l\x1b[?1000l";
|
|
366
|
+
/**
|
|
367
|
+
* 光标样式:闪烁的竖线(DECSCUSR 5,再加上 ATT610 的"开始闪烁"给只认它的终端),一眼能看出输入框有焦点;
|
|
368
|
+
* 退出时还原成终端自己的默认样式。光标什么时候显示仍由 Ink 管(只有输入框或输入对话框有焦点时才显示)
|
|
369
|
+
*/
|
|
370
|
+
const CURSOR_BLINK = "\x1b[5 q\x1b[?12h";
|
|
371
|
+
const CURSOR_RESET = "\x1b[0 q";
|
|
293
372
|
|
|
294
373
|
export function tuiScreen(opts: TuiOptions): Screen {
|
|
295
374
|
const stdout = opts.io?.stdout ?? process.stdout;
|
|
296
375
|
const store = new Store(opts.status);
|
|
297
376
|
|
|
298
377
|
/**
|
|
299
|
-
*
|
|
300
|
-
*
|
|
378
|
+
* 正在进行的思考:一开始就作为一项放进对话(可以点击展开),之后的增量攒起来,每隔一小段时间改写一次那一项。
|
|
379
|
+
* 收起时只占一行,显示模型最新想到了哪里;结束时换成用了多久
|
|
301
380
|
*/
|
|
381
|
+
let thinking:
|
|
382
|
+
| { id: string; think: Thinking; timer?: ReturnType<typeof setTimeout> }
|
|
383
|
+
| undefined;
|
|
384
|
+
let thinkingCount = 0;
|
|
385
|
+
const redrawThinking = () => {
|
|
386
|
+
if (!thinking) return;
|
|
387
|
+
clearTimeout(thinking.timer);
|
|
388
|
+
thinking.timer = undefined;
|
|
389
|
+
store.amend(thinking.id, () => {});
|
|
390
|
+
};
|
|
391
|
+
const endThinking = () => {
|
|
392
|
+
if (!thinking) return;
|
|
393
|
+
thinking.think.end = Date.now();
|
|
394
|
+
redrawThinking();
|
|
395
|
+
thinking = undefined;
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
/** 正文按 Markdown 排版;一段正文结束时换一个新的,清掉代码块、表格的状态 */
|
|
399
|
+
let md = markdownStream();
|
|
400
|
+
const appendMd = (lines: MdLine[]) => {
|
|
401
|
+
for (const l of lines) {
|
|
402
|
+
if (typeof l === "string") store.append([l]);
|
|
403
|
+
else store.append([""], { render: l.render });
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
/** 推进正文:已经完整的行排版后移进对话,只把最后不完整的一行留在底部;all 时全部移进去,连同思考一起结束 */
|
|
302
408
|
const flushLive = (all = false) => {
|
|
409
|
+
if (all) endThinking();
|
|
303
410
|
const live = store.state.live;
|
|
304
411
|
if (!live) return;
|
|
305
|
-
if (live.kind === "thinking") {
|
|
306
|
-
if (!all) return;
|
|
307
|
-
store.update({ live: undefined });
|
|
308
|
-
const text = live.text.replace(/\s+$/, "");
|
|
309
|
-
if (!text) return;
|
|
310
|
-
store.append([thinkingHead(text)], {
|
|
311
|
-
oneRow: true,
|
|
312
|
-
title: dim("✻ thinking"),
|
|
313
|
-
details: text
|
|
314
|
-
.split("\n")
|
|
315
|
-
.map((l) => (l ? dim(` ${l}`) : l))
|
|
316
|
-
.join("\n"),
|
|
317
|
-
});
|
|
318
|
-
return;
|
|
319
|
-
}
|
|
320
412
|
const lines = live.text.split("\n");
|
|
321
413
|
const rest = all ? "" : lines.pop()!;
|
|
322
414
|
if (all && lines.at(-1) === "") lines.pop();
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
415
|
+
appendMd(lines.flatMap((l) => md.push(l)));
|
|
416
|
+
if (all) {
|
|
417
|
+
appendMd(md.flush());
|
|
418
|
+
md = markdownStream();
|
|
419
|
+
}
|
|
420
|
+
store.update({ live: rest || !all ? { text: rest, pending: md.pending() } : undefined });
|
|
327
421
|
};
|
|
328
422
|
|
|
329
423
|
const ask = <T,>(dialog: Dialog) =>
|
|
@@ -362,11 +456,11 @@ export function tuiScreen(opts: TuiOptions): Screen {
|
|
|
362
456
|
: {}),
|
|
363
457
|
},
|
|
364
458
|
);
|
|
365
|
-
stdout.write(MOUSE_ON);
|
|
459
|
+
stdout.write(MOUSE_ON + CURSOR_BLINK);
|
|
366
460
|
// 没走 close 就退出(process.exit)时也要把终端还原,否则会留在备用屏幕、鼠标上报也开着
|
|
367
461
|
const restore = () => {
|
|
368
462
|
try {
|
|
369
|
-
writeSync(1, `${MOUSE_OFF}\x1b[?1049l\x1b[?25h`);
|
|
463
|
+
writeSync(1, `${MOUSE_OFF}${CURSOR_RESET}\x1b[?1049l\x1b[?25h`);
|
|
370
464
|
} catch {}
|
|
371
465
|
};
|
|
372
466
|
if (!opts.io) process.on("exit", restore);
|
|
@@ -403,18 +497,27 @@ export function tuiScreen(opts: TuiOptions): Screen {
|
|
|
403
497
|
});
|
|
404
498
|
},
|
|
405
499
|
stream(kind, text) {
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
500
|
+
if (kind === "thinking") {
|
|
501
|
+
if (store.state.live) {
|
|
502
|
+
flushLive(true);
|
|
503
|
+
store.update({ live: undefined });
|
|
504
|
+
}
|
|
505
|
+
if (!thinking) {
|
|
506
|
+
if (!text.trim()) return;
|
|
507
|
+
const think: Thinking = { text: "", start: Date.now() };
|
|
508
|
+
thinking = { id: `\0thinking-${thinkingCount++}`, think };
|
|
509
|
+
store.append([""], { id: thinking.id, think });
|
|
510
|
+
}
|
|
511
|
+
thinking.think.text += text;
|
|
512
|
+
thinking.timer ??= setTimeout(redrawThinking, 50);
|
|
513
|
+
return;
|
|
410
514
|
}
|
|
515
|
+
endThinking();
|
|
411
516
|
if (!store.state.live) {
|
|
412
|
-
|
|
517
|
+
text = text.replace(/^\n+/, "");
|
|
413
518
|
if (!text) return;
|
|
414
519
|
}
|
|
415
|
-
store.update({
|
|
416
|
-
live: { kind, text: (store.state.live?.text ?? "") + text },
|
|
417
|
-
});
|
|
520
|
+
store.update({ live: { text: (store.state.live?.text ?? "") + text } });
|
|
418
521
|
if (text.includes("\n")) flushLive();
|
|
419
522
|
},
|
|
420
523
|
endStream() {
|
|
@@ -449,6 +552,9 @@ export function tuiScreen(opts: TuiOptions): Screen {
|
|
|
449
552
|
resolve: () => {},
|
|
450
553
|
}),
|
|
451
554
|
clear() {
|
|
555
|
+
clearTimeout(thinking?.timer);
|
|
556
|
+
thinking = undefined;
|
|
557
|
+
md = markdownStream();
|
|
452
558
|
store.clear();
|
|
453
559
|
},
|
|
454
560
|
async close() {
|
|
@@ -456,7 +562,7 @@ export function tuiScreen(opts: TuiOptions): Screen {
|
|
|
456
562
|
store.update({ live: undefined, busy: undefined, progress: [] }, true);
|
|
457
563
|
// 等最后一帧画完再卸载;卸载时 Ink 退出备用屏幕
|
|
458
564
|
await new Promise((r) => setTimeout(r, 50));
|
|
459
|
-
stdout.write(MOUSE_OFF);
|
|
565
|
+
stdout.write(MOUSE_OFF + CURSOR_RESET);
|
|
460
566
|
instance.unmount();
|
|
461
567
|
await instance.waitUntilExit();
|
|
462
568
|
process.off("exit", restore);
|
|
@@ -472,6 +578,15 @@ export function tuiScreen(opts: TuiOptions): Screen {
|
|
|
472
578
|
*/
|
|
473
579
|
const MOUSE = /^\[<(\d+);\d+;(\d+)([Mm])$/;
|
|
474
580
|
const WHEEL_LINES = 3;
|
|
581
|
+
/** 终端矮于这么多行时标题只占一行 */
|
|
582
|
+
const COMPACT_ROWS = 24;
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* 底部的菜单、选择框、多行输入最多占多少行:终端很矮时整个界面会高过终端,
|
|
586
|
+
* 超出的部分被裁掉,输入框的边框和状态栏就显示不全了
|
|
587
|
+
*/
|
|
588
|
+
const roomFor = (rows: number, max: number) =>
|
|
589
|
+
Math.max(3, Math.min(max, Math.floor((rows - 10) / 2)));
|
|
475
590
|
|
|
476
591
|
function App({
|
|
477
592
|
store,
|
|
@@ -488,7 +603,16 @@ function App({
|
|
|
488
603
|
}) {
|
|
489
604
|
const state = useSyncExternalStore(store.subscribe, store.get);
|
|
490
605
|
const dialog = state.dialogs[0];
|
|
491
|
-
|
|
606
|
+
// 运行中每 100ms 整体重画一次,推动转圈动画。不能让 Spinner 自己重画:
|
|
607
|
+
// Ink 只在输入框也跟着提交的那一帧保留光标,否则把光标藏起来,运行中光标就不见了
|
|
608
|
+
const [, tick] = useReducer((n: number) => n + 1, 0);
|
|
609
|
+
const busy = state.busy !== undefined;
|
|
610
|
+
useEffect(() => {
|
|
611
|
+
if (!busy) return;
|
|
612
|
+
const t = setInterval(tick, 100);
|
|
613
|
+
return () => clearInterval(t);
|
|
614
|
+
}, [busy]);
|
|
615
|
+
const { columns, rows } = useScreenSize();
|
|
492
616
|
|
|
493
617
|
// 对话区占掉标题和底部之外的全部高度,只画能看见的那几行
|
|
494
618
|
const ref = useRef<DOMElement>(null);
|
|
@@ -497,11 +621,8 @@ function App({
|
|
|
497
621
|
const done = store.lines(width);
|
|
498
622
|
// 还没结束的流式块和正在运行的工具的进度:思考只显示最新的一行,进度每个工具一行
|
|
499
623
|
const live = [
|
|
500
|
-
...(state.live?.
|
|
501
|
-
|
|
502
|
-
? [oneRow(thinkingHead(state.live.text), width).row]
|
|
503
|
-
: wrap(state.live.text, width)
|
|
504
|
-
: []),
|
|
624
|
+
...(state.live?.pending ?? []).flatMap((l) => mdRows(l, width)),
|
|
625
|
+
...(state.live?.text ? wrap(state.live.text, width) : []),
|
|
505
626
|
...state.progress.map((p) => oneRow(p.line, width).row),
|
|
506
627
|
];
|
|
507
628
|
const total = done.length + live.length;
|
|
@@ -572,7 +693,11 @@ function App({
|
|
|
572
693
|
>
|
|
573
694
|
{/* 垫的空白放在外层:里面这层在重画那一帧和平时一样高,对话区的 top(相对父节点)不变,点击换算的行号不会差一行 */}
|
|
574
695
|
<Box flexDirection="column" flexGrow={1} overflowY="hidden">
|
|
575
|
-
<Header
|
|
696
|
+
<Header
|
|
697
|
+
status={state.status}
|
|
698
|
+
version={version}
|
|
699
|
+
compact={rows < COMPACT_ROWS}
|
|
700
|
+
/>
|
|
576
701
|
<Box
|
|
577
702
|
ref={ref}
|
|
578
703
|
flexDirection="column"
|
|
@@ -618,7 +743,7 @@ function App({
|
|
|
618
743
|
active={!dialog}
|
|
619
744
|
onSubmit={() => store.scrollBy(-max, max)}
|
|
620
745
|
/>
|
|
621
|
-
<StatusBar status={state.status} />
|
|
746
|
+
<StatusBar status={state.status} width={Math.max(10, columns - 2)} />
|
|
622
747
|
</Box>
|
|
623
748
|
</Box>
|
|
624
749
|
</Box>
|
|
@@ -648,10 +773,37 @@ function Bot() {
|
|
|
648
773
|
);
|
|
649
774
|
}
|
|
650
775
|
|
|
651
|
-
function Header({
|
|
776
|
+
function Header({
|
|
777
|
+
status,
|
|
778
|
+
version,
|
|
779
|
+
compact,
|
|
780
|
+
}: {
|
|
781
|
+
status: StatusInfo;
|
|
782
|
+
version: string;
|
|
783
|
+
/** 终端很矮时只占一行,把高度留给对话和输入框 */
|
|
784
|
+
compact: boolean;
|
|
785
|
+
}) {
|
|
786
|
+
const { columns } = useScreenSize();
|
|
787
|
+
if (compact)
|
|
788
|
+
return (
|
|
789
|
+
<Box flexShrink={0} paddingX={1}>
|
|
790
|
+
<Text wrap="truncate-end">
|
|
791
|
+
<Text color="cyan" bold>
|
|
792
|
+
jade
|
|
793
|
+
</Text>
|
|
794
|
+
<Text dimColor> · </Text>
|
|
795
|
+
<Text color="cyan">{status.model}</Text>
|
|
796
|
+
<Text dimColor> · </Text>
|
|
797
|
+
<Text>{status.cwd}</Text>
|
|
798
|
+
{status.branch ? (
|
|
799
|
+
<Text color="magenta"> ({status.branch})</Text>
|
|
800
|
+
) : null}
|
|
801
|
+
</Text>
|
|
802
|
+
</Box>
|
|
803
|
+
);
|
|
652
804
|
return (
|
|
653
805
|
<Box flexShrink={0} borderStyle="round" borderColor="cyan" paddingX={1}>
|
|
654
|
-
<Bot />
|
|
806
|
+
{columns >= 50 ? <Bot /> : null}
|
|
655
807
|
<Box flexDirection="column" flexGrow={1} flexShrink={1}>
|
|
656
808
|
<Text wrap="truncate-end">
|
|
657
809
|
<Text color="cyan" bold>
|
|
@@ -687,23 +839,56 @@ function dialogKey(d: Dialog): number {
|
|
|
687
839
|
|
|
688
840
|
const FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
689
841
|
|
|
842
|
+
/** 转圈动画由 App 每 100ms 整体重画一次推动(见 App 里的 tick),这里只按当前时间画 */
|
|
690
843
|
function Spinner({ label, since }: { label: string; since: number }) {
|
|
691
|
-
const
|
|
692
|
-
useEffect(() => {
|
|
693
|
-
const t = setInterval(() => setNow(Date.now()), 100);
|
|
694
|
-
return () => clearInterval(t);
|
|
695
|
-
}, []);
|
|
844
|
+
const now = Date.now();
|
|
696
845
|
const seconds = Math.floor((now - since) / 1000);
|
|
697
846
|
return (
|
|
698
847
|
<Box marginTop={1}>
|
|
699
|
-
<Text
|
|
700
|
-
|
|
701
|
-
|
|
848
|
+
<Text wrap="truncate-end">
|
|
849
|
+
<Text color="cyan">{FRAMES[Math.floor(now / 100) % FRAMES.length]} </Text>
|
|
850
|
+
<Text>{label}… </Text>
|
|
851
|
+
<Text dimColor>({seconds}s · esc to interrupt)</Text>
|
|
852
|
+
</Text>
|
|
702
853
|
</Box>
|
|
703
854
|
);
|
|
704
855
|
}
|
|
705
856
|
|
|
706
|
-
|
|
857
|
+
/**
|
|
858
|
+
* 状态栏的一段,段之间用 " · " 隔开。放不下时按步骤缩短:shortAt 是换成短写法 short 的那一步,
|
|
859
|
+
* dropAt 是去掉整段的那一步;数字大的先做,没有 dropAt 的段一直保留
|
|
860
|
+
*/
|
|
861
|
+
interface Segment {
|
|
862
|
+
text: string;
|
|
863
|
+
short?: string;
|
|
864
|
+
shortAt?: number;
|
|
865
|
+
dropAt?: number;
|
|
866
|
+
color?: string;
|
|
867
|
+
dim?: boolean;
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
/** 在 width 列里放下尽量多的内容(保持原来的顺序) */
|
|
871
|
+
export function fitSegments(segments: Segment[], width: number): Segment[] {
|
|
872
|
+
const kept = segments.map((s) => ({ ...s }));
|
|
873
|
+
const total = () =>
|
|
874
|
+
kept.reduce((n, s, i) => n + stringWidth(s.text) + (i ? 3 : 0), 0);
|
|
875
|
+
const steps = kept
|
|
876
|
+
.flatMap((s) => [
|
|
877
|
+
...(s.short !== undefined && s.shortAt !== undefined
|
|
878
|
+
? [{ s, at: s.shortAt, drop: false }]
|
|
879
|
+
: []),
|
|
880
|
+
...(s.dropAt !== undefined ? [{ s, at: s.dropAt, drop: true }] : []),
|
|
881
|
+
])
|
|
882
|
+
.sort((a, b) => b.at - a.at);
|
|
883
|
+
for (const { s, drop } of steps) {
|
|
884
|
+
if (total() <= width) break;
|
|
885
|
+
if (drop) kept.splice(kept.indexOf(s), 1);
|
|
886
|
+
else s.text = s.short!;
|
|
887
|
+
}
|
|
888
|
+
return kept;
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
function StatusBar({ status, width }: { status: StatusInfo; width: number }) {
|
|
707
892
|
const ctx = status.context;
|
|
708
893
|
const pct = ctx ? Math.round((ctx.used / ctx.limit) * 100) : 0;
|
|
709
894
|
/** 离自动压缩还有多远:到阈值的 85% 起变黄 */
|
|
@@ -711,44 +896,88 @@ function StatusBar({ status }: { status: StatusInfo }) {
|
|
|
711
896
|
const modeColor =
|
|
712
897
|
status.mode === "yolo"
|
|
713
898
|
? "red"
|
|
714
|
-
: status.mode === "auto
|
|
899
|
+
: status.mode === "auto"
|
|
715
900
|
? "yellow"
|
|
716
|
-
: status.mode === "
|
|
717
|
-
? "
|
|
718
|
-
:
|
|
719
|
-
|
|
720
|
-
|
|
901
|
+
: status.mode === "plan"
|
|
902
|
+
? "magenta"
|
|
903
|
+
: "green";
|
|
904
|
+
const speed = status.speed;
|
|
905
|
+
// 窄的时候依次:提示变短、去掉提示、用量变短、去掉缓存、模式变短、去掉用量、上下文变短、去掉速度
|
|
906
|
+
const segments: Segment[] = [
|
|
907
|
+
{
|
|
908
|
+
text: `${status.mode} (shift+tab)`,
|
|
909
|
+
short: status.mode,
|
|
910
|
+
shortAt: 6,
|
|
911
|
+
color: modeColor,
|
|
912
|
+
},
|
|
913
|
+
ctx
|
|
914
|
+
? {
|
|
915
|
+
text: `ctx ${tokens(ctx.used)}/${tokens(ctx.limit)} (${pct}%)${ctx.compactAt ? `, compacts at ${tokens(ctx.compactAt)}` : ""}`,
|
|
916
|
+
short: `ctx ${pct}%`,
|
|
917
|
+
shortAt: 4,
|
|
918
|
+
color: nearCompact ? "yellow" : undefined,
|
|
919
|
+
dim: !nearCompact,
|
|
920
|
+
}
|
|
921
|
+
: { text: "ctx —", dim: true },
|
|
922
|
+
...(speed
|
|
923
|
+
? [
|
|
924
|
+
{
|
|
925
|
+
text: `${speed.live ? "~" : ""}${speed.tps >= 100 ? Math.round(speed.tps) : speed.tps.toFixed(1)} tok/s`,
|
|
926
|
+
dropAt: 3,
|
|
927
|
+
dim: true,
|
|
928
|
+
},
|
|
929
|
+
]
|
|
930
|
+
: []),
|
|
931
|
+
...(status.cache !== undefined
|
|
932
|
+
? [{ text: `cache ${Math.round(status.cache * 100)}%`, dropAt: 7, dim: true }]
|
|
933
|
+
: []),
|
|
934
|
+
...(status.spent
|
|
935
|
+
? [
|
|
936
|
+
{
|
|
937
|
+
text: `session ${tokens(status.spent.input)} in / ${tokens(status.spent.output)} out`,
|
|
938
|
+
short: `${tokens(status.spent.input)} in / ${tokens(status.spent.output)} out`,
|
|
939
|
+
shortAt: 8,
|
|
940
|
+
dropAt: 5,
|
|
941
|
+
dim: true,
|
|
942
|
+
},
|
|
943
|
+
]
|
|
944
|
+
: []),
|
|
945
|
+
{
|
|
946
|
+
text: "PgUp/PgDn scroll · click ▸ to expand · shift+drag to select",
|
|
947
|
+
short: "click ▸ to expand",
|
|
948
|
+
shortAt: 10,
|
|
949
|
+
dropAt: 9,
|
|
950
|
+
dim: true,
|
|
951
|
+
},
|
|
952
|
+
];
|
|
953
|
+
const shown = fitSegments(segments, width);
|
|
721
954
|
return (
|
|
722
955
|
<Box paddingX={1}>
|
|
723
956
|
<Text wrap="truncate-end">
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
</Text>
|
|
731
|
-
) : (
|
|
732
|
-
<Text dimColor>ctx —</Text>
|
|
733
|
-
)}
|
|
734
|
-
{status.cache !== undefined && (
|
|
735
|
-
<Text dimColor> · cache {Math.round(status.cache * 100)}%</Text>
|
|
736
|
-
)}
|
|
737
|
-
{status.spent && (
|
|
738
|
-
<Text dimColor>
|
|
739
|
-
{" "}
|
|
740
|
-
· session {tokens(status.spent.input)} in / {tokens(status.spent.output)} out
|
|
957
|
+
{shown.map((s, i) => (
|
|
958
|
+
<Text key={i}>
|
|
959
|
+
{i ? <Text dimColor> · </Text> : null}
|
|
960
|
+
<Text color={s.color} dimColor={s.dim}>
|
|
961
|
+
{s.text}
|
|
962
|
+
</Text>
|
|
741
963
|
</Text>
|
|
742
|
-
)}
|
|
743
|
-
<Text dimColor>
|
|
744
|
-
{" "}
|
|
745
|
-
· PgUp/PgDn scroll · click ▸ to expand · shift+drag to select
|
|
746
|
-
</Text>
|
|
964
|
+
))}
|
|
747
965
|
</Text>
|
|
748
966
|
</Box>
|
|
749
967
|
);
|
|
750
968
|
}
|
|
751
969
|
|
|
970
|
+
/** 一行提示:按宽度选第一个放得下的写法,都放不下时用最后一个并截断 */
|
|
971
|
+
function Hint({ variants, width }: { variants: string[]; width: number }) {
|
|
972
|
+
const text =
|
|
973
|
+
variants.find((v) => stringWidth(v) <= width) ?? variants.at(-1)!;
|
|
974
|
+
return (
|
|
975
|
+
<Text dimColor wrap="truncate-end">
|
|
976
|
+
{text}
|
|
977
|
+
</Text>
|
|
978
|
+
);
|
|
979
|
+
}
|
|
980
|
+
|
|
752
981
|
const tokens = (n: number) =>
|
|
753
982
|
n >= 1_000_000
|
|
754
983
|
? `${(n / 1_000_000).toFixed(n >= 10_000_000 ? 0 : 1)}M`
|
|
@@ -813,6 +1042,7 @@ function EditableText({
|
|
|
813
1042
|
mask,
|
|
814
1043
|
placeholder,
|
|
815
1044
|
active,
|
|
1045
|
+
maxRows = Infinity,
|
|
816
1046
|
}: {
|
|
817
1047
|
state: EditState;
|
|
818
1048
|
width: number;
|
|
@@ -820,40 +1050,61 @@ function EditableText({
|
|
|
820
1050
|
mask?: boolean;
|
|
821
1051
|
placeholder?: string;
|
|
822
1052
|
active: boolean;
|
|
1053
|
+
/** 最多显示几行;输入更长时只显示光标附近的几行 */
|
|
1054
|
+
maxRows?: number;
|
|
823
1055
|
}) {
|
|
824
1056
|
const ref = useRef<DOMElement>(null);
|
|
825
1057
|
// 只用来在布局变化后重画一次、重新计算光标位置;它给的是相对父元素的坐标,光标要的是相对整个输出
|
|
826
1058
|
const metrics = useBoxMetrics(ref);
|
|
827
|
-
const { setCursorPosition } = useCursor();
|
|
828
1059
|
const shown = mask ? "•".repeat([...state.value].length) : state.value;
|
|
829
1060
|
const cursor = mask
|
|
830
1061
|
? [...state.value.slice(0, state.cursor)].length
|
|
831
1062
|
: state.cursor;
|
|
832
|
-
const
|
|
1063
|
+
const all = layout(shown, cursor, width - prefix.length);
|
|
1064
|
+
const { col } = all;
|
|
1065
|
+
// 行数超过 maxRows 时滚动,光标所在的行总在可见范围里
|
|
1066
|
+
const first = Math.max(
|
|
1067
|
+
0,
|
|
1068
|
+
Math.min(all.row - maxRows + 1, all.rows.length - maxRows),
|
|
1069
|
+
);
|
|
1070
|
+
const rows = all.rows.slice(first, first + maxRows);
|
|
1071
|
+
const row = all.row - first;
|
|
833
1072
|
|
|
834
1073
|
// 光标位置要在渲染时设:useCursor 在这次提交时交给 Ink,跟这一帧一起画出来。
|
|
835
1074
|
// 放在 useEffect 里会晚一帧,光标总是停在上一次按键的位置(只移动光标时最明显)。
|
|
836
|
-
//
|
|
1075
|
+
// 这里用的是上一次的布局,提交后要核对一遍:
|
|
1076
|
+
// - 输入框相对父元素挪了位置时 useBoxMetrics 会再触发一次渲染
|
|
1077
|
+
// - 只有祖先挪了位置时(窗口尺寸变化后整帧重画那一帧上下各垫一行,下一帧再去掉)它看不出来,
|
|
1078
|
+
// 光标会停在比输入行低一行的地方,要自己比一下整体位置
|
|
837
1079
|
const at =
|
|
838
1080
|
active && metrics.hasMeasured && ref.current
|
|
839
1081
|
? origin(ref.current)
|
|
840
1082
|
: undefined;
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
)
|
|
1083
|
+
const position = at && { x: at.x + prefix.length + col, y: at.y + row };
|
|
1084
|
+
const [, recheck] = useReducer((n: number) => n + 1, 0);
|
|
1085
|
+
useEffect(() => {
|
|
1086
|
+
if (!at || !ref.current) return;
|
|
1087
|
+
const now = origin(ref.current);
|
|
1088
|
+
if (now.x !== at.x || now.y !== at.y) recheck();
|
|
1089
|
+
});
|
|
844
1090
|
|
|
845
1091
|
return (
|
|
846
1092
|
<Box ref={ref} flexDirection="column">
|
|
1093
|
+
{active ? <CursorAt position={position} /> : null}
|
|
847
1094
|
{state.value === "" && placeholder ? (
|
|
848
|
-
<Text>
|
|
1095
|
+
<Text wrap="truncate-end">
|
|
849
1096
|
<Text color="cyan">{prefix}</Text>
|
|
850
1097
|
<Text dimColor>{placeholder}</Text>
|
|
851
1098
|
</Text>
|
|
852
1099
|
) : (
|
|
853
1100
|
rows.map((r, i) => (
|
|
854
|
-
<Text key={i} wrap="truncate-end">
|
|
1101
|
+
<Text key={first + i} wrap="truncate-end">
|
|
855
1102
|
<Text color="cyan">
|
|
856
|
-
{i === 0
|
|
1103
|
+
{first + i === 0
|
|
1104
|
+
? prefix
|
|
1105
|
+
: i === 0
|
|
1106
|
+
? "↑".padEnd(prefix.length)
|
|
1107
|
+
: " ".repeat(prefix.length)}
|
|
857
1108
|
</Text>
|
|
858
1109
|
{r}
|
|
859
1110
|
</Text>
|
|
@@ -863,6 +1114,16 @@ function EditableText({
|
|
|
863
1114
|
);
|
|
864
1115
|
}
|
|
865
1116
|
|
|
1117
|
+
/**
|
|
1118
|
+
* 终端光标只有一个,每个 useCursor 每次提交都会设一遍。没有焦点的输入框(弹出输入框时下面的主输入框)
|
|
1119
|
+
* 不能用它,否则它设的"不显示"会盖掉有焦点的那个,所以只在有焦点时挂上
|
|
1120
|
+
*/
|
|
1121
|
+
function CursorAt({ position }: { position: { x: number; y: number } | undefined }) {
|
|
1122
|
+
const { setCursorPosition } = useCursor();
|
|
1123
|
+
setCursorPosition(position);
|
|
1124
|
+
return null;
|
|
1125
|
+
}
|
|
1126
|
+
|
|
866
1127
|
/** 元素相对整个动态输出(Static 之外的部分)左上角的位置 */
|
|
867
1128
|
function origin(node: DOMElement): { x: number; y: number } {
|
|
868
1129
|
let x = 0;
|
|
@@ -898,7 +1159,11 @@ function Prompt({
|
|
|
898
1159
|
const browsing = useRef(-1);
|
|
899
1160
|
const draft = useRef("");
|
|
900
1161
|
const lastCtrlC = useRef(0);
|
|
901
|
-
const { columns } =
|
|
1162
|
+
const { columns, rows } = useScreenSize();
|
|
1163
|
+
/** 补全菜单、搜索结果最多显示几项;输入框最多几行 */
|
|
1164
|
+
const menuRows = roomFor(rows, MENTION_ROWS);
|
|
1165
|
+
const inputRows = roomFor(rows, 10);
|
|
1166
|
+
const hintWidth = Math.max(10, columns - 3);
|
|
902
1167
|
// @ 引用补全用的路径;排好的结果按(路径表,查询)缓存,按键处理和渲染都要用时不重复排
|
|
903
1168
|
const [getPaths, setPaths] = useLive<string[]>([]);
|
|
904
1169
|
const ranked = useRef<{ paths: string[]; query: string; result: string[] }>(
|
|
@@ -983,10 +1248,9 @@ function Prompt({
|
|
|
983
1248
|
};
|
|
984
1249
|
}, [mentioning]);
|
|
985
1250
|
// 结果多时只显示选中项附近的几条
|
|
986
|
-
const first =
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
);
|
|
1251
|
+
const first = around(hit, results.length, menuRows);
|
|
1252
|
+
const firstMatch = around(Math.max(0, matches.indexOf(pick!)), matches.length, menuRows);
|
|
1253
|
+
const firstFound = around(Math.max(0, found.indexOf(file!)), found.length, menuRows);
|
|
990
1254
|
|
|
991
1255
|
const set = (s: EditState) => {
|
|
992
1256
|
setState(s);
|
|
@@ -1163,6 +1427,7 @@ function Prompt({
|
|
|
1163
1427
|
prefix="search history› "
|
|
1164
1428
|
placeholder="type to filter"
|
|
1165
1429
|
active={active}
|
|
1430
|
+
maxRows={inputRows}
|
|
1166
1431
|
/>
|
|
1167
1432
|
) : (
|
|
1168
1433
|
<EditableText
|
|
@@ -1172,16 +1437,19 @@ function Prompt({
|
|
|
1172
1437
|
placeholder={
|
|
1173
1438
|
busy
|
|
1174
1439
|
? "Type to queue a message · esc to interrupt"
|
|
1175
|
-
:
|
|
1440
|
+
: columns >= 70
|
|
1441
|
+
? "Message jade · / commands · @ files · ↑ or ctrl+r for history"
|
|
1442
|
+
: "Message jade · / commands · @ files"
|
|
1176
1443
|
}
|
|
1177
1444
|
active={active}
|
|
1445
|
+
maxRows={inputRows}
|
|
1178
1446
|
/>
|
|
1179
1447
|
)}
|
|
1180
1448
|
</Box>
|
|
1181
1449
|
{search ? (
|
|
1182
1450
|
<Box flexDirection="column" paddingX={2}>
|
|
1183
1451
|
{results.length ? (
|
|
1184
|
-
results.slice(first, first +
|
|
1452
|
+
results.slice(first, first + menuRows).map((h, i) => {
|
|
1185
1453
|
const on = first + i === hit;
|
|
1186
1454
|
return (
|
|
1187
1455
|
<Text
|
|
@@ -1198,14 +1466,17 @@ function Prompt({
|
|
|
1198
1466
|
) : (
|
|
1199
1467
|
<Text dimColor> no match</Text>
|
|
1200
1468
|
)}
|
|
1201
|
-
<
|
|
1202
|
-
{
|
|
1203
|
-
|
|
1204
|
-
|
|
1469
|
+
<Hint
|
|
1470
|
+
width={hintWidth}
|
|
1471
|
+
variants={[
|
|
1472
|
+
" ↑↓ or ctrl+r select · enter/tab put in the input · esc cancel",
|
|
1473
|
+
" ↑↓ select · enter use · esc cancel",
|
|
1474
|
+
]}
|
|
1475
|
+
/>
|
|
1205
1476
|
</Box>
|
|
1206
1477
|
) : matches.length ? (
|
|
1207
1478
|
<Box flexDirection="column" paddingX={2}>
|
|
1208
|
-
{matches.map((c) => {
|
|
1479
|
+
{matches.slice(firstMatch, firstMatch + menuRows).map((c) => {
|
|
1209
1480
|
const on = c === pick;
|
|
1210
1481
|
return (
|
|
1211
1482
|
<Text key={c.name} wrap="truncate-end">
|
|
@@ -1217,11 +1488,17 @@ function Prompt({
|
|
|
1217
1488
|
</Text>
|
|
1218
1489
|
);
|
|
1219
1490
|
})}
|
|
1220
|
-
<
|
|
1491
|
+
<Hint
|
|
1492
|
+
width={hintWidth}
|
|
1493
|
+
variants={[
|
|
1494
|
+
` ↑↓ select · enter run · tab complete${matches.length > menuRows ? ` · ${matches.length} commands` : ""}`,
|
|
1495
|
+
" ↑↓ · enter run · tab complete",
|
|
1496
|
+
]}
|
|
1497
|
+
/>
|
|
1221
1498
|
</Box>
|
|
1222
1499
|
) : found.length ? (
|
|
1223
1500
|
<Box flexDirection="column" paddingX={2}>
|
|
1224
|
-
{found.map((p) => {
|
|
1501
|
+
{found.slice(firstFound, firstFound + menuRows).map((p) => {
|
|
1225
1502
|
const on = p === file;
|
|
1226
1503
|
return (
|
|
1227
1504
|
<Text
|
|
@@ -1235,11 +1512,14 @@ function Prompt({
|
|
|
1235
1512
|
</Text>
|
|
1236
1513
|
);
|
|
1237
1514
|
})}
|
|
1238
|
-
<
|
|
1239
|
-
{
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1515
|
+
<Hint
|
|
1516
|
+
width={hintWidth}
|
|
1517
|
+
variants={[
|
|
1518
|
+
" ↑↓ select · enter/tab insert the path · the file's content is sent with the message",
|
|
1519
|
+
" ↑↓ select · enter/tab insert the path",
|
|
1520
|
+
" ↑↓ · enter insert",
|
|
1521
|
+
]}
|
|
1522
|
+
/>
|
|
1243
1523
|
</Box>
|
|
1244
1524
|
) : notice ? (
|
|
1245
1525
|
<Text color="yellow"> {notice}</Text>
|
|
@@ -1251,8 +1531,15 @@ function Prompt({
|
|
|
1251
1531
|
/** @ 补全最多显示几项 */
|
|
1252
1532
|
const MENTION_ROWS = 8;
|
|
1253
1533
|
|
|
1534
|
+
/** 长列表只显示 size 项时,为了让第 selected 项在中间附近,从第几项开始显示 */
|
|
1535
|
+
function around(selected: number, total: number, size: number): number {
|
|
1536
|
+
return Math.max(0, Math.min(selected - Math.floor(size / 2), total - size));
|
|
1537
|
+
}
|
|
1538
|
+
|
|
1254
1539
|
/** 选择框一次最多显示几项 */
|
|
1255
1540
|
const VISIBLE = 10;
|
|
1541
|
+
/** 选项多于这么多时可以打字筛选(这时 j/k 和数字键不再是快捷键) */
|
|
1542
|
+
const FILTER_FROM = 10;
|
|
1256
1543
|
|
|
1257
1544
|
function SelectDialog({
|
|
1258
1545
|
dialog,
|
|
@@ -1260,12 +1547,25 @@ function SelectDialog({
|
|
|
1260
1547
|
dialog: Extract<Dialog, { kind: "select" }>;
|
|
1261
1548
|
}) {
|
|
1262
1549
|
const [getSelected, setSelected] = useLive(0);
|
|
1263
|
-
const
|
|
1264
|
-
const
|
|
1265
|
-
const
|
|
1550
|
+
const [getQuery, setQuery] = useLive("");
|
|
1551
|
+
const { rows, columns } = useScreenSize();
|
|
1552
|
+
const filtering = dialog.choices.length > FILTER_FROM;
|
|
1553
|
+
const query = getQuery();
|
|
1554
|
+
/** 筛选后的选项和它们原来的序号 */
|
|
1555
|
+
const filtered = () => {
|
|
1556
|
+
const q = getQuery().toLowerCase();
|
|
1557
|
+
return dialog.choices
|
|
1558
|
+
.map((c, i) => ({ c, i }))
|
|
1559
|
+
.filter(({ c }) => !q || `${c.label} ${c.hint ?? ""}`.toLowerCase().includes(q));
|
|
1560
|
+
};
|
|
1561
|
+
const items = filtered();
|
|
1562
|
+
const n = items.length;
|
|
1563
|
+
const selected = Math.min(getSelected(), Math.max(0, n - 1));
|
|
1564
|
+
// 终端矮时少显示几项,加上预览一起不超过终端的高度
|
|
1565
|
+
const visible = roomFor(rows, VISIBLE);
|
|
1266
1566
|
// 预览(例如要写入的文件)过长时截断:整个界面高过终端时 Ink 会每帧清屏重画
|
|
1267
1567
|
const lines = dialog.body?.split("\n") ?? [];
|
|
1268
|
-
const room = Math.max(3, rows - Math.min(n,
|
|
1568
|
+
const room = Math.max(3, rows - Math.min(n, visible) - 16);
|
|
1269
1569
|
const body =
|
|
1270
1570
|
lines.length > room
|
|
1271
1571
|
? [
|
|
@@ -1274,24 +1574,44 @@ function SelectDialog({
|
|
|
1274
1574
|
].join("\n")
|
|
1275
1575
|
: dialog.body;
|
|
1276
1576
|
useInput((input, key) => {
|
|
1277
|
-
const
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
if (key.
|
|
1281
|
-
|
|
1577
|
+
const items = filtered();
|
|
1578
|
+
const n = items.length;
|
|
1579
|
+
const selected = Math.min(getSelected(), Math.max(0, n - 1));
|
|
1580
|
+
if (key.upArrow || (!filtering && input === "k"))
|
|
1581
|
+
return n && setSelected((selected - 1 + n) % n);
|
|
1582
|
+
if (key.downArrow || (!filtering && input === "j"))
|
|
1583
|
+
return n && setSelected((selected + 1) % n);
|
|
1584
|
+
if (key.return) {
|
|
1585
|
+
if (n) dialog.resolve(items[selected]!.c.value);
|
|
1586
|
+
return;
|
|
1587
|
+
}
|
|
1588
|
+
if (key.escape && filtering && getQuery()) {
|
|
1589
|
+
setQuery("");
|
|
1590
|
+
return setSelected(0);
|
|
1591
|
+
}
|
|
1282
1592
|
if (key.escape || (key.ctrl && input === "c"))
|
|
1283
1593
|
return dialog.resolve(undefined);
|
|
1284
|
-
if (key.pageDown) return setSelected(Math.min(n - 1, selected +
|
|
1285
|
-
if (key.pageUp) return setSelected(Math.max(0, selected -
|
|
1594
|
+
if (key.pageDown) return setSelected(Math.min(n - 1, selected + visible));
|
|
1595
|
+
if (key.pageUp) return setSelected(Math.max(0, selected - visible));
|
|
1596
|
+
if (filtering) {
|
|
1597
|
+
if (key.backspace || key.delete) {
|
|
1598
|
+
setQuery([...getQuery()].slice(0, -1).join(""));
|
|
1599
|
+
return setSelected(0);
|
|
1600
|
+
}
|
|
1601
|
+
if (key.ctrl || key.meta || key.tab || MOUSE.test(input)) return;
|
|
1602
|
+
const text = input.replace(/[\x00-\x1f\x7f]/g, "");
|
|
1603
|
+
if (text) {
|
|
1604
|
+
setQuery(getQuery() + text);
|
|
1605
|
+
setSelected(0);
|
|
1606
|
+
}
|
|
1607
|
+
return;
|
|
1608
|
+
}
|
|
1286
1609
|
const digit = Number(input);
|
|
1287
1610
|
if (Number.isInteger(digit) && digit >= 1 && digit <= Math.min(n, 9))
|
|
1288
|
-
dialog.resolve(
|
|
1611
|
+
dialog.resolve(items[digit - 1]!.c.value);
|
|
1289
1612
|
});
|
|
1290
1613
|
// 选项多时只显示选中项附近的一段
|
|
1291
|
-
const start =
|
|
1292
|
-
0,
|
|
1293
|
-
Math.min(selected - Math.floor(VISIBLE / 2), n - VISIBLE),
|
|
1294
|
-
);
|
|
1614
|
+
const start = around(selected, n, visible);
|
|
1295
1615
|
return (
|
|
1296
1616
|
<Box
|
|
1297
1617
|
flexDirection="column"
|
|
@@ -1300,30 +1620,48 @@ function SelectDialog({
|
|
|
1300
1620
|
paddingX={1}
|
|
1301
1621
|
marginTop={1}
|
|
1302
1622
|
>
|
|
1303
|
-
<Text bold>
|
|
1623
|
+
<Text bold wrap="truncate-end">
|
|
1624
|
+
{dialog.title}
|
|
1625
|
+
</Text>
|
|
1304
1626
|
{body ? <Text>{body}</Text> : null}
|
|
1305
|
-
|
|
1627
|
+
{filtering ? (
|
|
1628
|
+
<Text wrap="truncate-end">
|
|
1629
|
+
<Text color="cyan">filter› </Text>
|
|
1630
|
+
{query ? (
|
|
1631
|
+
<Text>{query}</Text>
|
|
1632
|
+
) : (
|
|
1633
|
+
<Text dimColor>type to filter {dialog.choices.length} items</Text>
|
|
1634
|
+
)}
|
|
1635
|
+
</Text>
|
|
1636
|
+
) : null}
|
|
1637
|
+
<Box flexDirection="column" marginTop={filtering ? 0 : 1}>
|
|
1306
1638
|
{start > 0 ? <Text dimColor> ↑ {start} more</Text> : null}
|
|
1307
|
-
{
|
|
1308
|
-
|
|
1639
|
+
{n === 0 ? <Text dimColor> no match</Text> : null}
|
|
1640
|
+
{items.slice(start, start + visible).map(({ c, i }, j) => {
|
|
1641
|
+
const on = start + j === selected;
|
|
1309
1642
|
return (
|
|
1310
1643
|
<Text key={i} wrap="truncate-end">
|
|
1311
|
-
<Text
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
{i === selected ? "❯ " : " "}
|
|
1316
|
-
{i + 1}. {c.label}
|
|
1644
|
+
<Text color={on ? "cyan" : undefined} bold={on}>
|
|
1645
|
+
{on ? "❯ " : " "}
|
|
1646
|
+
{filtering ? "" : `${i + 1}. `}
|
|
1647
|
+
{c.label}
|
|
1317
1648
|
</Text>
|
|
1318
1649
|
{c.hint ? <Text dimColor> {c.hint}</Text> : null}
|
|
1319
1650
|
</Text>
|
|
1320
1651
|
);
|
|
1321
1652
|
})}
|
|
1322
|
-
{start +
|
|
1323
|
-
<Text dimColor> ↓ {n - start -
|
|
1653
|
+
{start + visible < n ? (
|
|
1654
|
+
<Text dimColor> ↓ {n - start - visible} more</Text>
|
|
1324
1655
|
) : null}
|
|
1325
1656
|
</Box>
|
|
1326
|
-
<
|
|
1657
|
+
<Hint
|
|
1658
|
+
width={Math.max(10, columns - 4)}
|
|
1659
|
+
variants={
|
|
1660
|
+
filtering
|
|
1661
|
+
? ["↑↓ select · type to filter · enter confirm · esc clear/cancel", "↑↓ · enter · esc"]
|
|
1662
|
+
: ["↑↓ select · enter confirm · esc cancel", "↑↓ · enter · esc"]
|
|
1663
|
+
}
|
|
1664
|
+
/>
|
|
1327
1665
|
</Box>
|
|
1328
1666
|
);
|
|
1329
1667
|
}
|
|
@@ -1339,7 +1677,7 @@ function InputDialog({
|
|
|
1339
1677
|
cursor: initial.length,
|
|
1340
1678
|
});
|
|
1341
1679
|
const state = current();
|
|
1342
|
-
const { columns } =
|
|
1680
|
+
const { columns } = useScreenSize();
|
|
1343
1681
|
usePaste((text) =>
|
|
1344
1682
|
setState(edit.insert(current(), text.replace(/\r?\n/g, ""))),
|
|
1345
1683
|
);
|