pi-model-manager 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.en.md +113 -20
- package/README.md +113 -20
- package/configuration-persistence.ts +11 -7
- package/index.ts +20 -1
- package/package.json +2 -3
- package/presets/builtin-client-headers.ts +7 -7
- package/presets/providers.ts +4 -1
- package/runtime-base-url.ts +9 -3
- package/state-document.ts +23 -0
- package/tui/dashboard.ts +47 -21
- package/tui/editor-model.ts +56 -95
- package/tui/editor-provider.ts +59 -59
- package/tui/header-profiles-panel.ts +40 -23
- package/tui/model-picker.ts +5 -3
- package/tui/persistent-menu.ts +229 -86
- package/tui/ui-helpers.ts +140 -84
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// 管理可复用的客户端请求头。接入编辑器只选择已存在的请求头,
|
|
4
4
|
// 不在模型内部新增一次性 JSON,避免配置分散。
|
|
5
5
|
|
|
6
|
-
import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import type { ExtensionAPI, ExtensionCommandContext, Theme } from "@earendil-works/pi-coding-agent";
|
|
7
7
|
import { formatUnknownError, parseStringRecordJson } from "../common.ts";
|
|
8
8
|
import { deleteRequestHeaderProfileConfiguration, saveRequestHeaderProfileConfiguration } from "../header-profile-mutations.ts";
|
|
9
9
|
import { CLAUDE_CODE_CLIENT_HEADERS, CODEX_CLI_CLIENT_HEADERS } from "../presets/builtin-client-headers.ts";
|
|
@@ -14,13 +14,16 @@ import {
|
|
|
14
14
|
validateRequestHeaderProfileDraft,
|
|
15
15
|
} from "../state-document.ts";
|
|
16
16
|
import { readState } from "../state-store.ts";
|
|
17
|
-
import type { RequestHeaderProfileDraft, StoredClientHeaderCapture, StoredRequestHeaderProfile } from "../types.ts";
|
|
17
|
+
import type { RequestHeaderProfileDraft, StateDocument, StoredClientHeaderCapture, StoredRequestHeaderProfile } from "../types.ts";
|
|
18
18
|
import { padLabel, showPersistentFormMenu, showPersistentShortcutMenu, type MenuCursor } from "./persistent-menu.ts";
|
|
19
|
-
import { fitColumn } from "./ui-helpers.ts";
|
|
19
|
+
import { fitColumn, formatDetailField, formatDetailTitle } from "./ui-helpers.ts";
|
|
20
20
|
|
|
21
21
|
interface ProfileRow {
|
|
22
22
|
profileId: string;
|
|
23
23
|
label: string;
|
|
24
|
+
searchText: string;
|
|
25
|
+
// [喵喵喵]: 引用计数要遍历全部 Provider,每帧重算浪费;构造行时算一次即可。
|
|
26
|
+
usedCount: number;
|
|
24
27
|
}
|
|
25
28
|
|
|
26
29
|
interface FieldRow {
|
|
@@ -57,8 +60,13 @@ const HEADER_TEMPLATES: HeaderTemplate[] = [
|
|
|
57
60
|
},
|
|
58
61
|
];
|
|
59
62
|
|
|
60
|
-
function
|
|
61
|
-
return profileIds.map((profileId) =>
|
|
63
|
+
function buildProfileRows(document: StateDocument, profileIds: string[]): ProfileRow[] {
|
|
64
|
+
return profileIds.map((profileId) => {
|
|
65
|
+
const profile = document.requestHeaderProfiles[profileId]!;
|
|
66
|
+
const usedCount = countModelsUsingRequestHeaderProfile(document, profileId);
|
|
67
|
+
const label = formatProfileRow(profileId, profile, usedCount);
|
|
68
|
+
return { profileId, label, usedCount, searchText: `${profileId} ${profile.name} ${label}` };
|
|
69
|
+
});
|
|
62
70
|
}
|
|
63
71
|
|
|
64
72
|
function formatCapturedAt(capturedAt: string): string {
|
|
@@ -121,15 +129,20 @@ function formatProfileTableHeader(menuWidth: number): string {
|
|
|
121
129
|
}, Math.max(0, menuWidth - 2))}`;
|
|
122
130
|
}
|
|
123
131
|
|
|
124
|
-
function formatProfileDetailLines(
|
|
132
|
+
function formatProfileDetailLines(
|
|
133
|
+
profileId: string,
|
|
134
|
+
profile: StoredRequestHeaderProfile,
|
|
135
|
+
usedCount: number,
|
|
136
|
+
theme?: Theme,
|
|
137
|
+
): string[] {
|
|
125
138
|
const headerNames = Object.keys(profile.headers);
|
|
126
139
|
const preview = headerNames.length > 0
|
|
127
140
|
? headerNames.slice(0, 6).join(", ") + (headerNames.length > 6 ? ", ..." : "")
|
|
128
141
|
: "<empty>";
|
|
129
142
|
return [
|
|
130
|
-
`${profile.name} (${profileId})`,
|
|
131
|
-
|
|
132
|
-
|
|
143
|
+
formatDetailTitle(`${profile.name} (${profileId})`, theme),
|
|
144
|
+
formatDetailField("usage", `${usedCount} models`, theme),
|
|
145
|
+
formatDetailField("headers", preview, theme),
|
|
133
146
|
];
|
|
134
147
|
}
|
|
135
148
|
|
|
@@ -225,7 +238,12 @@ async function editProfile(
|
|
|
225
238
|
summaryLines: [
|
|
226
239
|
"认证类敏感 header 会被拒绝;API key 请放在接入配置中",
|
|
227
240
|
],
|
|
228
|
-
|
|
241
|
+
hints: [
|
|
242
|
+
{ key: "↑↓", label: "选择" },
|
|
243
|
+
{ key: "Enter", label: "编辑" },
|
|
244
|
+
{ key: "Ctrl+S", label: "保存并同步" },
|
|
245
|
+
{ key: "Esc", label: "返回" },
|
|
246
|
+
],
|
|
229
247
|
},
|
|
230
248
|
);
|
|
231
249
|
if (action.type === "cancel") return { action: "cancel" };
|
|
@@ -296,19 +314,12 @@ export async function runHeaderProfilesPanel(pi: ExtensionAPI, ctx: ExtensionCom
|
|
|
296
314
|
while (true) {
|
|
297
315
|
const state = await readState();
|
|
298
316
|
const profileIds = Object.keys(state.requestHeaderProfiles).sort((a, b) => a.localeCompare(b));
|
|
299
|
-
const rows =
|
|
300
|
-
const profile = state.requestHeaderProfiles[row.profileId]!;
|
|
301
|
-
const usedCount = countModelsUsingRequestHeaderProfile(state, row.profileId);
|
|
302
|
-
return {
|
|
303
|
-
...row,
|
|
304
|
-
label: formatProfileRow(row.profileId, profile, usedCount),
|
|
305
|
-
};
|
|
306
|
-
});
|
|
317
|
+
const rows = buildProfileRows(state, profileIds);
|
|
307
318
|
const action = await showPersistentShortcutMenu<HeaderProfileShortcut>(
|
|
308
319
|
ctx,
|
|
309
320
|
"/model-manager / 请求头",
|
|
310
321
|
"",
|
|
311
|
-
rows.map((row, index) => ({ id: `${index}`, label: row.label })),
|
|
322
|
+
rows.map((row, index) => ({ id: `${index}`, label: row.label, searchText: row.searchText })),
|
|
312
323
|
cursor,
|
|
313
324
|
[
|
|
314
325
|
{ input: "n", shortcut: "new-profile" },
|
|
@@ -326,17 +337,23 @@ export async function runHeaderProfilesPanel(pi: ExtensionAPI, ctx: ExtensionCom
|
|
|
326
337
|
const row = rows[Number.parseInt(menuRow.id, 10)];
|
|
327
338
|
const profile = row ? state.requestHeaderProfiles[row.profileId] : undefined;
|
|
328
339
|
return row && profile
|
|
329
|
-
? formatProfileRow(row.profileId, profile,
|
|
340
|
+
? formatProfileRow(row.profileId, profile, row.usedCount, width)
|
|
330
341
|
: menuRow.label;
|
|
331
342
|
},
|
|
332
|
-
getDetailLines: (selectedRow) => {
|
|
343
|
+
getDetailLines: (selectedRow, theme) => {
|
|
333
344
|
const row = rows[Number.parseInt(selectedRow?.id ?? "", 10)];
|
|
334
345
|
const profile = row ? state.requestHeaderProfiles[row.profileId] : undefined;
|
|
335
346
|
return row && profile
|
|
336
|
-
? formatProfileDetailLines(row.profileId, profile,
|
|
347
|
+
? formatProfileDetailLines(row.profileId, profile, row.usedCount, theme)
|
|
337
348
|
: [];
|
|
338
349
|
},
|
|
339
|
-
|
|
350
|
+
hints: [
|
|
351
|
+
{ key: "↑↓", label: "选择" },
|
|
352
|
+
{ key: "Enter", label: "编辑请求头" },
|
|
353
|
+
{ key: "N", label: "新建自定义" },
|
|
354
|
+
{ key: "D", label: "删除" },
|
|
355
|
+
{ key: "Esc", label: "返回" },
|
|
356
|
+
],
|
|
340
357
|
emptyLabel: "暂无自定义请求头;按 n 新建",
|
|
341
358
|
},
|
|
342
359
|
);
|
package/tui/model-picker.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
6
6
|
import { CURSOR_MARKER, Key, matchesKey, truncateToWidth } from "@earendil-works/pi-tui";
|
|
7
|
+
import { padLabel } from "./persistent-menu.ts";
|
|
7
8
|
|
|
8
9
|
const MODEL_PAGE_SIZE = 8;
|
|
9
10
|
const EXACT_MATCH_SCORE = 50_000;
|
|
@@ -227,7 +228,7 @@ export async function pickModelIdFromList(
|
|
|
227
228
|
selectedIndex = clampSelectedIndex(selectedIndex, matchedModelIds.length);
|
|
228
229
|
const pageStart = getPageStart(selectedIndex);
|
|
229
230
|
const visibleModelIds = matchedModelIds.slice(pageStart, pageStart + MODEL_PAGE_SIZE);
|
|
230
|
-
const border = theme.fg("borderMuted", "─".repeat(Math.max(0,
|
|
231
|
+
const border = theme.fg("borderMuted", "─".repeat(Math.max(0, width)));
|
|
231
232
|
const queryCursor = focused ? `${CURSOR_MARKER}${theme.fg("accent", "▌")}` : "";
|
|
232
233
|
const searchLine = searchText
|
|
233
234
|
? `${theme.fg("muted", "搜索: ")}${searchText}${queryCursor}`
|
|
@@ -249,8 +250,9 @@ export async function pickModelIdFromList(
|
|
|
249
250
|
const selected = absoluteIndex === selectedIndex;
|
|
250
251
|
const currentSuffix = modelId === normalizedCurrentModelId ? theme.fg("dim", " ← 当前") : "";
|
|
251
252
|
const prefix = selected ? "❯ " : " ";
|
|
252
|
-
const rowText = `${prefix}${modelId}${currentSuffix}
|
|
253
|
-
|
|
253
|
+
const rowText = truncateToWidth(`${prefix}${modelId}${currentSuffix}`, width);
|
|
254
|
+
// [喵喵喵]: 与列表菜单用同一种选中表现(背景色),避免两套选择器手感不一致。
|
|
255
|
+
lines.push(selected ? theme.bg("selectedBg", padLabel(rowText, width)) : rowText);
|
|
254
256
|
}
|
|
255
257
|
}
|
|
256
258
|
|
package/tui/persistent-menu.ts
CHANGED
|
@@ -6,28 +6,41 @@
|
|
|
6
6
|
// 设计:列表页保留 KISS 的键盘模型,同时支持摘要、列头、详情区、底部快捷键。
|
|
7
7
|
// 表单页另有 Ctrl+S 保存;列表页可注册单键快捷操作,并用 / 做轻量过滤。
|
|
8
8
|
|
|
9
|
-
import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
9
|
+
import type { ExtensionCommandContext, Theme } from "@earendil-works/pi-coding-agent";
|
|
10
10
|
import { CURSOR_MARKER, Key, matchesKey, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
11
11
|
|
|
12
12
|
export interface MenuRow {
|
|
13
13
|
id: string;
|
|
14
14
|
label: string;
|
|
15
|
-
|
|
15
|
+
// [喵喵喵]: label 是按列宽截断后的表格文本,长 ID 的后半段不在其中;
|
|
16
|
+
// 需要按真实标识搜索的调用方必须显式提供未截断的 searchText。
|
|
17
|
+
searchText?: string;
|
|
18
|
+
// [喵喵喵]: 可调整性属于行自身;开关切换会让字段集变化(如开 Thinking 后才出现 Adaptive 行),
|
|
19
|
+
// 放在外部集合里就会在行重建后与实际行脱节,新出现的开关行按 ←→ 没反应。
|
|
20
|
+
adjustable?: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// 底部快捷键提示;key 与说明分开才能分别着色,并在窄终端按项折行而不是被截掉。
|
|
24
|
+
export interface MenuHint {
|
|
25
|
+
key: string;
|
|
26
|
+
label: string;
|
|
16
27
|
}
|
|
17
28
|
|
|
18
29
|
export interface PersistentMenuOptions {
|
|
19
30
|
summaryLines?: readonly string[];
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
31
|
+
// [喵喵喵]: 渲染回调拿到 theme 才能给列与详情上语义色;theme 只存在于 custom 回调作用域。
|
|
32
|
+
tableHeader?: string | ((width: number, theme: Theme) => string);
|
|
33
|
+
formatRow?: (row: MenuRow, width: number, theme: Theme) => string;
|
|
34
|
+
getDetailLines?: (selectedRow: MenuRow | undefined, theme: Theme) => readonly string[];
|
|
35
|
+
hints?: readonly MenuHint[];
|
|
24
36
|
emptyLabel?: string;
|
|
25
37
|
visibleRows?: number;
|
|
26
38
|
searchable?: boolean;
|
|
27
39
|
}
|
|
28
40
|
|
|
29
41
|
export interface PersistentFormMenuOptions extends PersistentMenuOptions {
|
|
30
|
-
|
|
42
|
+
// 就地切换该字段并返回重建后的行;返回 undefined 则忽略本次按键。
|
|
43
|
+
onAdjust?: (id: string, direction: HorizontalDirection) => MenuRow[] | undefined;
|
|
31
44
|
}
|
|
32
45
|
|
|
33
46
|
export type MenuAction =
|
|
@@ -35,7 +48,8 @@ export type MenuAction =
|
|
|
35
48
|
| { type: "cancel" };
|
|
36
49
|
|
|
37
50
|
export type HorizontalDirection = "left" | "right";
|
|
38
|
-
|
|
51
|
+
// 横向切换已改为组件内处理,不再作为外部动作返回。
|
|
52
|
+
export type FormMenuAction = MenuAction | { type: "save" };
|
|
39
53
|
|
|
40
54
|
export type ShortcutMenuAction<TShortcut extends string = string> = MenuAction | { type: "shortcut"; shortcut: TShortcut };
|
|
41
55
|
|
|
@@ -62,13 +76,52 @@ export function padLabel(label: string, columns: number): string {
|
|
|
62
76
|
return padToVisibleWidth(label, columns);
|
|
63
77
|
}
|
|
64
78
|
|
|
65
|
-
function
|
|
66
|
-
|
|
67
|
-
return typeof source === "string" ? source.split("\n") : [...(source ?? [])];
|
|
79
|
+
function getSearchText(row: MenuRow): string {
|
|
80
|
+
return (row.searchText ?? `${row.id}\n${row.label}`).toLocaleLowerCase();
|
|
68
81
|
}
|
|
69
82
|
|
|
70
|
-
|
|
71
|
-
|
|
83
|
+
const HINT_GAP = " ";
|
|
84
|
+
|
|
85
|
+
// 按项贪心排版:单行装不下就换行,保证窄终端下 Esc 等尾部提示不会被 truncate 丢失。
|
|
86
|
+
function layoutHintLines(hints: readonly MenuHint[], theme: Theme, width: number): string[] {
|
|
87
|
+
const lines: string[] = [];
|
|
88
|
+
let currentText = "";
|
|
89
|
+
let currentWidth = 0;
|
|
90
|
+
for (const hint of hints) {
|
|
91
|
+
const hintWidth = visibleWidth(`${hint.key} ${hint.label}`);
|
|
92
|
+
const styled = `${theme.fg("accent", hint.key)} ${theme.fg("dim", hint.label)}`;
|
|
93
|
+
if (!currentText) {
|
|
94
|
+
currentText = styled;
|
|
95
|
+
currentWidth = hintWidth;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (currentWidth + HINT_GAP.length + hintWidth > width) {
|
|
99
|
+
lines.push(currentText);
|
|
100
|
+
currentText = styled;
|
|
101
|
+
currentWidth = hintWidth;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
currentText += `${HINT_GAP}${styled}`;
|
|
105
|
+
currentWidth += HINT_GAP.length + hintWidth;
|
|
106
|
+
}
|
|
107
|
+
if (currentText) lines.push(currentText);
|
|
108
|
+
return lines;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// 滚动条字符在不同终端的宽度属于 ambiguous,因此固定预留 2 列,宁可多一个空格也不让行溢出。
|
|
112
|
+
const SCROLLBAR_WIDTH = 2;
|
|
113
|
+
|
|
114
|
+
// pi 在菜单下方还要渲染输入框与状态行,不预留就会把菜单底部的快捷键提示顶出屏幕。
|
|
115
|
+
const RESERVED_TERMINAL_ROWS = 3;
|
|
116
|
+
// 小于这个高度就无法同时容纳边框、标题、提示和列表,此时宁可溢出也不再继续压缩。
|
|
117
|
+
const MIN_MENU_ROWS = 8;
|
|
118
|
+
const MIN_LIST_ROWS = 1;
|
|
119
|
+
|
|
120
|
+
function getScrollbarGlyph(rowOffset: number, viewportRows: number, windowStart: number, totalRows: number): string {
|
|
121
|
+
const thumbRows = Math.max(1, Math.round((viewportRows * viewportRows) / totalRows));
|
|
122
|
+
const maxWindowStart = Math.max(1, totalRows - viewportRows);
|
|
123
|
+
const thumbStart = Math.round((windowStart / maxWindowStart) * (viewportRows - thumbRows));
|
|
124
|
+
return rowOffset >= thumbStart && rowOffset < thumbStart + thumbRows ? "█" : "│";
|
|
72
125
|
}
|
|
73
126
|
|
|
74
127
|
function filterRows(rows: MenuRow[], query: string): MenuRow[] {
|
|
@@ -115,8 +168,10 @@ function createPersistentMenu<TAction extends MenuAction | FormMenuAction | Shor
|
|
|
115
168
|
cursor: MenuCursor,
|
|
116
169
|
createSaveAction: (() => TAction) | undefined,
|
|
117
170
|
shortcuts: MenuShortcut[],
|
|
118
|
-
|
|
119
|
-
|
|
171
|
+
// [喵喵喵]: 横向切换是纯本地状态变更,必须在组件内完成。早期实现用 done() 结束组件再由
|
|
172
|
+
// 调用方重开,每按一次方向键都会销毁重建整个 TUI,在终端上表现为闪烁。
|
|
173
|
+
// 回调就地改写草稿并返回新行,返回 undefined 表示本次不可调整。
|
|
174
|
+
onAdjust: ((id: string, direction: HorizontalDirection) => MenuRow[] | undefined) | undefined = undefined,
|
|
120
175
|
options: PersistentMenuOptions = {},
|
|
121
176
|
): Promise<TAction> {
|
|
122
177
|
cursor.index = clampIndex(cursor.index, rows.length);
|
|
@@ -126,14 +181,19 @@ function createPersistentMenu<TAction extends MenuAction | FormMenuAction | Shor
|
|
|
126
181
|
let searchQuery = "";
|
|
127
182
|
let searchCursor = 0;
|
|
128
183
|
let focused = false;
|
|
129
|
-
const
|
|
184
|
+
const configuredVisibleRows = options.visibleRows ?? 18;
|
|
185
|
+
// [喵喵喵]: 真实可见行数要减去本帧的标题/摘要/详情/提示,每帧在 render 里重算,
|
|
186
|
+
// 翻页也必须用同一个值,否则矮终端上 PgUp/PgDn 会跳过看不见的行。
|
|
187
|
+
let viewportRows = configuredVisibleRows;
|
|
130
188
|
const searchable = options.searchable ?? false;
|
|
131
189
|
|
|
132
|
-
|
|
190
|
+
// onAdjust 会整批替换行数据,因此不能直接闭包参数 rows。
|
|
191
|
+
let currentRows = rows;
|
|
192
|
+
const getActiveRows = (): MenuRow[] => searchable ? filterRows(currentRows, searchQuery) : currentRows;
|
|
133
193
|
|
|
134
194
|
const syncCursor = (activeRows: MenuRow[]): void => {
|
|
135
195
|
const row = activeRows[selectedIndex];
|
|
136
|
-
if (row) cursor.index =
|
|
196
|
+
if (row) cursor.index = currentRows.indexOf(row);
|
|
137
197
|
};
|
|
138
198
|
|
|
139
199
|
const requestRender = (): void => {
|
|
@@ -148,7 +208,7 @@ function createPersistentMenu<TAction extends MenuAction | FormMenuAction | Shor
|
|
|
148
208
|
searchActive = false;
|
|
149
209
|
searchQuery = "";
|
|
150
210
|
searchCursor = 0;
|
|
151
|
-
selectedIndex = clampIndex(cursor.index,
|
|
211
|
+
selectedIndex = clampIndex(cursor.index, currentRows.length);
|
|
152
212
|
requestRender();
|
|
153
213
|
return true;
|
|
154
214
|
};
|
|
@@ -175,6 +235,35 @@ function createPersistentMenu<TAction extends MenuAction | FormMenuAction | Shor
|
|
|
175
235
|
selectedIndex = nextIndex;
|
|
176
236
|
requestRender();
|
|
177
237
|
};
|
|
238
|
+
// 输入态显示带光标的搜索行;Tab 退出后仍需告知用户过滤仍生效。
|
|
239
|
+
const renderQueryLine = (width: number): string => {
|
|
240
|
+
if (!searchActive) {
|
|
241
|
+
return truncateToWidth(`${theme.fg("dim", "过滤:")}${searchQuery}`, width, "");
|
|
242
|
+
}
|
|
243
|
+
const cursorGlyph = focused ? `${CURSOR_MARKER}${theme.fg("accent", "▌")}` : "";
|
|
244
|
+
const searchPrefix = theme.fg("accent", "搜索:");
|
|
245
|
+
const queryWidth = Math.max(0, width - visibleWidth(searchPrefix));
|
|
246
|
+
const queryDisplay = searchQuery
|
|
247
|
+
? fitSearchQueryAroundCursor(searchQuery, searchCursor, cursorGlyph, queryWidth)
|
|
248
|
+
: `${cursorGlyph}${theme.fg("dim", "<输入关键词>")}`;
|
|
249
|
+
return truncateToWidth(`${searchPrefix}${queryDisplay}`, width, "");
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
const getHints = (): MenuHint[] => {
|
|
253
|
+
const hints: MenuHint[] = [...(options.hints ?? [])];
|
|
254
|
+
if (!searchable) return hints;
|
|
255
|
+
if (searchActive) hints.push({ key: "Tab", label: "保留过滤" }, { key: "Esc", label: "清空搜索" });
|
|
256
|
+
else if (searchQuery) hints.push({ key: "Tab", label: "继续输入" }, { key: "Esc", label: "清空过滤" });
|
|
257
|
+
else hints.push({ key: "/", label: "搜索" });
|
|
258
|
+
return hints;
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
// 终端高度未知(如测试环境)时不限制总行数,交由调用方配置的 visibleRows 控制。
|
|
262
|
+
const getRowBudget = (): number => {
|
|
263
|
+
const terminalRows = tui.terminal?.rows ?? 0;
|
|
264
|
+
if (terminalRows <= 0) return Number.POSITIVE_INFINITY;
|
|
265
|
+
return Math.max(MIN_MENU_ROWS, terminalRows - RESERVED_TERMINAL_ROWS);
|
|
266
|
+
};
|
|
178
267
|
|
|
179
268
|
return {
|
|
180
269
|
get focused(): boolean {
|
|
@@ -195,11 +284,15 @@ function createPersistentMenu<TAction extends MenuAction | FormMenuAction | Shor
|
|
|
195
284
|
: matchesKey(data, Key.right)
|
|
196
285
|
? "right"
|
|
197
286
|
: undefined;
|
|
198
|
-
if (horizontalDirection &&
|
|
287
|
+
if (horizontalDirection && onAdjust) {
|
|
199
288
|
const row = getActiveRows()[selectedIndex];
|
|
200
|
-
if (row
|
|
289
|
+
if (row?.adjustable) {
|
|
201
290
|
syncCursor(getActiveRows());
|
|
202
|
-
|
|
291
|
+
const nextRows = onAdjust(row.id, horizontalDirection);
|
|
292
|
+
if (nextRows) {
|
|
293
|
+
currentRows = nextRows;
|
|
294
|
+
requestRender();
|
|
295
|
+
}
|
|
203
296
|
}
|
|
204
297
|
return;
|
|
205
298
|
}
|
|
@@ -246,6 +339,12 @@ function createPersistentMenu<TAction extends MenuAction | FormMenuAction | Shor
|
|
|
246
339
|
requestRender();
|
|
247
340
|
return;
|
|
248
341
|
}
|
|
342
|
+
// [喵喵喵]: Tab 只退出输入态并保留过滤结果,让用户搜到目标后还能按单字母快捷键;Esc 才清空。
|
|
343
|
+
if (matchesKey(data, Key.tab)) {
|
|
344
|
+
searchActive = false;
|
|
345
|
+
requestRender();
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
249
348
|
if (matchesKey(data, Key.enter)) {
|
|
250
349
|
pickSelected();
|
|
251
350
|
return;
|
|
@@ -257,12 +356,19 @@ function createPersistentMenu<TAction extends MenuAction | FormMenuAction | Shor
|
|
|
257
356
|
}
|
|
258
357
|
}
|
|
259
358
|
|
|
260
|
-
|
|
359
|
+
// [喵喵喵]: 提示里快捷键显示为大写,因此 Shift 组合也必须命中同一个动作。
|
|
360
|
+
const shortcut = shortcuts.find((candidate) => candidate.input.toLowerCase() === data.toLowerCase());
|
|
261
361
|
if (shortcut) {
|
|
262
362
|
syncCursor(getActiveRows());
|
|
263
363
|
done({ type: "shortcut", shortcut: shortcut.shortcut } as TAction);
|
|
264
364
|
return;
|
|
265
365
|
}
|
|
366
|
+
if (searchable && searchQuery && matchesKey(data, Key.tab)) {
|
|
367
|
+
searchActive = true;
|
|
368
|
+
searchCursor = Array.from(searchQuery).length;
|
|
369
|
+
requestRender();
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
266
372
|
if (searchable && data === "/") {
|
|
267
373
|
searchActive = true;
|
|
268
374
|
searchCursor = Array.from(searchQuery).length;
|
|
@@ -289,11 +395,11 @@ function createPersistentMenu<TAction extends MenuAction | FormMenuAction | Shor
|
|
|
289
395
|
return;
|
|
290
396
|
}
|
|
291
397
|
if (matchesKey(data, Key.pageUp)) {
|
|
292
|
-
moveSelection(Math.max(0, selectedIndex -
|
|
398
|
+
moveSelection(Math.max(0, selectedIndex - viewportRows));
|
|
293
399
|
return;
|
|
294
400
|
}
|
|
295
401
|
if (matchesKey(data, Key.pageDown)) {
|
|
296
|
-
moveSelection(Math.min(activeRows.length - 1, selectedIndex +
|
|
402
|
+
moveSelection(Math.min(activeRows.length - 1, selectedIndex + viewportRows));
|
|
297
403
|
return;
|
|
298
404
|
}
|
|
299
405
|
if (matchesKey(data, Key.home)) {
|
|
@@ -309,83 +415,93 @@ function createPersistentMenu<TAction extends MenuAction | FormMenuAction | Shor
|
|
|
309
415
|
const activeRows = getActiveRows();
|
|
310
416
|
selectedIndex = clampIndex(selectedIndex, activeRows.length);
|
|
311
417
|
syncCursor(activeRows);
|
|
312
|
-
const windowStart = Math.max(
|
|
313
|
-
0,
|
|
314
|
-
Math.min(selectedIndex - Math.floor(visibleRows / 2), Math.max(0, activeRows.length - visibleRows)),
|
|
315
|
-
);
|
|
316
|
-
const shownRows = activeRows.slice(windowStart, windowStart + visibleRows);
|
|
317
418
|
|
|
318
|
-
const border = theme.fg("borderMuted", "─".repeat(Math.max(0,
|
|
319
|
-
const
|
|
320
|
-
const
|
|
419
|
+
const border = theme.fg("borderMuted", "─".repeat(Math.max(0, width)));
|
|
420
|
+
const hintLines = layoutHintLines(getHints(), theme, width);
|
|
421
|
+
const summaryLines = (options.summaryLines ?? (help ? help.split("\n") : []))
|
|
422
|
+
.map((line) => truncateToWidth(theme.fg("dim", line), width));
|
|
423
|
+
const searchLine = searchable && (searchActive || searchQuery) ? renderQueryLine(width) : undefined;
|
|
424
|
+
const tableHeaderText = typeof options.tableHeader === "function"
|
|
425
|
+
? options.tableHeader(width, theme)
|
|
426
|
+
: options.tableHeader;
|
|
427
|
+
// [喵喵喵]: 详情文本由调用方按语义着色,这里只负责裁剪,避免外层样式与内层 reset 互相打断。
|
|
428
|
+
const detailLines = (options.getDetailLines?.(activeRows[selectedIndex], theme) ?? [])
|
|
429
|
+
.map((line) => truncateToWidth(line, width));
|
|
430
|
+
|
|
431
|
+
// [喵喵喵]: 矮终端下按优先级降级:上下边框、标题、快捷键提示和最小列表必须保留,
|
|
432
|
+
// 剩余空间才依次发给搜索行、表头、摘要、详情;否则总行数超过终端高度时底部提示会被裁掉。
|
|
433
|
+
const budget = getRowBudget();
|
|
434
|
+
const essentialRows = 2 + 1 + 1 + (hintLines.length > 0 ? hintLines.length + 1 : 0);
|
|
435
|
+
// [喵喵喵]: 列表装不下时底部会多一行位置提示,先按最坏情况预留;
|
|
436
|
+
// 放到算出 viewportRows 之后再扣,在列表已压到最小时就无处可扣了。
|
|
437
|
+
const scrollHintRows = activeRows.length > MIN_LIST_ROWS ? 1 : 0;
|
|
438
|
+
let spare = budget - essentialRows - MIN_LIST_ROWS - scrollHintRows;
|
|
439
|
+
|
|
440
|
+
const showSearchLine = searchLine !== undefined && spare >= 1;
|
|
441
|
+
if (showSearchLine) spare -= 1;
|
|
442
|
+
const showTableHeader = Boolean(tableHeaderText) && spare >= 1;
|
|
443
|
+
if (showTableHeader) spare -= 1;
|
|
444
|
+
const showSummary = summaryLines.length > 0 && spare >= summaryLines.length;
|
|
445
|
+
if (showSummary) spare -= summaryLines.length;
|
|
446
|
+
const detailBlockRows = detailLines.length > 0 ? detailLines.length + 2 : 0;
|
|
447
|
+
const showDetail = detailBlockRows > 0 && spare >= detailBlockRows;
|
|
448
|
+
if (showDetail) spare -= detailBlockRows;
|
|
449
|
+
|
|
450
|
+
viewportRows = Math.min(configuredVisibleRows, MIN_LIST_ROWS + Math.max(0, spare));
|
|
451
|
+
|
|
452
|
+
const headLines: string[] = [
|
|
321
453
|
border,
|
|
322
454
|
truncateToWidth(theme.fg("accent", theme.bold(title)), width),
|
|
323
455
|
];
|
|
456
|
+
if (showSummary) headLines.push(...summaryLines);
|
|
457
|
+
if (showSearchLine) headLines.push(searchLine!);
|
|
458
|
+
headLines.push("");
|
|
459
|
+
if (showTableHeader) headLines.push(truncateToWidth(theme.fg("dim", tableHeaderText!), width));
|
|
324
460
|
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
const cursorGlyph = focused ? `${CURSOR_MARKER}${theme.fg("accent", "▌")}` : "";
|
|
330
|
-
const searchPrefix = theme.fg("accent", "搜索:");
|
|
331
|
-
const queryWidth = Math.max(0, width - visibleWidth(searchPrefix));
|
|
332
|
-
const queryDisplay = searchQuery
|
|
333
|
-
? fitSearchQueryAroundCursor(searchQuery, searchCursor, cursorGlyph, queryWidth)
|
|
334
|
-
: `${cursorGlyph}${theme.fg("dim", "<输入关键词>")}`;
|
|
335
|
-
lines.push(truncateToWidth(`${searchPrefix}${queryDisplay}`, width, ""));
|
|
336
|
-
}
|
|
337
|
-
lines.push("");
|
|
461
|
+
const tailLines: string[] = [];
|
|
462
|
+
if (showDetail) tailLines.push("", border, ...detailLines);
|
|
463
|
+
if (hintLines.length > 0) tailLines.push("", ...hintLines.map((line) => truncateToWidth(line, width)));
|
|
464
|
+
tailLines.push(border);
|
|
338
465
|
|
|
339
|
-
const
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
466
|
+
const windowStart = Math.max(
|
|
467
|
+
0,
|
|
468
|
+
Math.min(selectedIndex - Math.floor(viewportRows / 2), Math.max(0, activeRows.length - viewportRows)),
|
|
469
|
+
);
|
|
470
|
+
const shownRows = activeRows.slice(windowStart, windowStart + viewportRows);
|
|
471
|
+
const scrolling = activeRows.length > viewportRows;
|
|
472
|
+
const contentWidth = Math.max(0, scrolling ? width - SCROLLBAR_WIDTH : width);
|
|
343
473
|
|
|
474
|
+
const bodyLines: string[] = [];
|
|
344
475
|
if (shownRows.length === 0) {
|
|
345
476
|
const emptyLabel = searchQuery ? `无匹配项:${searchQuery}` : options.emptyLabel ?? "暂无条目";
|
|
346
|
-
|
|
477
|
+
bodyLines.push(truncateToWidth(theme.fg("dim", ` ${emptyLabel}`), width));
|
|
347
478
|
} else {
|
|
348
|
-
for (let
|
|
349
|
-
const
|
|
350
|
-
const
|
|
351
|
-
const selected = absoluteIndex === selectedIndex;
|
|
479
|
+
for (let offset = 0; offset < shownRows.length; offset += 1) {
|
|
480
|
+
const row = shownRows[offset]!;
|
|
481
|
+
const selected = windowStart + offset === selectedIndex;
|
|
352
482
|
const prefix = selected ? "❯ " : " ";
|
|
353
|
-
const rowLabel = options.formatRow?.(row, Math.max(0,
|
|
354
|
-
const
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
const detailLines = options.getDetailLines?.(activeRows[selectedIndex]) ?? [];
|
|
363
|
-
if (detailLines.length > 0) {
|
|
364
|
-
lines.push("", border);
|
|
365
|
-
for (const line of detailLines) {
|
|
366
|
-
lines.push(truncateToWidth(theme.fg("dim", line), width));
|
|
483
|
+
const rowLabel = options.formatRow?.(row, Math.max(0, contentWidth - visibleWidth(prefix)), theme) ?? row.label;
|
|
484
|
+
const rowText = truncateToWidth(`${prefix}${rowLabel}`, contentWidth);
|
|
485
|
+
// [喵喵喵]: 选中行用背景色而不是整行前景色,列内的语义色才不会被抹平。
|
|
486
|
+
const styledRow = selected ? theme.bg("selectedBg", padToVisibleWidth(rowText, contentWidth)) : rowText;
|
|
487
|
+
bodyLines.push(scrolling
|
|
488
|
+
? `${styledRow} ${theme.fg("borderMuted", getScrollbarGlyph(offset, viewportRows, windowStart, activeRows.length))}`
|
|
489
|
+
: styledRow);
|
|
367
490
|
}
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
if (searchable) {
|
|
372
|
-
footerLines.push(searchActive ? `/ 搜索:${searchQuery || "<输入关键词>"} Backspace 删除 Esc 清空` : "/ 搜索");
|
|
373
|
-
}
|
|
374
|
-
if (footerLines.length > 0) {
|
|
375
|
-
lines.push("");
|
|
376
|
-
for (const line of footerLines) {
|
|
377
|
-
lines.push(truncateToWidth(theme.fg("dim", line), width));
|
|
491
|
+
if (scrolling) {
|
|
492
|
+
const position = `${windowStart + 1}-${windowStart + shownRows.length} / ${activeRows.length}`;
|
|
493
|
+
bodyLines.push(theme.fg("dim", padToVisibleWidth("", Math.max(0, contentWidth - visibleWidth(position))) + position));
|
|
378
494
|
}
|
|
379
495
|
}
|
|
380
496
|
|
|
381
|
-
|
|
382
|
-
return lines.map((line) => truncateToWidth(line, width));
|
|
497
|
+
return [...headLines, ...bodyLines, ...tailLines].map((line) => truncateToWidth(line, width));
|
|
383
498
|
},
|
|
384
499
|
};
|
|
385
500
|
});
|
|
386
501
|
}
|
|
387
502
|
|
|
388
|
-
|
|
503
|
+
// 内部基础菜单:对外只暴露 showOptionPicker / showPersistentFormMenu / showPersistentShortcutMenu 三个语义入口。
|
|
504
|
+
async function showPersistentMenu(
|
|
389
505
|
ctx: ExtensionCommandContext,
|
|
390
506
|
title: string,
|
|
391
507
|
help: string,
|
|
@@ -393,7 +509,35 @@ export async function showPersistentMenu(
|
|
|
393
509
|
cursor: MenuCursor,
|
|
394
510
|
options: PersistentMenuOptions = {},
|
|
395
511
|
): Promise<MenuAction> {
|
|
396
|
-
return createPersistentMenu<MenuAction>(ctx, title, help, rows, cursor, undefined, [], undefined,
|
|
512
|
+
return createPersistentMenu<MenuAction>(ctx, title, help, rows, cursor, undefined, [], undefined, options);
|
|
513
|
+
}
|
|
514
|
+
// 单选弹窗:两个编辑器的所有枚举字段都走这里,避免同一表单里出现两种选择器外观。
|
|
515
|
+
export async function showOptionPicker<TChoice extends { id: string; label: string }>(
|
|
516
|
+
ctx: ExtensionCommandContext,
|
|
517
|
+
title: string,
|
|
518
|
+
choices: readonly TChoice[],
|
|
519
|
+
currentId: string,
|
|
520
|
+
): Promise<TChoice | undefined> {
|
|
521
|
+
const cursor: MenuCursor = { index: Math.max(0, choices.findIndex((choice) => choice.id === currentId)) };
|
|
522
|
+
const action = await showPersistentMenu(
|
|
523
|
+
ctx,
|
|
524
|
+
title,
|
|
525
|
+
"",
|
|
526
|
+
choices.map((choice) => ({
|
|
527
|
+
id: choice.id,
|
|
528
|
+
label: choice.id === currentId ? `${choice.label} ← 当前` : choice.label,
|
|
529
|
+
})),
|
|
530
|
+
cursor,
|
|
531
|
+
{
|
|
532
|
+
hints: [
|
|
533
|
+
{ key: "↑↓", label: "移动" },
|
|
534
|
+
{ key: "Enter", label: "选择" },
|
|
535
|
+
{ key: "Esc", label: "返回" },
|
|
536
|
+
],
|
|
537
|
+
},
|
|
538
|
+
);
|
|
539
|
+
if (action.type === "cancel") return undefined;
|
|
540
|
+
return choices.find((choice) => choice.id === action.id);
|
|
397
541
|
}
|
|
398
542
|
|
|
399
543
|
export async function showPersistentFormMenu(
|
|
@@ -412,8 +556,7 @@ export async function showPersistentFormMenu(
|
|
|
412
556
|
cursor,
|
|
413
557
|
() => ({ type: "save" }),
|
|
414
558
|
[],
|
|
415
|
-
|
|
416
|
-
new Set(options.adjustableRowIds ?? []),
|
|
559
|
+
options.onAdjust,
|
|
417
560
|
options,
|
|
418
561
|
);
|
|
419
562
|
}
|
|
@@ -427,5 +570,5 @@ export async function showPersistentShortcutMenu<TShortcut extends string>(
|
|
|
427
570
|
shortcuts: MenuShortcut<TShortcut>[],
|
|
428
571
|
options: PersistentMenuOptions = {},
|
|
429
572
|
): Promise<ShortcutMenuAction<TShortcut>> {
|
|
430
|
-
return createPersistentMenu<ShortcutMenuAction<TShortcut>>(ctx, title, help, rows, cursor, undefined, shortcuts, undefined,
|
|
573
|
+
return createPersistentMenu<ShortcutMenuAction<TShortcut>>(ctx, title, help, rows, cursor, undefined, shortcuts, undefined, { searchable: true, ...options });
|
|
431
574
|
}
|