@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/configure.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// 每个模型带自己的设置。/model 可以直接切回去,换了服务或地址之前的也不会丢。
|
|
4
4
|
|
|
5
5
|
import { hasProvider, parseTokens, readSavedServices, updateEnvFile, writeSavedServices, type SavedModel, type SavedService } from "../config.ts";
|
|
6
|
-
import { EFFORTS, listModels, providerInfo, providerKind, rpmFromEnv, visionFromEnv, type ProviderKind } from "../provider.ts";
|
|
6
|
+
import { EFFORTS, isBuiltinKind, listModels, pluginProvider, pluginProviders, providerInfo, providerKind, rpmFromEnv, visionFromEnv, type ProviderKind } from "../provider.ts";
|
|
7
7
|
import { shortPath } from "./render.ts";
|
|
8
8
|
import { dim, green, red, type Screen } from "./screen.ts";
|
|
9
9
|
|
|
@@ -50,18 +50,22 @@ function visionSetting(): boolean | undefined {
|
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
function visionHint(kind:
|
|
53
|
+
function visionHint(kind: string): string {
|
|
54
54
|
const v = visionSetting();
|
|
55
|
-
return v === undefined ? `${kind
|
|
55
|
+
return v === undefined ? `${isBuiltinKind(kind) && kind !== "openai-compat" ? "yes" : "no"} (default)` : v ? "yes" : "no";
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
/**
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
]
|
|
58
|
+
/** 插件服务声明的配置项(见 ProviderDef.settings) */
|
|
59
|
+
const settingsOf = (kind: string) => pluginProvider(kind)?.def.settings ?? [];
|
|
60
|
+
|
|
61
|
+
/** 一个服务包含的变量(不含模型名);插件服务是它声明的配置项 */
|
|
62
|
+
const serviceKeys = (kind: string) =>
|
|
63
|
+
isBuiltinKind(kind)
|
|
64
|
+
? ["JADE_PROVIDER", URL_KEY[kind], KEY_KEY[kind], ...(kind === "openai-compat" ? ["OPENAI_COMPAT_EXTRA_BODY"] : [])]
|
|
65
|
+
: ["JADE_PROVIDER", ...settingsOf(kind).filter((x) => !x.model).map((x) => x.env)];
|
|
66
|
+
|
|
67
|
+
/** 保存模型名的变量;插件服务没有声明模型名时 undefined */
|
|
68
|
+
const modelKey = (kind: string): string | undefined => (isBuiltinKind(kind) ? MODEL_KEY[kind] : settingsOf(kind).find((x) => x.model)?.env);
|
|
65
69
|
|
|
66
70
|
type Env = Record<string, string | undefined>;
|
|
67
71
|
|
|
@@ -71,10 +75,14 @@ const pick = (env: Env, keys: string[]) => {
|
|
|
71
75
|
return out;
|
|
72
76
|
};
|
|
73
77
|
|
|
74
|
-
/**
|
|
78
|
+
/**
|
|
79
|
+
* 服务的类型、地址和密钥都相同的算同一个;插件服务是类型和它声明的配置项(模型名除外)都相同。
|
|
80
|
+
* 配置无效、插件服务的插件没有加载时返回 undefined
|
|
81
|
+
*/
|
|
75
82
|
function serviceId(env: Env): string | undefined {
|
|
76
83
|
try {
|
|
77
84
|
const i = providerInfo(env);
|
|
85
|
+
if (!isBuiltinKind(i.kind)) return pluginProvider(i.kind) ? [i.kind, ...serviceKeys(i.kind).slice(1).map((k) => env[k] ?? "")].join("|") : undefined;
|
|
78
86
|
return `${i.kind}|${i.baseURL ?? ""}|${env[KEY_KEY[i.kind]] ?? ""}`;
|
|
79
87
|
} catch {
|
|
80
88
|
return undefined;
|
|
@@ -110,9 +118,9 @@ function modelNames(models: SavedModel[]): string {
|
|
|
110
118
|
}
|
|
111
119
|
|
|
112
120
|
/** 主菜单里设置那一项的简要说明,例如 "64K out · 1M ctx · images" */
|
|
113
|
-
function settingsSummary(kind:
|
|
121
|
+
function settingsSummary(kind: string, contextWindow: number): string {
|
|
114
122
|
const env = process.env;
|
|
115
|
-
const images = visionSetting() ?? kind !== "openai-compat";
|
|
123
|
+
const images = visionSetting() ?? (isBuiltinKind(kind) && kind !== "openai-compat");
|
|
116
124
|
return [
|
|
117
125
|
env.JADE_MAX_TOKENS && `${env.JADE_MAX_TOKENS} out`,
|
|
118
126
|
env.JADE_EFFORT && `${env.JADE_EFFORT} effort`,
|
|
@@ -142,14 +150,18 @@ export function configurator(opts: ConfigureOptions): Configure {
|
|
|
142
150
|
const model = providerInfo().model;
|
|
143
151
|
if (!id || !model) return;
|
|
144
152
|
const kind = providerKind();
|
|
145
|
-
|
|
146
|
-
const
|
|
147
|
-
const
|
|
153
|
+
// 写回时保留现在认不出的服务(例如提供它的插件这次没加载),不然它和它的密钥就丢了
|
|
154
|
+
const all = await readSavedServices(modelsFile);
|
|
155
|
+
const old = all.filter((s) => {
|
|
156
|
+
const sid = serviceId(s.env);
|
|
157
|
+
return sid !== undefined && (sid === id || (replacing !== undefined && sid === replacing));
|
|
158
|
+
});
|
|
159
|
+
const models = old.flatMap((s) => s.models).filter((m, i, list) => m.name !== model && list.findIndex((x) => x.name === m.name) === i);
|
|
148
160
|
const service: SavedService = {
|
|
149
161
|
env: { ...pick(process.env, serviceKeys(kind)), JADE_PROVIDER: kind },
|
|
150
162
|
models: [{ name: model, settings: pick(process.env, MODEL_SETTINGS) }, ...models],
|
|
151
163
|
};
|
|
152
|
-
await writeSavedServices(modelsFile, [service, ...
|
|
164
|
+
await writeSavedServices(modelsFile, [service, ...all.filter((s) => !old.includes(s))]);
|
|
153
165
|
}
|
|
154
166
|
|
|
155
167
|
/** 保存配置并换上新的 provider;失败时报告原因,旧的 provider 继续用 */
|
|
@@ -175,7 +187,8 @@ export function configurator(opts: ConfigureOptions): Configure {
|
|
|
175
187
|
for (const k of serviceKeys(kind)) updates[k] = s.env[k];
|
|
176
188
|
for (const k of MODEL_SETTINGS) updates[k] = m.settings[k];
|
|
177
189
|
updates.JADE_PROVIDER = kind;
|
|
178
|
-
|
|
190
|
+
const mk = modelKey(kind);
|
|
191
|
+
if (mk) updates[mk] = m.name;
|
|
179
192
|
return applyConfig(updates);
|
|
180
193
|
}
|
|
181
194
|
|
|
@@ -188,7 +201,13 @@ export function configurator(opts: ConfigureOptions): Configure {
|
|
|
188
201
|
const service = (await savedServices()).find((s) => serviceId(s.env) === id);
|
|
189
202
|
const saved = service?.models.find((m) => m.name === model);
|
|
190
203
|
if (service && saved) return useSaved(service, saved);
|
|
191
|
-
|
|
204
|
+
const kind = providerKind();
|
|
205
|
+
const mk = modelKey(kind);
|
|
206
|
+
if (!mk) {
|
|
207
|
+
screen.log(red(`${kind} is provided by plugin ${pluginProvider(kind)?.plugin ?? "(not loaded)"}, which does not declare a model setting; set the model in ${shortPath(envFile)}.`));
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
return applyConfig({ [mk]: model, JADE_CONTEXT_WINDOW: undefined });
|
|
192
211
|
}
|
|
193
212
|
|
|
194
213
|
/** 选模型:服务能列出模型时从列表里选,否则手动输入 */
|
|
@@ -211,11 +230,12 @@ export function configurator(opts: ConfigureOptions): Configure {
|
|
|
211
230
|
async function configureProvider(): Promise<boolean> {
|
|
212
231
|
const current = hasProvider() ? providerInfo() : undefined;
|
|
213
232
|
const currentId = hasProvider() ? serviceId(process.env) : undefined;
|
|
214
|
-
const kind = await screen.select(
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
);
|
|
233
|
+
const kind = await screen.select<string>("Which service should jade use?", [
|
|
234
|
+
...PROVIDERS.map((p) => ({ label: p.label, value: p.kind, hint: p.kind === current?.kind ? `${p.hint} · current` : p.hint })),
|
|
235
|
+
...pluginProviders().map((p) => ({ label: p.def.kind, value: p.def.kind, hint: `plugin ${p.plugin}${p.def.kind === current?.kind ? " · current" : ""}` })),
|
|
236
|
+
]);
|
|
218
237
|
if (!kind) return false;
|
|
238
|
+
if (!isBuiltinKind(kind)) return configurePlugin(kind);
|
|
219
239
|
const same = kind === current?.kind;
|
|
220
240
|
|
|
221
241
|
let baseURL: string | undefined = process.env[URL_KEY[kind]];
|
|
@@ -272,6 +292,45 @@ export function configurator(opts: ConfigureOptions): Configure {
|
|
|
272
292
|
return applyConfig(updates, rekeyed ? currentId : undefined);
|
|
273
293
|
}
|
|
274
294
|
|
|
295
|
+
/** 插件提供的服务:依次询问它声明的配置项,再选模型(声明了模型名时) */
|
|
296
|
+
async function configurePlugin(kind: string): Promise<boolean> {
|
|
297
|
+
const settings = settingsOf(kind);
|
|
298
|
+
const same = providerKind() === kind && hasProvider();
|
|
299
|
+
const services = await savedServices();
|
|
300
|
+
/** 已知的值:当前用的就是这个服务时取当前的,否则取最近保存过的这个服务的 */
|
|
301
|
+
const known = (k: string) => (same ? process.env[k] : services.find((s) => providerKind(s.env) === kind)?.env[k]);
|
|
302
|
+
const env: Env = { JADE_PROVIDER: kind };
|
|
303
|
+
for (const x of settings.filter((s) => !s.model)) {
|
|
304
|
+
const old = known(x.env);
|
|
305
|
+
const answer = await screen.input(x.label, {
|
|
306
|
+
mask: x.secret,
|
|
307
|
+
initial: x.secret ? undefined : old,
|
|
308
|
+
placeholder: x.secret && old ? "leave empty to keep the saved value" : x.placeholder,
|
|
309
|
+
});
|
|
310
|
+
if (answer === undefined) return false;
|
|
311
|
+
// 密钥留空表示沿用,其他的留空表示不设
|
|
312
|
+
const value = answer.trim() || (x.secret ? old : undefined);
|
|
313
|
+
if (x.required && !value) {
|
|
314
|
+
screen.log(red(`${x.label} is required.`));
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
317
|
+
env[x.env] = value;
|
|
318
|
+
}
|
|
319
|
+
const updates: Record<string, string | undefined> = { ...env, JADE_CONTEXT_WINDOW: undefined };
|
|
320
|
+
// 思考力度的取值每种服务不同
|
|
321
|
+
if (!same) updates.JADE_EFFORT = undefined;
|
|
322
|
+
const mk = modelKey(kind);
|
|
323
|
+
if (mk) {
|
|
324
|
+
const target = services.find((s) => serviceId(s.env) === serviceId(env));
|
|
325
|
+
const model = await chooseModel({ ...process.env, ...env }, same ? process.env[mk] : undefined, target?.models.map((m) => m.name));
|
|
326
|
+
if (!model) return false;
|
|
327
|
+
const saved = target?.models.find((m) => m.name === model);
|
|
328
|
+
if (target && saved) return useSaved({ ...target, env: { ...target.env, ...pick(env, serviceKeys(kind)) } }, saved);
|
|
329
|
+
updates[mk] = model;
|
|
330
|
+
}
|
|
331
|
+
return applyConfig(updates);
|
|
332
|
+
}
|
|
333
|
+
|
|
275
334
|
async function configureModel(arg: string | undefined) {
|
|
276
335
|
const info = providerInfo();
|
|
277
336
|
// 之前只有 .env 时配置的模型也记下来,之后换了不会丢
|
|
@@ -317,8 +376,8 @@ export function configurator(opts: ConfigureOptions): Configure {
|
|
|
317
376
|
const gone = await pickSaved(owners, "Remove a model from which service?", (s, m) => !(serviceId(s.env) === currentId && m.name === info.model));
|
|
318
377
|
if (!gone) return;
|
|
319
378
|
const id = serviceId(gone.service.env);
|
|
320
|
-
//
|
|
321
|
-
const rest = (await
|
|
379
|
+
// 服务下没有模型了就连服务(和它的密钥)一起删掉;认不出的服务原样保留
|
|
380
|
+
const rest = (await readSavedServices(modelsFile))
|
|
322
381
|
.map((s) => (serviceId(s.env) === id ? { ...s, models: s.models.filter((m) => m.name !== gone.model.name) } : s))
|
|
323
382
|
.filter((s) => s.models.length > 0);
|
|
324
383
|
await writeSavedServices(modelsFile, rest);
|
|
@@ -329,7 +388,7 @@ export function configurator(opts: ConfigureOptions): Configure {
|
|
|
329
388
|
/** 当前模型自己的设置(切换模型时跟着换):输出上限、思考力度、上下文窗口、限速、能不能看图 */
|
|
330
389
|
async function configureSettings() {
|
|
331
390
|
const info = providerInfo();
|
|
332
|
-
const efforts = EFFORTS[info.kind];
|
|
391
|
+
const efforts = isBuiltinKind(info.kind) ? EFFORTS[info.kind] : undefined;
|
|
333
392
|
const action = await screen.select<string>(`Settings for ${info.model}`, [
|
|
334
393
|
{ label: "Output limit per reply", value: "max", hint: process.env.JADE_MAX_TOKENS ? `${process.env.JADE_MAX_TOKENS} tokens` : "service default" },
|
|
335
394
|
...(efforts
|
|
@@ -364,7 +423,11 @@ export function configurator(opts: ConfigureOptions): Configure {
|
|
|
364
423
|
const choice = await screen.select("Can this model see images (screenshots, image files)?", [
|
|
365
424
|
{ label: "Yes", value: "on", hint: current === true ? "current" : "the screenshot tool becomes available" },
|
|
366
425
|
{ label: "No", value: "off", hint: current === false ? "current" : "images are replaced with a note" },
|
|
367
|
-
{
|
|
426
|
+
{
|
|
427
|
+
label: "Default",
|
|
428
|
+
value: DEFAULT,
|
|
429
|
+
hint: info.kind === "openai-compat" ? "no for compatible services" : isBuiltinKind(info.kind) ? "yes" : "no for plugin services",
|
|
430
|
+
},
|
|
368
431
|
]);
|
|
369
432
|
if (choice === undefined) return;
|
|
370
433
|
// 换了 provider 立即生效:图片发不发、截图工具给不给模型都在每次请求时按它判断,不用重启
|
package/src/ui/diff.ts
CHANGED
|
@@ -110,8 +110,7 @@ export function diffStats(lines: DiffLine[]): DiffStats {
|
|
|
110
110
|
* 显示用:每行前面是行号(新增行用新文件的行号,其余用旧文件的),段与段之间用 ⋯ 分开。
|
|
111
111
|
* 超过 maxLines 行时截断
|
|
112
112
|
*/
|
|
113
|
-
export function renderDiff(before: string, after: string, maxLines = 60): string {
|
|
114
|
-
const lines = diffLines(before, after);
|
|
113
|
+
export function renderDiff(before: string, after: string, maxLines = 60, lines = diffLines(before, after)): string {
|
|
115
114
|
const parts = hunks(lines);
|
|
116
115
|
if (parts.length === 0) return dim(" (no changes)");
|
|
117
116
|
const width = String(lines.reduce((w, l) => Math.max(w, l.old ?? 0, l.new ?? 0), 0)).length;
|
package/src/ui/editor.ts
CHANGED
|
@@ -84,7 +84,7 @@ export interface Layout {
|
|
|
84
84
|
|
|
85
85
|
/** 单个字符的显示宽度;每次按键都要把整段输入过一遍,stringWidth 较慢,ASCII 直接算,其余缓存 */
|
|
86
86
|
const widths = new Map<string, number>();
|
|
87
|
-
function charWidth(ch: string): number {
|
|
87
|
+
export function charWidth(ch: string): number {
|
|
88
88
|
const c = ch.charCodeAt(0);
|
|
89
89
|
if (c >= 0x20 && c < 0x7f) return 1;
|
|
90
90
|
let w = widths.get(ch);
|
|
@@ -92,6 +92,55 @@ function charWidth(ch: string): number {
|
|
|
92
92
|
return w;
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
+
/** 光标可以停的每个位置(码点边界)在折行后的行和列,和 layout 的折法一致 */
|
|
96
|
+
function stops(value: string, width: number): { i: number; row: number; col: number }[] {
|
|
97
|
+
width = Math.max(2, width);
|
|
98
|
+
const out: { i: number; row: number; col: number }[] = [];
|
|
99
|
+
let row = 0;
|
|
100
|
+
let w = 0;
|
|
101
|
+
let i = 0;
|
|
102
|
+
// 正好停在满行末尾时算下一行开头(同 layout)
|
|
103
|
+
const stop = (at: number) => out.push(w >= width ? { i: at, row: row + 1, col: 0 } : { i: at, row, col: w });
|
|
104
|
+
for (const ch of value) {
|
|
105
|
+
if (ch === "\n") {
|
|
106
|
+
stop(i);
|
|
107
|
+
row++;
|
|
108
|
+
w = 0;
|
|
109
|
+
} else {
|
|
110
|
+
const cw = charWidth(ch);
|
|
111
|
+
if (w + cw > width) {
|
|
112
|
+
row++;
|
|
113
|
+
w = 0;
|
|
114
|
+
}
|
|
115
|
+
stop(i);
|
|
116
|
+
w += cw;
|
|
117
|
+
}
|
|
118
|
+
i += ch.length;
|
|
119
|
+
}
|
|
120
|
+
stop(value.length);
|
|
121
|
+
return out;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** 折行后第 row 行里最接近第 col 列(不超过它)的光标位置;没有这一行时返回 undefined */
|
|
125
|
+
export function offsetAt(value: string, width: number, row: number, col: number): number | undefined {
|
|
126
|
+
const inRow = stops(value, width).filter((s) => s.row === row);
|
|
127
|
+
if (inRow.length === 0) return undefined;
|
|
128
|
+
return (inRow.findLast((s) => s.col <= col) ?? inRow[0]!).i;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 按折行后的屏幕行上下移动光标,保持显示列(中文占两列)。已经在第一行 / 最后一行时返回 undefined,
|
|
133
|
+
* 由调用方去翻历史
|
|
134
|
+
*/
|
|
135
|
+
export function moveRow(s: EditState, width: number, by: -1 | 1): EditState | undefined {
|
|
136
|
+
const all = stops(s.value, width);
|
|
137
|
+
const here = all.find((x) => x.i === s.cursor) ?? all.at(-1)!;
|
|
138
|
+
const target = here.row + by;
|
|
139
|
+
if (target < 0 || target > all.at(-1)!.row) return undefined;
|
|
140
|
+
const cursor = offsetAt(s.value, width, target, here.col);
|
|
141
|
+
return cursor === undefined ? undefined : { ...s, cursor };
|
|
142
|
+
}
|
|
143
|
+
|
|
95
144
|
/** 按显示宽度折行(按字符折,不按词),并算出光标位置。width 至少为 2 */
|
|
96
145
|
export function layout(value: string, cursor: number, width: number): Layout {
|
|
97
146
|
width = Math.max(2, width);
|