@uncarved/jade 0.2.0 → 0.3.0
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 +277 -0
- package/assets/logo.svg +1 -0
- package/bin/jade.js +15 -0
- package/dist/api.d.ts +5 -250
- package/dist/api.js +5 -7
- package/dist/api.js.map +1 -1
- package/dist/fetch.d.ts +8 -0
- package/dist/fetch.js +19 -17
- package/dist/fetch.js.map +1 -1
- package/dist/host.d.ts +12 -1
- package/dist/host.js +49 -1
- package/dist/host.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/main.js +28 -5
- package/dist/main.js.map +1 -1
- package/dist/remote.js +4 -0
- package/dist/remote.js.map +1 -1
- package/dist/session.d.ts +2 -0
- package/dist/session.js +133 -13
- package/dist/session.js.map +1 -1
- package/dist/source.d.ts +2 -10
- package/dist/source.js.map +1 -1
- package/dist/update.d.ts +8 -20
- package/dist/update.js +12 -1
- package/dist/update.js.map +1 -1
- package/package.json +6 -4
- package/plugins/approval/index.ts +246 -84
- package/plugins/approval/plugin.json +5 -1
- package/plugins/approval/policy.ts +27 -6
- package/plugins/approval/review.ts +1 -1
- package/plugins/ask/index.ts +2 -2
- package/plugins/budget/index.ts +1 -1
- package/plugins/checks/checks.ts +3 -2
- package/plugins/checks/index.ts +2 -5
- package/plugins/commands/index.ts +2 -44
- package/plugins/compact/compact.ts +15 -4
- package/plugins/compact/index.ts +13 -16
- package/plugins/continue/index.ts +1 -1
- package/plugins/cost/index.ts +143 -0
- package/plugins/cost/plugin.json +13 -0
- package/plugins/custom-commands/index.ts +85 -0
- package/plugins/custom-commands/plugin.json +8 -0
- package/plugins/editor/index.ts +88 -0
- package/plugins/editor/plugin.json +17 -0
- package/plugins/fs/fs.ts +110 -17
- package/plugins/fs/grep.ts +41 -11
- package/plugins/fs/index.ts +9 -7
- package/plugins/fs/renderers.ts +1 -1
- package/plugins/hooks/index.ts +152 -0
- package/plugins/hooks/plugin.json +14 -0
- package/plugins/keys/index.ts +2 -18
- package/plugins/mcp/index.ts +40 -12
- package/plugins/mcp/install.ts +139 -0
- package/plugins/mcp/mcp.ts +2 -1
- package/plugins/mcp/plugin.json +1 -1
- package/plugins/mcp/project.ts +37 -1
- package/plugins/memory/index.ts +4 -9
- package/plugins/memory/plugin.json +1 -1
- package/plugins/mentions/index.ts +33 -13
- package/plugins/models/configure.ts +258 -150
- package/plugins/models/context.ts +2 -2
- package/plugins/models/index.ts +50 -42
- package/plugins/models/plugin.json +1 -1
- package/plugins/models/provider.ts +22 -4
- package/plugins/models/saved.ts +8 -4
- package/plugins/modes/index.ts +2 -17
- package/plugins/plugins/index.ts +2 -2
- package/plugins/print/index.ts +60 -12
- package/plugins/print/plugin.json +1 -0
- package/plugins/prompts/index.ts +50 -14
- package/plugins/{memory → prompts}/notes.ts +21 -9
- package/plugins/prompts/plugin.json +8 -2
- package/plugins/prompts/prompts.ts +14 -8
- package/plugins/prompts/results.ts +63 -0
- package/plugins/prompts/sys.ts +16 -4
- package/plugins/renderers/index.ts +2 -43
- package/plugins/repeats/index.ts +1 -1
- package/plugins/schedule/index.ts +2 -2
- package/plugins/scratch/index.ts +2 -3
- package/plugins/screenshot/index.ts +26 -13
- package/plugins/screenshot/plugin.json +1 -1
- package/plugins/sessions/index.ts +2 -17
- package/plugins/shell/index.ts +56 -6
- package/plugins/shell/plugin.json +3 -1
- package/plugins/shell/renderers.ts +1 -1
- package/plugins/shell/service.ts +2 -13
- package/plugins/shell/shell.ts +31 -16
- package/plugins/skills/index.ts +172 -37
- package/plugins/skills/install.ts +212 -0
- package/plugins/skills/plugin.json +3 -2
- package/plugins/skills/project.ts +38 -1
- package/plugins/skills/skills.ts +28 -9
- package/plugins/steer/index.ts +142 -0
- package/plugins/steer/plugin.json +8 -0
- package/plugins/suggest/index.ts +16 -11
- package/plugins/suggest/plugin.json +1 -1
- package/plugins/task/index.ts +8 -16
- package/plugins/task/plugin.json +9 -2
- package/plugins/task/subagent.ts +119 -15
- package/plugins/todo/index.ts +4 -4
- package/plugins/tui/editor.ts +10 -2
- package/plugins/tui/frontend.ts +130 -25
- package/plugins/tui/index.ts +3 -8
- package/plugins/tui/markdown.ts +51 -6
- package/plugins/tui/mascot.ts +505 -0
- package/plugins/tui/mentions.ts +48 -8
- package/plugins/tui/plugin.json +2 -2
- package/plugins/tui/screen.ts +6 -0
- package/plugins/tui/transcript.ts +22 -3
- package/plugins/tui/tui.ts +323 -95
- package/plugins/tui/vendor.ts +16 -0
- package/plugins/undo/checkpoint.ts +26 -2
- package/plugins/undo/index.ts +86 -24
- package/plugins/undo/plugin.json +8 -2
- package/plugins/undo/snapshot.ts +129 -0
- package/plugins/update/index.ts +11 -15
- package/plugins/update/plugin.json +2 -1
- package/plugins/web/index.ts +2 -5
- package/plugins/web/renderers.ts +1 -1
- package/plugins/web/web.ts +6 -5
- package/plugins-dist/approval/index.js +232 -50
- package/plugins-dist/approval/plugin.json +5 -1
- package/plugins-dist/approval/policy.js +27 -5
- package/plugins-dist/ask/index.js +1 -1
- package/plugins-dist/budget/index.js +1 -1
- package/plugins-dist/checks/checks.js +2 -1
- package/plugins-dist/checks/index.js +1 -1
- package/plugins-dist/commands/index.js +1 -1
- package/plugins-dist/compact/compact.js +13 -2
- package/plugins-dist/compact/index.js +12 -8
- package/plugins-dist/continue/index.js +1 -1
- package/plugins-dist/cost/index.js +135 -0
- package/plugins-dist/cost/plugin.json +16 -0
- package/plugins-dist/custom-commands/index.js +86 -0
- package/plugins-dist/custom-commands/plugin.json +12 -0
- package/plugins-dist/editor/index.js +89 -0
- package/plugins-dist/editor/plugin.json +21 -0
- package/plugins-dist/fs/fs.js +106 -17
- package/plugins-dist/fs/grep.js +40 -12
- package/plugins-dist/fs/index.js +8 -5
- package/plugins-dist/hooks/index.js +145 -0
- package/plugins-dist/hooks/plugin.json +19 -0
- package/plugins-dist/keys/index.js +1 -1
- package/plugins-dist/mcp/index.js +41 -9
- package/plugins-dist/mcp/install.js +160 -0
- package/plugins-dist/mcp/mcp.js +2 -1
- package/plugins-dist/mcp/plugin.json +2 -1
- package/plugins-dist/mcp/project.js +42 -1
- package/plugins-dist/memory/index.js +3 -5
- package/plugins-dist/memory/plugin.json +1 -1
- package/plugins-dist/mentions/index.js +29 -11
- package/plugins-dist/models/configure.js +252 -151
- package/plugins-dist/models/context.js +2 -2
- package/plugins-dist/models/index.js +53 -16
- package/plugins-dist/models/plugin.json +1 -1
- package/plugins-dist/models/provider.js +4 -2
- package/plugins-dist/models/saved.js +9 -4
- package/plugins-dist/modes/index.js +1 -1
- package/plugins-dist/plugins/index.js +1 -1
- package/plugins-dist/print/index.js +62 -9
- package/plugins-dist/print/plugin.json +3 -0
- package/plugins-dist/prompts/index.js +49 -2
- package/plugins-dist/{memory → prompts}/notes.js +10 -7
- package/plugins-dist/prompts/plugin.json +8 -2
- package/plugins-dist/prompts/prompts.js +13 -8
- package/plugins-dist/prompts/results.js +60 -0
- package/plugins-dist/prompts/sys.js +15 -3
- package/plugins-dist/renderers/index.js +1 -1
- package/plugins-dist/repeats/index.js +1 -1
- package/plugins-dist/schedule/index.js +1 -1
- package/plugins-dist/scratch/index.js +1 -1
- package/plugins-dist/screenshot/index.js +25 -11
- package/plugins-dist/screenshot/plugin.json +1 -1
- package/plugins-dist/sessions/index.js +1 -1
- package/plugins-dist/shell/index.js +55 -3
- package/plugins-dist/shell/plugin.json +3 -1
- package/plugins-dist/shell/shell.js +26 -8
- package/plugins-dist/skills/index.js +184 -35
- package/plugins-dist/skills/install.js +178 -0
- package/plugins-dist/skills/plugin.json +6 -2
- package/plugins-dist/skills/project.js +43 -1
- package/plugins-dist/skills/skills.js +27 -10
- package/plugins-dist/steer/index.js +135 -0
- package/plugins-dist/steer/plugin.json +12 -0
- package/plugins-dist/suggest/index.js +15 -9
- package/plugins-dist/suggest/plugin.json +1 -1
- package/plugins-dist/task/index.js +7 -4
- package/plugins-dist/task/plugin.json +10 -2
- package/plugins-dist/task/subagent.js +106 -15
- package/plugins-dist/todo/index.js +3 -3
- package/plugins-dist/tui/editor.js +7 -2
- package/plugins-dist/tui/frontend.js +135 -18
- package/plugins-dist/tui/index.js +2 -1
- package/plugins-dist/tui/markdown.js +48 -6
- package/plugins-dist/tui/mascot.js +439 -0
- package/plugins-dist/tui/mentions.js +44 -6
- package/plugins-dist/tui/plugin.json +3 -2
- package/plugins-dist/tui/screen.js +2 -0
- package/plugins-dist/tui/transcript.js +19 -2
- package/plugins-dist/tui/tui.js +303 -83
- package/plugins-dist/tui/vendor.js +13822 -0
- package/plugins-dist/undo/checkpoint.js +28 -1
- package/plugins-dist/undo/index.js +92 -22
- package/plugins-dist/undo/plugin.json +10 -3
- package/plugins-dist/undo/snapshot.js +111 -0
- package/plugins-dist/update/index.js +9 -10
- package/plugins-dist/update/plugin.json +4 -1
- package/plugins-dist/web/index.js +1 -1
- package/plugins-dist/web/web.js +6 -5
- package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
- package/sdk/python/jade_plugin.py +8 -0
- package/src/api.ts +7 -262
- package/src/fetch.ts +18 -16
- package/src/host.ts +49 -1
- package/src/index.ts +1 -1
- package/src/main.ts +25 -6
- package/src/remote.ts +2 -0
- package/src/session.ts +134 -15
- package/src/source.ts +2 -11
- package/src/update.ts +16 -10
- package/plugins/undo/notes.ts +0 -43
- package/plugins-dist/undo/notes.js +0 -36
|
@@ -0,0 +1,505 @@
|
|
|
1
|
+
// 标题左边的吉祥物:一个玉做的团子,按当前在做的事换表情、带一点动画;还有终端标签页标题上的 jade 标志。
|
|
2
|
+
// 不碰 Ink:动画由 App 统一重画推动(见 tui.ts 的 tick),这里每次按时间算出这一帧。
|
|
3
|
+
//
|
|
4
|
+
// 画布 15×6 个像素,每格字符上下两个像素(▀ 前景是上面、背景是下面),6 个像素高正好三行,和右边三行文字并排。
|
|
5
|
+
// 左边 9 列是身体,空一列,右边 5 列放表情以外的小东西(思考的三个点、工具的转圈、提问的感叹号、做完的 ✓……都用像素画)。画布宽度不随表情变,标题右边的文字不会跟着左右跳。
|
|
6
|
+
//
|
|
7
|
+
// 精度只有这么多,细节靠颜色:身体按光照着色(左上亮、边缘暗、一点高光),眼睛是深色的形状,
|
|
8
|
+
// 每个像素按 4×4 次采样算出眼睛盖住了多少、和身体的颜色混合,所以眼睛可以不按整格移动、眨眼也有中间的几帧。
|
|
9
|
+
// 身体外面是终端背景,颜色未知、没法混合,所以轮廓只按整格变(只有做完后蹦那一下会变)、混色只在身体里面做。
|
|
10
|
+
// 视线、睁眼的程度、颜色在状态之间缓动(Mascot),不会一下跳过去。
|
|
11
|
+
|
|
12
|
+
export const JADE = "#00a86b";
|
|
13
|
+
|
|
14
|
+
/** 正在做的事,决定吉祥物的样子 */
|
|
15
|
+
export type Mood =
|
|
16
|
+
| "idle" // 空闲:偶尔眨眼、往旁边瞟一眼
|
|
17
|
+
| "waiting" // 等模型、泛泛的工作中:眼睛左右看
|
|
18
|
+
| "thinking" // 思考:眯眼望左上方琢磨、身体慢慢亮起来;想到了就转头看右上角,三个点一个个冒出来
|
|
19
|
+
| "writing" // 输出正文或工具参数:低头,眼睛随打字左右移,旁边打字的点
|
|
20
|
+
| "tool" // 运行工具:看向右边转着的圈,身上扫过一道光
|
|
21
|
+
| "asking" // 弹出对话框等用户:睁大眼,头上一个闪烁的感叹号
|
|
22
|
+
| "stopping" // 正在中断:眯眼,变成琥珀色,冒一滴汗
|
|
23
|
+
| "happy"; // 刚做完:^ ^ 地原地蹦一下,落地一亮,转头看右边画出一个 ✓,再回过头冲你眨一只眼
|
|
24
|
+
|
|
25
|
+
/** 做完后笑多久 */
|
|
26
|
+
export const HAPPY_MS = 4000;
|
|
27
|
+
|
|
28
|
+
export interface MoodInput {
|
|
29
|
+
/** 运行状态的标签(screen.busy);不在运行时 undefined */
|
|
30
|
+
busy?: string;
|
|
31
|
+
/** 有对话框在等用户 */
|
|
32
|
+
asking: boolean;
|
|
33
|
+
/** 上一次正常做完的时间 */
|
|
34
|
+
doneAt?: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function moodOf(s: MoodInput, now: number): Mood {
|
|
38
|
+
if (s.asking) return "asking";
|
|
39
|
+
const b = s.busy;
|
|
40
|
+
if (b === undefined) return s.doneAt !== undefined && now - s.doneAt < HAPPY_MS ? "happy" : "idle";
|
|
41
|
+
if (b === "Interrupting") return "stopping";
|
|
42
|
+
if (b === "Thinking") return "thinking";
|
|
43
|
+
if (b.startsWith("Writing")) return "writing";
|
|
44
|
+
if (b.startsWith("Running ")) return "tool";
|
|
45
|
+
return "waiting";
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// ---------- 颜色 ----------
|
|
49
|
+
|
|
50
|
+
type RGB = [number, number, number];
|
|
51
|
+
const rgb = (hex: string): RGB => [1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16)) as RGB;
|
|
52
|
+
const hex = (c: RGB) => `#${c.map((v) => Math.round(Math.max(0, Math.min(255, v))).toString(16).padStart(2, "0")).join("")}`;
|
|
53
|
+
const mix = (a: RGB, b: RGB, t: number): RGB => [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t];
|
|
54
|
+
/** 按 t(0–1)在一串颜色之间插值 */
|
|
55
|
+
const ramp = (stops: RGB[], t: number): RGB => {
|
|
56
|
+
const x = Math.max(0, Math.min(1, t)) * (stops.length - 1);
|
|
57
|
+
const i = Math.min(stops.length - 2, Math.floor(x));
|
|
58
|
+
return mix(stops[i]!, stops[i + 1]!, x - i);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** 从暗到亮的玉色;中断时换成琥珀色那一套 */
|
|
62
|
+
const JADE_RAMP = ["#004a33", "#00744c", JADE, "#2ccc8a", "#8eeec0"].map(rgb);
|
|
63
|
+
const AMBER_RAMP = ["#5a3a00", "#9a6500", "#e0a526", "#f4c75a", "#fde4a0"].map(rgb);
|
|
64
|
+
const SPECULAR = rgb("#effff6");
|
|
65
|
+
const EYE = rgb("#0b2219");
|
|
66
|
+
const PALE = rgb("#b6f2d4");
|
|
67
|
+
const DEEP = rgb("#00744c");
|
|
68
|
+
const BANG = [rgb("#f5c518"), rgb("#fff1a8")];
|
|
69
|
+
const SWEAT = rgb("#8fd3ff");
|
|
70
|
+
/** 蹦起来时地上的影子:身体外面没法和终端背景混合,用一个暗的玉色 */
|
|
71
|
+
const SHADOW = mix(DEEP, EYE, 0.55);
|
|
72
|
+
|
|
73
|
+
// ---------- 形状 ----------
|
|
74
|
+
|
|
75
|
+
const W = 15;
|
|
76
|
+
/** 右边小东西的第一列:和身体之间空一列,不贴着 */
|
|
77
|
+
const SIDE = 10;
|
|
78
|
+
const H = 6;
|
|
79
|
+
/** 身体的轮廓(9 列),轮廓外是终端背景 */
|
|
80
|
+
const MASK = ["..#####..", ".#######.", "#########", "#########", "#########", ".#######."];
|
|
81
|
+
const CX = 4.5;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 身体的几种样子:平时、蹲下(压扁,头顶低一格)、跳在空中(离地一格,脚下一块影子)。
|
|
85
|
+
* top/bottom 是身体占的像素行 [top, bottom),光照按它算;dy 是眼睛跟着上下挪多少
|
|
86
|
+
*/
|
|
87
|
+
interface Shape {
|
|
88
|
+
mask: string[];
|
|
89
|
+
top: number;
|
|
90
|
+
bottom: number;
|
|
91
|
+
dy: number;
|
|
92
|
+
}
|
|
93
|
+
const SHAPES = {
|
|
94
|
+
rest: { mask: MASK, top: 0, bottom: 6, dy: 0 },
|
|
95
|
+
crouch: { mask: [".........", ".#######.", "#########", "#########", "#########", ".#######."], top: 1, bottom: 6, dy: 1 },
|
|
96
|
+
air: { mask: ["..#####..", ".#######.", "#########", "#########", ".#######.", "........."], top: 0, bottom: 5, dy: -1 },
|
|
97
|
+
} satisfies Record<string, Shape>;
|
|
98
|
+
|
|
99
|
+
/** 做完后的一跳:蹲下蓄力 → 跳起 → 落地压扁一下 → 站好(t 是进入 happy 之后的毫秒) */
|
|
100
|
+
const HOP = { crouch: 90, air: 380, land: 470 };
|
|
101
|
+
function shapeAt(mood: Mood, t: number): Shape {
|
|
102
|
+
if (mood !== "happy" || t >= HOP.land) return SHAPES.rest;
|
|
103
|
+
return t < HOP.crouch || t >= HOP.air ? SHAPES.crouch : SHAPES.air;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* 两只眼睛:中心的 x、下沿的 y(像素坐标,像素 (x, y) 覆盖 [x, x+1)×[y, y+1)),宽 1、高 2 个像素。
|
|
107
|
+
* 停住的位置都正好对齐像素,眼睛是清楚的两块;只有移动、眨眼的中间几帧才会混出过渡色
|
|
108
|
+
*/
|
|
109
|
+
const EYES_X = [2.5, 6.5];
|
|
110
|
+
const EYE_BOTTOM = 4;
|
|
111
|
+
const EYE_H = 2;
|
|
112
|
+
|
|
113
|
+
const smooth = (t: number) => {
|
|
114
|
+
const x = Math.max(0, Math.min(1, t));
|
|
115
|
+
return x * x * (3 - 2 * x);
|
|
116
|
+
};
|
|
117
|
+
const gauss = (dx: number, dy: number, s: number) => Math.exp(-(dx * dx + dy * dy) / (2 * s * s));
|
|
118
|
+
|
|
119
|
+
/** 会在状态之间缓动的量 */
|
|
120
|
+
export interface Pose {
|
|
121
|
+
/** 视线:x 向右、y 向下,单位是像素(停住时取整数,眼睛才清楚) */
|
|
122
|
+
gx: number;
|
|
123
|
+
gy: number;
|
|
124
|
+
/** 睁眼的程度,1 正常(2 像素高),1.5 睁大,0 闭上(剩 1 像素的一条线) */
|
|
125
|
+
open: number;
|
|
126
|
+
/** 0 普通的眼睛,1 笑眯眯的 ^ ^ */
|
|
127
|
+
happy: number;
|
|
128
|
+
/** 只有左眼变成 ^:眨一只眼 */
|
|
129
|
+
wink: number;
|
|
130
|
+
/** 0 玉色,1 琥珀色 */
|
|
131
|
+
tint: number;
|
|
132
|
+
/** 整体提亮(思考时一闪一闪) */
|
|
133
|
+
glow: number;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const REST: Pose = { gx: 0, gy: 0, open: 1, happy: 0, wink: 0, tint: 0, glow: 0 };
|
|
137
|
+
|
|
138
|
+
// ---------- 时间轴 ----------
|
|
139
|
+
|
|
140
|
+
const ease = (now: number, keys: [number, number][], period: number): number => {
|
|
141
|
+
// keys:[时刻, 值],时刻在 [0, period) 里递增;相邻两个之间平滑过渡,最后一个过渡回第一个
|
|
142
|
+
const t = now % period;
|
|
143
|
+
for (let i = 0; i < keys.length; i++) {
|
|
144
|
+
const [t0, v0] = keys[i]!;
|
|
145
|
+
const [t1raw, v1] = keys[(i + 1) % keys.length]!;
|
|
146
|
+
const t1 = i + 1 < keys.length ? t1raw : t1raw + period;
|
|
147
|
+
if (t >= t0 && t < t1) return v0 + (v1 - v0) * smooth((t - t0) / (t1 - t0));
|
|
148
|
+
}
|
|
149
|
+
return keys[0]![1];
|
|
150
|
+
};
|
|
151
|
+
/** 关键帧之间停一会儿再动:每个值先停 hold 毫秒,再用 move 毫秒移到下一个 */
|
|
152
|
+
const steps = (values: number[], hold: number, move: number): [[number, number][], number] => {
|
|
153
|
+
const keys: [number, number][] = [];
|
|
154
|
+
values.forEach((v, i) => {
|
|
155
|
+
keys.push([i * (hold + move), v], [i * (hold + move) + hold, v]);
|
|
156
|
+
});
|
|
157
|
+
return [keys, values.length * (hold + move)];
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
/** 每隔多久眨一次眼,眨一下多久 */
|
|
161
|
+
const BLINK_EVERY = 4200;
|
|
162
|
+
const BLINK_MS = 220;
|
|
163
|
+
/** 空闲时每隔多久往旁边瞟一眼,从什么时候到什么时候 */
|
|
164
|
+
const GLANCE_EVERY = 11_000;
|
|
165
|
+
const GLANCE: [number, number] = [7000, 8600];
|
|
166
|
+
|
|
167
|
+
/** 眨眼:返回睁眼程度的系数。每第三次连眨两下 */
|
|
168
|
+
function blink(now: number): number {
|
|
169
|
+
const n = Math.floor(now / BLINK_EVERY);
|
|
170
|
+
const t = now % BLINK_EVERY;
|
|
171
|
+
const one = (t0: number) => (t >= t0 && t < t0 + BLINK_MS ? 1 - Math.sin((Math.PI * (t - t0)) / BLINK_MS) : 1);
|
|
172
|
+
return Math.min(one(0), n % 3 === 2 ? one(BLINK_MS + 90) : 1);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** 做完后什么时候回过头眨眼(落地后先看着 ✓ 画出来) */
|
|
176
|
+
const WINK_AT = 1500;
|
|
177
|
+
|
|
178
|
+
/** 这个状态此刻想要的样子(Mascot 往这里缓动);t 是进入这个状态之后的毫秒,只有 happy 用 */
|
|
179
|
+
export function goal(mood: Mood, now: number, t = HAPPY_MS / 2): Pose {
|
|
180
|
+
switch (mood) {
|
|
181
|
+
case "idle": {
|
|
182
|
+
const t = now % GLANCE_EVERY;
|
|
183
|
+
const look = t >= GLANCE[0] && t < GLANCE[1] ? -1 : 0;
|
|
184
|
+
return { ...REST, gx: look, open: blink(now) };
|
|
185
|
+
}
|
|
186
|
+
case "waiting": {
|
|
187
|
+
const [keys, period] = steps([0, -1, 0, 1], 600, 350);
|
|
188
|
+
return { ...REST, gx: ease(now, keys, period), open: blink(now) };
|
|
189
|
+
}
|
|
190
|
+
case "thinking": {
|
|
191
|
+
// 琢磨:眯起眼往左上方望,身体一点点亮起来(在攒想法);
|
|
192
|
+
// 想到了:睁开眼转向右上角,看着三个点一个个冒出来,攒的亮光随着点冒出来慢慢退掉
|
|
193
|
+
const gx = ease(now, [[0, -1], [THINK.ponder - 350, -1], [THINK.ponder, 1], [THINK.period - 350, 1]], THINK.period);
|
|
194
|
+
const open = ease(now, [[0, 0.5], [THINK.ponder - 350, 0.5], [THINK.ponder, 1], [THINK.period - 350, 1]], THINK.period);
|
|
195
|
+
const glow = ease(now, [[0, 0], [THINK.ponder, 0.75], [THINK.dot(2), 0]], THINK.period);
|
|
196
|
+
return { ...REST, gx, gy: -1, open: open * blink(now + 1700), glow };
|
|
197
|
+
}
|
|
198
|
+
case "writing": {
|
|
199
|
+
// 读着刚写的字:一行里从左往右扫,到头回来
|
|
200
|
+
const [keys, period] = steps([-1, 0, 1], 260, 140);
|
|
201
|
+
return { ...REST, gx: ease(now, keys, period), gy: 1, open: 0.5 + 0.5 * blink(now + 600) };
|
|
202
|
+
}
|
|
203
|
+
case "tool":
|
|
204
|
+
return { ...REST, gx: 1, open: blink(now + 900) };
|
|
205
|
+
case "asking":
|
|
206
|
+
return { ...REST, open: 1.5 * blink(now + 2500) };
|
|
207
|
+
case "stopping":
|
|
208
|
+
return { ...REST, open: 0, tint: 1 };
|
|
209
|
+
case "happy": {
|
|
210
|
+
// 只有眼睛,不加嘴和脸红:别的表情都只靠眼睛,加了反而不像同一个。
|
|
211
|
+
// 蹦的时候 ^ ^;落地那一下身体一亮,睁眼看右边的 ✓ 画出来;然后回过头,左眼 ^ 冲你眨一下
|
|
212
|
+
const glow = t >= HOP.air ? Math.exp(-(t - HOP.air) / 260) : 0;
|
|
213
|
+
if (t < HOP.land) return { ...REST, happy: 1, glow };
|
|
214
|
+
if (t < WINK_AT) return { ...REST, gx: 1, glow };
|
|
215
|
+
return { ...REST, wink: 1 };
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** 画一帧:返回 6 行、每行 15 个颜色("#rrggbb"),undefined 是透明(终端背景);t 同 goal */
|
|
221
|
+
export function paint(p: Pose, mood: Mood, now: number, t = HAPPY_MS / 2): (string | undefined)[][] {
|
|
222
|
+
const shape = shapeAt(mood, t);
|
|
223
|
+
const cy = (shape.top + shape.bottom) / 2;
|
|
224
|
+
const half = (shape.bottom - shape.top) / 2;
|
|
225
|
+
const ramp_ = (t: number) => mix(ramp(JADE_RAMP, t), ramp(AMBER_RAMP, t), p.tint);
|
|
226
|
+
const grid: (RGB | undefined)[][] = Array.from({ length: H }, () => Array<RGB | undefined>(W).fill(undefined));
|
|
227
|
+
// 工具运行时,一道光从左往右扫过身体
|
|
228
|
+
const sweep = mood === "tool" ? ((now % 1400) / 1400) * 16 - 4 : undefined;
|
|
229
|
+
for (let y = 0; y < H; y++)
|
|
230
|
+
for (let x = 0; x < 9; x++) {
|
|
231
|
+
if (shape.mask[y]?.[x] !== "#") continue;
|
|
232
|
+
const nx = (x + 0.5 - CX) / 4.5;
|
|
233
|
+
const ny = (y + 0.5 - cy) / half;
|
|
234
|
+
const r = Math.max(Math.abs(nx), Math.abs(ny) * 0.9, Math.hypot(nx, ny) * 0.82);
|
|
235
|
+
let light = 0.62 - 0.3 * ny - 0.14 * nx - 0.32 * smooth((r - 0.55) / 0.45) + 0.18 * p.glow;
|
|
236
|
+
if (sweep !== undefined) light += 0.22 * Math.exp(-(((x - y * 0.5 - sweep) / 0.8) ** 2));
|
|
237
|
+
let c = ramp_(light);
|
|
238
|
+
// 高光:左上角一个像素,旁边一圈淡淡的
|
|
239
|
+
c = mix(c, SPECULAR, 0.9 * gauss(x - 2, y - shape.top - 1, 0.45));
|
|
240
|
+
// 眼睛:4×4 次采样,算出盖住了多少
|
|
241
|
+
let cover = 0;
|
|
242
|
+
for (let sy = 0; sy < 4; sy++)
|
|
243
|
+
for (let sx = 0; sx < 4; sx++) {
|
|
244
|
+
const px = x + (sx + 0.5) / 4;
|
|
245
|
+
const py = y + (sy + 0.5) / 4;
|
|
246
|
+
cover += eyeAt(p, px, py - shape.dy) / 16;
|
|
247
|
+
}
|
|
248
|
+
c = mix(c, mix(EYE, ramp_(0.05), p.tint * 0.4), Math.min(1, cover));
|
|
249
|
+
grid[y]![x] = c;
|
|
250
|
+
}
|
|
251
|
+
// 跳在空中时脚下一块影子
|
|
252
|
+
if (shape === SHAPES.air) for (let x = 3; x < 6; x++) grid[5]![x] = SHADOW;
|
|
253
|
+
for (const [x, y, c] of extras(mood, now, t)) grid[y]![x] = c;
|
|
254
|
+
return grid.map((row) => row.map((c) => (c ? hex(c) : undefined)));
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** 眼睛在 (px, py) 处的覆盖(0 或 1):普通的眼睛和 ^ 形的笑眼按 happy 混合(左眼还算上 wink) */
|
|
258
|
+
function eyeAt(p: Pose, px: number, py: number): number {
|
|
259
|
+
let best = 0;
|
|
260
|
+
for (const [i, bx] of EYES_X.entries()) {
|
|
261
|
+
const happy = i === 0 ? Math.max(p.happy, p.wink) : p.happy;
|
|
262
|
+
// 笑分两步:先是下眼皮往上推,眼睛压成上面一格的一条线(连续的);
|
|
263
|
+
// 推到头之后一下子变成 ^(像素画按帧换形状,滑过去的中间几帧是糊的)
|
|
264
|
+
const smile = happy > 0.6;
|
|
265
|
+
// 两只都变成 ^ 时各往外一格:^ 有三格宽,不挪开的话两只里边的眼角只隔一格,连成一排锯齿;
|
|
266
|
+
// 只眨一只眼时另一只是普通的眼睛,不会连起来,^ 留在原位
|
|
267
|
+
// ^ 对齐整格:从别的表情缓过来时视线还没回正,不对齐的话 ^ 是糊的
|
|
268
|
+
const out = p.happy > 0.6 ? (i === 0 ? -1 : 1) : 0;
|
|
269
|
+
const ex = smile ? Math.round(bx + p.gx - 0.5) + 0.5 + out : bx + p.gx;
|
|
270
|
+
const bottom = smile ? Math.round(EYE_BOTTOM + p.gy) : EYE_BOTTOM + p.gy;
|
|
271
|
+
if (Math.abs(px - ex) < 0.5) {
|
|
272
|
+
// 闭眼是上眼皮往下:下沿不动,高度变小,最少留 1 像素的一条线
|
|
273
|
+
const lower = bottom - Math.min(1, happy / 0.6);
|
|
274
|
+
const upper = Math.min(bottom - Math.max(1, EYE_H * p.open), lower - 1);
|
|
275
|
+
if (py >= upper && py < lower) best = 1;
|
|
276
|
+
}
|
|
277
|
+
if (smile && Math.abs(Math.abs(px - ex) - 1) < 0.5 && py >= bottom - 1 && py < bottom) best = 1;
|
|
278
|
+
}
|
|
279
|
+
return best;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** 右边的小东西:[列, 行, 颜色],列是 SIDE 起的 0–4 */
|
|
283
|
+
function extras(mood: Mood, now: number, t: number): [number, number, RGB][] {
|
|
284
|
+
return side(mood, now, t).map(([x, y, c]) => [SIDE + x, y, c]);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** ✓ 的五个像素,按笔画的顺序:短的一笔往右下,长的一笔往右上 */
|
|
288
|
+
const CHECK: [number, number][] = [
|
|
289
|
+
[0, 3],
|
|
290
|
+
[1, 4],
|
|
291
|
+
[2, 3],
|
|
292
|
+
[3, 2],
|
|
293
|
+
[4, 1],
|
|
294
|
+
];
|
|
295
|
+
/** ✓ 落地后一笔画出来,每个像素隔多久;画完过一会儿一道光沿着笔画走一遍;最后按笔画的顺序暗下去消失 */
|
|
296
|
+
const CHECK_STEP = 55;
|
|
297
|
+
const CHECK_SHINE = 1900;
|
|
298
|
+
const CHECK_FADE = HAPPY_MS - 600;
|
|
299
|
+
|
|
300
|
+
function check(t: number): [number, number, RGB][] {
|
|
301
|
+
const white = rgb("#ffffff");
|
|
302
|
+
return CHECK.flatMap(([x, y], i) => {
|
|
303
|
+
const age = t - (HOP.land + i * CHECK_STEP);
|
|
304
|
+
if (age < 0) return [];
|
|
305
|
+
// 刚画上的是白的笔尖,很快沉成淡玉色
|
|
306
|
+
let c = mix(PALE, white, Math.exp(-age / 120));
|
|
307
|
+
// 一道光:亮点沿着笔画走过去
|
|
308
|
+
c = mix(c, white, 0.85 * Math.exp(-(((t - CHECK_SHINE) / 60 - i) ** 2)));
|
|
309
|
+
// 消失:一个个暗下去再拿掉(外面是终端背景,没法渐隐)
|
|
310
|
+
const out = (t - CHECK_FADE - i * 70) / 280;
|
|
311
|
+
if (out >= 1) return [];
|
|
312
|
+
if (out > 0) c = mix(c, DEEP, out);
|
|
313
|
+
return [[x, y, c] as [number, number, RGB]];
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const fade = (t: number) => ramp([DEEP, rgb(JADE), PALE], t);
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* 思考的节奏(一圈的毫秒数):先琢磨 ponder,然后三个点隔 gap 一个个冒出来;
|
|
321
|
+
* 三个都在了停一会儿,从 dim 起一起暗下去、拿掉,再接着琢磨
|
|
322
|
+
*/
|
|
323
|
+
const THINK = {
|
|
324
|
+
period: 4800,
|
|
325
|
+
ponder: 1700,
|
|
326
|
+
gap: 550,
|
|
327
|
+
/** 第 i 个点冒出来的时刻 */
|
|
328
|
+
dot: (i: number) => THINK.ponder + 250 + i * THINK.gap,
|
|
329
|
+
dim: 4200,
|
|
330
|
+
/** 暗下去用多久:之后拿掉(外面是终端背景,没法渐隐) */
|
|
331
|
+
fade: 400,
|
|
332
|
+
};
|
|
333
|
+
/** 三个点:在右上方从左往右排成一排,和往右上看的眼睛同一高度附近,像省略号 */
|
|
334
|
+
const DOTS: [number, number][] = [
|
|
335
|
+
[0, 1],
|
|
336
|
+
[2, 1],
|
|
337
|
+
[4, 1],
|
|
338
|
+
];
|
|
339
|
+
|
|
340
|
+
function dots(now: number): [number, number, RGB][] {
|
|
341
|
+
const t = now % THINK.period;
|
|
342
|
+
const out = (t - THINK.dim) / THINK.fade;
|
|
343
|
+
if (out >= 1) return [];
|
|
344
|
+
return DOTS.flatMap(([x, y], i) => {
|
|
345
|
+
const age = t - THINK.dot(i);
|
|
346
|
+
if (age < 0) return [];
|
|
347
|
+
// 冒出来时白一下,很快沉成淡玉色
|
|
348
|
+
let c = mix(fade(0.85), rgb("#ffffff"), Math.exp(-age / 140));
|
|
349
|
+
if (out > 0) c = mix(c, DEEP, out);
|
|
350
|
+
return [[x, y, c] as [number, number, RGB]];
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function side(mood: Mood, now: number, t: number): [number, number, RGB][] {
|
|
355
|
+
switch (mood) {
|
|
356
|
+
case "happy":
|
|
357
|
+
return check(t);
|
|
358
|
+
case "thinking":
|
|
359
|
+
return dots(now);
|
|
360
|
+
case "writing": {
|
|
361
|
+
// 打字的点:一个个亮起来,亮过之后慢慢暗下去
|
|
362
|
+
const t = (now % 1200) / 1200;
|
|
363
|
+
return [0, 1, 2].flatMap((x) => {
|
|
364
|
+
const age = t - x * 0.18;
|
|
365
|
+
return age >= 0 && age < 0.75 ? [[x, 5, fade(1 - Math.max(0, age - 0.25) * 2)] as [number, number, RGB]] : [];
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
case "tool": {
|
|
369
|
+
// 彗星一样转圈:亮的头,后面跟着越来越暗的尾巴
|
|
370
|
+
const ring: [number, number][] = [
|
|
371
|
+
[1, 1],
|
|
372
|
+
[2, 2],
|
|
373
|
+
[2, 3],
|
|
374
|
+
[1, 4],
|
|
375
|
+
[0, 3],
|
|
376
|
+
[0, 2],
|
|
377
|
+
];
|
|
378
|
+
const head = Math.floor(now / 90) % ring.length;
|
|
379
|
+
return [0, 1, 2, 3].map((k) => {
|
|
380
|
+
const [x, y] = ring[(head - k + ring.length) % ring.length]!;
|
|
381
|
+
return [x, y, fade(1 - k / 3.2)];
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
case "asking": {
|
|
385
|
+
// 感叹号在两种黄之间呼吸
|
|
386
|
+
const c = mix(BANG[0]!, BANG[1]!, 0.5 + 0.5 * Math.sin(now / 160));
|
|
387
|
+
return [0, 1, 2, 4].map((y) => [1, y, c]);
|
|
388
|
+
}
|
|
389
|
+
case "stopping":
|
|
390
|
+
// 汗珠挂在头的右上角,贴着身体
|
|
391
|
+
return [
|
|
392
|
+
[-1, 0, SWEAT],
|
|
393
|
+
[-1, 1, mix(SWEAT, rgb("#ffffff"), 0.4)],
|
|
394
|
+
];
|
|
395
|
+
default:
|
|
396
|
+
return [];
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* 有状态的动画:每次 frame 把 Pose 往当前状态想要的样子缓动一点,所以换状态时眼睛是滑过去的。
|
|
402
|
+
* 眨眼这种很快的动作直接用目标值,不缓动
|
|
403
|
+
*/
|
|
404
|
+
export class Mascot {
|
|
405
|
+
pose: Pose = { ...REST };
|
|
406
|
+
#last?: number;
|
|
407
|
+
#mood?: Mood;
|
|
408
|
+
/** 进入当前状态的时刻:做完后那一套动作按它排时间 */
|
|
409
|
+
#since = 0;
|
|
410
|
+
|
|
411
|
+
frame(mood: Mood, now: number): (string | undefined)[][] {
|
|
412
|
+
if (mood !== this.#mood) {
|
|
413
|
+
this.#mood = mood;
|
|
414
|
+
this.#since = now;
|
|
415
|
+
}
|
|
416
|
+
const t = now - this.#since;
|
|
417
|
+
const want = goal(mood, now, t);
|
|
418
|
+
const dt = this.#last === undefined ? 1e9 : Math.max(0, now - this.#last);
|
|
419
|
+
this.#last = now;
|
|
420
|
+
const k = 1 - Math.exp(-dt / 110);
|
|
421
|
+
const p = this.pose;
|
|
422
|
+
for (const key of ["gx", "gy", "happy", "wink", "tint"] as const) p[key] += (want[key] - p[key]) * k;
|
|
423
|
+
p.open = want.open < p.open ? want.open : p.open + (want.open - p.open) * (1 - Math.exp(-dt / 50));
|
|
424
|
+
p.glow = want.glow;
|
|
425
|
+
return paint(p, mood, now, t);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/** 还在往目标缓动(空闲时也要接着重画几帧) */
|
|
429
|
+
moving(mood: Mood, now: number): boolean {
|
|
430
|
+
const want = goal(mood, now, mood === this.#mood ? now - this.#since : 0);
|
|
431
|
+
const p = this.pose;
|
|
432
|
+
return (["gx", "gy", "happy", "wink", "tint", "open"] as const).some((k) => Math.abs(want[k] - p[k]) > 0.02);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/** 一段同样颜色的字符;fg、bg 是颜色,没有就是终端默认 */
|
|
437
|
+
export interface Run {
|
|
438
|
+
text: string;
|
|
439
|
+
fg?: string;
|
|
440
|
+
bg?: string;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/** 把像素合成半格字符,每一行是若干段同色的字符 */
|
|
444
|
+
export function cells(grid: (string | undefined)[][]): Run[][] {
|
|
445
|
+
const out: Run[][] = [];
|
|
446
|
+
for (let y = 0; y < grid.length; y += 2) {
|
|
447
|
+
const top = grid[y]!;
|
|
448
|
+
const bottom = grid[y + 1] ?? [];
|
|
449
|
+
const line: Run[] = [];
|
|
450
|
+
for (let x = 0; x < top.length; x++) {
|
|
451
|
+
const t = top[x];
|
|
452
|
+
const b = bottom[x];
|
|
453
|
+
const cell: Run =
|
|
454
|
+
!t && !b ? { text: " " }
|
|
455
|
+
: t === b ? { text: "█", fg: t }
|
|
456
|
+
: !t ? { text: "▄", fg: b }
|
|
457
|
+
: !b ? { text: "▀", fg: t }
|
|
458
|
+
: { text: "▀", fg: t, bg: b };
|
|
459
|
+
const last = line.at(-1);
|
|
460
|
+
if (last && last.fg === cell.fg && last.bg === cell.bg) last.text += cell.text;
|
|
461
|
+
else line.push(cell);
|
|
462
|
+
}
|
|
463
|
+
out.push(line);
|
|
464
|
+
}
|
|
465
|
+
return out;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/** 动画帧之间隔多久 */
|
|
469
|
+
export const FRAME_MS = 50;
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* 不在运行时,下一次需要重画还要多久(毫秒):缓动没结束、正在眨眼或瞟眼时按帧画,
|
|
473
|
+
* 否则等到下一次眨眼、瞟眼或笑完。运行中由 App 定时重画,用不到这个
|
|
474
|
+
*/
|
|
475
|
+
export function nextChange(mood: Mood, now: number, doneAt?: number, moving = false): number {
|
|
476
|
+
if (moving) return FRAME_MS;
|
|
477
|
+
if (mood === "asking" || mood === "happy") {
|
|
478
|
+
const end = mood === "happy" && doneAt !== undefined ? doneAt + HAPPY_MS - now : Infinity;
|
|
479
|
+
return Math.max(16, Math.min(FRAME_MS, end));
|
|
480
|
+
}
|
|
481
|
+
const tb = now % BLINK_EVERY;
|
|
482
|
+
// 眨眼(含连眨的第二下)期间按帧画
|
|
483
|
+
if (tb < 2 * BLINK_MS + 90) return FRAME_MS;
|
|
484
|
+
const tg = now % GLANCE_EVERY;
|
|
485
|
+
const toGlance = tg < GLANCE[0] ? GLANCE[0] - tg : tg < GLANCE[1] ? GLANCE[1] - tg : GLANCE_EVERY - tg + GLANCE[0];
|
|
486
|
+
return Math.max(16, Math.min(BLINK_EVERY - tb, toGlance));
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
// ---------- 终端标题 ----------
|
|
490
|
+
|
|
491
|
+
/** jade 的标志:一块玉 */
|
|
492
|
+
export const LOGO = "◆";
|
|
493
|
+
const SPIN = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* 终端标签页标题:空闲时是标志,运行中换成转圈,等用户时加感叹号,在别的标签页里也能看出 jade 在做什么。
|
|
497
|
+
* dir 是当前目录的名字
|
|
498
|
+
*/
|
|
499
|
+
export function title(mood: Mood, dir: string, now: number): string {
|
|
500
|
+
const mark =
|
|
501
|
+
mood === "asking" ? `${LOGO}!`
|
|
502
|
+
: mood === "idle" || mood === "happy" ? LOGO
|
|
503
|
+
: SPIN[Math.floor(now / 250) % SPIN.length]!;
|
|
504
|
+
return `${mark} jade · ${dir}`;
|
|
505
|
+
}
|
package/plugins/tui/mentions.ts
CHANGED
|
@@ -18,6 +18,32 @@ export function mentionAt(value: string, cursor: number): { start: number; query
|
|
|
18
18
|
/** 补全后的文本:@ 引用加一个空格,含空白的路径加引号 */
|
|
19
19
|
export const mentionText = (p: string) => (/\s/.test(p) ? `@"${p}"` : `@${p}`);
|
|
20
20
|
|
|
21
|
+
/** 候选路径预先算好的小写形式和文件名(目录去掉末尾的 /),每按一个键都要用,不重复算 */
|
|
22
|
+
interface Prepared {
|
|
23
|
+
lower: string;
|
|
24
|
+
base: string;
|
|
25
|
+
}
|
|
26
|
+
const prepared = new WeakMap<readonly string[], Prepared[]>();
|
|
27
|
+
|
|
28
|
+
function prepare(paths: readonly string[]): Prepared[] {
|
|
29
|
+
let out = prepared.get(paths);
|
|
30
|
+
if (!out) {
|
|
31
|
+
out = paths.map((p) => {
|
|
32
|
+
const lower = p.toLowerCase();
|
|
33
|
+
const trimmed = lower.endsWith("/") ? lower.slice(0, -1) : lower;
|
|
34
|
+
return { lower, base: trimmed.slice(trimmed.lastIndexOf("/") + 1) };
|
|
35
|
+
});
|
|
36
|
+
prepared.set(paths, out);
|
|
37
|
+
}
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 上一次排序时命中的全部候选(下标)。query 只是在后面追加了字符时,新的命中一定在这些里面
|
|
43
|
+
* (包含 q 加字符的也包含 q,按顺序出现的同理),只筛它们,不用再扫一遍全部路径
|
|
44
|
+
*/
|
|
45
|
+
let last: { paths: readonly string[]; q: string; hits: number[] } | undefined;
|
|
46
|
+
|
|
21
47
|
/**
|
|
22
48
|
* 按 query 给候选路径排序:文件名以 query 开头的最前,其次文件名包含、路径包含,
|
|
23
49
|
* 最后是按顺序出现 query 每个字符的模糊匹配;同一档里短的在前
|
|
@@ -25,21 +51,35 @@ export const mentionText = (p: string) => (/\s/.test(p) ? `@"${p}"` : `@${p}`);
|
|
|
25
51
|
export function rankPaths(paths: string[], query: string, limit: number): string[] {
|
|
26
52
|
const q = query.toLowerCase().replaceAll("\\", "/");
|
|
27
53
|
if (!q) return paths.filter((p) => !p.slice(0, -1).includes("/")).slice(0, limit);
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
54
|
+
const info = prepare(paths);
|
|
55
|
+
const from = last && last.paths === paths && q.startsWith(last.q) ? last.hits : undefined;
|
|
56
|
+
const scored: { i: number; score: number }[] = [];
|
|
57
|
+
const check = (i: number) => {
|
|
58
|
+
const { lower, base } = info[i]!;
|
|
32
59
|
let score: number;
|
|
33
60
|
if (lower.startsWith(q)) score = 0;
|
|
34
61
|
else if (base.startsWith(q)) score = 1;
|
|
35
62
|
else if (base.includes(q)) score = 2;
|
|
36
63
|
else if (lower.includes(q)) score = 3;
|
|
37
64
|
else if (subsequence(lower, q)) score = 4;
|
|
38
|
-
else
|
|
39
|
-
scored.push({
|
|
65
|
+
else return;
|
|
66
|
+
scored.push({ i, score });
|
|
67
|
+
};
|
|
68
|
+
if (from) for (const i of from) check(i);
|
|
69
|
+
else for (let i = 0; i < paths.length; i++) check(i);
|
|
70
|
+
last = { paths, q, hits: scored.map((s) => s.i) };
|
|
71
|
+
const cmp = (a: { i: number; score: number }, b: { i: number; score: number }) =>
|
|
72
|
+
a.score - b.score || paths[a.i]!.length - paths[b.i]!.length || paths[a.i]!.localeCompare(paths[b.i]!);
|
|
73
|
+
// 只要前 limit 条:命中常有上万条,全部排序(localeCompare 很慢)每按一个键要十几毫秒
|
|
74
|
+
const top: { i: number; score: number }[] = [];
|
|
75
|
+
for (const s of scored) {
|
|
76
|
+
if (top.length === limit && cmp(s, top[limit - 1]!) >= 0) continue;
|
|
77
|
+
let at = top.length;
|
|
78
|
+
while (at > 0 && cmp(s, top[at - 1]!) < 0) at--;
|
|
79
|
+
top.splice(at, 0, s);
|
|
80
|
+
if (top.length > limit) top.pop();
|
|
40
81
|
}
|
|
41
|
-
|
|
42
|
-
return scored.slice(0, limit).map((s) => s.p);
|
|
82
|
+
return top.map((s) => paths[s.i]!);
|
|
43
83
|
}
|
|
44
84
|
|
|
45
85
|
function subsequence(text: string, q: string): boolean {
|
package/plugins/tui/plugin.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"id": "jade/tui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"api": "^1",
|
|
5
5
|
"entry": "./index.ts",
|
|
6
6
|
"provides": ["frontend@1", "ui@1"],
|
|
7
7
|
"requires": ["commands@1", "keys@1", "renderers@1"],
|
|
8
|
-
"optional": ["modes@1", "models@1", "compact@1", "sessions@1"],
|
|
8
|
+
"optional": ["modes@1", "models@1", "compact@1", "sessions@1", "steer@1"],
|
|
9
9
|
"frontend": { "name": "tui", "default": true }
|
|
10
10
|
}
|
package/plugins/tui/screen.ts
CHANGED
|
@@ -71,6 +71,10 @@ export interface Screen {
|
|
|
71
71
|
input(title: string, opts?: { initial?: string; mask?: boolean; placeholder?: string; signal?: AbortSignal }): Promise<string | undefined>;
|
|
72
72
|
/** 输入框里还没提交的文字和灰色提示(插件的 ctx.input 用);只有终端界面有 */
|
|
73
73
|
readonly draft?: { text(): string; insert(text: string): void; set(text: string): void; hint(text: string | undefined): void };
|
|
74
|
+
/** 把文字放进系统剪贴板(OSC 52);只有终端界面有 */
|
|
75
|
+
copy?(text: string): void;
|
|
76
|
+
/** 把终端让出来执行 fn(例如启动 $EDITOR),结束后重新接管、重画界面;只有终端界面有 */
|
|
77
|
+
suspend?<T>(fn: () => T | Promise<T>): Promise<T>;
|
|
74
78
|
/** 清空已经显示的对话(开始新会话时) */
|
|
75
79
|
clear(): void;
|
|
76
80
|
close(): Promise<void>;
|
|
@@ -169,6 +173,8 @@ export function safeScreen(screen: Screen): Screen {
|
|
|
169
173
|
set: (t) => screen.draft!.set(plain(lf(t))),
|
|
170
174
|
hint: (t) => screen.draft!.hint(t === undefined ? undefined : plain(lf(t))),
|
|
171
175
|
},
|
|
176
|
+
...(screen.copy ? { copy: (t: string) => screen.copy!(t) } : {}),
|
|
177
|
+
...(screen.suspend ? { suspend: <T>(fn: () => T | Promise<T>) => screen.suspend!(fn) } : {}),
|
|
172
178
|
clear: () => screen.clear(),
|
|
173
179
|
close: () => screen.close(),
|
|
174
180
|
};
|