@uncarved/jade 0.1.2 → 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 +204 -198
- 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 +43 -6
- package/dist/distro.js +198 -48
- package/dist/distro.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/main.js +436 -300
- package/dist/main.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 +2 -0
- package/dist/plugins/policy.js +17 -1
- package/dist/plugins/policy.js.map +1 -1
- package/dist/plugins/prompts.d.ts +6 -1
- package/dist/plugins/prompts.js +103 -15
- 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 +2 -0
- package/dist/plugins/review.js +41 -3
- package/dist/plugins/review.js.map +1 -1
- 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 +56 -2
- package/dist/provider.js +73 -7
- 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 +6 -2
- package/dist/tools/task.js +36 -8
- 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 +91 -22
- 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 +6 -1
- package/dist/ui/markdown.js +57 -19
- package/dist/ui/markdown.js.map +1 -1
- package/dist/ui/render.d.ts +9 -10
- package/dist/ui/render.js +35 -72
- 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 +22 -2
- package/dist/ui/screen.js +104 -15
- package/dist/ui/screen.js.map +1 -1
- package/dist/ui/transcript.d.ts +13 -0
- package/dist/ui/transcript.js +105 -15
- package/dist/ui/transcript.js.map +1 -1
- package/dist/ui/tui.d.ts +2 -0
- package/dist/ui/tui.js +309 -58
- 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 +6 -5
- package/src/commands.ts +24 -0
- package/src/distro.ts +228 -51
- package/src/index.ts +2 -0
- package/src/main.ts +413 -292
- 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 +17 -1
- package/src/plugins/prompts.ts +108 -15
- package/src/plugins/repeats.ts +75 -14
- package/src/plugins/review.ts +40 -3
- package/src/plugins/sessions.ts +109 -0
- package/src/provider.ts +109 -10
- 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 +51 -11
- package/src/tools/web.ts +370 -140
- package/src/ui/configure.ts +90 -27
- package/src/ui/diff.ts +1 -2
- package/src/ui/editor.ts +50 -1
- package/src/ui/markdown.ts +258 -219
- package/src/ui/render.ts +35 -57
- package/src/ui/renderers.ts +183 -0
- package/src/ui/screen.ts +113 -19
- package/src/ui/transcript.ts +109 -16
- package/src/ui/tui.tsx +353 -58
- 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,8 +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";
|
|
29
|
-
import { markdownStream, type MdLine } from "./markdown.ts";
|
|
30
|
+
import { charWidth, edit, empty, layout, moveRow, offsetAt, type EditState } from "./editor.ts";
|
|
31
|
+
import { expandTabs, markdownStream, type MdLine } from "./markdown.ts";
|
|
30
32
|
import {
|
|
31
33
|
dim,
|
|
32
34
|
type Choice,
|
|
@@ -56,6 +58,20 @@ export interface TuiHandlers {
|
|
|
56
58
|
files?(): Promise<string[]>;
|
|
57
59
|
/** Shift+Tab:切换到下一个权限模式 */
|
|
58
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;
|
|
59
75
|
}
|
|
60
76
|
|
|
61
77
|
type Dialog =
|
|
@@ -93,9 +109,9 @@ interface UiState {
|
|
|
93
109
|
/** 对话最多保留这么多行(折行前),超出时丢掉最早的一段 */
|
|
94
110
|
const MAX_LINES = 10_000;
|
|
95
111
|
|
|
96
|
-
/**
|
|
112
|
+
/** 按显示宽度折行,保留颜色;每一项是屏幕上的一行。制表符先换成空格:按 0 列算宽度会对不齐、超宽 */
|
|
97
113
|
const wrap = (text: string, width: number) =>
|
|
98
|
-
wrapAnsi(text, width, { hard: true, trim: false }).split("\n");
|
|
114
|
+
wrapAnsi(expandTabs(text), width, { hard: true, trim: false }).split("\n");
|
|
99
115
|
|
|
100
116
|
/**
|
|
101
117
|
* 对话里的一项:普通的一行,或者可以点击展开、收起的一段。
|
|
@@ -341,6 +357,13 @@ class Store {
|
|
|
341
357
|
);
|
|
342
358
|
}
|
|
343
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
|
+
|
|
344
367
|
/** 往上(正数)或往下滚 n 行,不超过 max */
|
|
345
368
|
scrollBy(n: number, max: number) {
|
|
346
369
|
const scroll = Math.max(0, Math.min(max, this.state.scroll + n));
|
|
@@ -360,9 +383,12 @@ export interface TuiOptions {
|
|
|
360
383
|
io?: { stdin: NodeJS.ReadStream; stdout: NodeJS.WriteStream };
|
|
361
384
|
}
|
|
362
385
|
|
|
363
|
-
/**
|
|
364
|
-
|
|
365
|
-
|
|
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";
|
|
366
392
|
/**
|
|
367
393
|
* 光标样式:闪烁的竖线(DECSCUSR 5,再加上 ATT610 的"开始闪烁"给只认它的终端),一眼能看出输入框有焦点;
|
|
368
394
|
* 退出时还原成终端自己的默认样式。光标什么时候显示仍由 Ink 管(只有输入框或输入对话框有焦点时才显示)
|
|
@@ -370,9 +396,30 @@ const MOUSE_OFF = "\x1b[?1006l\x1b[?1000l";
|
|
|
370
396
|
const CURSOR_BLINK = "\x1b[5 q\x1b[?12h";
|
|
371
397
|
const CURSOR_RESET = "\x1b[0 q";
|
|
372
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
|
+
}
|
|
411
|
+
|
|
373
412
|
export function tuiScreen(opts: TuiOptions): Screen {
|
|
374
413
|
const stdout = opts.io?.stdout ?? process.stdout;
|
|
375
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;
|
|
376
423
|
|
|
377
424
|
/**
|
|
378
425
|
* 正在进行的思考:一开始就作为一项放进对话(可以点击展开),之后的增量攒起来,每隔一小段时间改写一次那一项。
|
|
@@ -412,7 +459,7 @@ export function tuiScreen(opts: TuiOptions): Screen {
|
|
|
412
459
|
const lines = live.text.split("\n");
|
|
413
460
|
const rest = all ? "" : lines.pop()!;
|
|
414
461
|
if (all && lines.at(-1) === "") lines.pop();
|
|
415
|
-
appendMd(lines.flatMap((l) => md.push(l)));
|
|
462
|
+
appendMd(lines.flatMap((l) => md.push(expandTabs(l))));
|
|
416
463
|
if (all) {
|
|
417
464
|
appendMd(md.flush());
|
|
418
465
|
md = markdownStream();
|
|
@@ -420,11 +467,15 @@ export function tuiScreen(opts: TuiOptions): Screen {
|
|
|
420
467
|
store.update({ live: rest || !all ? { text: rest, pending: md.pending() } : undefined });
|
|
421
468
|
};
|
|
422
469
|
|
|
423
|
-
|
|
470
|
+
/** signal 中止时关掉对话框,返回 undefined */
|
|
471
|
+
const ask = <T,>(dialog: Dialog, signal?: AbortSignal) =>
|
|
424
472
|
new Promise<T>((resolve) => {
|
|
473
|
+
if (signal?.aborted) return resolve(undefined as T);
|
|
474
|
+
const close = () => d.resolve(undefined);
|
|
425
475
|
const d = {
|
|
426
476
|
...dialog,
|
|
427
477
|
resolve: (v: unknown) => {
|
|
478
|
+
signal?.removeEventListener("abort", close);
|
|
428
479
|
store.update(
|
|
429
480
|
{ dialogs: store.state.dialogs.filter((x) => x !== d) },
|
|
430
481
|
true,
|
|
@@ -432,6 +483,7 @@ export function tuiScreen(opts: TuiOptions): Screen {
|
|
|
432
483
|
resolve(v as T);
|
|
433
484
|
},
|
|
434
485
|
} as Dialog;
|
|
486
|
+
signal?.addEventListener("abort", close, { once: true });
|
|
435
487
|
store.update({ dialogs: [...store.state.dialogs, d] }, true);
|
|
436
488
|
});
|
|
437
489
|
|
|
@@ -441,10 +493,12 @@ export function tuiScreen(opts: TuiOptions): Screen {
|
|
|
441
493
|
const instance = render(
|
|
442
494
|
<App
|
|
443
495
|
store={store}
|
|
496
|
+
copy={(text) => stdout.write(osc52(text))}
|
|
444
497
|
version={opts.version}
|
|
445
498
|
commands={opts.commands}
|
|
446
499
|
handlers={opts.handlers}
|
|
447
500
|
history={opts.history ?? []}
|
|
501
|
+
draft={draft}
|
|
448
502
|
/>,
|
|
449
503
|
{
|
|
450
504
|
exitOnCtrlC: false,
|
|
@@ -526,9 +580,15 @@ export function tuiScreen(opts: TuiOptions): Screen {
|
|
|
526
580
|
},
|
|
527
581
|
busy(label) {
|
|
528
582
|
const current = store.state.busy;
|
|
529
|
-
if (label === undefined)
|
|
530
|
-
|
|
531
|
-
|
|
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() } });
|
|
532
592
|
},
|
|
533
593
|
status(info) {
|
|
534
594
|
store.update({ status: { ...store.state.status, ...info } });
|
|
@@ -536,21 +596,37 @@ export function tuiScreen(opts: TuiOptions): Screen {
|
|
|
536
596
|
queued(items) {
|
|
537
597
|
store.update({ queued: [...items] });
|
|
538
598
|
},
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
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
|
+
),
|
|
554
630
|
clear() {
|
|
555
631
|
clearTimeout(thinking?.timer);
|
|
556
632
|
thinking = undefined;
|
|
@@ -558,6 +634,7 @@ export function tuiScreen(opts: TuiOptions): Screen {
|
|
|
558
634
|
store.clear();
|
|
559
635
|
},
|
|
560
636
|
async close() {
|
|
637
|
+
clearTimeout(doneTimer);
|
|
561
638
|
flushLive(true);
|
|
562
639
|
store.update({ live: undefined, busy: undefined, progress: [] }, true);
|
|
563
640
|
// 等最后一帧画完再卸载;卸载时 Ink 退出备用屏幕
|
|
@@ -576,8 +653,103 @@ export function tuiScreen(opts: TuiOptions): Screen {
|
|
|
576
653
|
* SGR 鼠标事件(Ink 去掉了开头的 ESC):按钮号、列、行(从 1 开始),M 是按下、m 是松开。
|
|
577
654
|
* 按钮号去掉 Shift/Alt/Ctrl 位后 0 是左键,64 是滚轮向上,65 向下
|
|
578
655
|
*/
|
|
579
|
-
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
|
+
}
|
|
580
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: () => {} });
|
|
581
753
|
/** 终端矮于这么多行时标题只占一行 */
|
|
582
754
|
const COMPACT_ROWS = 24;
|
|
583
755
|
|
|
@@ -590,16 +762,21 @@ const roomFor = (rows: number, max: number) =>
|
|
|
590
762
|
|
|
591
763
|
function App({
|
|
592
764
|
store,
|
|
765
|
+
copy,
|
|
593
766
|
version,
|
|
594
767
|
commands,
|
|
595
768
|
handlers,
|
|
596
769
|
history,
|
|
770
|
+
draft,
|
|
597
771
|
}: {
|
|
598
772
|
store: Store;
|
|
773
|
+
/** 把选中的文字放进剪贴板 */
|
|
774
|
+
copy(text: string): void;
|
|
599
775
|
version: string;
|
|
600
776
|
commands: CommandInfo[];
|
|
601
777
|
handlers: TuiHandlers;
|
|
602
778
|
history: string[];
|
|
779
|
+
draft: DraftBridge;
|
|
603
780
|
}) {
|
|
604
781
|
const state = useSyncExternalStore(store.subscribe, store.get);
|
|
605
782
|
const dialog = state.dialogs[0];
|
|
@@ -642,6 +819,31 @@ function App({
|
|
|
642
819
|
];
|
|
643
820
|
const page = Math.max(1, view - 2);
|
|
644
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
|
+
|
|
645
847
|
// Ink 增量渲染只用相对光标移动,第一帧起点一旦偏了(启动时终端尺寸还没稳定、窗口缩放、别的输出插进来),
|
|
646
848
|
// 之后每帧都会画错行。启动后、尺寸变化后和按 Ctrl+L 时让一帧比终端高,Ink 会清屏从左上角整帧重画(包在同步输出里),下一帧再恢复
|
|
647
849
|
const [redraws, setRedraws] = useState(0);
|
|
@@ -659,19 +861,38 @@ function App({
|
|
|
659
861
|
|
|
660
862
|
useInput((input, key) => {
|
|
661
863
|
if (key.ctrl && input === "l") return setRedraws((n) => n + 1);
|
|
662
|
-
|
|
864
|
+
// 展开或收起最近的一项(工具结果、思考、检查报告),鼠标点不了时也能看完整内容
|
|
865
|
+
if (key.ctrl && input === "o") return store.toggleLatest(start, view);
|
|
866
|
+
const mouse = mouseEvent(input);
|
|
663
867
|
if (mouse) {
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
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
|
+
}
|
|
672
891
|
}
|
|
673
892
|
return;
|
|
674
893
|
}
|
|
894
|
+
// 按别的键取消选中;Ctrl+C 由输入框和对话框处理(见 SelectionContext)
|
|
895
|
+
if (selected.current && !(key.ctrl && input === "c")) setSelection(undefined);
|
|
675
896
|
// 选择框自己用 PgUp/PgDn 翻选项
|
|
676
897
|
if (dialog?.kind === "select") return;
|
|
677
898
|
if (key.pageUp) store.scrollBy(page, max);
|
|
@@ -684,6 +905,7 @@ function App({
|
|
|
684
905
|
// 重画那一帧上下各垫一行空白:终端把顶上那行滚出去后,看到的和平时逐行一样,所以不会闪。
|
|
685
906
|
// 只做纵向裁剪:每行都已按宽度折好或截断,横向裁剪会让 Ink 每帧对每一行再切一遍 ANSI 字符串
|
|
686
907
|
return (
|
|
908
|
+
<SelectionContext.Provider value={selectionApi}>
|
|
687
909
|
<Box
|
|
688
910
|
flexDirection="column"
|
|
689
911
|
width={columns}
|
|
@@ -707,11 +929,14 @@ function App({
|
|
|
707
929
|
overflowY="hidden"
|
|
708
930
|
paddingX={1}
|
|
709
931
|
>
|
|
710
|
-
{shown.map((l, i) =>
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
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
|
+
})}
|
|
715
940
|
{scrolled ? (
|
|
716
941
|
<Text dimColor wrap="truncate-end">
|
|
717
942
|
{`── ↓ ${scroll} more lines · wheel / PgDn to scroll · ctrl+end to jump to the bottom ──`}
|
|
@@ -739,14 +964,17 @@ function App({
|
|
|
739
964
|
commands={commands}
|
|
740
965
|
handlers={handlers}
|
|
741
966
|
history={history}
|
|
967
|
+
bridge={draft}
|
|
742
968
|
busy={Boolean(state.busy)}
|
|
743
969
|
active={!dialog}
|
|
744
970
|
onSubmit={() => store.scrollBy(-max, max)}
|
|
971
|
+
flash={copied}
|
|
745
972
|
/>
|
|
746
973
|
<StatusBar status={state.status} width={Math.max(10, columns - 2)} />
|
|
747
974
|
</Box>
|
|
748
975
|
</Box>
|
|
749
976
|
</Box>
|
|
977
|
+
</SelectionContext.Provider>
|
|
750
978
|
);
|
|
751
979
|
}
|
|
752
980
|
|
|
@@ -942,9 +1170,11 @@ function StatusBar({ status, width }: { status: StatusInfo; width: number }) {
|
|
|
942
1170
|
},
|
|
943
1171
|
]
|
|
944
1172
|
: []),
|
|
1173
|
+
// 插件的段落:比提示晚、比缓存早去掉
|
|
1174
|
+
...(status.plugins ?? []).map((p) => ({ text: p.text, dropAt: 7.5, dim: true })),
|
|
945
1175
|
{
|
|
946
|
-
text: "PgUp/PgDn scroll · click ▸ to expand ·
|
|
947
|
-
short: "
|
|
1176
|
+
text: "PgUp/PgDn scroll · click ▸ or ctrl+o to expand · drag to select and copy",
|
|
1177
|
+
short: "ctrl+o to expand",
|
|
948
1178
|
shortAt: 10,
|
|
949
1179
|
dropAt: 9,
|
|
950
1180
|
dim: true,
|
|
@@ -1034,6 +1264,9 @@ function editKey(s: EditState, input: string, key: Key): EditState | undefined {
|
|
|
1034
1264
|
return text ? edit.insert(s, text) : undefined;
|
|
1035
1265
|
}
|
|
1036
1266
|
|
|
1267
|
+
/** 可编辑文本在屏幕上的位置:第一个字符的列 x、第一行可见行的行 y、可见的是折行后的第 first 行起的 rows 行 */
|
|
1268
|
+
type Placement = { x: number; y: number; first: number; rows: number } | undefined;
|
|
1269
|
+
|
|
1037
1270
|
/** 渲染可编辑文本并把终端光标放到输入位置(输入法的候选框跟着它) */
|
|
1038
1271
|
function EditableText({
|
|
1039
1272
|
state,
|
|
@@ -1043,7 +1276,10 @@ function EditableText({
|
|
|
1043
1276
|
placeholder,
|
|
1044
1277
|
active,
|
|
1045
1278
|
maxRows = Infinity,
|
|
1279
|
+
place,
|
|
1046
1280
|
}: {
|
|
1281
|
+
/** 每次渲染时写入文字的位置,点击换算光标用 */
|
|
1282
|
+
place?: { current: Placement };
|
|
1047
1283
|
state: EditState;
|
|
1048
1284
|
width: number;
|
|
1049
1285
|
prefix: string;
|
|
@@ -1081,6 +1317,7 @@ function EditableText({
|
|
|
1081
1317
|
? origin(ref.current)
|
|
1082
1318
|
: undefined;
|
|
1083
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 };
|
|
1084
1321
|
const [, recheck] = useReducer((n: number) => n + 1, 0);
|
|
1085
1322
|
useEffect(() => {
|
|
1086
1323
|
if (!at || !ref.current) return;
|
|
@@ -1139,18 +1376,25 @@ function Prompt({
|
|
|
1139
1376
|
commands,
|
|
1140
1377
|
handlers,
|
|
1141
1378
|
history: initial,
|
|
1379
|
+
bridge,
|
|
1142
1380
|
busy,
|
|
1143
1381
|
active,
|
|
1144
1382
|
onSubmit,
|
|
1383
|
+
flash,
|
|
1145
1384
|
}: {
|
|
1146
1385
|
commands: CommandInfo[];
|
|
1147
1386
|
handlers: TuiHandlers;
|
|
1148
1387
|
history: string[];
|
|
1388
|
+
/** 让 tuiScreen 的 draft 读写输入框 */
|
|
1389
|
+
bridge: DraftBridge;
|
|
1149
1390
|
busy: boolean;
|
|
1150
1391
|
active: boolean;
|
|
1151
1392
|
/** 提交后对话区回到底部 */
|
|
1152
1393
|
onSubmit(): void;
|
|
1394
|
+
/** 临时显示在输入框右上角的提示(例如"已复制") */
|
|
1395
|
+
flash?: string;
|
|
1153
1396
|
}) {
|
|
1397
|
+
const selection = useContext(SelectionContext);
|
|
1154
1398
|
const [current, setState] = useLive<EditState>(empty);
|
|
1155
1399
|
const [getSelected, setSelected] = useLive(0);
|
|
1156
1400
|
const [getNotice, setNotice] = useLive<string | undefined>(undefined);
|
|
@@ -1159,7 +1403,12 @@ function Prompt({
|
|
|
1159
1403
|
const browsing = useRef(-1);
|
|
1160
1404
|
const draft = useRef("");
|
|
1161
1405
|
const lastCtrlC = useRef(0);
|
|
1406
|
+
const lastEsc = useRef(0);
|
|
1407
|
+
/** 输入框文字在屏幕上的位置,点击时换算光标位置用(见 EditableText 的 place) */
|
|
1408
|
+
const place = useRef<Placement>(undefined);
|
|
1162
1409
|
const { columns, rows } = useScreenSize();
|
|
1410
|
+
/** 输入框里文字的宽度:边框、内边距和前面的 "› " 之外 */
|
|
1411
|
+
const textWidth = Math.max(10, columns - 5) - 2;
|
|
1163
1412
|
/** 补全菜单、搜索结果最多显示几项;输入框最多几行 */
|
|
1164
1413
|
const menuRows = roomFor(rows, MENTION_ROWS);
|
|
1165
1414
|
const inputRows = roomFor(rows, 10);
|
|
@@ -1256,18 +1505,28 @@ function Prompt({
|
|
|
1256
1505
|
setState(s);
|
|
1257
1506
|
setSelected(0);
|
|
1258
1507
|
setNotice(undefined);
|
|
1508
|
+
lastEsc.current = 0;
|
|
1259
1509
|
};
|
|
1260
|
-
const
|
|
1261
|
-
if (!text.trim()) return;
|
|
1510
|
+
const remember = (text: string) => {
|
|
1262
1511
|
history.current = [
|
|
1263
1512
|
...history.current.filter((h) => h !== text),
|
|
1264
1513
|
text,
|
|
1265
1514
|
].slice(-100);
|
|
1266
1515
|
browsing.current = -1;
|
|
1516
|
+
};
|
|
1517
|
+
const submit = (text: string) => {
|
|
1518
|
+
if (!text.trim()) return;
|
|
1519
|
+
remember(text);
|
|
1267
1520
|
set(empty);
|
|
1268
1521
|
onSubmit();
|
|
1269
1522
|
handlers.submit(text);
|
|
1270
1523
|
};
|
|
1524
|
+
/** 清空输入;清掉的内容放进(本次运行的)历史,按 ↑ 能找回来 */
|
|
1525
|
+
const discard = () => {
|
|
1526
|
+
const text = current().value;
|
|
1527
|
+
if (text.trim()) remember(text);
|
|
1528
|
+
set(empty);
|
|
1529
|
+
};
|
|
1271
1530
|
/** 用选中的路径替换正在输入的 @ 引用;目录不加空格,接着补全里面的文件 */
|
|
1272
1531
|
const complete = (p: string) => {
|
|
1273
1532
|
const { state, mention } = derive();
|
|
@@ -1280,6 +1539,7 @@ function Prompt({
|
|
|
1280
1539
|
cursor: mention!.start + text.length,
|
|
1281
1540
|
});
|
|
1282
1541
|
};
|
|
1542
|
+
bridge.current = { get: () => current().value, apply: (f) => set(f(current())) };
|
|
1283
1543
|
const recall = (i: number) => {
|
|
1284
1544
|
browsing.current = i;
|
|
1285
1545
|
const value =
|
|
@@ -1336,6 +1596,7 @@ function Prompt({
|
|
|
1336
1596
|
|
|
1337
1597
|
useInput(
|
|
1338
1598
|
(input, key) => {
|
|
1599
|
+
if (key.ctrl && input === "c" && selection.active()) return selection.clear();
|
|
1339
1600
|
const search = getSearch();
|
|
1340
1601
|
if (search) return searchKey(search, input, key);
|
|
1341
1602
|
const state = current();
|
|
@@ -1345,14 +1606,30 @@ function Prompt({
|
|
|
1345
1606
|
: undefined;
|
|
1346
1607
|
if (key.ctrl && input === "c") {
|
|
1347
1608
|
if (busy) return handlers.interrupt();
|
|
1348
|
-
if (state.value) return
|
|
1609
|
+
if (state.value) return discard();
|
|
1349
1610
|
if (Date.now() - lastCtrlC.current < 2000) return handlers.exit();
|
|
1350
1611
|
lastCtrlC.current = Date.now();
|
|
1351
1612
|
return setNotice("Press Ctrl+C again to exit");
|
|
1352
1613
|
}
|
|
1353
1614
|
if (key.escape) {
|
|
1354
1615
|
if (busy) return handlers.interrupt();
|
|
1355
|
-
|
|
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;
|
|
1356
1633
|
}
|
|
1357
1634
|
// 文字和回车在同一块里到达(输入很快时终端可能合并):插入文字后发送
|
|
1358
1635
|
if (
|
|
@@ -1388,7 +1665,9 @@ function Prompt({
|
|
|
1388
1665
|
if (key.upArrow) {
|
|
1389
1666
|
const { menu, selected } = derive();
|
|
1390
1667
|
if (menu) return setSelected((selected - 1 + menu) % menu);
|
|
1391
|
-
|
|
1668
|
+
// 按折行后的屏幕行移动:一大段没有换行的文字也能上下走,到了第一行才翻历史
|
|
1669
|
+
const up = moveRow(state, textWidth, -1);
|
|
1670
|
+
if (up) return setState(up);
|
|
1392
1671
|
if (browsing.current + 1 < history.current.length) {
|
|
1393
1672
|
if (browsing.current === -1) draft.current = state.value;
|
|
1394
1673
|
recall(browsing.current + 1);
|
|
@@ -1398,10 +1677,13 @@ function Prompt({
|
|
|
1398
1677
|
if (key.downArrow) {
|
|
1399
1678
|
const { menu, selected } = derive();
|
|
1400
1679
|
if (menu) return setSelected((selected + 1) % menu);
|
|
1401
|
-
|
|
1680
|
+
const down = moveRow(state, textWidth, 1);
|
|
1681
|
+
if (down) return setState(down);
|
|
1402
1682
|
if (browsing.current >= 0) recall(browsing.current - 1);
|
|
1403
1683
|
return;
|
|
1404
1684
|
}
|
|
1685
|
+
const spec = keySpec(input, key);
|
|
1686
|
+
if (spec && handlers.key?.(spec)) return;
|
|
1405
1687
|
const next = editKey(state, input, key);
|
|
1406
1688
|
if (next) set(next);
|
|
1407
1689
|
},
|
|
@@ -1443,6 +1725,7 @@ function Prompt({
|
|
|
1443
1725
|
}
|
|
1444
1726
|
active={active}
|
|
1445
1727
|
maxRows={inputRows}
|
|
1728
|
+
place={place}
|
|
1446
1729
|
/>
|
|
1447
1730
|
)}
|
|
1448
1731
|
</Box>
|
|
@@ -1524,6 +1807,12 @@ function Prompt({
|
|
|
1524
1807
|
) : notice ? (
|
|
1525
1808
|
<Text color="yellow"> {notice}</Text>
|
|
1526
1809
|
) : null}
|
|
1810
|
+
{/* 压在输入框上边框的右边,不占行,出现和消失时下面的内容不跳 */}
|
|
1811
|
+
{flash ? (
|
|
1812
|
+
<Box position="absolute" top={0} right={2}>
|
|
1813
|
+
<Text color="green"> {flash} </Text>
|
|
1814
|
+
</Box>
|
|
1815
|
+
) : null}
|
|
1527
1816
|
</Box>
|
|
1528
1817
|
);
|
|
1529
1818
|
}
|
|
@@ -1546,6 +1835,7 @@ function SelectDialog({
|
|
|
1546
1835
|
}: {
|
|
1547
1836
|
dialog: Extract<Dialog, { kind: "select" }>;
|
|
1548
1837
|
}) {
|
|
1838
|
+
const selection = useContext(SelectionContext);
|
|
1549
1839
|
const [getSelected, setSelected] = useLive(0);
|
|
1550
1840
|
const [getQuery, setQuery] = useLive("");
|
|
1551
1841
|
const { rows, columns } = useScreenSize();
|
|
@@ -1563,17 +1853,16 @@ function SelectDialog({
|
|
|
1563
1853
|
const selected = Math.min(getSelected(), Math.max(0, n - 1));
|
|
1564
1854
|
// 终端矮时少显示几项,加上预览一起不超过终端的高度
|
|
1565
1855
|
const visible = roomFor(rows, VISIBLE);
|
|
1566
|
-
// 预览(例如要写入的文件)过长时截断:整个界面高过终端时 Ink
|
|
1567
|
-
|
|
1856
|
+
// 预览(例如要写入的文件)过长时截断:整个界面高过终端时 Ink 会每帧清屏重画,底部的选项和提示也会被裁掉。
|
|
1857
|
+
// 按折行后的屏幕行数截断(一行长代码会占好几行),每一行单独显示、不再折行
|
|
1858
|
+
const lines = !dialog.body ? [] : wrap(dialog.body, Math.max(1, columns - 4));
|
|
1568
1859
|
const room = Math.max(3, rows - Math.min(n, visible) - 16);
|
|
1569
1860
|
const body =
|
|
1570
1861
|
lines.length > room
|
|
1571
|
-
? [
|
|
1572
|
-
|
|
1573
|
-
dim(`… ${lines.length - room} more lines`),
|
|
1574
|
-
].join("\n")
|
|
1575
|
-
: dialog.body;
|
|
1862
|
+
? [...lines.slice(0, room - 1), dim(`… ${lines.length - room + 1} more lines`)]
|
|
1863
|
+
: lines;
|
|
1576
1864
|
useInput((input, key) => {
|
|
1865
|
+
if (key.ctrl && input === "c" && selection.active()) return selection.clear();
|
|
1577
1866
|
const items = filtered();
|
|
1578
1867
|
const n = items.length;
|
|
1579
1868
|
const selected = Math.min(getSelected(), Math.max(0, n - 1));
|
|
@@ -1623,7 +1912,11 @@ function SelectDialog({
|
|
|
1623
1912
|
<Text bold wrap="truncate-end">
|
|
1624
1913
|
{dialog.title}
|
|
1625
1914
|
</Text>
|
|
1626
|
-
{body
|
|
1915
|
+
{body.map((l, i) => (
|
|
1916
|
+
<Text key={i} wrap="truncate-end">
|
|
1917
|
+
{l || " "}
|
|
1918
|
+
</Text>
|
|
1919
|
+
))}
|
|
1627
1920
|
{filtering ? (
|
|
1628
1921
|
<Text wrap="truncate-end">
|
|
1629
1922
|
<Text color="cyan">filter› </Text>
|
|
@@ -1671,6 +1964,7 @@ function InputDialog({
|
|
|
1671
1964
|
}: {
|
|
1672
1965
|
dialog: Extract<Dialog, { kind: "input" }>;
|
|
1673
1966
|
}) {
|
|
1967
|
+
const selection = useContext(SelectionContext);
|
|
1674
1968
|
const initial = dialog.initial ?? "";
|
|
1675
1969
|
const [current, setState] = useLive<EditState>({
|
|
1676
1970
|
value: initial,
|
|
@@ -1682,6 +1976,7 @@ function InputDialog({
|
|
|
1682
1976
|
setState(edit.insert(current(), text.replace(/\r?\n/g, ""))),
|
|
1683
1977
|
);
|
|
1684
1978
|
useInput((input, key) => {
|
|
1979
|
+
if (key.ctrl && input === "c" && selection.active()) return selection.clear();
|
|
1685
1980
|
const state = current();
|
|
1686
1981
|
if (key.return) return dialog.resolve(state.value.trim());
|
|
1687
1982
|
if (key.escape || (key.ctrl && input === "c"))
|