@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/src/ui/tui.tsx
CHANGED
|
@@ -16,6 +16,8 @@ import {
|
|
|
16
16
|
type Key,
|
|
17
17
|
} from "ink";
|
|
18
18
|
import {
|
|
19
|
+
createContext,
|
|
20
|
+
useContext,
|
|
19
21
|
useEffect,
|
|
20
22
|
useReducer,
|
|
21
23
|
useRef,
|
|
@@ -25,7 +27,8 @@ import {
|
|
|
25
27
|
import stringWidth from "string-width";
|
|
26
28
|
import wrapAnsi from "wrap-ansi";
|
|
27
29
|
import { mentionAt, mentionText, rankPaths } from "../mentions.ts";
|
|
28
|
-
import { edit, empty, layout, type EditState } from "./editor.ts";
|
|
30
|
+
import { charWidth, edit, empty, layout, moveRow, offsetAt, type EditState } from "./editor.ts";
|
|
31
|
+
import { expandTabs, markdownStream, type MdLine } from "./markdown.ts";
|
|
29
32
|
import {
|
|
30
33
|
dim,
|
|
31
34
|
type Choice,
|
|
@@ -55,6 +58,20 @@ export interface TuiHandlers {
|
|
|
55
58
|
files?(): Promise<string[]>;
|
|
56
59
|
/** Shift+Tab:切换到下一个权限模式 */
|
|
57
60
|
cycleMode?(): void;
|
|
61
|
+
/** 输入框有焦点时按下 jade 自己不用的 ctrl+<字母>、alt+<字母或数字>(见 keySpec);返回 true 表示有插件处理了 */
|
|
62
|
+
key?(spec: string): boolean;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** 输入框交给 tuiScreen 的读写入口(Prompt 每次渲染时更新) */
|
|
66
|
+
type DraftBridge = { current?: { get(): string; apply(f: (s: EditState) => EditState): void } };
|
|
67
|
+
|
|
68
|
+
/** 按键写成 "ctrl+g"、"alt+v"(小写);别的键返回 undefined */
|
|
69
|
+
function keySpec(input: string, key: Key): string | undefined {
|
|
70
|
+
if (input.length !== 1) return undefined;
|
|
71
|
+
const c = input.toLowerCase();
|
|
72
|
+
if (key.ctrl && !key.meta && /^[a-z]$/.test(c)) return `ctrl+${c}`;
|
|
73
|
+
if (key.meta && !key.ctrl && /^[a-z0-9]$/.test(c)) return `alt+${c}`;
|
|
74
|
+
return undefined;
|
|
58
75
|
}
|
|
59
76
|
|
|
60
77
|
type Dialog =
|
|
@@ -78,7 +95,7 @@ interface UiState {
|
|
|
78
95
|
/** 对话内容变化时递增,让界面重画 */
|
|
79
96
|
version: number;
|
|
80
97
|
/** 还没结束的正文:完整的行已经移进对话,这里只有最后不完整的一行(思考是对话里的一项,见 Thinking) */
|
|
81
|
-
live?: { text: string };
|
|
98
|
+
live?: { text: string; /** Markdown 里攒着的部分(还没结束的表格),显示在 text 前面 */ pending?: MdLine[] };
|
|
82
99
|
/** 对话区离底部滚上去了多少行;0 表示跟着最新的输出 */
|
|
83
100
|
scroll: number;
|
|
84
101
|
busy?: { label: string; since: number };
|
|
@@ -92,9 +109,9 @@ interface UiState {
|
|
|
92
109
|
/** 对话最多保留这么多行(折行前),超出时丢掉最早的一段 */
|
|
93
110
|
const MAX_LINES = 10_000;
|
|
94
111
|
|
|
95
|
-
/**
|
|
112
|
+
/** 按显示宽度折行,保留颜色;每一项是屏幕上的一行。制表符先换成空格:按 0 列算宽度会对不齐、超宽 */
|
|
96
113
|
const wrap = (text: string, width: number) =>
|
|
97
|
-
wrapAnsi(text, width, { hard: true, trim: false }).split("\n");
|
|
114
|
+
wrapAnsi(expandTabs(text), width, { hard: true, trim: false }).split("\n");
|
|
98
115
|
|
|
99
116
|
/**
|
|
100
117
|
* 对话里的一项:普通的一行,或者可以点击展开、收起的一段。
|
|
@@ -112,8 +129,14 @@ interface Line {
|
|
|
112
129
|
open?: boolean;
|
|
113
130
|
/** 之后用 amend 补充内容时用的名字 */
|
|
114
131
|
id?: string;
|
|
132
|
+
/** 按宽度现排的内容(Markdown 表格、分隔线),代替 text */
|
|
133
|
+
render?(width: number): string[];
|
|
115
134
|
}
|
|
116
135
|
|
|
136
|
+
/** Markdown 排好的一行折成屏幕上的行 */
|
|
137
|
+
const mdRows = (l: MdLine, width: number) =>
|
|
138
|
+
typeof l === "string" ? wrap(l, width) : l.render(width);
|
|
139
|
+
|
|
117
140
|
/** 按宽度截成一行,截掉了内容时以 … 结尾;留出两列给 … 和展开标记 */
|
|
118
141
|
function oneRow(text: string, width: number): { row: string; cut: boolean } {
|
|
119
142
|
const rows = wrap(text, Math.max(1, width - 3));
|
|
@@ -170,6 +193,7 @@ function thinkingRow(t: Thinking, width: number): string {
|
|
|
170
193
|
}
|
|
171
194
|
|
|
172
195
|
function wrapLine(l: Line, width: number): string[] {
|
|
196
|
+
if (l.render) return l.render(width);
|
|
173
197
|
if (l.think) {
|
|
174
198
|
if (!foldable(l, width)) return [thinkingRow(l.think, width)];
|
|
175
199
|
if (!l.open) return [`${thinkingRow(l.think, width)}${dim(" ▸")}`];
|
|
@@ -333,6 +357,13 @@ class Store {
|
|
|
333
357
|
);
|
|
334
358
|
}
|
|
335
359
|
|
|
360
|
+
/** 展开或收起最后一个可折叠的项(Ctrl+O);start、view 同 toggle */
|
|
361
|
+
toggleLatest(start: number, view: number) {
|
|
362
|
+
for (let r = this.#owners.length - 1; r >= 0; r--) {
|
|
363
|
+
if (foldable(this.#owners[r]!, this.#width)) return this.toggle(r, start, view);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
336
367
|
/** 往上(正数)或往下滚 n 行,不超过 max */
|
|
337
368
|
scrollBy(n: number, max: number) {
|
|
338
369
|
const scroll = Math.max(0, Math.min(max, this.state.scroll + n));
|
|
@@ -352,13 +383,43 @@ export interface TuiOptions {
|
|
|
352
383
|
io?: { stdin: NodeJS.ReadStream; stdout: NodeJS.WriteStream };
|
|
353
384
|
}
|
|
354
385
|
|
|
355
|
-
/**
|
|
356
|
-
|
|
357
|
-
|
|
386
|
+
/**
|
|
387
|
+
* 开关鼠标事件上报(SGR 格式),用来接收滚轮、点击和按住左键拖动(1002)。
|
|
388
|
+
* 开着时终端自己不再处理点击,所以对话区的文字由 jade 自己选中和复制(见 Selection)
|
|
389
|
+
*/
|
|
390
|
+
const MOUSE_ON = "\x1b[?1000h\x1b[?1002h\x1b[?1006h";
|
|
391
|
+
const MOUSE_OFF = "\x1b[?1006l\x1b[?1002l\x1b[?1000l";
|
|
392
|
+
/**
|
|
393
|
+
* 光标样式:闪烁的竖线(DECSCUSR 5,再加上 ATT610 的"开始闪烁"给只认它的终端),一眼能看出输入框有焦点;
|
|
394
|
+
* 退出时还原成终端自己的默认样式。光标什么时候显示仍由 Ink 管(只有输入框或输入对话框有焦点时才显示)
|
|
395
|
+
*/
|
|
396
|
+
const CURSOR_BLINK = "\x1b[5 q\x1b[?12h";
|
|
397
|
+
const CURSOR_RESET = "\x1b[0 q";
|
|
398
|
+
|
|
399
|
+
/** 工作超过这么久后结束,或者这时弹出要用户选择的对话框,用户多半去做别的了:提醒一下 */
|
|
400
|
+
const NOTIFY_AFTER = 10_000;
|
|
401
|
+
/** 支持 OSC 9 桌面通知的终端;其他终端只响铃(不认识的 OSC 9 在有的终端里会弹别的东西) */
|
|
402
|
+
const OSC9_TERMINALS = new Set(["iTerm.app", "WezTerm", "ghostty"]);
|
|
403
|
+
|
|
404
|
+
/** 响铃,支持的终端再发一条桌面通知。JADE_NOTIFY=off 关闭 */
|
|
405
|
+
function notifier(stdout: NodeJS.WriteStream, env = process.env): (message: string) => void {
|
|
406
|
+
if (env.JADE_NOTIFY === "off") return () => {};
|
|
407
|
+
const osc9 = OSC9_TERMINALS.has(env.TERM_PROGRAM ?? "");
|
|
408
|
+
return (message) =>
|
|
409
|
+
stdout.write(osc9 ? `\x1b]9;jade: ${message.replace(/[\x00-\x1f\x7f]/g, " ")}\x07` : "\x07");
|
|
410
|
+
}
|
|
358
411
|
|
|
359
412
|
export function tuiScreen(opts: TuiOptions): Screen {
|
|
360
413
|
const stdout = opts.io?.stdout ?? process.stdout;
|
|
361
414
|
const store = new Store(opts.status);
|
|
415
|
+
const draft: DraftBridge = {};
|
|
416
|
+
const notify = notifier(stdout);
|
|
417
|
+
/** 已经工作了很久(见 NOTIFY_AFTER) */
|
|
418
|
+
const longBusy = () => {
|
|
419
|
+
const since = store.state.busy?.since;
|
|
420
|
+
return since !== undefined && Date.now() - since >= NOTIFY_AFTER;
|
|
421
|
+
};
|
|
422
|
+
let doneTimer: ReturnType<typeof setTimeout> | undefined;
|
|
362
423
|
|
|
363
424
|
/**
|
|
364
425
|
* 正在进行的思考:一开始就作为一项放进对话(可以点击展开),之后的增量攒起来,每隔一小段时间改写一次那一项。
|
|
@@ -381,7 +442,16 @@ export function tuiScreen(opts: TuiOptions): Screen {
|
|
|
381
442
|
thinking = undefined;
|
|
382
443
|
};
|
|
383
444
|
|
|
384
|
-
/**
|
|
445
|
+
/** 正文按 Markdown 排版;一段正文结束时换一个新的,清掉代码块、表格的状态 */
|
|
446
|
+
let md = markdownStream();
|
|
447
|
+
const appendMd = (lines: MdLine[]) => {
|
|
448
|
+
for (const l of lines) {
|
|
449
|
+
if (typeof l === "string") store.append([l]);
|
|
450
|
+
else store.append([""], { render: l.render });
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
|
|
454
|
+
/** 推进正文:已经完整的行排版后移进对话,只把最后不完整的一行留在底部;all 时全部移进去,连同思考一起结束 */
|
|
385
455
|
const flushLive = (all = false) => {
|
|
386
456
|
if (all) endThinking();
|
|
387
457
|
const live = store.state.live;
|
|
@@ -389,15 +459,23 @@ export function tuiScreen(opts: TuiOptions): Screen {
|
|
|
389
459
|
const lines = live.text.split("\n");
|
|
390
460
|
const rest = all ? "" : lines.pop()!;
|
|
391
461
|
if (all && lines.at(-1) === "") lines.pop();
|
|
392
|
-
|
|
393
|
-
|
|
462
|
+
appendMd(lines.flatMap((l) => md.push(expandTabs(l))));
|
|
463
|
+
if (all) {
|
|
464
|
+
appendMd(md.flush());
|
|
465
|
+
md = markdownStream();
|
|
466
|
+
}
|
|
467
|
+
store.update({ live: rest || !all ? { text: rest, pending: md.pending() } : undefined });
|
|
394
468
|
};
|
|
395
469
|
|
|
396
|
-
|
|
470
|
+
/** signal 中止时关掉对话框,返回 undefined */
|
|
471
|
+
const ask = <T,>(dialog: Dialog, signal?: AbortSignal) =>
|
|
397
472
|
new Promise<T>((resolve) => {
|
|
473
|
+
if (signal?.aborted) return resolve(undefined as T);
|
|
474
|
+
const close = () => d.resolve(undefined);
|
|
398
475
|
const d = {
|
|
399
476
|
...dialog,
|
|
400
477
|
resolve: (v: unknown) => {
|
|
478
|
+
signal?.removeEventListener("abort", close);
|
|
401
479
|
store.update(
|
|
402
480
|
{ dialogs: store.state.dialogs.filter((x) => x !== d) },
|
|
403
481
|
true,
|
|
@@ -405,6 +483,7 @@ export function tuiScreen(opts: TuiOptions): Screen {
|
|
|
405
483
|
resolve(v as T);
|
|
406
484
|
},
|
|
407
485
|
} as Dialog;
|
|
486
|
+
signal?.addEventListener("abort", close, { once: true });
|
|
408
487
|
store.update({ dialogs: [...store.state.dialogs, d] }, true);
|
|
409
488
|
});
|
|
410
489
|
|
|
@@ -414,10 +493,12 @@ export function tuiScreen(opts: TuiOptions): Screen {
|
|
|
414
493
|
const instance = render(
|
|
415
494
|
<App
|
|
416
495
|
store={store}
|
|
496
|
+
copy={(text) => stdout.write(osc52(text))}
|
|
417
497
|
version={opts.version}
|
|
418
498
|
commands={opts.commands}
|
|
419
499
|
handlers={opts.handlers}
|
|
420
500
|
history={opts.history ?? []}
|
|
501
|
+
draft={draft}
|
|
421
502
|
/>,
|
|
422
503
|
{
|
|
423
504
|
exitOnCtrlC: false,
|
|
@@ -429,11 +510,11 @@ export function tuiScreen(opts: TuiOptions): Screen {
|
|
|
429
510
|
: {}),
|
|
430
511
|
},
|
|
431
512
|
);
|
|
432
|
-
stdout.write(MOUSE_ON);
|
|
513
|
+
stdout.write(MOUSE_ON + CURSOR_BLINK);
|
|
433
514
|
// 没走 close 就退出(process.exit)时也要把终端还原,否则会留在备用屏幕、鼠标上报也开着
|
|
434
515
|
const restore = () => {
|
|
435
516
|
try {
|
|
436
|
-
writeSync(1, `${MOUSE_OFF}\x1b[?1049l\x1b[?25h`);
|
|
517
|
+
writeSync(1, `${MOUSE_OFF}${CURSOR_RESET}\x1b[?1049l\x1b[?25h`);
|
|
437
518
|
} catch {}
|
|
438
519
|
};
|
|
439
520
|
if (!opts.io) process.on("exit", restore);
|
|
@@ -499,9 +580,15 @@ export function tuiScreen(opts: TuiOptions): Screen {
|
|
|
499
580
|
},
|
|
500
581
|
busy(label) {
|
|
501
582
|
const current = store.state.busy;
|
|
502
|
-
if (label === undefined)
|
|
503
|
-
|
|
504
|
-
|
|
583
|
+
if (label === undefined) {
|
|
584
|
+
// 稍等一下再提醒:排队的消息接着开始时(马上又 busy)不算结束
|
|
585
|
+
if (longBusy()) doneTimer ??= setTimeout(() => notify("done"), 300);
|
|
586
|
+
store.update({ busy: undefined });
|
|
587
|
+
return;
|
|
588
|
+
}
|
|
589
|
+
clearTimeout(doneTimer);
|
|
590
|
+
doneTimer = undefined;
|
|
591
|
+
store.update({ busy: { label, since: current?.since ?? Date.now() } });
|
|
505
592
|
},
|
|
506
593
|
status(info) {
|
|
507
594
|
store.update({ status: { ...store.state.status, ...info } });
|
|
@@ -509,32 +596,50 @@ export function tuiScreen(opts: TuiOptions): Screen {
|
|
|
509
596
|
queued(items) {
|
|
510
597
|
store.update({ queued: [...items] });
|
|
511
598
|
},
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
599
|
+
draft: {
|
|
600
|
+
text: () => draft.current?.get() ?? "",
|
|
601
|
+
insert: (text) => draft.current?.apply((s) => edit.insert(s, text.replace(/\r\n?/g, "\n"))),
|
|
602
|
+
set: (text) => {
|
|
603
|
+
const value = text.replace(/\r\n?/g, "\n");
|
|
604
|
+
draft.current?.apply(() => ({ value, cursor: value.length }));
|
|
605
|
+
},
|
|
606
|
+
},
|
|
607
|
+
select: <T,>(title: string, choices: Choice<T>[], o?: { body?: string; signal?: AbortSignal }) => {
|
|
608
|
+
if (longBusy()) notify(title);
|
|
609
|
+
return ask<T | undefined>(
|
|
610
|
+
{
|
|
611
|
+
kind: "select",
|
|
612
|
+
title,
|
|
613
|
+
body: o?.body,
|
|
614
|
+
choices: choices as Choice<unknown>[],
|
|
615
|
+
resolve: () => {},
|
|
616
|
+
},
|
|
617
|
+
o?.signal,
|
|
618
|
+
);
|
|
619
|
+
},
|
|
620
|
+
input: (title, { signal, ...o } = {}) =>
|
|
621
|
+
ask<string | undefined>(
|
|
622
|
+
{
|
|
623
|
+
kind: "input",
|
|
624
|
+
title,
|
|
625
|
+
...o,
|
|
626
|
+
resolve: () => {},
|
|
627
|
+
},
|
|
628
|
+
signal,
|
|
629
|
+
),
|
|
527
630
|
clear() {
|
|
528
631
|
clearTimeout(thinking?.timer);
|
|
529
632
|
thinking = undefined;
|
|
633
|
+
md = markdownStream();
|
|
530
634
|
store.clear();
|
|
531
635
|
},
|
|
532
636
|
async close() {
|
|
637
|
+
clearTimeout(doneTimer);
|
|
533
638
|
flushLive(true);
|
|
534
639
|
store.update({ live: undefined, busy: undefined, progress: [] }, true);
|
|
535
640
|
// 等最后一帧画完再卸载;卸载时 Ink 退出备用屏幕
|
|
536
641
|
await new Promise((r) => setTimeout(r, 50));
|
|
537
|
-
stdout.write(MOUSE_OFF);
|
|
642
|
+
stdout.write(MOUSE_OFF + CURSOR_RESET);
|
|
538
643
|
instance.unmount();
|
|
539
644
|
await instance.waitUntilExit();
|
|
540
645
|
process.off("exit", restore);
|
|
@@ -548,8 +653,103 @@ export function tuiScreen(opts: TuiOptions): Screen {
|
|
|
548
653
|
* SGR 鼠标事件(Ink 去掉了开头的 ESC):按钮号、列、行(从 1 开始),M 是按下、m 是松开。
|
|
549
654
|
* 按钮号去掉 Shift/Alt/Ctrl 位后 0 是左键,64 是滚轮向上,65 向下
|
|
550
655
|
*/
|
|
551
|
-
const MOUSE = /^\[<(\d+)
|
|
656
|
+
const MOUSE = /^\[<(\d+);(\d+);(\d+)([Mm])$/;
|
|
657
|
+
/** 解析鼠标事件:button 是去掉 Shift/Alt/Ctrl 位的按钮号,press 表示按下;x、y 是屏幕上的列和行(从 0 开始) */
|
|
658
|
+
function mouseEvent(input: string): { button: number; press: boolean; x: number; y: number } | undefined {
|
|
659
|
+
const m = MOUSE.exec(input);
|
|
660
|
+
if (!m) return undefined;
|
|
661
|
+
return { button: Number(m[1]) & ~0b11100, press: m[4] === "M", x: Number(m[2]) - 1, y: Number(m[3]) - 1 };
|
|
662
|
+
}
|
|
552
663
|
const WHEEL_LINES = 3;
|
|
664
|
+
|
|
665
|
+
/** 按住左键拖动时的按钮号(左键 0 加上移动位 32) */
|
|
666
|
+
const DRAG = 32;
|
|
667
|
+
/** "已复制"的提示显示多久 */
|
|
668
|
+
const COPIED_MS = 2000;
|
|
669
|
+
|
|
670
|
+
// ---------- 选中文字 ----------
|
|
671
|
+
|
|
672
|
+
/** 对话区里的位置:row 是整个对话(已完成的加上正在输出的)的第几行,col 按显示宽度(中文占两列) */
|
|
673
|
+
type Point = { row: number; col: number };
|
|
674
|
+
/** anchor 是按下左键的位置,focus 是拖到的位置;两端都包含在内 */
|
|
675
|
+
type Selection = { anchor: Point; focus: Point };
|
|
676
|
+
|
|
677
|
+
const same = (a: Point, b: Point) => a.row === b.row && a.col === b.col;
|
|
678
|
+
|
|
679
|
+
/** 第 row 行选中的列 [from, to);这一行不在选区里时 undefined */
|
|
680
|
+
function selectedCols(s: Selection, row: number): [number, number] | undefined {
|
|
681
|
+
const { anchor: a, focus: f } = s;
|
|
682
|
+
const [first, last] = a.row < f.row || (a.row === f.row && a.col <= f.col) ? [a, f] : [f, a];
|
|
683
|
+
if (row < first.row || row > last.row) return undefined;
|
|
684
|
+
return [row === first.row ? first.col : 0, row === last.row ? last.col + 1 : Infinity];
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
/** ANSI 转义序列(CSI,和以 BEL 或 ST 结束的 OSC,例如超链接) */
|
|
688
|
+
const ANSI = /\x1b\[[0-?]*[ -/]*[@-~]|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/y;
|
|
689
|
+
const INVERSE = "\x1b[7m";
|
|
690
|
+
const INVERSE_OFF = "\x1b[27m";
|
|
691
|
+
|
|
692
|
+
/**
|
|
693
|
+
* 逐个走过一行里的字符:visit 收到每个字符和它是否落在列 [from, to) 里(宽字符跨进去一半也算)。
|
|
694
|
+
* 转义序列原样交给 escape
|
|
695
|
+
*/
|
|
696
|
+
function walk(line: string, from: number, to: number, visit: (ch: string, inside: boolean) => void, escape?: (seq: string) => void) {
|
|
697
|
+
let col = 0;
|
|
698
|
+
for (let i = 0; i < line.length; ) {
|
|
699
|
+
ANSI.lastIndex = i;
|
|
700
|
+
const m = ANSI.exec(line);
|
|
701
|
+
if (m) {
|
|
702
|
+
escape?.(m[0]);
|
|
703
|
+
i += m[0].length;
|
|
704
|
+
continue;
|
|
705
|
+
}
|
|
706
|
+
const ch = String.fromCodePoint(line.codePointAt(i)!);
|
|
707
|
+
const w = charWidth(ch);
|
|
708
|
+
visit(ch, col < to && col + Math.max(w, 1) > from);
|
|
709
|
+
col += w;
|
|
710
|
+
i += ch.length;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
/** 给一行里选中的部分加上反色;中间的转义序列可能关掉反色,之后重新打开 */
|
|
715
|
+
function highlight(line: string, from: number, to: number): string {
|
|
716
|
+
let out = "";
|
|
717
|
+
let on = false;
|
|
718
|
+
walk(
|
|
719
|
+
line,
|
|
720
|
+
from,
|
|
721
|
+
to,
|
|
722
|
+
(ch, inside) => {
|
|
723
|
+
if (inside !== on) out += inside ? INVERSE : INVERSE_OFF;
|
|
724
|
+
on = inside;
|
|
725
|
+
out += ch;
|
|
726
|
+
},
|
|
727
|
+
(seq) => (out += seq + (on ? INVERSE : "")),
|
|
728
|
+
);
|
|
729
|
+
return out + (on ? INVERSE_OFF : "");
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
/** 选中的纯文字:每行去掉行尾空白,用换行连起来(自动折行的地方也是换行) */
|
|
733
|
+
function selectedText(s: Selection, row: (i: number) => string | undefined): string {
|
|
734
|
+
const lines: string[] = [];
|
|
735
|
+
const { anchor: a, focus: f } = s;
|
|
736
|
+
for (let r = Math.min(a.row, f.row); r <= Math.max(a.row, f.row); r++) {
|
|
737
|
+
const [from, to] = selectedCols(s, r)!;
|
|
738
|
+
let text = "";
|
|
739
|
+
walk(row(r) ?? "", from, to, (ch, inside) => void (inside && (text += ch)));
|
|
740
|
+
lines.push(text.trimEnd());
|
|
741
|
+
}
|
|
742
|
+
return lines.join("\n");
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
/** OSC 52:让终端把文字放进系统剪贴板(Windows Terminal、iTerm2、kitty、WezTerm 等支持) */
|
|
746
|
+
const osc52 = (text: string) => `\x1b]52;c;${Buffer.from(text, "utf8").toString("base64")}\x07`;
|
|
747
|
+
|
|
748
|
+
/**
|
|
749
|
+
* 输入框和对话框用:有选中的文字时 Ctrl+C 只取消选中(很多人选完习惯按 Ctrl+C 复制),
|
|
750
|
+
* 不中断模型、不清空输入、不取消对话框
|
|
751
|
+
*/
|
|
752
|
+
const SelectionContext = createContext<{ active(): boolean; clear(): void }>({ active: () => false, clear: () => {} });
|
|
553
753
|
/** 终端矮于这么多行时标题只占一行 */
|
|
554
754
|
const COMPACT_ROWS = 24;
|
|
555
755
|
|
|
@@ -562,19 +762,33 @@ const roomFor = (rows: number, max: number) =>
|
|
|
562
762
|
|
|
563
763
|
function App({
|
|
564
764
|
store,
|
|
765
|
+
copy,
|
|
565
766
|
version,
|
|
566
767
|
commands,
|
|
567
768
|
handlers,
|
|
568
769
|
history,
|
|
770
|
+
draft,
|
|
569
771
|
}: {
|
|
570
772
|
store: Store;
|
|
773
|
+
/** 把选中的文字放进剪贴板 */
|
|
774
|
+
copy(text: string): void;
|
|
571
775
|
version: string;
|
|
572
776
|
commands: CommandInfo[];
|
|
573
777
|
handlers: TuiHandlers;
|
|
574
778
|
history: string[];
|
|
779
|
+
draft: DraftBridge;
|
|
575
780
|
}) {
|
|
576
781
|
const state = useSyncExternalStore(store.subscribe, store.get);
|
|
577
782
|
const dialog = state.dialogs[0];
|
|
783
|
+
// 运行中每 100ms 整体重画一次,推动转圈动画。不能让 Spinner 自己重画:
|
|
784
|
+
// Ink 只在输入框也跟着提交的那一帧保留光标,否则把光标藏起来,运行中光标就不见了
|
|
785
|
+
const [, tick] = useReducer((n: number) => n + 1, 0);
|
|
786
|
+
const busy = state.busy !== undefined;
|
|
787
|
+
useEffect(() => {
|
|
788
|
+
if (!busy) return;
|
|
789
|
+
const t = setInterval(tick, 100);
|
|
790
|
+
return () => clearInterval(t);
|
|
791
|
+
}, [busy]);
|
|
578
792
|
const { columns, rows } = useScreenSize();
|
|
579
793
|
|
|
580
794
|
// 对话区占掉标题和底部之外的全部高度,只画能看见的那几行
|
|
@@ -584,6 +798,7 @@ function App({
|
|
|
584
798
|
const done = store.lines(width);
|
|
585
799
|
// 还没结束的流式块和正在运行的工具的进度:思考只显示最新的一行,进度每个工具一行
|
|
586
800
|
const live = [
|
|
801
|
+
...(state.live?.pending ?? []).flatMap((l) => mdRows(l, width)),
|
|
587
802
|
...(state.live?.text ? wrap(state.live.text, width) : []),
|
|
588
803
|
...state.progress.map((p) => oneRow(p.line, width).row),
|
|
589
804
|
];
|
|
@@ -604,6 +819,31 @@ function App({
|
|
|
604
819
|
];
|
|
605
820
|
const page = Math.max(1, view - 2);
|
|
606
821
|
|
|
822
|
+
// 选中文字:按下左键记下位置,拖动时更新,松开时复制;点一下(没拖动)还是展开或收起那一项。
|
|
823
|
+
// 几个事件可能在同一块输入里连着到达,来不及重画,所以另外用 ref 记着最新的值
|
|
824
|
+
const [selection, setSelectionState] = useState<Selection>();
|
|
825
|
+
const selected = useRef<Selection>(undefined);
|
|
826
|
+
const pressed = useRef<Point>(undefined);
|
|
827
|
+
const setSelection = (s: Selection | undefined) => {
|
|
828
|
+
selected.current = s;
|
|
829
|
+
setSelectionState(s);
|
|
830
|
+
};
|
|
831
|
+
const [selectionApi] = useState(() => ({ active: () => selected.current !== undefined, clear: () => setSelection(undefined) }));
|
|
832
|
+
// 宽度变了,行重新折过,原来的位置对不上了
|
|
833
|
+
useEffect(() => setSelection(undefined), [width]);
|
|
834
|
+
const rowAt = (i: number) => (i < done.length ? done[i] : live[i - done.length]);
|
|
835
|
+
// 复制之后在输入框下面提示一会儿
|
|
836
|
+
const [copied, setCopied] = useState<string>();
|
|
837
|
+
const copiedTimer = useRef<ReturnType<typeof setTimeout>>(undefined);
|
|
838
|
+
useEffect(() => () => clearTimeout(copiedTimer.current), []);
|
|
839
|
+
const copySelection = (s: Selection) => {
|
|
840
|
+
const text = selectedText(s, rowAt);
|
|
841
|
+
copy(text);
|
|
842
|
+
clearTimeout(copiedTimer.current);
|
|
843
|
+
setCopied(`Copied ${[...text].length} characters to the clipboard`);
|
|
844
|
+
copiedTimer.current = setTimeout(() => setCopied(undefined), COPIED_MS);
|
|
845
|
+
};
|
|
846
|
+
|
|
607
847
|
// Ink 增量渲染只用相对光标移动,第一帧起点一旦偏了(启动时终端尺寸还没稳定、窗口缩放、别的输出插进来),
|
|
608
848
|
// 之后每帧都会画错行。启动后、尺寸变化后和按 Ctrl+L 时让一帧比终端高,Ink 会清屏从左上角整帧重画(包在同步输出里),下一帧再恢复
|
|
609
849
|
const [redraws, setRedraws] = useState(0);
|
|
@@ -621,19 +861,38 @@ function App({
|
|
|
621
861
|
|
|
622
862
|
useInput((input, key) => {
|
|
623
863
|
if (key.ctrl && input === "l") return setRedraws((n) => n + 1);
|
|
624
|
-
|
|
864
|
+
// 展开或收起最近的一项(工具结果、思考、检查报告),鼠标点不了时也能看完整内容
|
|
865
|
+
if (key.ctrl && input === "o") return store.toggleLatest(start, view);
|
|
866
|
+
const mouse = mouseEvent(input);
|
|
625
867
|
if (mouse) {
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
868
|
+
// 对话区里的位置;clamp 时拖出对话区停在第一行或最后一行,否则在对话区外返回 undefined
|
|
869
|
+
const at = (clamp: boolean): Point | undefined => {
|
|
870
|
+
const i = mouse.y - top;
|
|
871
|
+
if (!clamp && (i < 0 || i >= shown.length)) return undefined;
|
|
872
|
+
return { row: start + Math.min(Math.max(i, 0), shown.length - 1), col: Math.max(0, mouse.x - 1) };
|
|
873
|
+
};
|
|
874
|
+
if (mouse.button === 64) store.scrollBy(WHEEL_LINES, max);
|
|
875
|
+
else if (mouse.button === 65) store.scrollBy(-WHEEL_LINES, max);
|
|
876
|
+
else if (mouse.button === 0 && mouse.press) {
|
|
877
|
+
if (selected.current) setSelection(undefined);
|
|
878
|
+
pressed.current = at(false);
|
|
879
|
+
} else if (mouse.button === DRAG && pressed.current) {
|
|
880
|
+
setSelection({ anchor: pressed.current, focus: at(true)! });
|
|
881
|
+
} else if (mouse.button === 0 && !mouse.press && pressed.current) {
|
|
882
|
+
const p = pressed.current;
|
|
883
|
+
pressed.current = undefined;
|
|
884
|
+
const s = selected.current;
|
|
885
|
+
if (s && !same(s.anchor, s.focus)) copySelection(s);
|
|
886
|
+
else {
|
|
887
|
+
if (s) setSelection(undefined);
|
|
888
|
+
// 点在对话区里已经完成的输出上:展开或收起那一项
|
|
889
|
+
if (p.row < done.length) store.toggle(p.row, start, view);
|
|
890
|
+
}
|
|
634
891
|
}
|
|
635
892
|
return;
|
|
636
893
|
}
|
|
894
|
+
// 按别的键取消选中;Ctrl+C 由输入框和对话框处理(见 SelectionContext)
|
|
895
|
+
if (selected.current && !(key.ctrl && input === "c")) setSelection(undefined);
|
|
637
896
|
// 选择框自己用 PgUp/PgDn 翻选项
|
|
638
897
|
if (dialog?.kind === "select") return;
|
|
639
898
|
if (key.pageUp) store.scrollBy(page, max);
|
|
@@ -646,6 +905,7 @@ function App({
|
|
|
646
905
|
// 重画那一帧上下各垫一行空白:终端把顶上那行滚出去后,看到的和平时逐行一样,所以不会闪。
|
|
647
906
|
// 只做纵向裁剪:每行都已按宽度折好或截断,横向裁剪会让 Ink 每帧对每一行再切一遍 ANSI 字符串
|
|
648
907
|
return (
|
|
908
|
+
<SelectionContext.Provider value={selectionApi}>
|
|
649
909
|
<Box
|
|
650
910
|
flexDirection="column"
|
|
651
911
|
width={columns}
|
|
@@ -669,11 +929,14 @@ function App({
|
|
|
669
929
|
overflowY="hidden"
|
|
670
930
|
paddingX={1}
|
|
671
931
|
>
|
|
672
|
-
{shown.map((l, i) =>
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
932
|
+
{shown.map((l, i) => {
|
|
933
|
+
const cols = selection && selectedCols(selection, start + i);
|
|
934
|
+
return (
|
|
935
|
+
<Text key={i} wrap="truncate-end">
|
|
936
|
+
{cols ? highlight(l || " ", ...cols) : l || " "}
|
|
937
|
+
</Text>
|
|
938
|
+
);
|
|
939
|
+
})}
|
|
677
940
|
{scrolled ? (
|
|
678
941
|
<Text dimColor wrap="truncate-end">
|
|
679
942
|
{`── ↓ ${scroll} more lines · wheel / PgDn to scroll · ctrl+end to jump to the bottom ──`}
|
|
@@ -701,14 +964,17 @@ function App({
|
|
|
701
964
|
commands={commands}
|
|
702
965
|
handlers={handlers}
|
|
703
966
|
history={history}
|
|
967
|
+
bridge={draft}
|
|
704
968
|
busy={Boolean(state.busy)}
|
|
705
969
|
active={!dialog}
|
|
706
970
|
onSubmit={() => store.scrollBy(-max, max)}
|
|
971
|
+
flash={copied}
|
|
707
972
|
/>
|
|
708
973
|
<StatusBar status={state.status} width={Math.max(10, columns - 2)} />
|
|
709
974
|
</Box>
|
|
710
975
|
</Box>
|
|
711
976
|
</Box>
|
|
977
|
+
</SelectionContext.Provider>
|
|
712
978
|
);
|
|
713
979
|
}
|
|
714
980
|
|
|
@@ -801,12 +1067,9 @@ function dialogKey(d: Dialog): number {
|
|
|
801
1067
|
|
|
802
1068
|
const FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
803
1069
|
|
|
1070
|
+
/** 转圈动画由 App 每 100ms 整体重画一次推动(见 App 里的 tick),这里只按当前时间画 */
|
|
804
1071
|
function Spinner({ label, since }: { label: string; since: number }) {
|
|
805
|
-
const
|
|
806
|
-
useEffect(() => {
|
|
807
|
-
const t = setInterval(() => setNow(Date.now()), 100);
|
|
808
|
-
return () => clearInterval(t);
|
|
809
|
-
}, []);
|
|
1072
|
+
const now = Date.now();
|
|
810
1073
|
const seconds = Math.floor((now - since) / 1000);
|
|
811
1074
|
return (
|
|
812
1075
|
<Box marginTop={1}>
|
|
@@ -861,13 +1124,11 @@ function StatusBar({ status, width }: { status: StatusInfo; width: number }) {
|
|
|
861
1124
|
const modeColor =
|
|
862
1125
|
status.mode === "yolo"
|
|
863
1126
|
? "red"
|
|
864
|
-
: status.mode === "auto
|
|
1127
|
+
: status.mode === "auto"
|
|
865
1128
|
? "yellow"
|
|
866
|
-
: status.mode === "
|
|
867
|
-
? "
|
|
868
|
-
:
|
|
869
|
-
? "magenta"
|
|
870
|
-
: "green";
|
|
1129
|
+
: status.mode === "plan"
|
|
1130
|
+
? "magenta"
|
|
1131
|
+
: "green";
|
|
871
1132
|
const speed = status.speed;
|
|
872
1133
|
// 窄的时候依次:提示变短、去掉提示、用量变短、去掉缓存、模式变短、去掉用量、上下文变短、去掉速度
|
|
873
1134
|
const segments: Segment[] = [
|
|
@@ -909,9 +1170,11 @@ function StatusBar({ status, width }: { status: StatusInfo; width: number }) {
|
|
|
909
1170
|
},
|
|
910
1171
|
]
|
|
911
1172
|
: []),
|
|
1173
|
+
// 插件的段落:比提示晚、比缓存早去掉
|
|
1174
|
+
...(status.plugins ?? []).map((p) => ({ text: p.text, dropAt: 7.5, dim: true })),
|
|
912
1175
|
{
|
|
913
|
-
text: "PgUp/PgDn scroll · click ▸ to expand ·
|
|
914
|
-
short: "
|
|
1176
|
+
text: "PgUp/PgDn scroll · click ▸ or ctrl+o to expand · drag to select and copy",
|
|
1177
|
+
short: "ctrl+o to expand",
|
|
915
1178
|
shortAt: 10,
|
|
916
1179
|
dropAt: 9,
|
|
917
1180
|
dim: true,
|
|
@@ -1001,6 +1264,9 @@ function editKey(s: EditState, input: string, key: Key): EditState | undefined {
|
|
|
1001
1264
|
return text ? edit.insert(s, text) : undefined;
|
|
1002
1265
|
}
|
|
1003
1266
|
|
|
1267
|
+
/** 可编辑文本在屏幕上的位置:第一个字符的列 x、第一行可见行的行 y、可见的是折行后的第 first 行起的 rows 行 */
|
|
1268
|
+
type Placement = { x: number; y: number; first: number; rows: number } | undefined;
|
|
1269
|
+
|
|
1004
1270
|
/** 渲染可编辑文本并把终端光标放到输入位置(输入法的候选框跟着它) */
|
|
1005
1271
|
function EditableText({
|
|
1006
1272
|
state,
|
|
@@ -1010,7 +1276,10 @@ function EditableText({
|
|
|
1010
1276
|
placeholder,
|
|
1011
1277
|
active,
|
|
1012
1278
|
maxRows = Infinity,
|
|
1279
|
+
place,
|
|
1013
1280
|
}: {
|
|
1281
|
+
/** 每次渲染时写入文字的位置,点击换算光标用 */
|
|
1282
|
+
place?: { current: Placement };
|
|
1014
1283
|
state: EditState;
|
|
1015
1284
|
width: number;
|
|
1016
1285
|
prefix: string;
|
|
@@ -1023,7 +1292,6 @@ function EditableText({
|
|
|
1023
1292
|
const ref = useRef<DOMElement>(null);
|
|
1024
1293
|
// 只用来在布局变化后重画一次、重新计算光标位置;它给的是相对父元素的坐标,光标要的是相对整个输出
|
|
1025
1294
|
const metrics = useBoxMetrics(ref);
|
|
1026
|
-
const { setCursorPosition } = useCursor();
|
|
1027
1295
|
const shown = mask ? "•".repeat([...state.value].length) : state.value;
|
|
1028
1296
|
const cursor = mask
|
|
1029
1297
|
? [...state.value.slice(0, state.cursor)].length
|
|
@@ -1040,17 +1308,26 @@ function EditableText({
|
|
|
1040
1308
|
|
|
1041
1309
|
// 光标位置要在渲染时设:useCursor 在这次提交时交给 Ink,跟这一帧一起画出来。
|
|
1042
1310
|
// 放在 useEffect 里会晚一帧,光标总是停在上一次按键的位置(只移动光标时最明显)。
|
|
1043
|
-
//
|
|
1311
|
+
// 这里用的是上一次的布局,提交后要核对一遍:
|
|
1312
|
+
// - 输入框相对父元素挪了位置时 useBoxMetrics 会再触发一次渲染
|
|
1313
|
+
// - 只有祖先挪了位置时(窗口尺寸变化后整帧重画那一帧上下各垫一行,下一帧再去掉)它看不出来,
|
|
1314
|
+
// 光标会停在比输入行低一行的地方,要自己比一下整体位置
|
|
1044
1315
|
const at =
|
|
1045
1316
|
active && metrics.hasMeasured && ref.current
|
|
1046
1317
|
? origin(ref.current)
|
|
1047
1318
|
: undefined;
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
);
|
|
1319
|
+
const position = at && { x: at.x + prefix.length + col, y: at.y + row };
|
|
1320
|
+
if (place) place.current = at && { x: at.x + prefix.length, y: at.y, first, rows: rows.length };
|
|
1321
|
+
const [, recheck] = useReducer((n: number) => n + 1, 0);
|
|
1322
|
+
useEffect(() => {
|
|
1323
|
+
if (!at || !ref.current) return;
|
|
1324
|
+
const now = origin(ref.current);
|
|
1325
|
+
if (now.x !== at.x || now.y !== at.y) recheck();
|
|
1326
|
+
});
|
|
1051
1327
|
|
|
1052
1328
|
return (
|
|
1053
1329
|
<Box ref={ref} flexDirection="column">
|
|
1330
|
+
{active ? <CursorAt position={position} /> : null}
|
|
1054
1331
|
{state.value === "" && placeholder ? (
|
|
1055
1332
|
<Text wrap="truncate-end">
|
|
1056
1333
|
<Text color="cyan">{prefix}</Text>
|
|
@@ -1074,6 +1351,16 @@ function EditableText({
|
|
|
1074
1351
|
);
|
|
1075
1352
|
}
|
|
1076
1353
|
|
|
1354
|
+
/**
|
|
1355
|
+
* 终端光标只有一个,每个 useCursor 每次提交都会设一遍。没有焦点的输入框(弹出输入框时下面的主输入框)
|
|
1356
|
+
* 不能用它,否则它设的"不显示"会盖掉有焦点的那个,所以只在有焦点时挂上
|
|
1357
|
+
*/
|
|
1358
|
+
function CursorAt({ position }: { position: { x: number; y: number } | undefined }) {
|
|
1359
|
+
const { setCursorPosition } = useCursor();
|
|
1360
|
+
setCursorPosition(position);
|
|
1361
|
+
return null;
|
|
1362
|
+
}
|
|
1363
|
+
|
|
1077
1364
|
/** 元素相对整个动态输出(Static 之外的部分)左上角的位置 */
|
|
1078
1365
|
function origin(node: DOMElement): { x: number; y: number } {
|
|
1079
1366
|
let x = 0;
|
|
@@ -1089,18 +1376,25 @@ function Prompt({
|
|
|
1089
1376
|
commands,
|
|
1090
1377
|
handlers,
|
|
1091
1378
|
history: initial,
|
|
1379
|
+
bridge,
|
|
1092
1380
|
busy,
|
|
1093
1381
|
active,
|
|
1094
1382
|
onSubmit,
|
|
1383
|
+
flash,
|
|
1095
1384
|
}: {
|
|
1096
1385
|
commands: CommandInfo[];
|
|
1097
1386
|
handlers: TuiHandlers;
|
|
1098
1387
|
history: string[];
|
|
1388
|
+
/** 让 tuiScreen 的 draft 读写输入框 */
|
|
1389
|
+
bridge: DraftBridge;
|
|
1099
1390
|
busy: boolean;
|
|
1100
1391
|
active: boolean;
|
|
1101
1392
|
/** 提交后对话区回到底部 */
|
|
1102
1393
|
onSubmit(): void;
|
|
1394
|
+
/** 临时显示在输入框右上角的提示(例如"已复制") */
|
|
1395
|
+
flash?: string;
|
|
1103
1396
|
}) {
|
|
1397
|
+
const selection = useContext(SelectionContext);
|
|
1104
1398
|
const [current, setState] = useLive<EditState>(empty);
|
|
1105
1399
|
const [getSelected, setSelected] = useLive(0);
|
|
1106
1400
|
const [getNotice, setNotice] = useLive<string | undefined>(undefined);
|
|
@@ -1109,7 +1403,12 @@ function Prompt({
|
|
|
1109
1403
|
const browsing = useRef(-1);
|
|
1110
1404
|
const draft = useRef("");
|
|
1111
1405
|
const lastCtrlC = useRef(0);
|
|
1406
|
+
const lastEsc = useRef(0);
|
|
1407
|
+
/** 输入框文字在屏幕上的位置,点击时换算光标位置用(见 EditableText 的 place) */
|
|
1408
|
+
const place = useRef<Placement>(undefined);
|
|
1112
1409
|
const { columns, rows } = useScreenSize();
|
|
1410
|
+
/** 输入框里文字的宽度:边框、内边距和前面的 "› " 之外 */
|
|
1411
|
+
const textWidth = Math.max(10, columns - 5) - 2;
|
|
1113
1412
|
/** 补全菜单、搜索结果最多显示几项;输入框最多几行 */
|
|
1114
1413
|
const menuRows = roomFor(rows, MENTION_ROWS);
|
|
1115
1414
|
const inputRows = roomFor(rows, 10);
|
|
@@ -1206,18 +1505,28 @@ function Prompt({
|
|
|
1206
1505
|
setState(s);
|
|
1207
1506
|
setSelected(0);
|
|
1208
1507
|
setNotice(undefined);
|
|
1508
|
+
lastEsc.current = 0;
|
|
1209
1509
|
};
|
|
1210
|
-
const
|
|
1211
|
-
if (!text.trim()) return;
|
|
1510
|
+
const remember = (text: string) => {
|
|
1212
1511
|
history.current = [
|
|
1213
1512
|
...history.current.filter((h) => h !== text),
|
|
1214
1513
|
text,
|
|
1215
1514
|
].slice(-100);
|
|
1216
1515
|
browsing.current = -1;
|
|
1516
|
+
};
|
|
1517
|
+
const submit = (text: string) => {
|
|
1518
|
+
if (!text.trim()) return;
|
|
1519
|
+
remember(text);
|
|
1217
1520
|
set(empty);
|
|
1218
1521
|
onSubmit();
|
|
1219
1522
|
handlers.submit(text);
|
|
1220
1523
|
};
|
|
1524
|
+
/** 清空输入;清掉的内容放进(本次运行的)历史,按 ↑ 能找回来 */
|
|
1525
|
+
const discard = () => {
|
|
1526
|
+
const text = current().value;
|
|
1527
|
+
if (text.trim()) remember(text);
|
|
1528
|
+
set(empty);
|
|
1529
|
+
};
|
|
1221
1530
|
/** 用选中的路径替换正在输入的 @ 引用;目录不加空格,接着补全里面的文件 */
|
|
1222
1531
|
const complete = (p: string) => {
|
|
1223
1532
|
const { state, mention } = derive();
|
|
@@ -1230,6 +1539,7 @@ function Prompt({
|
|
|
1230
1539
|
cursor: mention!.start + text.length,
|
|
1231
1540
|
});
|
|
1232
1541
|
};
|
|
1542
|
+
bridge.current = { get: () => current().value, apply: (f) => set(f(current())) };
|
|
1233
1543
|
const recall = (i: number) => {
|
|
1234
1544
|
browsing.current = i;
|
|
1235
1545
|
const value =
|
|
@@ -1286,6 +1596,7 @@ function Prompt({
|
|
|
1286
1596
|
|
|
1287
1597
|
useInput(
|
|
1288
1598
|
(input, key) => {
|
|
1599
|
+
if (key.ctrl && input === "c" && selection.active()) return selection.clear();
|
|
1289
1600
|
const search = getSearch();
|
|
1290
1601
|
if (search) return searchKey(search, input, key);
|
|
1291
1602
|
const state = current();
|
|
@@ -1295,14 +1606,30 @@ function Prompt({
|
|
|
1295
1606
|
: undefined;
|
|
1296
1607
|
if (key.ctrl && input === "c") {
|
|
1297
1608
|
if (busy) return handlers.interrupt();
|
|
1298
|
-
if (state.value) return
|
|
1609
|
+
if (state.value) return discard();
|
|
1299
1610
|
if (Date.now() - lastCtrlC.current < 2000) return handlers.exit();
|
|
1300
1611
|
lastCtrlC.current = Date.now();
|
|
1301
1612
|
return setNotice("Press Ctrl+C again to exit");
|
|
1302
1613
|
}
|
|
1303
1614
|
if (key.escape) {
|
|
1304
1615
|
if (busy) return handlers.interrupt();
|
|
1305
|
-
|
|
1616
|
+
if (!state.value) return;
|
|
1617
|
+
// 误按一下 Esc 不清掉写了半天的内容:连按两次才清空
|
|
1618
|
+
if (Date.now() - lastEsc.current < 1500) return discard();
|
|
1619
|
+
setNotice("Press Esc again to clear the input");
|
|
1620
|
+
lastEsc.current = Date.now();
|
|
1621
|
+
return;
|
|
1622
|
+
}
|
|
1623
|
+
// 点击输入框里的文字:光标移到点的位置
|
|
1624
|
+
const mouse = mouseEvent(input);
|
|
1625
|
+
if (mouse) {
|
|
1626
|
+
const at = place.current;
|
|
1627
|
+
if (!at || mouse.button !== 0 || !mouse.press) return;
|
|
1628
|
+
const row = mouse.y - at.y;
|
|
1629
|
+
if (row < 0 || row >= at.rows) return;
|
|
1630
|
+
const cursor = offsetAt(state.value, textWidth, at.first + row, mouse.x - at.x);
|
|
1631
|
+
if (cursor !== undefined) setState({ ...state, cursor });
|
|
1632
|
+
return;
|
|
1306
1633
|
}
|
|
1307
1634
|
// 文字和回车在同一块里到达(输入很快时终端可能合并):插入文字后发送
|
|
1308
1635
|
if (
|
|
@@ -1338,7 +1665,9 @@ function Prompt({
|
|
|
1338
1665
|
if (key.upArrow) {
|
|
1339
1666
|
const { menu, selected } = derive();
|
|
1340
1667
|
if (menu) return setSelected((selected - 1 + menu) % menu);
|
|
1341
|
-
|
|
1668
|
+
// 按折行后的屏幕行移动:一大段没有换行的文字也能上下走,到了第一行才翻历史
|
|
1669
|
+
const up = moveRow(state, textWidth, -1);
|
|
1670
|
+
if (up) return setState(up);
|
|
1342
1671
|
if (browsing.current + 1 < history.current.length) {
|
|
1343
1672
|
if (browsing.current === -1) draft.current = state.value;
|
|
1344
1673
|
recall(browsing.current + 1);
|
|
@@ -1348,10 +1677,13 @@ function Prompt({
|
|
|
1348
1677
|
if (key.downArrow) {
|
|
1349
1678
|
const { menu, selected } = derive();
|
|
1350
1679
|
if (menu) return setSelected((selected + 1) % menu);
|
|
1351
|
-
|
|
1680
|
+
const down = moveRow(state, textWidth, 1);
|
|
1681
|
+
if (down) return setState(down);
|
|
1352
1682
|
if (browsing.current >= 0) recall(browsing.current - 1);
|
|
1353
1683
|
return;
|
|
1354
1684
|
}
|
|
1685
|
+
const spec = keySpec(input, key);
|
|
1686
|
+
if (spec && handlers.key?.(spec)) return;
|
|
1355
1687
|
const next = editKey(state, input, key);
|
|
1356
1688
|
if (next) set(next);
|
|
1357
1689
|
},
|
|
@@ -1393,6 +1725,7 @@ function Prompt({
|
|
|
1393
1725
|
}
|
|
1394
1726
|
active={active}
|
|
1395
1727
|
maxRows={inputRows}
|
|
1728
|
+
place={place}
|
|
1396
1729
|
/>
|
|
1397
1730
|
)}
|
|
1398
1731
|
</Box>
|
|
@@ -1474,6 +1807,12 @@ function Prompt({
|
|
|
1474
1807
|
) : notice ? (
|
|
1475
1808
|
<Text color="yellow"> {notice}</Text>
|
|
1476
1809
|
) : null}
|
|
1810
|
+
{/* 压在输入框上边框的右边,不占行,出现和消失时下面的内容不跳 */}
|
|
1811
|
+
{flash ? (
|
|
1812
|
+
<Box position="absolute" top={0} right={2}>
|
|
1813
|
+
<Text color="green"> {flash} </Text>
|
|
1814
|
+
</Box>
|
|
1815
|
+
) : null}
|
|
1477
1816
|
</Box>
|
|
1478
1817
|
);
|
|
1479
1818
|
}
|
|
@@ -1496,6 +1835,7 @@ function SelectDialog({
|
|
|
1496
1835
|
}: {
|
|
1497
1836
|
dialog: Extract<Dialog, { kind: "select" }>;
|
|
1498
1837
|
}) {
|
|
1838
|
+
const selection = useContext(SelectionContext);
|
|
1499
1839
|
const [getSelected, setSelected] = useLive(0);
|
|
1500
1840
|
const [getQuery, setQuery] = useLive("");
|
|
1501
1841
|
const { rows, columns } = useScreenSize();
|
|
@@ -1513,17 +1853,16 @@ function SelectDialog({
|
|
|
1513
1853
|
const selected = Math.min(getSelected(), Math.max(0, n - 1));
|
|
1514
1854
|
// 终端矮时少显示几项,加上预览一起不超过终端的高度
|
|
1515
1855
|
const visible = roomFor(rows, VISIBLE);
|
|
1516
|
-
// 预览(例如要写入的文件)过长时截断:整个界面高过终端时 Ink
|
|
1517
|
-
|
|
1856
|
+
// 预览(例如要写入的文件)过长时截断:整个界面高过终端时 Ink 会每帧清屏重画,底部的选项和提示也会被裁掉。
|
|
1857
|
+
// 按折行后的屏幕行数截断(一行长代码会占好几行),每一行单独显示、不再折行
|
|
1858
|
+
const lines = !dialog.body ? [] : wrap(dialog.body, Math.max(1, columns - 4));
|
|
1518
1859
|
const room = Math.max(3, rows - Math.min(n, visible) - 16);
|
|
1519
1860
|
const body =
|
|
1520
1861
|
lines.length > room
|
|
1521
|
-
? [
|
|
1522
|
-
|
|
1523
|
-
dim(`… ${lines.length - room} more lines`),
|
|
1524
|
-
].join("\n")
|
|
1525
|
-
: dialog.body;
|
|
1862
|
+
? [...lines.slice(0, room - 1), dim(`… ${lines.length - room + 1} more lines`)]
|
|
1863
|
+
: lines;
|
|
1526
1864
|
useInput((input, key) => {
|
|
1865
|
+
if (key.ctrl && input === "c" && selection.active()) return selection.clear();
|
|
1527
1866
|
const items = filtered();
|
|
1528
1867
|
const n = items.length;
|
|
1529
1868
|
const selected = Math.min(getSelected(), Math.max(0, n - 1));
|
|
@@ -1573,7 +1912,11 @@ function SelectDialog({
|
|
|
1573
1912
|
<Text bold wrap="truncate-end">
|
|
1574
1913
|
{dialog.title}
|
|
1575
1914
|
</Text>
|
|
1576
|
-
{body
|
|
1915
|
+
{body.map((l, i) => (
|
|
1916
|
+
<Text key={i} wrap="truncate-end">
|
|
1917
|
+
{l || " "}
|
|
1918
|
+
</Text>
|
|
1919
|
+
))}
|
|
1577
1920
|
{filtering ? (
|
|
1578
1921
|
<Text wrap="truncate-end">
|
|
1579
1922
|
<Text color="cyan">filter› </Text>
|
|
@@ -1621,6 +1964,7 @@ function InputDialog({
|
|
|
1621
1964
|
}: {
|
|
1622
1965
|
dialog: Extract<Dialog, { kind: "input" }>;
|
|
1623
1966
|
}) {
|
|
1967
|
+
const selection = useContext(SelectionContext);
|
|
1624
1968
|
const initial = dialog.initial ?? "";
|
|
1625
1969
|
const [current, setState] = useLive<EditState>({
|
|
1626
1970
|
value: initial,
|
|
@@ -1632,6 +1976,7 @@ function InputDialog({
|
|
|
1632
1976
|
setState(edit.insert(current(), text.replace(/\r?\n/g, ""))),
|
|
1633
1977
|
);
|
|
1634
1978
|
useInput((input, key) => {
|
|
1979
|
+
if (key.ctrl && input === "c" && selection.active()) return selection.clear();
|
|
1635
1980
|
const state = current();
|
|
1636
1981
|
if (key.return) return dialog.resolve(state.value.trim());
|
|
1637
1982
|
if (key.escape || (key.ctrl && input === "c"))
|