@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/dist/ui/tui.js
CHANGED
|
@@ -5,14 +5,27 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
5
5
|
import { writeSync } from "node:fs";
|
|
6
6
|
import { Box, render, Text, useBoxMetrics, useCursor, useInput, usePaste, useWindowSize, } from "ink";
|
|
7
7
|
import { useEffect, useReducer, useRef, useState, useSyncExternalStore, } from "react";
|
|
8
|
+
import stringWidth from "string-width";
|
|
8
9
|
import wrapAnsi from "wrap-ansi";
|
|
9
10
|
import { mentionAt, mentionText, rankPaths } from "../mentions.js";
|
|
10
11
|
import { edit, empty, layout } from "./editor.js";
|
|
12
|
+
import { markdownStream } from "./markdown.js";
|
|
11
13
|
import { dim, } from "./screen.js";
|
|
14
|
+
/**
|
|
15
|
+
* 界面可用的尺寸:宽度比终端少一列。
|
|
16
|
+
* Ink 增量重画时每行写成「内容 + ESC[K」;一行正好占满终端宽度时光标停在最后一列(等待换行),
|
|
17
|
+
* 紧跟的 ESC[K 会把最后一个字符擦掉,输入框之类的右边框就缺了一块。留出最后一列就不会这样
|
|
18
|
+
*/
|
|
19
|
+
function useScreenSize() {
|
|
20
|
+
const { columns, rows } = useWindowSize();
|
|
21
|
+
return { columns: Math.max(1, columns - 1), rows };
|
|
22
|
+
}
|
|
12
23
|
/** 对话最多保留这么多行(折行前),超出时丢掉最早的一段 */
|
|
13
24
|
const MAX_LINES = 10_000;
|
|
14
25
|
/** 按显示宽度折行,保留颜色;每一项是屏幕上的一行 */
|
|
15
26
|
const wrap = (text, width) => wrapAnsi(text, width, { hard: true, trim: false }).split("\n");
|
|
27
|
+
/** Markdown 排好的一行折成屏幕上的行 */
|
|
28
|
+
const mdRows = (l, width) => typeof l === "string" ? wrap(l, width) : l.render(width);
|
|
16
29
|
/** 按宽度截成一行,截掉了内容时以 … 结尾;留出两列给 … 和展开标记 */
|
|
17
30
|
function oneRow(text, width) {
|
|
18
31
|
const rows = wrap(text, Math.max(1, width - 3));
|
|
@@ -21,11 +34,61 @@ function oneRow(text, width) {
|
|
|
21
34
|
}
|
|
22
35
|
/** 这一项能不能展开:有折叠的内容,或者收起时被截掉了一部分 */
|
|
23
36
|
function foldable(l, width) {
|
|
37
|
+
if (l.think)
|
|
38
|
+
return l.think.text.trim() !== "";
|
|
24
39
|
return l.details !== undefined || (Boolean(l.oneRow) && oneRow(l.text, width).cut);
|
|
25
40
|
}
|
|
26
|
-
|
|
27
|
-
|
|
41
|
+
const elapsed = (t) => `${Math.max(0, Math.round(((t.end ?? Date.now()) - t.start) / 1000))}s`;
|
|
42
|
+
/** 按显示宽度取 text 末尾最多 cols 列;截掉了开头时以 … 开头 */
|
|
43
|
+
export function tail(text, cols) {
|
|
44
|
+
if (stringWidth(text) <= cols)
|
|
45
|
+
return text;
|
|
46
|
+
const chars = [...text];
|
|
47
|
+
let out = "";
|
|
48
|
+
let w = 1;
|
|
49
|
+
for (let i = chars.length - 1; i >= 0; i--) {
|
|
50
|
+
const cw = stringWidth(chars[i]);
|
|
51
|
+
if (w + cw > cols)
|
|
52
|
+
break;
|
|
53
|
+
out = chars[i] + out;
|
|
54
|
+
w += cw;
|
|
55
|
+
}
|
|
56
|
+
return `…${out}`;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* 思考收起时的一行(不含展开标记):思考中显示"✻ Thinking 12s · "加上最新一行的末尾,跟着模型实时往前走;
|
|
60
|
+
* 结束后显示"✻ Thought for 12s"
|
|
61
|
+
*/
|
|
62
|
+
function thinkingRow(t, width) {
|
|
63
|
+
if (t.end !== undefined)
|
|
64
|
+
return dim(`✻ Thought for ${elapsed(t)}`);
|
|
65
|
+
const label = `✻ Thinking ${elapsed(t)}`;
|
|
66
|
+
const latest = t.text
|
|
67
|
+
.split("\n")
|
|
68
|
+
.findLast((l) => l.trim())
|
|
69
|
+
?.trim()
|
|
70
|
+
.replace(/\s+/g, " ");
|
|
71
|
+
// 留出 " · "、" ▸" 和一列余量
|
|
72
|
+
const room = width - stringWidth(label) - 6;
|
|
73
|
+
return dim(latest && room >= 8 ? `${label} · ${tail(latest, room)}` : `${label}…`);
|
|
74
|
+
}
|
|
28
75
|
function wrapLine(l, width) {
|
|
76
|
+
if (l.render)
|
|
77
|
+
return l.render(width);
|
|
78
|
+
if (l.think) {
|
|
79
|
+
if (!foldable(l, width))
|
|
80
|
+
return [thinkingRow(l.think, width)];
|
|
81
|
+
if (!l.open)
|
|
82
|
+
return [`${thinkingRow(l.think, width)}${dim(" ▸")}`];
|
|
83
|
+
const t = l.think;
|
|
84
|
+
const title = dim(t.end === undefined ? `✻ Thinking ${elapsed(t)}` : `✻ Thought for ${elapsed(t)}`);
|
|
85
|
+
const body = t.text
|
|
86
|
+
.replace(/\s+$/, "")
|
|
87
|
+
.split("\n")
|
|
88
|
+
.map((r) => (r ? dim(` ${r}`) : r))
|
|
89
|
+
.join("\n");
|
|
90
|
+
return [...wrap(`${title}${dim(" ▾")}`, width), ...wrap(body, width)];
|
|
91
|
+
}
|
|
29
92
|
if (!foldable(l, width))
|
|
30
93
|
return wrap(l.text, width);
|
|
31
94
|
if (!l.open) {
|
|
@@ -180,42 +243,62 @@ class Store {
|
|
|
180
243
|
/** 开关鼠标事件上报(SGR 格式),用来接收滚轮和点击;开着时终端里选中文字要按住 Shift */
|
|
181
244
|
const MOUSE_ON = "\x1b[?1000h\x1b[?1006h";
|
|
182
245
|
const MOUSE_OFF = "\x1b[?1006l\x1b[?1000l";
|
|
246
|
+
/**
|
|
247
|
+
* 光标样式:闪烁的竖线(DECSCUSR 5,再加上 ATT610 的"开始闪烁"给只认它的终端),一眼能看出输入框有焦点;
|
|
248
|
+
* 退出时还原成终端自己的默认样式。光标什么时候显示仍由 Ink 管(只有输入框或输入对话框有焦点时才显示)
|
|
249
|
+
*/
|
|
250
|
+
const CURSOR_BLINK = "\x1b[5 q\x1b[?12h";
|
|
251
|
+
const CURSOR_RESET = "\x1b[0 q";
|
|
183
252
|
export function tuiScreen(opts) {
|
|
184
253
|
const stdout = opts.io?.stdout ?? process.stdout;
|
|
185
254
|
const store = new Store(opts.status);
|
|
186
255
|
/**
|
|
187
|
-
*
|
|
188
|
-
*
|
|
256
|
+
* 正在进行的思考:一开始就作为一项放进对话(可以点击展开),之后的增量攒起来,每隔一小段时间改写一次那一项。
|
|
257
|
+
* 收起时只占一行,显示模型最新想到了哪里;结束时换成用了多久
|
|
189
258
|
*/
|
|
259
|
+
let thinking;
|
|
260
|
+
let thinkingCount = 0;
|
|
261
|
+
const redrawThinking = () => {
|
|
262
|
+
if (!thinking)
|
|
263
|
+
return;
|
|
264
|
+
clearTimeout(thinking.timer);
|
|
265
|
+
thinking.timer = undefined;
|
|
266
|
+
store.amend(thinking.id, () => { });
|
|
267
|
+
};
|
|
268
|
+
const endThinking = () => {
|
|
269
|
+
if (!thinking)
|
|
270
|
+
return;
|
|
271
|
+
thinking.think.end = Date.now();
|
|
272
|
+
redrawThinking();
|
|
273
|
+
thinking = undefined;
|
|
274
|
+
};
|
|
275
|
+
/** 正文按 Markdown 排版;一段正文结束时换一个新的,清掉代码块、表格的状态 */
|
|
276
|
+
let md = markdownStream();
|
|
277
|
+
const appendMd = (lines) => {
|
|
278
|
+
for (const l of lines) {
|
|
279
|
+
if (typeof l === "string")
|
|
280
|
+
store.append([l]);
|
|
281
|
+
else
|
|
282
|
+
store.append([""], { render: l.render });
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
/** 推进正文:已经完整的行排版后移进对话,只把最后不完整的一行留在底部;all 时全部移进去,连同思考一起结束 */
|
|
190
286
|
const flushLive = (all = false) => {
|
|
287
|
+
if (all)
|
|
288
|
+
endThinking();
|
|
191
289
|
const live = store.state.live;
|
|
192
290
|
if (!live)
|
|
193
291
|
return;
|
|
194
|
-
if (live.kind === "thinking") {
|
|
195
|
-
if (!all)
|
|
196
|
-
return;
|
|
197
|
-
store.update({ live: undefined });
|
|
198
|
-
const text = live.text.replace(/\s+$/, "");
|
|
199
|
-
if (!text)
|
|
200
|
-
return;
|
|
201
|
-
store.append([thinkingHead(text)], {
|
|
202
|
-
oneRow: true,
|
|
203
|
-
title: dim("✻ thinking"),
|
|
204
|
-
details: text
|
|
205
|
-
.split("\n")
|
|
206
|
-
.map((l) => (l ? dim(` ${l}`) : l))
|
|
207
|
-
.join("\n"),
|
|
208
|
-
});
|
|
209
|
-
return;
|
|
210
|
-
}
|
|
211
292
|
const lines = live.text.split("\n");
|
|
212
293
|
const rest = all ? "" : lines.pop();
|
|
213
294
|
if (all && lines.at(-1) === "")
|
|
214
295
|
lines.pop();
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
296
|
+
appendMd(lines.flatMap((l) => md.push(l)));
|
|
297
|
+
if (all) {
|
|
298
|
+
appendMd(md.flush());
|
|
299
|
+
md = markdownStream();
|
|
300
|
+
}
|
|
301
|
+
store.update({ live: rest || !all ? { text: rest, pending: md.pending() } : undefined });
|
|
219
302
|
};
|
|
220
303
|
const ask = (dialog) => new Promise((resolve) => {
|
|
221
304
|
const d = {
|
|
@@ -239,11 +322,11 @@ export function tuiScreen(opts) {
|
|
|
239
322
|
? { ...opts.io, interactive: true, patchConsole: false }
|
|
240
323
|
: {}),
|
|
241
324
|
});
|
|
242
|
-
stdout.write(MOUSE_ON);
|
|
325
|
+
stdout.write(MOUSE_ON + CURSOR_BLINK);
|
|
243
326
|
// 没走 close 就退出(process.exit)时也要把终端还原,否则会留在备用屏幕、鼠标上报也开着
|
|
244
327
|
const restore = () => {
|
|
245
328
|
try {
|
|
246
|
-
writeSync(1, `${MOUSE_OFF}\x1b[?1049l\x1b[?25h`);
|
|
329
|
+
writeSync(1, `${MOUSE_OFF}${CURSOR_RESET}\x1b[?1049l\x1b[?25h`);
|
|
247
330
|
}
|
|
248
331
|
catch { }
|
|
249
332
|
};
|
|
@@ -276,20 +359,29 @@ export function tuiScreen(opts) {
|
|
|
276
359
|
});
|
|
277
360
|
},
|
|
278
361
|
stream(kind, text) {
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
362
|
+
if (kind === "thinking") {
|
|
363
|
+
if (store.state.live) {
|
|
364
|
+
flushLive(true);
|
|
365
|
+
store.update({ live: undefined });
|
|
366
|
+
}
|
|
367
|
+
if (!thinking) {
|
|
368
|
+
if (!text.trim())
|
|
369
|
+
return;
|
|
370
|
+
const think = { text: "", start: Date.now() };
|
|
371
|
+
thinking = { id: `\0thinking-${thinkingCount++}`, think };
|
|
372
|
+
store.append([""], { id: thinking.id, think });
|
|
373
|
+
}
|
|
374
|
+
thinking.think.text += text;
|
|
375
|
+
thinking.timer ??= setTimeout(redrawThinking, 50);
|
|
376
|
+
return;
|
|
283
377
|
}
|
|
378
|
+
endThinking();
|
|
284
379
|
if (!store.state.live) {
|
|
285
|
-
|
|
286
|
-
text = text.replace(/^\n+/, "");
|
|
380
|
+
text = text.replace(/^\n+/, "");
|
|
287
381
|
if (!text)
|
|
288
382
|
return;
|
|
289
383
|
}
|
|
290
|
-
store.update({
|
|
291
|
-
live: { kind, text: (store.state.live?.text ?? "") + text },
|
|
292
|
-
});
|
|
384
|
+
store.update({ live: { text: (store.state.live?.text ?? "") + text } });
|
|
293
385
|
if (text.includes("\n"))
|
|
294
386
|
flushLive();
|
|
295
387
|
},
|
|
@@ -324,6 +416,9 @@ export function tuiScreen(opts) {
|
|
|
324
416
|
resolve: () => { },
|
|
325
417
|
}),
|
|
326
418
|
clear() {
|
|
419
|
+
clearTimeout(thinking?.timer);
|
|
420
|
+
thinking = undefined;
|
|
421
|
+
md = markdownStream();
|
|
327
422
|
store.clear();
|
|
328
423
|
},
|
|
329
424
|
async close() {
|
|
@@ -331,7 +426,7 @@ export function tuiScreen(opts) {
|
|
|
331
426
|
store.update({ live: undefined, busy: undefined, progress: [] }, true);
|
|
332
427
|
// 等最后一帧画完再卸载;卸载时 Ink 退出备用屏幕
|
|
333
428
|
await new Promise((r) => setTimeout(r, 50));
|
|
334
|
-
stdout.write(MOUSE_OFF);
|
|
429
|
+
stdout.write(MOUSE_OFF + CURSOR_RESET);
|
|
335
430
|
instance.unmount();
|
|
336
431
|
await instance.waitUntilExit();
|
|
337
432
|
process.off("exit", restore);
|
|
@@ -345,10 +440,27 @@ export function tuiScreen(opts) {
|
|
|
345
440
|
*/
|
|
346
441
|
const MOUSE = /^\[<(\d+);\d+;(\d+)([Mm])$/;
|
|
347
442
|
const WHEEL_LINES = 3;
|
|
443
|
+
/** 终端矮于这么多行时标题只占一行 */
|
|
444
|
+
const COMPACT_ROWS = 24;
|
|
445
|
+
/**
|
|
446
|
+
* 底部的菜单、选择框、多行输入最多占多少行:终端很矮时整个界面会高过终端,
|
|
447
|
+
* 超出的部分被裁掉,输入框的边框和状态栏就显示不全了
|
|
448
|
+
*/
|
|
449
|
+
const roomFor = (rows, max) => Math.max(3, Math.min(max, Math.floor((rows - 10) / 2)));
|
|
348
450
|
function App({ store, version, commands, handlers, history, }) {
|
|
349
451
|
const state = useSyncExternalStore(store.subscribe, store.get);
|
|
350
452
|
const dialog = state.dialogs[0];
|
|
351
|
-
|
|
453
|
+
// 运行中每 100ms 整体重画一次,推动转圈动画。不能让 Spinner 自己重画:
|
|
454
|
+
// Ink 只在输入框也跟着提交的那一帧保留光标,否则把光标藏起来,运行中光标就不见了
|
|
455
|
+
const [, tick] = useReducer((n) => n + 1, 0);
|
|
456
|
+
const busy = state.busy !== undefined;
|
|
457
|
+
useEffect(() => {
|
|
458
|
+
if (!busy)
|
|
459
|
+
return;
|
|
460
|
+
const t = setInterval(tick, 100);
|
|
461
|
+
return () => clearInterval(t);
|
|
462
|
+
}, [busy]);
|
|
463
|
+
const { columns, rows } = useScreenSize();
|
|
352
464
|
// 对话区占掉标题和底部之外的全部高度,只画能看见的那几行
|
|
353
465
|
const ref = useRef(null);
|
|
354
466
|
const { height, top } = useBoxMetrics(ref);
|
|
@@ -356,11 +468,8 @@ function App({ store, version, commands, handlers, history, }) {
|
|
|
356
468
|
const done = store.lines(width);
|
|
357
469
|
// 还没结束的流式块和正在运行的工具的进度:思考只显示最新的一行,进度每个工具一行
|
|
358
470
|
const live = [
|
|
359
|
-
...(state.live?.
|
|
360
|
-
|
|
361
|
-
? [oneRow(thinkingHead(state.live.text), width).row]
|
|
362
|
-
: wrap(state.live.text, width)
|
|
363
|
-
: []),
|
|
471
|
+
...(state.live?.pending ?? []).flatMap((l) => mdRows(l, width)),
|
|
472
|
+
...(state.live?.text ? wrap(state.live.text, width) : []),
|
|
364
473
|
...state.progress.map((p) => oneRow(p.line, width).row),
|
|
365
474
|
];
|
|
366
475
|
const total = done.length + live.length;
|
|
@@ -424,7 +533,7 @@ function App({ store, version, commands, handlers, history, }) {
|
|
|
424
533
|
// 总高度要比终端少一行,否则 Ink 认为输出占满了屏幕,每帧清屏重画。
|
|
425
534
|
// 重画那一帧上下各垫一行空白:终端把顶上那行滚出去后,看到的和平时逐行一样,所以不会闪。
|
|
426
535
|
// 只做纵向裁剪:每行都已按宽度折好或截断,横向裁剪会让 Ink 每帧对每一行再切一遍 ANSI 字符串
|
|
427
|
-
return (_jsx(Box, { flexDirection: "column", width: columns, height: repaint ? rows + 1 : Math.max(1, rows - 1), paddingY: repaint ? 1 : 0, overflowY: "hidden", children: _jsxs(Box, { flexDirection: "column", flexGrow: 1, overflowY: "hidden", children: [_jsx(Header, { status: state.status, version: version }), _jsxs(Box, { ref: ref, flexDirection: "column", flexGrow: 1, flexShrink: 1, flexBasis: 0, overflowY: "hidden", paddingX: 1, children: [shown.map((l, i) => (_jsx(Text, { wrap: "truncate-end", children: l || " " }, i))), scrolled ? (_jsx(Text, { dimColor: true, wrap: "truncate-end", children: `── ↓ ${scroll} more lines · wheel / PgDn to scroll · ctrl+end to jump to the bottom ──` })) : null] }), _jsxs(Box, { flexDirection: "column", flexShrink: 0, children: [state.busy && !dialog ? (_jsx(Spinner, { label: state.busy.label, since: state.busy.since })) : null, state.queued.map((q, i) => (_jsxs(Text, { dimColor: true, wrap: "truncate-end", children: [" ↳ queued: ", q.replace(/\s+/g, " ")] }, i))), dialog ? (dialog.kind === "select" ? (_jsx(SelectDialog, { dialog: dialog }, dialogKey(dialog))) : (_jsx(InputDialog, { dialog: dialog }, dialogKey(dialog)))) : null, _jsx(Prompt, { commands: commands, handlers: handlers, history: history, busy: Boolean(state.busy), active: !dialog, onSubmit: () => store.scrollBy(-max, max) }), _jsx(StatusBar, { status: state.status })] })] }) }));
|
|
536
|
+
return (_jsx(Box, { flexDirection: "column", width: columns, height: repaint ? rows + 1 : Math.max(1, rows - 1), paddingY: repaint ? 1 : 0, overflowY: "hidden", children: _jsxs(Box, { flexDirection: "column", flexGrow: 1, overflowY: "hidden", children: [_jsx(Header, { status: state.status, version: version, compact: rows < COMPACT_ROWS }), _jsxs(Box, { ref: ref, flexDirection: "column", flexGrow: 1, flexShrink: 1, flexBasis: 0, overflowY: "hidden", paddingX: 1, children: [shown.map((l, i) => (_jsx(Text, { wrap: "truncate-end", children: l || " " }, i))), scrolled ? (_jsx(Text, { dimColor: true, wrap: "truncate-end", children: `── ↓ ${scroll} more lines · wheel / PgDn to scroll · ctrl+end to jump to the bottom ──` })) : null] }), _jsxs(Box, { flexDirection: "column", flexShrink: 0, children: [state.busy && !dialog ? (_jsx(Spinner, { label: state.busy.label, since: state.busy.since })) : null, state.queued.map((q, i) => (_jsxs(Text, { dimColor: true, wrap: "truncate-end", children: [" ↳ queued: ", q.replace(/\s+/g, " ")] }, i))), dialog ? (dialog.kind === "select" ? (_jsx(SelectDialog, { dialog: dialog }, dialogKey(dialog))) : (_jsx(InputDialog, { dialog: dialog }, dialogKey(dialog)))) : null, _jsx(Prompt, { commands: commands, handlers: handlers, history: history, busy: Boolean(state.busy), active: !dialog, onSubmit: () => store.scrollBy(-max, max) }), _jsx(StatusBar, { status: state.status, width: Math.max(10, columns - 2) })] })] }) }));
|
|
428
537
|
}
|
|
429
538
|
const JADE = "#00a86b";
|
|
430
539
|
const SHINE = "#b6f2d4";
|
|
@@ -435,8 +544,11 @@ const SHINE = "#b6f2d4";
|
|
|
435
544
|
function Bot() {
|
|
436
545
|
return (_jsxs(Box, { flexDirection: "column", flexShrink: 0, marginRight: 2, children: [_jsxs(Text, { color: JADE, children: [" ▄", _jsx(Text, { color: SHINE, backgroundColor: JADE, children: "\u2584" }), "████▄"] }), _jsx(Text, { color: JADE, children: "\u2588\u2588 \u2588\u2588\u2588 \u2588\u2588" }), _jsx(Text, { color: JADE, children: "\u2580\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2580" })] }));
|
|
437
546
|
}
|
|
438
|
-
function Header({ status, version }) {
|
|
439
|
-
|
|
547
|
+
function Header({ status, version, compact, }) {
|
|
548
|
+
const { columns } = useScreenSize();
|
|
549
|
+
if (compact)
|
|
550
|
+
return (_jsx(Box, { flexShrink: 0, paddingX: 1, children: _jsxs(Text, { wrap: "truncate-end", children: [_jsx(Text, { color: "cyan", bold: true, children: "jade" }), _jsx(Text, { dimColor: true, children: " \u00B7 " }), _jsx(Text, { color: "cyan", children: status.model }), _jsx(Text, { dimColor: true, children: " \u00B7 " }), _jsx(Text, { children: status.cwd }), status.branch ? (_jsxs(Text, { color: "magenta", children: [" (", status.branch, ")"] })) : null] }) }));
|
|
551
|
+
return (_jsxs(Box, { flexShrink: 0, borderStyle: "round", borderColor: "cyan", paddingX: 1, children: [columns >= 50 ? _jsx(Bot, {}) : null, _jsxs(Box, { flexDirection: "column", flexGrow: 1, flexShrink: 1, children: [_jsxs(Text, { wrap: "truncate-end", children: [_jsx(Text, { color: "cyan", bold: true, children: "jade" }), _jsxs(Text, { dimColor: true, children: [" v", version] }), status.session ? (_jsx(Text, { dimColor: true, children: ` · session ${status.session}` })) : null] }), _jsxs(Text, { wrap: "truncate-end", children: [_jsx(Text, { color: "cyan", children: status.model }), _jsx(Text, { dimColor: true, children: " \u00B7 " }), _jsx(Text, { children: status.cwd }), status.branch ? (_jsxs(Text, { color: "magenta", children: [" (", status.branch, ")"] })) : null] }), _jsx(Text, { dimColor: true, wrap: "truncate-end", children: "/help for commands \u00B7 @ to attach files" })] })] }));
|
|
440
552
|
}
|
|
441
553
|
const dialogIds = new WeakMap();
|
|
442
554
|
let dialogCounter = 0;
|
|
@@ -446,30 +558,102 @@ function dialogKey(d) {
|
|
|
446
558
|
return dialogIds.get(d);
|
|
447
559
|
}
|
|
448
560
|
const FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
561
|
+
/** 转圈动画由 App 每 100ms 整体重画一次推动(见 App 里的 tick),这里只按当前时间画 */
|
|
449
562
|
function Spinner({ label, since }) {
|
|
450
|
-
const
|
|
451
|
-
useEffect(() => {
|
|
452
|
-
const t = setInterval(() => setNow(Date.now()), 100);
|
|
453
|
-
return () => clearInterval(t);
|
|
454
|
-
}, []);
|
|
563
|
+
const now = Date.now();
|
|
455
564
|
const seconds = Math.floor((now - since) / 1000);
|
|
456
|
-
return (
|
|
565
|
+
return (_jsx(Box, { marginTop: 1, children: _jsxs(Text, { wrap: "truncate-end", children: [_jsxs(Text, { color: "cyan", children: [FRAMES[Math.floor(now / 100) % FRAMES.length], " "] }), _jsxs(Text, { children: [label, "\u2026 "] }), _jsxs(Text, { dimColor: true, children: ["(", seconds, "s \u00B7 esc to interrupt)"] })] }) }));
|
|
457
566
|
}
|
|
458
|
-
|
|
567
|
+
/** 在 width 列里放下尽量多的内容(保持原来的顺序) */
|
|
568
|
+
export function fitSegments(segments, width) {
|
|
569
|
+
const kept = segments.map((s) => ({ ...s }));
|
|
570
|
+
const total = () => kept.reduce((n, s, i) => n + stringWidth(s.text) + (i ? 3 : 0), 0);
|
|
571
|
+
const steps = kept
|
|
572
|
+
.flatMap((s) => [
|
|
573
|
+
...(s.short !== undefined && s.shortAt !== undefined
|
|
574
|
+
? [{ s, at: s.shortAt, drop: false }]
|
|
575
|
+
: []),
|
|
576
|
+
...(s.dropAt !== undefined ? [{ s, at: s.dropAt, drop: true }] : []),
|
|
577
|
+
])
|
|
578
|
+
.sort((a, b) => b.at - a.at);
|
|
579
|
+
for (const { s, drop } of steps) {
|
|
580
|
+
if (total() <= width)
|
|
581
|
+
break;
|
|
582
|
+
if (drop)
|
|
583
|
+
kept.splice(kept.indexOf(s), 1);
|
|
584
|
+
else
|
|
585
|
+
s.text = s.short;
|
|
586
|
+
}
|
|
587
|
+
return kept;
|
|
588
|
+
}
|
|
589
|
+
function StatusBar({ status, width }) {
|
|
459
590
|
const ctx = status.context;
|
|
460
591
|
const pct = ctx ? Math.round((ctx.used / ctx.limit) * 100) : 0;
|
|
461
592
|
/** 离自动压缩还有多远:到阈值的 85% 起变黄 */
|
|
462
593
|
const nearCompact = ctx?.compactAt ? ctx.used >= ctx.compactAt * 0.85 : pct >= 80;
|
|
463
594
|
const modeColor = status.mode === "yolo"
|
|
464
595
|
? "red"
|
|
465
|
-
: status.mode === "auto
|
|
596
|
+
: status.mode === "auto"
|
|
466
597
|
? "yellow"
|
|
467
|
-
: status.mode === "
|
|
468
|
-
? "
|
|
469
|
-
:
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
598
|
+
: status.mode === "plan"
|
|
599
|
+
? "magenta"
|
|
600
|
+
: "green";
|
|
601
|
+
const speed = status.speed;
|
|
602
|
+
// 窄的时候依次:提示变短、去掉提示、用量变短、去掉缓存、模式变短、去掉用量、上下文变短、去掉速度
|
|
603
|
+
const segments = [
|
|
604
|
+
{
|
|
605
|
+
text: `${status.mode} (shift+tab)`,
|
|
606
|
+
short: status.mode,
|
|
607
|
+
shortAt: 6,
|
|
608
|
+
color: modeColor,
|
|
609
|
+
},
|
|
610
|
+
ctx
|
|
611
|
+
? {
|
|
612
|
+
text: `ctx ${tokens(ctx.used)}/${tokens(ctx.limit)} (${pct}%)${ctx.compactAt ? `, compacts at ${tokens(ctx.compactAt)}` : ""}`,
|
|
613
|
+
short: `ctx ${pct}%`,
|
|
614
|
+
shortAt: 4,
|
|
615
|
+
color: nearCompact ? "yellow" : undefined,
|
|
616
|
+
dim: !nearCompact,
|
|
617
|
+
}
|
|
618
|
+
: { text: "ctx —", dim: true },
|
|
619
|
+
...(speed
|
|
620
|
+
? [
|
|
621
|
+
{
|
|
622
|
+
text: `${speed.live ? "~" : ""}${speed.tps >= 100 ? Math.round(speed.tps) : speed.tps.toFixed(1)} tok/s`,
|
|
623
|
+
dropAt: 3,
|
|
624
|
+
dim: true,
|
|
625
|
+
},
|
|
626
|
+
]
|
|
627
|
+
: []),
|
|
628
|
+
...(status.cache !== undefined
|
|
629
|
+
? [{ text: `cache ${Math.round(status.cache * 100)}%`, dropAt: 7, dim: true }]
|
|
630
|
+
: []),
|
|
631
|
+
...(status.spent
|
|
632
|
+
? [
|
|
633
|
+
{
|
|
634
|
+
text: `session ${tokens(status.spent.input)} in / ${tokens(status.spent.output)} out`,
|
|
635
|
+
short: `${tokens(status.spent.input)} in / ${tokens(status.spent.output)} out`,
|
|
636
|
+
shortAt: 8,
|
|
637
|
+
dropAt: 5,
|
|
638
|
+
dim: true,
|
|
639
|
+
},
|
|
640
|
+
]
|
|
641
|
+
: []),
|
|
642
|
+
{
|
|
643
|
+
text: "PgUp/PgDn scroll · click ▸ to expand · shift+drag to select",
|
|
644
|
+
short: "click ▸ to expand",
|
|
645
|
+
shortAt: 10,
|
|
646
|
+
dropAt: 9,
|
|
647
|
+
dim: true,
|
|
648
|
+
},
|
|
649
|
+
];
|
|
650
|
+
const shown = fitSegments(segments, width);
|
|
651
|
+
return (_jsx(Box, { paddingX: 1, children: _jsx(Text, { wrap: "truncate-end", children: shown.map((s, i) => (_jsxs(Text, { children: [i ? _jsx(Text, { dimColor: true, children: " \u00B7 " }) : null, _jsx(Text, { color: s.color, dimColor: s.dim, children: s.text })] }, i))) }) }));
|
|
652
|
+
}
|
|
653
|
+
/** 一行提示:按宽度选第一个放得下的写法,都放不下时用最后一个并截断 */
|
|
654
|
+
function Hint({ variants, width }) {
|
|
655
|
+
const text = variants.find((v) => stringWidth(v) <= width) ?? variants.at(-1);
|
|
656
|
+
return (_jsx(Text, { dimColor: true, wrap: "truncate-end", children: text }));
|
|
473
657
|
}
|
|
474
658
|
const tokens = (n) => n >= 1_000_000
|
|
475
659
|
? `${(n / 1_000_000).toFixed(n >= 10_000_000 ? 0 : 1)}M`
|
|
@@ -532,24 +716,52 @@ function editKey(s, input, key) {
|
|
|
532
716
|
return text ? edit.insert(s, text) : undefined;
|
|
533
717
|
}
|
|
534
718
|
/** 渲染可编辑文本并把终端光标放到输入位置(输入法的候选框跟着它) */
|
|
535
|
-
function EditableText({ state, width, prefix, mask, placeholder, active, }) {
|
|
719
|
+
function EditableText({ state, width, prefix, mask, placeholder, active, maxRows = Infinity, }) {
|
|
536
720
|
const ref = useRef(null);
|
|
537
721
|
// 只用来在布局变化后重画一次、重新计算光标位置;它给的是相对父元素的坐标,光标要的是相对整个输出
|
|
538
722
|
const metrics = useBoxMetrics(ref);
|
|
539
|
-
const { setCursorPosition } = useCursor();
|
|
540
723
|
const shown = mask ? "•".repeat([...state.value].length) : state.value;
|
|
541
724
|
const cursor = mask
|
|
542
725
|
? [...state.value.slice(0, state.cursor)].length
|
|
543
726
|
: state.cursor;
|
|
544
|
-
const
|
|
727
|
+
const all = layout(shown, cursor, width - prefix.length);
|
|
728
|
+
const { col } = all;
|
|
729
|
+
// 行数超过 maxRows 时滚动,光标所在的行总在可见范围里
|
|
730
|
+
const first = Math.max(0, Math.min(all.row - maxRows + 1, all.rows.length - maxRows));
|
|
731
|
+
const rows = all.rows.slice(first, first + maxRows);
|
|
732
|
+
const row = all.row - first;
|
|
545
733
|
// 光标位置要在渲染时设:useCursor 在这次提交时交给 Ink,跟这一帧一起画出来。
|
|
546
734
|
// 放在 useEffect 里会晚一帧,光标总是停在上一次按键的位置(只移动光标时最明显)。
|
|
547
|
-
//
|
|
735
|
+
// 这里用的是上一次的布局,提交后要核对一遍:
|
|
736
|
+
// - 输入框相对父元素挪了位置时 useBoxMetrics 会再触发一次渲染
|
|
737
|
+
// - 只有祖先挪了位置时(窗口尺寸变化后整帧重画那一帧上下各垫一行,下一帧再去掉)它看不出来,
|
|
738
|
+
// 光标会停在比输入行低一行的地方,要自己比一下整体位置
|
|
548
739
|
const at = active && metrics.hasMeasured && ref.current
|
|
549
740
|
? origin(ref.current)
|
|
550
741
|
: undefined;
|
|
551
|
-
|
|
552
|
-
|
|
742
|
+
const position = at && { x: at.x + prefix.length + col, y: at.y + row };
|
|
743
|
+
const [, recheck] = useReducer((n) => n + 1, 0);
|
|
744
|
+
useEffect(() => {
|
|
745
|
+
if (!at || !ref.current)
|
|
746
|
+
return;
|
|
747
|
+
const now = origin(ref.current);
|
|
748
|
+
if (now.x !== at.x || now.y !== at.y)
|
|
749
|
+
recheck();
|
|
750
|
+
});
|
|
751
|
+
return (_jsxs(Box, { ref: ref, flexDirection: "column", children: [active ? _jsx(CursorAt, { position: position }) : null, state.value === "" && placeholder ? (_jsxs(Text, { wrap: "truncate-end", children: [_jsx(Text, { color: "cyan", children: prefix }), _jsx(Text, { dimColor: true, children: placeholder })] })) : (rows.map((r, i) => (_jsxs(Text, { wrap: "truncate-end", children: [_jsx(Text, { color: "cyan", children: first + i === 0
|
|
752
|
+
? prefix
|
|
753
|
+
: i === 0
|
|
754
|
+
? "↑".padEnd(prefix.length)
|
|
755
|
+
: " ".repeat(prefix.length) }), r] }, first + i))))] }));
|
|
756
|
+
}
|
|
757
|
+
/**
|
|
758
|
+
* 终端光标只有一个,每个 useCursor 每次提交都会设一遍。没有焦点的输入框(弹出输入框时下面的主输入框)
|
|
759
|
+
* 不能用它,否则它设的"不显示"会盖掉有焦点的那个,所以只在有焦点时挂上
|
|
760
|
+
*/
|
|
761
|
+
function CursorAt({ position }) {
|
|
762
|
+
const { setCursorPosition } = useCursor();
|
|
763
|
+
setCursorPosition(position);
|
|
764
|
+
return null;
|
|
553
765
|
}
|
|
554
766
|
/** 元素相对整个动态输出(Static 之外的部分)左上角的位置 */
|
|
555
767
|
function origin(node) {
|
|
@@ -570,7 +782,11 @@ function Prompt({ commands, handlers, history: initial, busy, active, onSubmit,
|
|
|
570
782
|
const browsing = useRef(-1);
|
|
571
783
|
const draft = useRef("");
|
|
572
784
|
const lastCtrlC = useRef(0);
|
|
573
|
-
const { columns } =
|
|
785
|
+
const { columns, rows } = useScreenSize();
|
|
786
|
+
/** 补全菜单、搜索结果最多显示几项;输入框最多几行 */
|
|
787
|
+
const menuRows = roomFor(rows, MENTION_ROWS);
|
|
788
|
+
const inputRows = roomFor(rows, 10);
|
|
789
|
+
const hintWidth = Math.max(10, columns - 3);
|
|
574
790
|
// @ 引用补全用的路径;排好的结果按(路径表,查询)缓存,按键处理和渲染都要用时不重复排
|
|
575
791
|
const [getPaths, setPaths] = useLive([]);
|
|
576
792
|
const ranked = useRef(undefined);
|
|
@@ -642,7 +858,9 @@ function Prompt({ commands, handlers, history: initial, busy, active, onSubmit,
|
|
|
642
858
|
};
|
|
643
859
|
}, [mentioning]);
|
|
644
860
|
// 结果多时只显示选中项附近的几条
|
|
645
|
-
const first =
|
|
861
|
+
const first = around(hit, results.length, menuRows);
|
|
862
|
+
const firstMatch = around(Math.max(0, matches.indexOf(pick)), matches.length, menuRows);
|
|
863
|
+
const firstFound = around(Math.max(0, found.indexOf(file)), found.length, menuRows);
|
|
646
864
|
const set = (s) => {
|
|
647
865
|
setState(s);
|
|
648
866
|
setSelected(0);
|
|
@@ -804,31 +1022,62 @@ function Prompt({ commands, handlers, history: initial, busy, active, onSubmit,
|
|
|
804
1022
|
set(next);
|
|
805
1023
|
}, { isActive: active });
|
|
806
1024
|
// 对话框打开时隐藏但不卸载,输入到一半的内容和历史都保留
|
|
807
|
-
return (_jsxs(Box, { flexDirection: "column", marginTop: 1, display: active ? "flex" : "none", children: [_jsx(Box, { borderStyle: "round", borderColor: busy ? "gray" : "cyan", paddingX: 1, children: search ? (_jsx(EditableText, { state: { value: search.query, cursor: search.query.length }, width: Math.max(10, columns - 5), prefix: "search history\u203A ", placeholder: "type to filter", active: active })) : (_jsx(EditableText, { state: state, width: Math.max(10, columns - 5), prefix: "\u203A ", placeholder: busy
|
|
1025
|
+
return (_jsxs(Box, { flexDirection: "column", marginTop: 1, display: active ? "flex" : "none", children: [_jsx(Box, { borderStyle: "round", borderColor: busy ? "gray" : "cyan", paddingX: 1, children: search ? (_jsx(EditableText, { state: { value: search.query, cursor: search.query.length }, width: Math.max(10, columns - 5), prefix: "search history\u203A ", placeholder: "type to filter", active: active, maxRows: inputRows })) : (_jsx(EditableText, { state: state, width: Math.max(10, columns - 5), prefix: "\u203A ", placeholder: busy
|
|
808
1026
|
? "Type to queue a message · esc to interrupt"
|
|
809
|
-
:
|
|
1027
|
+
: columns >= 70
|
|
1028
|
+
? "Message jade · / commands · @ files · ↑ or ctrl+r for history"
|
|
1029
|
+
: "Message jade · / commands · @ files", active: active, maxRows: inputRows })) }), search ? (_jsxs(Box, { flexDirection: "column", paddingX: 2, children: [results.length ? (results.slice(first, first + menuRows).map((h, i) => {
|
|
810
1030
|
const on = first + i === hit;
|
|
811
1031
|
return (_jsxs(Text, { wrap: "truncate-end", color: on ? "cyan" : undefined, bold: on, children: [on ? "❯ " : " ", h.replace(/\s*\n\s*/g, " ⏎ ")] }, first + i));
|
|
812
|
-
})) : (_jsx(Text, { dimColor: true, children: " no match" })),
|
|
1032
|
+
})) : (_jsx(Text, { dimColor: true, children: " no match" })), _jsx(Hint, { width: hintWidth, variants: [
|
|
1033
|
+
" ↑↓ or ctrl+r select · enter/tab put in the input · esc cancel",
|
|
1034
|
+
" ↑↓ select · enter use · esc cancel",
|
|
1035
|
+
] })] })) : matches.length ? (_jsxs(Box, { flexDirection: "column", paddingX: 2, children: [matches.slice(firstMatch, firstMatch + menuRows).map((c) => {
|
|
813
1036
|
const on = c === pick;
|
|
814
1037
|
return (_jsxs(Text, { wrap: "truncate-end", children: [_jsxs(Text, { color: on ? "cyan" : undefined, bold: on, children: [on ? "❯ " : " ", usageOf(c).padEnd(column)] }), _jsx(Text, { dimColor: true, children: c.about })] }, c.name));
|
|
815
|
-
}), _jsx(
|
|
1038
|
+
}), _jsx(Hint, { width: hintWidth, variants: [
|
|
1039
|
+
` ↑↓ select · enter run · tab complete${matches.length > menuRows ? ` · ${matches.length} commands` : ""}`,
|
|
1040
|
+
" ↑↓ · enter run · tab complete",
|
|
1041
|
+
] })] })) : found.length ? (_jsxs(Box, { flexDirection: "column", paddingX: 2, children: [found.slice(firstFound, firstFound + menuRows).map((p) => {
|
|
816
1042
|
const on = p === file;
|
|
817
1043
|
return (_jsxs(Text, { wrap: "truncate-middle", color: on ? "cyan" : p.endsWith("/") ? "blue" : undefined, bold: on, children: [on ? "❯ " : " ", p] }, p));
|
|
818
|
-
}),
|
|
1044
|
+
}), _jsx(Hint, { width: hintWidth, variants: [
|
|
1045
|
+
" ↑↓ select · enter/tab insert the path · the file's content is sent with the message",
|
|
1046
|
+
" ↑↓ select · enter/tab insert the path",
|
|
1047
|
+
" ↑↓ · enter insert",
|
|
1048
|
+
] })] })) : notice ? (_jsxs(Text, { color: "yellow", children: [" ", notice] })) : null] }));
|
|
819
1049
|
}
|
|
820
1050
|
/** @ 补全最多显示几项 */
|
|
821
1051
|
const MENTION_ROWS = 8;
|
|
1052
|
+
/** 长列表只显示 size 项时,为了让第 selected 项在中间附近,从第几项开始显示 */
|
|
1053
|
+
function around(selected, total, size) {
|
|
1054
|
+
return Math.max(0, Math.min(selected - Math.floor(size / 2), total - size));
|
|
1055
|
+
}
|
|
822
1056
|
/** 选择框一次最多显示几项 */
|
|
823
1057
|
const VISIBLE = 10;
|
|
1058
|
+
/** 选项多于这么多时可以打字筛选(这时 j/k 和数字键不再是快捷键) */
|
|
1059
|
+
const FILTER_FROM = 10;
|
|
824
1060
|
function SelectDialog({ dialog, }) {
|
|
825
1061
|
const [getSelected, setSelected] = useLive(0);
|
|
826
|
-
const
|
|
827
|
-
const
|
|
828
|
-
const
|
|
1062
|
+
const [getQuery, setQuery] = useLive("");
|
|
1063
|
+
const { rows, columns } = useScreenSize();
|
|
1064
|
+
const filtering = dialog.choices.length > FILTER_FROM;
|
|
1065
|
+
const query = getQuery();
|
|
1066
|
+
/** 筛选后的选项和它们原来的序号 */
|
|
1067
|
+
const filtered = () => {
|
|
1068
|
+
const q = getQuery().toLowerCase();
|
|
1069
|
+
return dialog.choices
|
|
1070
|
+
.map((c, i) => ({ c, i }))
|
|
1071
|
+
.filter(({ c }) => !q || `${c.label} ${c.hint ?? ""}`.toLowerCase().includes(q));
|
|
1072
|
+
};
|
|
1073
|
+
const items = filtered();
|
|
1074
|
+
const n = items.length;
|
|
1075
|
+
const selected = Math.min(getSelected(), Math.max(0, n - 1));
|
|
1076
|
+
// 终端矮时少显示几项,加上预览一起不超过终端的高度
|
|
1077
|
+
const visible = roomFor(rows, VISIBLE);
|
|
829
1078
|
// 预览(例如要写入的文件)过长时截断:整个界面高过终端时 Ink 会每帧清屏重画
|
|
830
1079
|
const lines = dialog.body?.split("\n") ?? [];
|
|
831
|
-
const room = Math.max(3, rows - Math.min(n,
|
|
1080
|
+
const room = Math.max(3, rows - Math.min(n, visible) - 16);
|
|
832
1081
|
const body = lines.length > room
|
|
833
1082
|
? [
|
|
834
1083
|
...lines.slice(0, room),
|
|
@@ -836,29 +1085,54 @@ function SelectDialog({ dialog, }) {
|
|
|
836
1085
|
].join("\n")
|
|
837
1086
|
: dialog.body;
|
|
838
1087
|
useInput((input, key) => {
|
|
839
|
-
const
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
if (key.
|
|
843
|
-
return setSelected((selected +
|
|
844
|
-
if (key.
|
|
845
|
-
return
|
|
1088
|
+
const items = filtered();
|
|
1089
|
+
const n = items.length;
|
|
1090
|
+
const selected = Math.min(getSelected(), Math.max(0, n - 1));
|
|
1091
|
+
if (key.upArrow || (!filtering && input === "k"))
|
|
1092
|
+
return n && setSelected((selected - 1 + n) % n);
|
|
1093
|
+
if (key.downArrow || (!filtering && input === "j"))
|
|
1094
|
+
return n && setSelected((selected + 1) % n);
|
|
1095
|
+
if (key.return) {
|
|
1096
|
+
if (n)
|
|
1097
|
+
dialog.resolve(items[selected].c.value);
|
|
1098
|
+
return;
|
|
1099
|
+
}
|
|
1100
|
+
if (key.escape && filtering && getQuery()) {
|
|
1101
|
+
setQuery("");
|
|
1102
|
+
return setSelected(0);
|
|
1103
|
+
}
|
|
846
1104
|
if (key.escape || (key.ctrl && input === "c"))
|
|
847
1105
|
return dialog.resolve(undefined);
|
|
848
1106
|
if (key.pageDown)
|
|
849
|
-
return setSelected(Math.min(n - 1, selected +
|
|
1107
|
+
return setSelected(Math.min(n - 1, selected + visible));
|
|
850
1108
|
if (key.pageUp)
|
|
851
|
-
return setSelected(Math.max(0, selected -
|
|
1109
|
+
return setSelected(Math.max(0, selected - visible));
|
|
1110
|
+
if (filtering) {
|
|
1111
|
+
if (key.backspace || key.delete) {
|
|
1112
|
+
setQuery([...getQuery()].slice(0, -1).join(""));
|
|
1113
|
+
return setSelected(0);
|
|
1114
|
+
}
|
|
1115
|
+
if (key.ctrl || key.meta || key.tab || MOUSE.test(input))
|
|
1116
|
+
return;
|
|
1117
|
+
const text = input.replace(/[\x00-\x1f\x7f]/g, "");
|
|
1118
|
+
if (text) {
|
|
1119
|
+
setQuery(getQuery() + text);
|
|
1120
|
+
setSelected(0);
|
|
1121
|
+
}
|
|
1122
|
+
return;
|
|
1123
|
+
}
|
|
852
1124
|
const digit = Number(input);
|
|
853
1125
|
if (Number.isInteger(digit) && digit >= 1 && digit <= Math.min(n, 9))
|
|
854
|
-
dialog.resolve(
|
|
1126
|
+
dialog.resolve(items[digit - 1].c.value);
|
|
855
1127
|
});
|
|
856
1128
|
// 选项多时只显示选中项附近的一段
|
|
857
|
-
const start =
|
|
858
|
-
return (_jsxs(Box, { flexDirection: "column", borderStyle: "round", borderColor: "yellow", paddingX: 1, marginTop: 1, children: [_jsx(Text, { bold: true, children: dialog.title }), body ? _jsx(Text, { children: body }) : null, _jsxs(Box, { flexDirection: "column", marginTop: 1, children: [start > 0 ? _jsxs(Text, { dimColor: true, children: [" \u2191 ", start, " more"] }) : null,
|
|
859
|
-
const
|
|
860
|
-
return (_jsxs(Text, { wrap: "truncate-end", children: [_jsxs(Text, { color:
|
|
861
|
-
}), start +
|
|
1129
|
+
const start = around(selected, n, visible);
|
|
1130
|
+
return (_jsxs(Box, { flexDirection: "column", borderStyle: "round", borderColor: "yellow", paddingX: 1, marginTop: 1, children: [_jsx(Text, { bold: true, wrap: "truncate-end", children: dialog.title }), body ? _jsx(Text, { children: body }) : null, filtering ? (_jsxs(Text, { wrap: "truncate-end", children: [_jsx(Text, { color: "cyan", children: "filter\u203A " }), query ? (_jsx(Text, { children: query })) : (_jsxs(Text, { dimColor: true, children: ["type to filter ", dialog.choices.length, " items"] }))] })) : null, _jsxs(Box, { flexDirection: "column", marginTop: filtering ? 0 : 1, children: [start > 0 ? _jsxs(Text, { dimColor: true, children: [" \u2191 ", start, " more"] }) : null, n === 0 ? _jsx(Text, { dimColor: true, children: " no match" }) : null, items.slice(start, start + visible).map(({ c, i }, j) => {
|
|
1131
|
+
const on = start + j === selected;
|
|
1132
|
+
return (_jsxs(Text, { wrap: "truncate-end", children: [_jsxs(Text, { color: on ? "cyan" : undefined, bold: on, children: [on ? "❯ " : " ", filtering ? "" : `${i + 1}. `, c.label] }), c.hint ? _jsxs(Text, { dimColor: true, children: [" ", c.hint] }) : null] }, i));
|
|
1133
|
+
}), start + visible < n ? (_jsxs(Text, { dimColor: true, children: [" \u2193 ", n - start - visible, " more"] })) : null] }), _jsx(Hint, { width: Math.max(10, columns - 4), variants: filtering
|
|
1134
|
+
? ["↑↓ select · type to filter · enter confirm · esc clear/cancel", "↑↓ · enter · esc"]
|
|
1135
|
+
: ["↑↓ select · enter confirm · esc cancel", "↑↓ · enter · esc"] })] }));
|
|
862
1136
|
}
|
|
863
1137
|
function InputDialog({ dialog, }) {
|
|
864
1138
|
const initial = dialog.initial ?? "";
|
|
@@ -867,7 +1141,7 @@ function InputDialog({ dialog, }) {
|
|
|
867
1141
|
cursor: initial.length,
|
|
868
1142
|
});
|
|
869
1143
|
const state = current();
|
|
870
|
-
const { columns } =
|
|
1144
|
+
const { columns } = useScreenSize();
|
|
871
1145
|
usePaste((text) => setState(edit.insert(current(), text.replace(/\r?\n/g, ""))));
|
|
872
1146
|
useInput((input, key) => {
|
|
873
1147
|
const state = current();
|