@uncarved/jade 0.1.1 → 0.1.3
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 +18 -6
- package/bin/jade.js +23 -0
- package/dist/commands.d.ts +20 -0
- package/dist/commands.js +20 -0
- package/dist/commands.js.map +1 -1
- package/dist/distro.d.ts +51 -7
- package/dist/distro.js +199 -48
- package/dist/distro.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/main.js +479 -303
- package/dist/main.js.map +1 -1
- package/dist/mcp.js +1 -1
- package/dist/mcp.js.map +1 -1
- package/dist/plugin-cli.d.ts +1 -1
- package/dist/plugin-cli.js +79 -36
- package/dist/plugin-cli.js.map +1 -1
- package/dist/plugins/budget.d.ts +2 -2
- package/dist/plugins/budget.js +2 -2
- package/dist/plugins/budget.js.map +1 -1
- package/dist/plugins/builtins.js +6 -3
- package/dist/plugins/builtins.js.map +1 -1
- package/dist/plugins/checks.d.ts +2 -1
- package/dist/plugins/checks.js +25 -5
- package/dist/plugins/checks.js.map +1 -1
- package/dist/plugins/compact.js +45 -1
- package/dist/plugins/compact.js.map +1 -1
- package/dist/plugins/core.d.ts +5 -0
- package/dist/plugins/core.js +39 -0
- package/dist/plugins/core.js.map +1 -1
- package/dist/plugins/host.d.ts +52 -0
- package/dist/plugins/host.js +115 -0
- package/dist/plugins/host.js.map +1 -0
- package/dist/plugins/install.d.ts +1 -1
- package/dist/plugins/loader.d.ts +238 -10
- package/dist/plugins/loader.js +175 -21
- package/dist/plugins/loader.js.map +1 -1
- package/dist/plugins/policy.d.ts +23 -5
- package/dist/plugins/policy.js +63 -10
- package/dist/plugins/policy.js.map +1 -1
- package/dist/plugins/prompts.d.ts +14 -1
- package/dist/plugins/prompts.js +118 -16
- package/dist/plugins/prompts.js.map +1 -1
- package/dist/plugins/repeats.d.ts +7 -0
- package/dist/plugins/repeats.js +74 -13
- package/dist/plugins/repeats.js.map +1 -1
- package/dist/plugins/review.d.ts +18 -0
- package/dist/plugins/review.js +130 -0
- package/dist/plugins/review.js.map +1 -0
- package/dist/plugins/sessions.d.ts +7 -0
- package/dist/plugins/sessions.js +110 -0
- package/dist/plugins/sessions.js.map +1 -0
- package/dist/provider.d.ts +57 -3
- package/dist/provider.js +80 -10
- package/dist/provider.js.map +1 -1
- package/dist/session-lifecycle.d.ts +61 -0
- package/dist/session-lifecycle.js +167 -0
- package/dist/session-lifecycle.js.map +1 -0
- package/dist/session-scope.d.ts +53 -0
- package/dist/session-scope.js +116 -0
- package/dist/session-scope.js.map +1 -0
- package/dist/sessions.d.ts +6 -0
- package/dist/sessions.js +20 -0
- package/dist/sessions.js.map +1 -1
- package/dist/sys.d.ts +4 -1
- package/dist/sys.js +18 -1
- package/dist/sys.js.map +1 -1
- package/dist/tools/ask.d.ts +11 -0
- package/dist/tools/ask.js +62 -0
- package/dist/tools/ask.js.map +1 -0
- package/dist/tools/fs.d.ts +4 -0
- package/dist/tools/fs.js +53 -12
- package/dist/tools/fs.js.map +1 -1
- package/dist/tools/grep.js +3 -2
- package/dist/tools/grep.js.map +1 -1
- package/dist/tools/shell.js +5 -1
- package/dist/tools/shell.js.map +1 -1
- package/dist/tools/task.d.ts +10 -2
- package/dist/tools/task.js +40 -9
- package/dist/tools/task.js.map +1 -1
- package/dist/tools/web.d.ts +33 -3
- package/dist/tools/web.js +351 -143
- package/dist/tools/web.js.map +1 -1
- package/dist/ui/configure.js +144 -66
- package/dist/ui/configure.js.map +1 -1
- package/dist/ui/diff.d.ts +1 -1
- package/dist/ui/diff.js +1 -2
- package/dist/ui/diff.js.map +1 -1
- package/dist/ui/editor.d.ts +8 -0
- package/dist/ui/editor.js +50 -1
- package/dist/ui/editor.js.map +1 -1
- package/dist/ui/markdown.d.ts +22 -0
- package/dist/ui/markdown.js +242 -0
- package/dist/ui/markdown.js.map +1 -0
- package/dist/ui/render.d.ts +9 -8
- package/dist/ui/render.js +35 -62
- package/dist/ui/render.js.map +1 -1
- package/dist/ui/renderers.d.ts +8 -0
- package/dist/ui/renderers.js +169 -0
- package/dist/ui/renderers.js.map +1 -0
- package/dist/ui/screen.d.ts +25 -2
- package/dist/ui/screen.js +104 -12
- package/dist/ui/screen.js.map +1 -1
- package/dist/ui/transcript.d.ts +13 -0
- package/dist/ui/transcript.js +157 -31
- package/dist/ui/transcript.js.map +1 -1
- package/dist/ui/tui.d.ts +2 -0
- package/dist/ui/tui.js +383 -81
- package/dist/ui/tui.js.map +1 -1
- package/dist/update.d.ts +53 -0
- package/dist/update.js +255 -0
- package/dist/update.js.map +1 -0
- package/package.json +7 -6
- package/src/commands.ts +24 -0
- package/src/distro.ts +234 -52
- package/src/index.ts +3 -0
- package/src/main.ts +459 -300
- package/src/mcp.ts +1 -1
- package/src/plugin-cli.ts +73 -34
- package/src/plugins/budget.ts +4 -4
- package/src/plugins/builtins.ts +6 -3
- package/src/plugins/checks.ts +25 -6
- package/src/plugins/compact.ts +40 -1
- package/src/plugins/core.ts +40 -0
- package/src/plugins/host.ts +146 -0
- package/src/plugins/install.ts +1 -1
- package/src/plugins/loader.ts +379 -23
- package/src/plugins/policy.ts +70 -13
- package/src/plugins/prompts.ts +128 -16
- package/src/plugins/repeats.ts +75 -14
- package/src/plugins/review.ts +146 -0
- package/src/plugins/sessions.ts +109 -0
- package/src/provider.ts +117 -14
- package/src/session-lifecycle.ts +194 -0
- package/src/session-scope.ts +135 -0
- package/src/sessions.ts +17 -0
- package/src/sys.ts +18 -1
- package/src/tools/ask.ts +72 -0
- package/src/tools/fs.ts +53 -12
- package/src/tools/grep.ts +3 -2
- package/src/tools/shell.ts +5 -1
- package/src/tools/task.ts +56 -12
- package/src/tools/web.ts +370 -140
- package/src/ui/configure.ts +141 -67
- package/src/ui/diff.ts +1 -2
- package/src/ui/editor.ts +50 -1
- package/src/ui/markdown.ts +258 -0
- package/src/ui/render.ts +35 -48
- package/src/ui/renderers.ts +183 -0
- package/src/ui/screen.ts +113 -16
- package/src/ui/transcript.ts +164 -35
- package/src/ui/tui.tsx +424 -79
- package/src/update.ts +262 -0
package/dist/ui/tui.js
CHANGED
|
@@ -4,11 +4,12 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
4
4
|
// 运行中也能输入:提交的内容由宿主决定立即执行还是排队。
|
|
5
5
|
import { writeSync } from "node:fs";
|
|
6
6
|
import { Box, render, Text, useBoxMetrics, useCursor, useInput, usePaste, useWindowSize, } from "ink";
|
|
7
|
-
import { useEffect, useReducer, useRef, useState, useSyncExternalStore, } from "react";
|
|
7
|
+
import { createContext, useContext, useEffect, useReducer, useRef, useState, useSyncExternalStore, } from "react";
|
|
8
8
|
import stringWidth from "string-width";
|
|
9
9
|
import wrapAnsi from "wrap-ansi";
|
|
10
10
|
import { mentionAt, mentionText, rankPaths } from "../mentions.js";
|
|
11
|
-
import { edit, empty, layout } from "./editor.js";
|
|
11
|
+
import { charWidth, edit, empty, layout, moveRow, offsetAt } from "./editor.js";
|
|
12
|
+
import { expandTabs, markdownStream } from "./markdown.js";
|
|
12
13
|
import { dim, } from "./screen.js";
|
|
13
14
|
/**
|
|
14
15
|
* 界面可用的尺寸:宽度比终端少一列。
|
|
@@ -19,10 +20,23 @@ function useScreenSize() {
|
|
|
19
20
|
const { columns, rows } = useWindowSize();
|
|
20
21
|
return { columns: Math.max(1, columns - 1), rows };
|
|
21
22
|
}
|
|
23
|
+
/** 按键写成 "ctrl+g"、"alt+v"(小写);别的键返回 undefined */
|
|
24
|
+
function keySpec(input, key) {
|
|
25
|
+
if (input.length !== 1)
|
|
26
|
+
return undefined;
|
|
27
|
+
const c = input.toLowerCase();
|
|
28
|
+
if (key.ctrl && !key.meta && /^[a-z]$/.test(c))
|
|
29
|
+
return `ctrl+${c}`;
|
|
30
|
+
if (key.meta && !key.ctrl && /^[a-z0-9]$/.test(c))
|
|
31
|
+
return `alt+${c}`;
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
22
34
|
/** 对话最多保留这么多行(折行前),超出时丢掉最早的一段 */
|
|
23
35
|
const MAX_LINES = 10_000;
|
|
24
|
-
/**
|
|
25
|
-
const wrap = (text, width) => wrapAnsi(text, width, { hard: true, trim: false }).split("\n");
|
|
36
|
+
/** 按显示宽度折行,保留颜色;每一项是屏幕上的一行。制表符先换成空格:按 0 列算宽度会对不齐、超宽 */
|
|
37
|
+
const wrap = (text, width) => wrapAnsi(expandTabs(text), width, { hard: true, trim: false }).split("\n");
|
|
38
|
+
/** Markdown 排好的一行折成屏幕上的行 */
|
|
39
|
+
const mdRows = (l, width) => typeof l === "string" ? wrap(l, width) : l.render(width);
|
|
26
40
|
/** 按宽度截成一行,截掉了内容时以 … 结尾;留出两列给 … 和展开标记 */
|
|
27
41
|
function oneRow(text, width) {
|
|
28
42
|
const rows = wrap(text, Math.max(1, width - 3));
|
|
@@ -70,6 +84,8 @@ function thinkingRow(t, width) {
|
|
|
70
84
|
return dim(latest && room >= 8 ? `${label} · ${tail(latest, room)}` : `${label}…`);
|
|
71
85
|
}
|
|
72
86
|
function wrapLine(l, width) {
|
|
87
|
+
if (l.render)
|
|
88
|
+
return l.render(width);
|
|
73
89
|
if (l.think) {
|
|
74
90
|
if (!foldable(l, width))
|
|
75
91
|
return [thinkingRow(l.think, width)];
|
|
@@ -228,6 +244,13 @@ class Store {
|
|
|
228
244
|
this.#named.clear();
|
|
229
245
|
this.update({ version: this.state.version + 1, live: undefined, progress: [], scroll: 0 }, true);
|
|
230
246
|
}
|
|
247
|
+
/** 展开或收起最后一个可折叠的项(Ctrl+O);start、view 同 toggle */
|
|
248
|
+
toggleLatest(start, view) {
|
|
249
|
+
for (let r = this.#owners.length - 1; r >= 0; r--) {
|
|
250
|
+
if (foldable(this.#owners[r], this.#width))
|
|
251
|
+
return this.toggle(r, start, view);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
231
254
|
/** 往上(正数)或往下滚 n 行,不超过 max */
|
|
232
255
|
scrollBy(n, max) {
|
|
233
256
|
const scroll = Math.max(0, Math.min(max, this.state.scroll + n));
|
|
@@ -235,12 +258,40 @@ class Store {
|
|
|
235
258
|
this.update({ scroll }, true);
|
|
236
259
|
}
|
|
237
260
|
}
|
|
238
|
-
/**
|
|
239
|
-
|
|
240
|
-
|
|
261
|
+
/**
|
|
262
|
+
* 开关鼠标事件上报(SGR 格式),用来接收滚轮、点击和按住左键拖动(1002)。
|
|
263
|
+
* 开着时终端自己不再处理点击,所以对话区的文字由 jade 自己选中和复制(见 Selection)
|
|
264
|
+
*/
|
|
265
|
+
const MOUSE_ON = "\x1b[?1000h\x1b[?1002h\x1b[?1006h";
|
|
266
|
+
const MOUSE_OFF = "\x1b[?1006l\x1b[?1002l\x1b[?1000l";
|
|
267
|
+
/**
|
|
268
|
+
* 光标样式:闪烁的竖线(DECSCUSR 5,再加上 ATT610 的"开始闪烁"给只认它的终端),一眼能看出输入框有焦点;
|
|
269
|
+
* 退出时还原成终端自己的默认样式。光标什么时候显示仍由 Ink 管(只有输入框或输入对话框有焦点时才显示)
|
|
270
|
+
*/
|
|
271
|
+
const CURSOR_BLINK = "\x1b[5 q\x1b[?12h";
|
|
272
|
+
const CURSOR_RESET = "\x1b[0 q";
|
|
273
|
+
/** 工作超过这么久后结束,或者这时弹出要用户选择的对话框,用户多半去做别的了:提醒一下 */
|
|
274
|
+
const NOTIFY_AFTER = 10_000;
|
|
275
|
+
/** 支持 OSC 9 桌面通知的终端;其他终端只响铃(不认识的 OSC 9 在有的终端里会弹别的东西) */
|
|
276
|
+
const OSC9_TERMINALS = new Set(["iTerm.app", "WezTerm", "ghostty"]);
|
|
277
|
+
/** 响铃,支持的终端再发一条桌面通知。JADE_NOTIFY=off 关闭 */
|
|
278
|
+
function notifier(stdout, env = process.env) {
|
|
279
|
+
if (env.JADE_NOTIFY === "off")
|
|
280
|
+
return () => { };
|
|
281
|
+
const osc9 = OSC9_TERMINALS.has(env.TERM_PROGRAM ?? "");
|
|
282
|
+
return (message) => stdout.write(osc9 ? `\x1b]9;jade: ${message.replace(/[\x00-\x1f\x7f]/g, " ")}\x07` : "\x07");
|
|
283
|
+
}
|
|
241
284
|
export function tuiScreen(opts) {
|
|
242
285
|
const stdout = opts.io?.stdout ?? process.stdout;
|
|
243
286
|
const store = new Store(opts.status);
|
|
287
|
+
const draft = {};
|
|
288
|
+
const notify = notifier(stdout);
|
|
289
|
+
/** 已经工作了很久(见 NOTIFY_AFTER) */
|
|
290
|
+
const longBusy = () => {
|
|
291
|
+
const since = store.state.busy?.since;
|
|
292
|
+
return since !== undefined && Date.now() - since >= NOTIFY_AFTER;
|
|
293
|
+
};
|
|
294
|
+
let doneTimer;
|
|
244
295
|
/**
|
|
245
296
|
* 正在进行的思考:一开始就作为一项放进对话(可以点击展开),之后的增量攒起来,每隔一小段时间改写一次那一项。
|
|
246
297
|
* 收起时只占一行,显示模型最新想到了哪里;结束时换成用了多久
|
|
@@ -261,7 +312,17 @@ export function tuiScreen(opts) {
|
|
|
261
312
|
redrawThinking();
|
|
262
313
|
thinking = undefined;
|
|
263
314
|
};
|
|
264
|
-
/**
|
|
315
|
+
/** 正文按 Markdown 排版;一段正文结束时换一个新的,清掉代码块、表格的状态 */
|
|
316
|
+
let md = markdownStream();
|
|
317
|
+
const appendMd = (lines) => {
|
|
318
|
+
for (const l of lines) {
|
|
319
|
+
if (typeof l === "string")
|
|
320
|
+
store.append([l]);
|
|
321
|
+
else
|
|
322
|
+
store.append([""], { render: l.render });
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
/** 推进正文:已经完整的行排版后移进对话,只把最后不完整的一行留在底部;all 时全部移进去,连同思考一起结束 */
|
|
265
326
|
const flushLive = (all = false) => {
|
|
266
327
|
if (all)
|
|
267
328
|
endThinking();
|
|
@@ -272,23 +333,33 @@ export function tuiScreen(opts) {
|
|
|
272
333
|
const rest = all ? "" : lines.pop();
|
|
273
334
|
if (all && lines.at(-1) === "")
|
|
274
335
|
lines.pop();
|
|
275
|
-
|
|
276
|
-
|
|
336
|
+
appendMd(lines.flatMap((l) => md.push(expandTabs(l))));
|
|
337
|
+
if (all) {
|
|
338
|
+
appendMd(md.flush());
|
|
339
|
+
md = markdownStream();
|
|
340
|
+
}
|
|
341
|
+
store.update({ live: rest || !all ? { text: rest, pending: md.pending() } : undefined });
|
|
277
342
|
};
|
|
278
|
-
|
|
343
|
+
/** signal 中止时关掉对话框,返回 undefined */
|
|
344
|
+
const ask = (dialog, signal) => new Promise((resolve) => {
|
|
345
|
+
if (signal?.aborted)
|
|
346
|
+
return resolve(undefined);
|
|
347
|
+
const close = () => d.resolve(undefined);
|
|
279
348
|
const d = {
|
|
280
349
|
...dialog,
|
|
281
350
|
resolve: (v) => {
|
|
351
|
+
signal?.removeEventListener("abort", close);
|
|
282
352
|
store.update({ dialogs: store.state.dialogs.filter((x) => x !== d) }, true);
|
|
283
353
|
resolve(v);
|
|
284
354
|
},
|
|
285
355
|
};
|
|
356
|
+
signal?.addEventListener("abort", close, { once: true });
|
|
286
357
|
store.update({ dialogs: [...store.state.dialogs, d] }, true);
|
|
287
358
|
});
|
|
288
359
|
// 切到备用屏幕后光标还停在原来的位置(不少终端不会自动归位),Ink 从那里往下画第一帧会让屏幕滚动,
|
|
289
360
|
// 之后每帧只重写变化的行,整个界面就错位了。所以先自己切过去、清屏、把光标移到左上角
|
|
290
361
|
stdout.write("\x1b[?1049h\x1b[2J\x1b[H");
|
|
291
|
-
const instance = render(_jsx(App, { store: store, version: opts.version, commands: opts.commands, handlers: opts.handlers, history: opts.history ?? [] }), {
|
|
362
|
+
const instance = render(_jsx(App, { store: store, copy: (text) => stdout.write(osc52(text)), version: opts.version, commands: opts.commands, handlers: opts.handlers, history: opts.history ?? [], draft: draft }), {
|
|
292
363
|
exitOnCtrlC: false,
|
|
293
364
|
alternateScreen: true,
|
|
294
365
|
// 全屏每帧只重写变化的行
|
|
@@ -297,11 +368,11 @@ export function tuiScreen(opts) {
|
|
|
297
368
|
? { ...opts.io, interactive: true, patchConsole: false }
|
|
298
369
|
: {}),
|
|
299
370
|
});
|
|
300
|
-
stdout.write(MOUSE_ON);
|
|
371
|
+
stdout.write(MOUSE_ON + CURSOR_BLINK);
|
|
301
372
|
// 没走 close 就退出(process.exit)时也要把终端还原,否则会留在备用屏幕、鼠标上报也开着
|
|
302
373
|
const restore = () => {
|
|
303
374
|
try {
|
|
304
|
-
writeSync(1, `${MOUSE_OFF}\x1b[?1049l\x1b[?25h`);
|
|
375
|
+
writeSync(1, `${MOUSE_OFF}${CURSOR_RESET}\x1b[?1049l\x1b[?25h`);
|
|
305
376
|
}
|
|
306
377
|
catch { }
|
|
307
378
|
};
|
|
@@ -366,10 +437,16 @@ export function tuiScreen(opts) {
|
|
|
366
437
|
},
|
|
367
438
|
busy(label) {
|
|
368
439
|
const current = store.state.busy;
|
|
369
|
-
if (label === undefined)
|
|
440
|
+
if (label === undefined) {
|
|
441
|
+
// 稍等一下再提醒:排队的消息接着开始时(马上又 busy)不算结束
|
|
442
|
+
if (longBusy())
|
|
443
|
+
doneTimer ??= setTimeout(() => notify("done"), 300);
|
|
370
444
|
store.update({ busy: undefined });
|
|
371
|
-
|
|
372
|
-
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
clearTimeout(doneTimer);
|
|
448
|
+
doneTimer = undefined;
|
|
449
|
+
store.update({ busy: { label, since: current?.since ?? Date.now() } });
|
|
373
450
|
},
|
|
374
451
|
status(info) {
|
|
375
452
|
store.update({ status: { ...store.state.status, ...info } });
|
|
@@ -377,30 +454,44 @@ export function tuiScreen(opts) {
|
|
|
377
454
|
queued(items) {
|
|
378
455
|
store.update({ queued: [...items] });
|
|
379
456
|
},
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
457
|
+
draft: {
|
|
458
|
+
text: () => draft.current?.get() ?? "",
|
|
459
|
+
insert: (text) => draft.current?.apply((s) => edit.insert(s, text.replace(/\r\n?/g, "\n"))),
|
|
460
|
+
set: (text) => {
|
|
461
|
+
const value = text.replace(/\r\n?/g, "\n");
|
|
462
|
+
draft.current?.apply(() => ({ value, cursor: value.length }));
|
|
463
|
+
},
|
|
464
|
+
},
|
|
465
|
+
select: (title, choices, o) => {
|
|
466
|
+
if (longBusy())
|
|
467
|
+
notify(title);
|
|
468
|
+
return ask({
|
|
469
|
+
kind: "select",
|
|
470
|
+
title,
|
|
471
|
+
body: o?.body,
|
|
472
|
+
choices: choices,
|
|
473
|
+
resolve: () => { },
|
|
474
|
+
}, o?.signal);
|
|
475
|
+
},
|
|
476
|
+
input: (title, { signal, ...o } = {}) => ask({
|
|
388
477
|
kind: "input",
|
|
389
478
|
title,
|
|
390
479
|
...o,
|
|
391
480
|
resolve: () => { },
|
|
392
|
-
}),
|
|
481
|
+
}, signal),
|
|
393
482
|
clear() {
|
|
394
483
|
clearTimeout(thinking?.timer);
|
|
395
484
|
thinking = undefined;
|
|
485
|
+
md = markdownStream();
|
|
396
486
|
store.clear();
|
|
397
487
|
},
|
|
398
488
|
async close() {
|
|
489
|
+
clearTimeout(doneTimer);
|
|
399
490
|
flushLive(true);
|
|
400
491
|
store.update({ live: undefined, busy: undefined, progress: [] }, true);
|
|
401
492
|
// 等最后一帧画完再卸载;卸载时 Ink 退出备用屏幕
|
|
402
493
|
await new Promise((r) => setTimeout(r, 50));
|
|
403
|
-
stdout.write(MOUSE_OFF);
|
|
494
|
+
stdout.write(MOUSE_OFF + CURSOR_RESET);
|
|
404
495
|
instance.unmount();
|
|
405
496
|
await instance.waitUntilExit();
|
|
406
497
|
process.off("exit", restore);
|
|
@@ -412,8 +503,84 @@ export function tuiScreen(opts) {
|
|
|
412
503
|
* SGR 鼠标事件(Ink 去掉了开头的 ESC):按钮号、列、行(从 1 开始),M 是按下、m 是松开。
|
|
413
504
|
* 按钮号去掉 Shift/Alt/Ctrl 位后 0 是左键,64 是滚轮向上,65 向下
|
|
414
505
|
*/
|
|
415
|
-
const MOUSE = /^\[<(\d+)
|
|
506
|
+
const MOUSE = /^\[<(\d+);(\d+);(\d+)([Mm])$/;
|
|
507
|
+
/** 解析鼠标事件:button 是去掉 Shift/Alt/Ctrl 位的按钮号,press 表示按下;x、y 是屏幕上的列和行(从 0 开始) */
|
|
508
|
+
function mouseEvent(input) {
|
|
509
|
+
const m = MOUSE.exec(input);
|
|
510
|
+
if (!m)
|
|
511
|
+
return undefined;
|
|
512
|
+
return { button: Number(m[1]) & ~0b11100, press: m[4] === "M", x: Number(m[2]) - 1, y: Number(m[3]) - 1 };
|
|
513
|
+
}
|
|
416
514
|
const WHEEL_LINES = 3;
|
|
515
|
+
/** 按住左键拖动时的按钮号(左键 0 加上移动位 32) */
|
|
516
|
+
const DRAG = 32;
|
|
517
|
+
/** "已复制"的提示显示多久 */
|
|
518
|
+
const COPIED_MS = 2000;
|
|
519
|
+
const same = (a, b) => a.row === b.row && a.col === b.col;
|
|
520
|
+
/** 第 row 行选中的列 [from, to);这一行不在选区里时 undefined */
|
|
521
|
+
function selectedCols(s, row) {
|
|
522
|
+
const { anchor: a, focus: f } = s;
|
|
523
|
+
const [first, last] = a.row < f.row || (a.row === f.row && a.col <= f.col) ? [a, f] : [f, a];
|
|
524
|
+
if (row < first.row || row > last.row)
|
|
525
|
+
return undefined;
|
|
526
|
+
return [row === first.row ? first.col : 0, row === last.row ? last.col + 1 : Infinity];
|
|
527
|
+
}
|
|
528
|
+
/** ANSI 转义序列(CSI,和以 BEL 或 ST 结束的 OSC,例如超链接) */
|
|
529
|
+
const ANSI = /\x1b\[[0-?]*[ -/]*[@-~]|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/y;
|
|
530
|
+
const INVERSE = "\x1b[7m";
|
|
531
|
+
const INVERSE_OFF = "\x1b[27m";
|
|
532
|
+
/**
|
|
533
|
+
* 逐个走过一行里的字符:visit 收到每个字符和它是否落在列 [from, to) 里(宽字符跨进去一半也算)。
|
|
534
|
+
* 转义序列原样交给 escape
|
|
535
|
+
*/
|
|
536
|
+
function walk(line, from, to, visit, escape) {
|
|
537
|
+
let col = 0;
|
|
538
|
+
for (let i = 0; i < line.length;) {
|
|
539
|
+
ANSI.lastIndex = i;
|
|
540
|
+
const m = ANSI.exec(line);
|
|
541
|
+
if (m) {
|
|
542
|
+
escape?.(m[0]);
|
|
543
|
+
i += m[0].length;
|
|
544
|
+
continue;
|
|
545
|
+
}
|
|
546
|
+
const ch = String.fromCodePoint(line.codePointAt(i));
|
|
547
|
+
const w = charWidth(ch);
|
|
548
|
+
visit(ch, col < to && col + Math.max(w, 1) > from);
|
|
549
|
+
col += w;
|
|
550
|
+
i += ch.length;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
/** 给一行里选中的部分加上反色;中间的转义序列可能关掉反色,之后重新打开 */
|
|
554
|
+
function highlight(line, from, to) {
|
|
555
|
+
let out = "";
|
|
556
|
+
let on = false;
|
|
557
|
+
walk(line, from, to, (ch, inside) => {
|
|
558
|
+
if (inside !== on)
|
|
559
|
+
out += inside ? INVERSE : INVERSE_OFF;
|
|
560
|
+
on = inside;
|
|
561
|
+
out += ch;
|
|
562
|
+
}, (seq) => (out += seq + (on ? INVERSE : "")));
|
|
563
|
+
return out + (on ? INVERSE_OFF : "");
|
|
564
|
+
}
|
|
565
|
+
/** 选中的纯文字:每行去掉行尾空白,用换行连起来(自动折行的地方也是换行) */
|
|
566
|
+
function selectedText(s, row) {
|
|
567
|
+
const lines = [];
|
|
568
|
+
const { anchor: a, focus: f } = s;
|
|
569
|
+
for (let r = Math.min(a.row, f.row); r <= Math.max(a.row, f.row); r++) {
|
|
570
|
+
const [from, to] = selectedCols(s, r);
|
|
571
|
+
let text = "";
|
|
572
|
+
walk(row(r) ?? "", from, to, (ch, inside) => void (inside && (text += ch)));
|
|
573
|
+
lines.push(text.trimEnd());
|
|
574
|
+
}
|
|
575
|
+
return lines.join("\n");
|
|
576
|
+
}
|
|
577
|
+
/** OSC 52:让终端把文字放进系统剪贴板(Windows Terminal、iTerm2、kitty、WezTerm 等支持) */
|
|
578
|
+
const osc52 = (text) => `\x1b]52;c;${Buffer.from(text, "utf8").toString("base64")}\x07`;
|
|
579
|
+
/**
|
|
580
|
+
* 输入框和对话框用:有选中的文字时 Ctrl+C 只取消选中(很多人选完习惯按 Ctrl+C 复制),
|
|
581
|
+
* 不中断模型、不清空输入、不取消对话框
|
|
582
|
+
*/
|
|
583
|
+
const SelectionContext = createContext({ active: () => false, clear: () => { } });
|
|
417
584
|
/** 终端矮于这么多行时标题只占一行 */
|
|
418
585
|
const COMPACT_ROWS = 24;
|
|
419
586
|
/**
|
|
@@ -421,9 +588,19 @@ const COMPACT_ROWS = 24;
|
|
|
421
588
|
* 超出的部分被裁掉,输入框的边框和状态栏就显示不全了
|
|
422
589
|
*/
|
|
423
590
|
const roomFor = (rows, max) => Math.max(3, Math.min(max, Math.floor((rows - 10) / 2)));
|
|
424
|
-
function App({ store, version, commands, handlers, history, }) {
|
|
591
|
+
function App({ store, copy, version, commands, handlers, history, draft, }) {
|
|
425
592
|
const state = useSyncExternalStore(store.subscribe, store.get);
|
|
426
593
|
const dialog = state.dialogs[0];
|
|
594
|
+
// 运行中每 100ms 整体重画一次,推动转圈动画。不能让 Spinner 自己重画:
|
|
595
|
+
// Ink 只在输入框也跟着提交的那一帧保留光标,否则把光标藏起来,运行中光标就不见了
|
|
596
|
+
const [, tick] = useReducer((n) => n + 1, 0);
|
|
597
|
+
const busy = state.busy !== undefined;
|
|
598
|
+
useEffect(() => {
|
|
599
|
+
if (!busy)
|
|
600
|
+
return;
|
|
601
|
+
const t = setInterval(tick, 100);
|
|
602
|
+
return () => clearInterval(t);
|
|
603
|
+
}, [busy]);
|
|
427
604
|
const { columns, rows } = useScreenSize();
|
|
428
605
|
// 对话区占掉标题和底部之外的全部高度,只画能看见的那几行
|
|
429
606
|
const ref = useRef(null);
|
|
@@ -432,6 +609,7 @@ function App({ store, version, commands, handlers, history, }) {
|
|
|
432
609
|
const done = store.lines(width);
|
|
433
610
|
// 还没结束的流式块和正在运行的工具的进度:思考只显示最新的一行,进度每个工具一行
|
|
434
611
|
const live = [
|
|
612
|
+
...(state.live?.pending ?? []).flatMap((l) => mdRows(l, width)),
|
|
435
613
|
...(state.live?.text ? wrap(state.live.text, width) : []),
|
|
436
614
|
...state.progress.map((p) => oneRow(p.line, width).row),
|
|
437
615
|
];
|
|
@@ -448,6 +626,30 @@ function App({ store, version, commands, handlers, history, }) {
|
|
|
448
626
|
...live.slice(Math.max(0, start - done.length), Math.max(0, end - done.length)),
|
|
449
627
|
];
|
|
450
628
|
const page = Math.max(1, view - 2);
|
|
629
|
+
// 选中文字:按下左键记下位置,拖动时更新,松开时复制;点一下(没拖动)还是展开或收起那一项。
|
|
630
|
+
// 几个事件可能在同一块输入里连着到达,来不及重画,所以另外用 ref 记着最新的值
|
|
631
|
+
const [selection, setSelectionState] = useState();
|
|
632
|
+
const selected = useRef(undefined);
|
|
633
|
+
const pressed = useRef(undefined);
|
|
634
|
+
const setSelection = (s) => {
|
|
635
|
+
selected.current = s;
|
|
636
|
+
setSelectionState(s);
|
|
637
|
+
};
|
|
638
|
+
const [selectionApi] = useState(() => ({ active: () => selected.current !== undefined, clear: () => setSelection(undefined) }));
|
|
639
|
+
// 宽度变了,行重新折过,原来的位置对不上了
|
|
640
|
+
useEffect(() => setSelection(undefined), [width]);
|
|
641
|
+
const rowAt = (i) => (i < done.length ? done[i] : live[i - done.length]);
|
|
642
|
+
// 复制之后在输入框下面提示一会儿
|
|
643
|
+
const [copied, setCopied] = useState();
|
|
644
|
+
const copiedTimer = useRef(undefined);
|
|
645
|
+
useEffect(() => () => clearTimeout(copiedTimer.current), []);
|
|
646
|
+
const copySelection = (s) => {
|
|
647
|
+
const text = selectedText(s, rowAt);
|
|
648
|
+
copy(text);
|
|
649
|
+
clearTimeout(copiedTimer.current);
|
|
650
|
+
setCopied(`Copied ${[...text].length} characters to the clipboard`);
|
|
651
|
+
copiedTimer.current = setTimeout(() => setCopied(undefined), COPIED_MS);
|
|
652
|
+
};
|
|
451
653
|
// Ink 增量渲染只用相对光标移动,第一帧起点一旦偏了(启动时终端尺寸还没稳定、窗口缩放、别的输出插进来),
|
|
452
654
|
// 之后每帧都会画错行。启动后、尺寸变化后和按 Ctrl+L 时让一帧比终端高,Ink 会清屏从左上角整帧重画(包在同步输出里),下一帧再恢复
|
|
453
655
|
const [redraws, setRedraws] = useState(0);
|
|
@@ -466,21 +668,49 @@ function App({ store, version, commands, handlers, history, }) {
|
|
|
466
668
|
useInput((input, key) => {
|
|
467
669
|
if (key.ctrl && input === "l")
|
|
468
670
|
return setRedraws((n) => n + 1);
|
|
469
|
-
|
|
671
|
+
// 展开或收起最近的一项(工具结果、思考、检查报告),鼠标点不了时也能看完整内容
|
|
672
|
+
if (key.ctrl && input === "o")
|
|
673
|
+
return store.toggleLatest(start, view);
|
|
674
|
+
const mouse = mouseEvent(input);
|
|
470
675
|
if (mouse) {
|
|
471
|
-
|
|
472
|
-
|
|
676
|
+
// 对话区里的位置;clamp 时拖出对话区停在第一行或最后一行,否则在对话区外返回 undefined
|
|
677
|
+
const at = (clamp) => {
|
|
678
|
+
const i = mouse.y - top;
|
|
679
|
+
if (!clamp && (i < 0 || i >= shown.length))
|
|
680
|
+
return undefined;
|
|
681
|
+
return { row: start + Math.min(Math.max(i, 0), shown.length - 1), col: Math.max(0, mouse.x - 1) };
|
|
682
|
+
};
|
|
683
|
+
if (mouse.button === 64)
|
|
473
684
|
store.scrollBy(WHEEL_LINES, max);
|
|
474
|
-
else if (button === 65)
|
|
685
|
+
else if (mouse.button === 65)
|
|
475
686
|
store.scrollBy(-WHEEL_LINES, max);
|
|
476
|
-
else if (button === 0 && mouse
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
687
|
+
else if (mouse.button === 0 && mouse.press) {
|
|
688
|
+
if (selected.current)
|
|
689
|
+
setSelection(undefined);
|
|
690
|
+
pressed.current = at(false);
|
|
691
|
+
}
|
|
692
|
+
else if (mouse.button === DRAG && pressed.current) {
|
|
693
|
+
setSelection({ anchor: pressed.current, focus: at(true) });
|
|
694
|
+
}
|
|
695
|
+
else if (mouse.button === 0 && !mouse.press && pressed.current) {
|
|
696
|
+
const p = pressed.current;
|
|
697
|
+
pressed.current = undefined;
|
|
698
|
+
const s = selected.current;
|
|
699
|
+
if (s && !same(s.anchor, s.focus))
|
|
700
|
+
copySelection(s);
|
|
701
|
+
else {
|
|
702
|
+
if (s)
|
|
703
|
+
setSelection(undefined);
|
|
704
|
+
// 点在对话区里已经完成的输出上:展开或收起那一项
|
|
705
|
+
if (p.row < done.length)
|
|
706
|
+
store.toggle(p.row, start, view);
|
|
707
|
+
}
|
|
481
708
|
}
|
|
482
709
|
return;
|
|
483
710
|
}
|
|
711
|
+
// 按别的键取消选中;Ctrl+C 由输入框和对话框处理(见 SelectionContext)
|
|
712
|
+
if (selected.current && !(key.ctrl && input === "c"))
|
|
713
|
+
setSelection(undefined);
|
|
484
714
|
// 选择框自己用 PgUp/PgDn 翻选项
|
|
485
715
|
if (dialog?.kind === "select")
|
|
486
716
|
return;
|
|
@@ -496,7 +726,10 @@ function App({ store, version, commands, handlers, history, }) {
|
|
|
496
726
|
// 总高度要比终端少一行,否则 Ink 认为输出占满了屏幕,每帧清屏重画。
|
|
497
727
|
// 重画那一帧上下各垫一行空白:终端把顶上那行滚出去后,看到的和平时逐行一样,所以不会闪。
|
|
498
728
|
// 只做纵向裁剪:每行都已按宽度折好或截断,横向裁剪会让 Ink 每帧对每一行再切一遍 ANSI 字符串
|
|
499
|
-
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) =>
|
|
729
|
+
return (_jsx(SelectionContext.Provider, { value: selectionApi, children: _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) => {
|
|
730
|
+
const cols = selection && selectedCols(selection, start + i);
|
|
731
|
+
return (_jsx(Text, { wrap: "truncate-end", children: cols ? highlight(l || " ", ...cols) : l || " " }, i));
|
|
732
|
+
}), 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, bridge: draft, busy: Boolean(state.busy), active: !dialog, onSubmit: () => store.scrollBy(-max, max), flash: copied }), _jsx(StatusBar, { status: state.status, width: Math.max(10, columns - 2) })] })] }) }) }));
|
|
500
733
|
}
|
|
501
734
|
const JADE = "#00a86b";
|
|
502
735
|
const SHINE = "#b6f2d4";
|
|
@@ -521,12 +754,9 @@ function dialogKey(d) {
|
|
|
521
754
|
return dialogIds.get(d);
|
|
522
755
|
}
|
|
523
756
|
const FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
757
|
+
/** 转圈动画由 App 每 100ms 整体重画一次推动(见 App 里的 tick),这里只按当前时间画 */
|
|
524
758
|
function Spinner({ label, since }) {
|
|
525
|
-
const
|
|
526
|
-
useEffect(() => {
|
|
527
|
-
const t = setInterval(() => setNow(Date.now()), 100);
|
|
528
|
-
return () => clearInterval(t);
|
|
529
|
-
}, []);
|
|
759
|
+
const now = Date.now();
|
|
530
760
|
const seconds = Math.floor((now - since) / 1000);
|
|
531
761
|
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)"] })] }) }));
|
|
532
762
|
}
|
|
@@ -559,13 +789,11 @@ function StatusBar({ status, width }) {
|
|
|
559
789
|
const nearCompact = ctx?.compactAt ? ctx.used >= ctx.compactAt * 0.85 : pct >= 80;
|
|
560
790
|
const modeColor = status.mode === "yolo"
|
|
561
791
|
? "red"
|
|
562
|
-
: status.mode === "auto
|
|
792
|
+
: status.mode === "auto"
|
|
563
793
|
? "yellow"
|
|
564
|
-
: status.mode === "
|
|
565
|
-
? "
|
|
566
|
-
:
|
|
567
|
-
? "magenta"
|
|
568
|
-
: "green";
|
|
794
|
+
: status.mode === "plan"
|
|
795
|
+
? "magenta"
|
|
796
|
+
: "green";
|
|
569
797
|
const speed = status.speed;
|
|
570
798
|
// 窄的时候依次:提示变短、去掉提示、用量变短、去掉缓存、模式变短、去掉用量、上下文变短、去掉速度
|
|
571
799
|
const segments = [
|
|
@@ -607,9 +835,11 @@ function StatusBar({ status, width }) {
|
|
|
607
835
|
},
|
|
608
836
|
]
|
|
609
837
|
: []),
|
|
838
|
+
// 插件的段落:比提示晚、比缓存早去掉
|
|
839
|
+
...(status.plugins ?? []).map((p) => ({ text: p.text, dropAt: 7.5, dim: true })),
|
|
610
840
|
{
|
|
611
|
-
text: "PgUp/PgDn scroll · click ▸ to expand ·
|
|
612
|
-
short: "
|
|
841
|
+
text: "PgUp/PgDn scroll · click ▸ or ctrl+o to expand · drag to select and copy",
|
|
842
|
+
short: "ctrl+o to expand",
|
|
613
843
|
shortAt: 10,
|
|
614
844
|
dropAt: 9,
|
|
615
845
|
dim: true,
|
|
@@ -684,11 +914,10 @@ function editKey(s, input, key) {
|
|
|
684
914
|
return text ? edit.insert(s, text) : undefined;
|
|
685
915
|
}
|
|
686
916
|
/** 渲染可编辑文本并把终端光标放到输入位置(输入法的候选框跟着它) */
|
|
687
|
-
function EditableText({ state, width, prefix, mask, placeholder, active, maxRows = Infinity, }) {
|
|
917
|
+
function EditableText({ state, width, prefix, mask, placeholder, active, maxRows = Infinity, place, }) {
|
|
688
918
|
const ref = useRef(null);
|
|
689
919
|
// 只用来在布局变化后重画一次、重新计算光标位置;它给的是相对父元素的坐标,光标要的是相对整个输出
|
|
690
920
|
const metrics = useBoxMetrics(ref);
|
|
691
|
-
const { setCursorPosition } = useCursor();
|
|
692
921
|
const shown = mask ? "•".repeat([...state.value].length) : state.value;
|
|
693
922
|
const cursor = mask
|
|
694
923
|
? [...state.value.slice(0, state.cursor)].length
|
|
@@ -701,16 +930,38 @@ function EditableText({ state, width, prefix, mask, placeholder, active, maxRows
|
|
|
701
930
|
const row = all.row - first;
|
|
702
931
|
// 光标位置要在渲染时设:useCursor 在这次提交时交给 Ink,跟这一帧一起画出来。
|
|
703
932
|
// 放在 useEffect 里会晚一帧,光标总是停在上一次按键的位置(只移动光标时最明显)。
|
|
704
|
-
//
|
|
933
|
+
// 这里用的是上一次的布局,提交后要核对一遍:
|
|
934
|
+
// - 输入框相对父元素挪了位置时 useBoxMetrics 会再触发一次渲染
|
|
935
|
+
// - 只有祖先挪了位置时(窗口尺寸变化后整帧重画那一帧上下各垫一行,下一帧再去掉)它看不出来,
|
|
936
|
+
// 光标会停在比输入行低一行的地方,要自己比一下整体位置
|
|
705
937
|
const at = active && metrics.hasMeasured && ref.current
|
|
706
938
|
? origin(ref.current)
|
|
707
939
|
: undefined;
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
940
|
+
const position = at && { x: at.x + prefix.length + col, y: at.y + row };
|
|
941
|
+
if (place)
|
|
942
|
+
place.current = at && { x: at.x + prefix.length, y: at.y, first, rows: rows.length };
|
|
943
|
+
const [, recheck] = useReducer((n) => n + 1, 0);
|
|
944
|
+
useEffect(() => {
|
|
945
|
+
if (!at || !ref.current)
|
|
946
|
+
return;
|
|
947
|
+
const now = origin(ref.current);
|
|
948
|
+
if (now.x !== at.x || now.y !== at.y)
|
|
949
|
+
recheck();
|
|
950
|
+
});
|
|
951
|
+
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
|
|
952
|
+
? prefix
|
|
953
|
+
: i === 0
|
|
954
|
+
? "↑".padEnd(prefix.length)
|
|
955
|
+
: " ".repeat(prefix.length) }), r] }, first + i))))] }));
|
|
956
|
+
}
|
|
957
|
+
/**
|
|
958
|
+
* 终端光标只有一个,每个 useCursor 每次提交都会设一遍。没有焦点的输入框(弹出输入框时下面的主输入框)
|
|
959
|
+
* 不能用它,否则它设的"不显示"会盖掉有焦点的那个,所以只在有焦点时挂上
|
|
960
|
+
*/
|
|
961
|
+
function CursorAt({ position }) {
|
|
962
|
+
const { setCursorPosition } = useCursor();
|
|
963
|
+
setCursorPosition(position);
|
|
964
|
+
return null;
|
|
714
965
|
}
|
|
715
966
|
/** 元素相对整个动态输出(Static 之外的部分)左上角的位置 */
|
|
716
967
|
function origin(node) {
|
|
@@ -722,7 +973,8 @@ function origin(node) {
|
|
|
722
973
|
}
|
|
723
974
|
return { x, y };
|
|
724
975
|
}
|
|
725
|
-
function Prompt({ commands, handlers, history: initial, busy, active, onSubmit, }) {
|
|
976
|
+
function Prompt({ commands, handlers, history: initial, bridge, busy, active, onSubmit, flash, }) {
|
|
977
|
+
const selection = useContext(SelectionContext);
|
|
726
978
|
const [current, setState] = useLive(empty);
|
|
727
979
|
const [getSelected, setSelected] = useLive(0);
|
|
728
980
|
const [getNotice, setNotice] = useLive(undefined);
|
|
@@ -731,7 +983,12 @@ function Prompt({ commands, handlers, history: initial, busy, active, onSubmit,
|
|
|
731
983
|
const browsing = useRef(-1);
|
|
732
984
|
const draft = useRef("");
|
|
733
985
|
const lastCtrlC = useRef(0);
|
|
986
|
+
const lastEsc = useRef(0);
|
|
987
|
+
/** 输入框文字在屏幕上的位置,点击时换算光标位置用(见 EditableText 的 place) */
|
|
988
|
+
const place = useRef(undefined);
|
|
734
989
|
const { columns, rows } = useScreenSize();
|
|
990
|
+
/** 输入框里文字的宽度:边框、内边距和前面的 "› " 之外 */
|
|
991
|
+
const textWidth = Math.max(10, columns - 5) - 2;
|
|
735
992
|
/** 补全菜单、搜索结果最多显示几项;输入框最多几行 */
|
|
736
993
|
const menuRows = roomFor(rows, MENTION_ROWS);
|
|
737
994
|
const inputRows = roomFor(rows, 10);
|
|
@@ -814,19 +1071,30 @@ function Prompt({ commands, handlers, history: initial, busy, active, onSubmit,
|
|
|
814
1071
|
setState(s);
|
|
815
1072
|
setSelected(0);
|
|
816
1073
|
setNotice(undefined);
|
|
1074
|
+
lastEsc.current = 0;
|
|
817
1075
|
};
|
|
818
|
-
const
|
|
819
|
-
if (!text.trim())
|
|
820
|
-
return;
|
|
1076
|
+
const remember = (text) => {
|
|
821
1077
|
history.current = [
|
|
822
1078
|
...history.current.filter((h) => h !== text),
|
|
823
1079
|
text,
|
|
824
1080
|
].slice(-100);
|
|
825
1081
|
browsing.current = -1;
|
|
1082
|
+
};
|
|
1083
|
+
const submit = (text) => {
|
|
1084
|
+
if (!text.trim())
|
|
1085
|
+
return;
|
|
1086
|
+
remember(text);
|
|
826
1087
|
set(empty);
|
|
827
1088
|
onSubmit();
|
|
828
1089
|
handlers.submit(text);
|
|
829
1090
|
};
|
|
1091
|
+
/** 清空输入;清掉的内容放进(本次运行的)历史,按 ↑ 能找回来 */
|
|
1092
|
+
const discard = () => {
|
|
1093
|
+
const text = current().value;
|
|
1094
|
+
if (text.trim())
|
|
1095
|
+
remember(text);
|
|
1096
|
+
set(empty);
|
|
1097
|
+
};
|
|
830
1098
|
/** 用选中的路径替换正在输入的 @ 引用;目录不加空格,接着补全里面的文件 */
|
|
831
1099
|
const complete = (p) => {
|
|
832
1100
|
const { state, mention } = derive();
|
|
@@ -838,6 +1106,7 @@ function Prompt({ commands, handlers, history: initial, busy, active, onSubmit,
|
|
|
838
1106
|
cursor: mention.start + text.length,
|
|
839
1107
|
});
|
|
840
1108
|
};
|
|
1109
|
+
bridge.current = { get: () => current().value, apply: (f) => set(f(current())) };
|
|
841
1110
|
const recall = (i) => {
|
|
842
1111
|
browsing.current = i;
|
|
843
1112
|
const value = i === -1
|
|
@@ -887,6 +1156,8 @@ function Prompt({ commands, handlers, history: initial, busy, active, onSubmit,
|
|
|
887
1156
|
setSearch({ query: s.query + text, index: 0 });
|
|
888
1157
|
};
|
|
889
1158
|
useInput((input, key) => {
|
|
1159
|
+
if (key.ctrl && input === "c" && selection.active())
|
|
1160
|
+
return selection.clear();
|
|
890
1161
|
const search = getSearch();
|
|
891
1162
|
if (search)
|
|
892
1163
|
return searchKey(search, input, key);
|
|
@@ -899,7 +1170,7 @@ function Prompt({ commands, handlers, history: initial, busy, active, onSubmit,
|
|
|
899
1170
|
if (busy)
|
|
900
1171
|
return handlers.interrupt();
|
|
901
1172
|
if (state.value)
|
|
902
|
-
return
|
|
1173
|
+
return discard();
|
|
903
1174
|
if (Date.now() - lastCtrlC.current < 2000)
|
|
904
1175
|
return handlers.exit();
|
|
905
1176
|
lastCtrlC.current = Date.now();
|
|
@@ -908,7 +1179,28 @@ function Prompt({ commands, handlers, history: initial, busy, active, onSubmit,
|
|
|
908
1179
|
if (key.escape) {
|
|
909
1180
|
if (busy)
|
|
910
1181
|
return handlers.interrupt();
|
|
911
|
-
|
|
1182
|
+
if (!state.value)
|
|
1183
|
+
return;
|
|
1184
|
+
// 误按一下 Esc 不清掉写了半天的内容:连按两次才清空
|
|
1185
|
+
if (Date.now() - lastEsc.current < 1500)
|
|
1186
|
+
return discard();
|
|
1187
|
+
setNotice("Press Esc again to clear the input");
|
|
1188
|
+
lastEsc.current = Date.now();
|
|
1189
|
+
return;
|
|
1190
|
+
}
|
|
1191
|
+
// 点击输入框里的文字:光标移到点的位置
|
|
1192
|
+
const mouse = mouseEvent(input);
|
|
1193
|
+
if (mouse) {
|
|
1194
|
+
const at = place.current;
|
|
1195
|
+
if (!at || mouse.button !== 0 || !mouse.press)
|
|
1196
|
+
return;
|
|
1197
|
+
const row = mouse.y - at.y;
|
|
1198
|
+
if (row < 0 || row >= at.rows)
|
|
1199
|
+
return;
|
|
1200
|
+
const cursor = offsetAt(state.value, textWidth, at.first + row, mouse.x - at.x);
|
|
1201
|
+
if (cursor !== undefined)
|
|
1202
|
+
setState({ ...state, cursor });
|
|
1203
|
+
return;
|
|
912
1204
|
}
|
|
913
1205
|
// 文字和回车在同一块里到达(输入很快时终端可能合并):插入文字后发送
|
|
914
1206
|
if (!key.ctrl &&
|
|
@@ -947,8 +1239,10 @@ function Prompt({ commands, handlers, history: initial, busy, active, onSubmit,
|
|
|
947
1239
|
const { menu, selected } = derive();
|
|
948
1240
|
if (menu)
|
|
949
1241
|
return setSelected((selected - 1 + menu) % menu);
|
|
950
|
-
|
|
951
|
-
|
|
1242
|
+
// 按折行后的屏幕行移动:一大段没有换行的文字也能上下走,到了第一行才翻历史
|
|
1243
|
+
const up = moveRow(state, textWidth, -1);
|
|
1244
|
+
if (up)
|
|
1245
|
+
return setState(up);
|
|
952
1246
|
if (browsing.current + 1 < history.current.length) {
|
|
953
1247
|
if (browsing.current === -1)
|
|
954
1248
|
draft.current = state.value;
|
|
@@ -960,12 +1254,16 @@ function Prompt({ commands, handlers, history: initial, busy, active, onSubmit,
|
|
|
960
1254
|
const { menu, selected } = derive();
|
|
961
1255
|
if (menu)
|
|
962
1256
|
return setSelected((selected + 1) % menu);
|
|
963
|
-
|
|
964
|
-
|
|
1257
|
+
const down = moveRow(state, textWidth, 1);
|
|
1258
|
+
if (down)
|
|
1259
|
+
return setState(down);
|
|
965
1260
|
if (browsing.current >= 0)
|
|
966
1261
|
recall(browsing.current - 1);
|
|
967
1262
|
return;
|
|
968
1263
|
}
|
|
1264
|
+
const spec = keySpec(input, key);
|
|
1265
|
+
if (spec && handlers.key?.(spec))
|
|
1266
|
+
return;
|
|
969
1267
|
const next = editKey(state, input, key);
|
|
970
1268
|
if (next)
|
|
971
1269
|
set(next);
|
|
@@ -975,7 +1273,7 @@ function Prompt({ commands, handlers, history: initial, busy, active, onSubmit,
|
|
|
975
1273
|
? "Type to queue a message · esc to interrupt"
|
|
976
1274
|
: columns >= 70
|
|
977
1275
|
? "Message jade · / commands · @ files · ↑ or ctrl+r for history"
|
|
978
|
-
: "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) => {
|
|
1276
|
+
: "Message jade · / commands · @ files", active: active, maxRows: inputRows, place: place })) }), search ? (_jsxs(Box, { flexDirection: "column", paddingX: 2, children: [results.length ? (results.slice(first, first + menuRows).map((h, i) => {
|
|
979
1277
|
const on = first + i === hit;
|
|
980
1278
|
return (_jsxs(Text, { wrap: "truncate-end", color: on ? "cyan" : undefined, bold: on, children: [on ? "❯ " : " ", h.replace(/\s*\n\s*/g, " ⏎ ")] }, first + i));
|
|
981
1279
|
})) : (_jsx(Text, { dimColor: true, children: " no match" })), _jsx(Hint, { width: hintWidth, variants: [
|
|
@@ -994,7 +1292,7 @@ function Prompt({ commands, handlers, history: initial, busy, active, onSubmit,
|
|
|
994
1292
|
" ↑↓ select · enter/tab insert the path · the file's content is sent with the message",
|
|
995
1293
|
" ↑↓ select · enter/tab insert the path",
|
|
996
1294
|
" ↑↓ · enter insert",
|
|
997
|
-
] })] })) : notice ? (_jsxs(Text, { color: "yellow", children: [" ", notice] })) : null] }));
|
|
1295
|
+
] })] })) : notice ? (_jsxs(Text, { color: "yellow", children: [" ", notice] })) : null, flash ? (_jsx(Box, { position: "absolute", top: 0, right: 2, children: _jsxs(Text, { color: "green", children: [" ", flash, " "] }) })) : null] }));
|
|
998
1296
|
}
|
|
999
1297
|
/** @ 补全最多显示几项 */
|
|
1000
1298
|
const MENTION_ROWS = 8;
|
|
@@ -1007,6 +1305,7 @@ const VISIBLE = 10;
|
|
|
1007
1305
|
/** 选项多于这么多时可以打字筛选(这时 j/k 和数字键不再是快捷键) */
|
|
1008
1306
|
const FILTER_FROM = 10;
|
|
1009
1307
|
function SelectDialog({ dialog, }) {
|
|
1308
|
+
const selection = useContext(SelectionContext);
|
|
1010
1309
|
const [getSelected, setSelected] = useLive(0);
|
|
1011
1310
|
const [getQuery, setQuery] = useLive("");
|
|
1012
1311
|
const { rows, columns } = useScreenSize();
|
|
@@ -1024,16 +1323,16 @@ function SelectDialog({ dialog, }) {
|
|
|
1024
1323
|
const selected = Math.min(getSelected(), Math.max(0, n - 1));
|
|
1025
1324
|
// 终端矮时少显示几项,加上预览一起不超过终端的高度
|
|
1026
1325
|
const visible = roomFor(rows, VISIBLE);
|
|
1027
|
-
// 预览(例如要写入的文件)过长时截断:整个界面高过终端时 Ink
|
|
1028
|
-
|
|
1326
|
+
// 预览(例如要写入的文件)过长时截断:整个界面高过终端时 Ink 会每帧清屏重画,底部的选项和提示也会被裁掉。
|
|
1327
|
+
// 按折行后的屏幕行数截断(一行长代码会占好几行),每一行单独显示、不再折行
|
|
1328
|
+
const lines = !dialog.body ? [] : wrap(dialog.body, Math.max(1, columns - 4));
|
|
1029
1329
|
const room = Math.max(3, rows - Math.min(n, visible) - 16);
|
|
1030
1330
|
const body = lines.length > room
|
|
1031
|
-
? [
|
|
1032
|
-
|
|
1033
|
-
dim(`… ${lines.length - room} more lines`),
|
|
1034
|
-
].join("\n")
|
|
1035
|
-
: dialog.body;
|
|
1331
|
+
? [...lines.slice(0, room - 1), dim(`… ${lines.length - room + 1} more lines`)]
|
|
1332
|
+
: lines;
|
|
1036
1333
|
useInput((input, key) => {
|
|
1334
|
+
if (key.ctrl && input === "c" && selection.active())
|
|
1335
|
+
return selection.clear();
|
|
1037
1336
|
const items = filtered();
|
|
1038
1337
|
const n = items.length;
|
|
1039
1338
|
const selected = Math.min(getSelected(), Math.max(0, n - 1));
|
|
@@ -1076,7 +1375,7 @@ function SelectDialog({ dialog, }) {
|
|
|
1076
1375
|
});
|
|
1077
1376
|
// 选项多时只显示选中项附近的一段
|
|
1078
1377
|
const start = around(selected, n, visible);
|
|
1079
|
-
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
|
|
1378
|
+
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.map((l, i) => (_jsx(Text, { wrap: "truncate-end", children: l || " " }, i))), 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) => {
|
|
1080
1379
|
const on = start + j === selected;
|
|
1081
1380
|
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));
|
|
1082
1381
|
}), start + visible < n ? (_jsxs(Text, { dimColor: true, children: [" \u2193 ", n - start - visible, " more"] })) : null] }), _jsx(Hint, { width: Math.max(10, columns - 4), variants: filtering
|
|
@@ -1084,6 +1383,7 @@ function SelectDialog({ dialog, }) {
|
|
|
1084
1383
|
: ["↑↓ select · enter confirm · esc cancel", "↑↓ · enter · esc"] })] }));
|
|
1085
1384
|
}
|
|
1086
1385
|
function InputDialog({ dialog, }) {
|
|
1386
|
+
const selection = useContext(SelectionContext);
|
|
1087
1387
|
const initial = dialog.initial ?? "";
|
|
1088
1388
|
const [current, setState] = useLive({
|
|
1089
1389
|
value: initial,
|
|
@@ -1093,6 +1393,8 @@ function InputDialog({ dialog, }) {
|
|
|
1093
1393
|
const { columns } = useScreenSize();
|
|
1094
1394
|
usePaste((text) => setState(edit.insert(current(), text.replace(/\r?\n/g, ""))));
|
|
1095
1395
|
useInput((input, key) => {
|
|
1396
|
+
if (key.ctrl && input === "c" && selection.active())
|
|
1397
|
+
return selection.clear();
|
|
1096
1398
|
const state = current();
|
|
1097
1399
|
if (key.return)
|
|
1098
1400
|
return dialog.resolve(state.value.trim());
|