pi-open-tui 0.2.9 → 0.2.11

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.
@@ -1,289 +1,361 @@
1
- import type { ExtensionAPI, ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
2
- import {
3
- Box,
4
- Key,
5
- matchesKey,
6
- SelectList,
7
- type SelectItem,
8
- type TUI,
9
- Text,
10
- } from "@earendil-works/pi-tui";
11
- import type { IconMode, OpenTuiConfig } from "./config.ts";
12
-
13
- interface SettingItem {
14
- id: string;
15
- label: string;
16
- currentValue: string;
17
- values: string[];
18
- }
19
-
20
- type Tab = "features" | "icons" | "segments" | "telemetry";
21
-
22
- const TABS: { id: Tab; label: string }[] = [
23
- { id: "features", label: "Features" },
24
- { id: "icons", label: "Icons" },
25
- { id: "segments", label: "Segments" },
26
- { id: "telemetry", label: "Telemetry" },
27
- ];
28
-
29
- function toggleSetting(config: OpenTuiConfig, key: keyof OpenTuiConfig["footerSegments"]): OpenTuiConfig {
30
- return {
31
- ...config,
32
- footerSegments: {
33
- ...config.footerSegments,
34
- [key]: !config.footerSegments[key],
35
- },
36
- };
37
- }
38
-
39
- function cycleIconMode(config: OpenTuiConfig): OpenTuiConfig {
40
- const order: IconMode[] = ["auto", "nerd", "ascii"];
41
- const currentIdx = order.indexOf(config.icons.mode);
42
- const next = order[(currentIdx + 1) % order.length]!;
43
- return { ...config, icons: { mode: next } };
44
- }
45
-
46
- function toggleEnabled(config: OpenTuiConfig): OpenTuiConfig {
47
- return { ...config, enabled: !config.enabled };
48
- }
49
-
50
- function toggleTelemetry(config: OpenTuiConfig, key: keyof OpenTuiConfig["telemetry"]): OpenTuiConfig {
51
- return {
52
- ...config,
53
- telemetry: { ...config.telemetry, [key]: !config.telemetry[key] },
54
- };
55
- }
56
-
57
- function buildFeaturesItems(config: OpenTuiConfig): SettingItem[] {
58
- return [
59
- {
60
- id: "enabled",
61
- label: "Enabled",
62
- currentValue: config.enabled ? "on" : "off",
63
- values: ["on", "off"],
64
- },
65
- ];
66
- }
67
-
68
- function buildIconsItems(config: OpenTuiConfig): SettingItem[] {
69
- return [
70
- {
71
- id: "mode",
72
- label: "Icon mode",
73
- currentValue: config.icons.mode,
74
- values: ["auto", "nerd", "ascii"],
75
- },
76
- ];
77
- }
78
-
79
- function buildSegmentsItems(config: OpenTuiConfig): SettingItem[] {
80
- const segs = config.footerSegments;
81
- return [
82
- { id: "cwd", label: "cwd", currentValue: segs.cwd ? "on" : "off", values: ["on", "off"] },
83
- { id: "gitBranch", label: "git branch", currentValue: segs.gitBranch ? "on" : "off", values: ["on", "off"] },
84
- { id: "gitStatus", label: "git status", currentValue: segs.gitStatus ? "on" : "off", values: ["on", "off"] },
85
- { id: "gitCommit", label: "git commit (detached)", currentValue: segs.gitCommit ? "on" : "off", values: ["on", "off"] },
86
- { id: "runtime", label: "runtime", currentValue: segs.runtime ? "on" : "off", values: ["on", "off"] },
87
- { id: "context", label: "context bar", currentValue: segs.context ? "on" : "off", values: ["on", "off"] },
88
- { id: "tokens", label: "tokens", currentValue: segs.tokens ? "on" : "off", values: ["on", "off"] },
89
- { id: "cost", label: "cost", currentValue: segs.cost ? "on" : "off", values: ["on", "off"] },
90
- ];
91
- }
92
-
93
- function buildTelemetryItems(config: OpenTuiConfig): SettingItem[] {
94
- const telemetry = config.telemetry;
95
- return [
96
- { id: "enabled", label: "Enabled", currentValue: telemetry.enabled ? "on" : "off", values: ["on", "off"] },
97
- { id: "tps", label: "TPS", currentValue: telemetry.tps ? "on" : "off", values: ["on", "off"] },
98
- { id: "ttft", label: "TTFT", currentValue: telemetry.ttft ? "on" : "off", values: ["on", "off"] },
99
- { id: "duration", label: "total duration", currentValue: telemetry.duration ? "on" : "off", values: ["on", "off"] },
100
- { id: "tokens", label: "token counts", currentValue: telemetry.tokens ? "on" : "off", values: ["on", "off"] },
101
- { id: "stalls", label: "stall details", currentValue: telemetry.stalls ? "on" : "off", values: ["on", "off"] },
102
- { id: "cost", label: "cost rate", currentValue: telemetry.cost ? "on" : "off", values: ["on", "off"] },
103
- ];
104
- }
105
-
106
- function buildItems(tab: Tab, config: OpenTuiConfig): SettingItem[] {
107
- switch (tab) {
108
- case "features": return buildFeaturesItems(config);
109
- case "icons": return buildIconsItems(config);
110
- case "segments": return buildSegmentsItems(config);
111
- case "telemetry": return buildTelemetryItems(config);
112
- }
113
- }
114
-
115
- function handleSettingChange(
116
- tab: Tab,
117
- itemId: string,
118
- config: OpenTuiConfig,
119
- ): OpenTuiConfig {
120
- if (tab === "features" && itemId === "enabled") {
121
- return toggleEnabled(config);
122
- }
123
- if (tab === "icons" && itemId === "mode") {
124
- return cycleIconMode(config);
125
- }
126
- if (tab === "segments") {
127
- return toggleSetting(config, itemId as keyof OpenTuiConfig["footerSegments"]);
128
- }
129
- if (tab === "telemetry") {
130
- return toggleTelemetry(config, itemId as keyof OpenTuiConfig["telemetry"]);
131
- }
132
- return config;
133
- }
134
-
135
- interface SettingsUiHandle {
136
- render: (width: number) => string[];
137
- invalidate: () => void;
138
- handleInput: (data: string) => void;
139
- }
140
-
141
- class SettingsUi implements SettingsUiHandle {
142
- private tab: Tab = "features";
143
- private config: OpenTuiConfig;
144
- private selectList: SelectList;
145
- private readonly selectedItemByTab: Partial<Record<Tab, string>> = {};
146
- private readonly container: Box;
147
- private readonly theme: Theme;
148
- private readonly onChange: (config: OpenTuiConfig) => void;
149
- private readonly onClose: () => void;
150
- private cachedWidth: number | undefined;
151
- private cachedLines: string[] | undefined;
152
-
153
- constructor(
154
- theme: Theme,
155
- config: OpenTuiConfig,
156
- onChange: (config: OpenTuiConfig) => void,
157
- onClose: () => void,
158
- ) {
159
- this.theme = theme;
160
- this.config = config;
161
- this.onChange = onChange;
162
- this.onClose = onClose;
163
- this.container = new Box(1, 1, (s: string) => theme.bg("customMessageBg", s));
164
- this.selectList = new SelectList([], 12, {
165
- selectedPrefix: (t) => theme.fg("accent", t),
166
- selectedText: (t) => theme.fg("accent", t),
167
- description: (t) => theme.fg("muted", t),
168
- scrollInfo: (t) => theme.fg("dim", t),
169
- noMatch: (t) => theme.fg("warning", t),
170
- });
171
- this.rebuild();
172
- }
173
-
174
- private rebuild(preferredItemId = this.selectedItemByTab[this.tab]): void {
175
- this.container.clear();
176
- this.container.addChild(new Text(this.theme.bold(this.theme.fg("accent", "Open TUI Settings")), 1, 0));
177
-
178
- const tabBar = TABS.map((t) => {
179
- const active = t.id === this.tab;
180
- const label = active ? `[${t.label}]` : ` ${t.label} `;
181
- return active ? this.theme.fg("accent", label) : this.theme.fg("dim", label);
182
- }).join(" ");
183
- this.container.addChild(new Text(tabBar, 1, 0));
184
- this.container.addChild(new Text(this.theme.fg("dim", "Tab/Shift+Tab to switch · Enter to toggle · Esc to close"), 1, 0));
185
-
186
- const items = buildItems(this.tab, this.config).map((item) => ({
187
- value: item.id,
188
- label: item.label,
189
- description: item.currentValue,
190
- } as SelectItem));
191
- this.selectList = new SelectList(items, Math.min(items.length, 10), {
192
- selectedPrefix: (t) => this.theme.fg("accent", t),
193
- selectedText: (t) => this.theme.fg("accent", t),
194
- description: (t) => this.theme.fg("muted", t),
195
- scrollInfo: (t) => this.theme.fg("dim", t),
196
- noMatch: (t) => this.theme.fg("warning", t),
197
- });
198
- const selectedIndex = items.findIndex((item) => item.value === preferredItemId);
199
- if (selectedIndex >= 0) {
200
- this.selectList.setSelectedIndex(selectedIndex);
201
- }
202
- this.selectedItemByTab[this.tab] = this.selectList.getSelectedItem()?.value;
203
- this.selectList.onSelectionChange = (item) => {
204
- this.selectedItemByTab[this.tab] = item.value;
205
- };
206
- this.selectList.onSelect = (item) => {
207
- this.selectedItemByTab[this.tab] = item.value;
208
- this.config = handleSettingChange(this.tab, item.value, this.config);
209
- this.onChange(this.config);
210
- this.rebuild(item.value);
211
- this.invalidate();
212
- };
213
- this.selectList.onCancel = () => {
214
- this.onClose();
215
- };
216
- this.container.addChild(this.selectList);
217
-
218
- this.cachedWidth = undefined;
219
- this.cachedLines = undefined;
220
- }
221
-
222
- handleInput(data: string): void {
223
- if (matchesKey(data, Key.tab)) {
224
- const idx = TABS.findIndex((t) => t.id === this.tab);
225
- this.tab = TABS[(idx + 1) % TABS.length]!.id;
226
- this.rebuild();
227
- this.invalidate();
228
- return;
229
- }
230
- if (matchesKey(data, Key.shift("tab"))) {
231
- const idx = TABS.findIndex((t) => t.id === this.tab);
232
- this.tab = TABS[(idx - 1 + TABS.length) % TABS.length]!.id;
233
- this.rebuild();
234
- this.invalidate();
235
- return;
236
- }
237
- if (matchesKey(data, Key.escape)) {
238
- this.onClose();
239
- return;
240
- }
241
- this.selectList.handleInput?.(data);
242
- this.invalidate();
243
- this.invalidate();
244
- }
245
-
246
- render(width: number): string[] {
247
- if (this.cachedLines && this.cachedWidth === width) return this.cachedLines;
248
- this.cachedWidth = width;
249
- this.cachedLines = this.container.render(width);
250
- return this.cachedLines;
251
- }
252
-
253
- invalidate(): void {
254
- this.cachedWidth = undefined;
255
- this.cachedLines = undefined;
256
- this.container.invalidate();
257
- }
258
- }
259
-
260
- export function registerSettingsCommand(
261
- pi: ExtensionAPI,
262
- hooks: {
263
- getConfig: () => OpenTuiConfig;
264
- onConfigChanged: (config: OpenTuiConfig) => void;
265
- },
266
- ): void {
267
- pi.registerCommand("open-tui", {
268
- description: "Open the open-tui settings UI",
269
- handler: async (_args, ctx: ExtensionContext) => {
270
- if (!ctx.hasUI) return;
271
- await ctx.ui.custom<void>((tui: TUI, theme, _kb, done) => {
272
- const ui = new SettingsUi(
273
- theme,
274
- hooks.getConfig(),
275
- (config) => hooks.onConfigChanged(config),
276
- () => done(undefined),
277
- );
278
- return {
279
- render: (w: number) => ui.render(w),
280
- invalidate: () => ui.invalidate(),
281
- handleInput: (data: string) => {
282
- ui.handleInput(data);
283
- tui.requestRender();
284
- },
285
- };
286
- }, { overlay: true });
287
- },
288
- });
289
- }
1
+ import type { ExtensionAPI, ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
2
+ import {
3
+ Box,
4
+ Key,
5
+ matchesKey,
6
+ SelectList,
7
+ type SelectItem,
8
+ type TUI,
9
+ Text,
10
+ } from "@earendil-works/pi-tui";
11
+ import type { IconMode, OpenTuiConfig, SettingsLanguage } from "./config.ts";
12
+
13
+ interface SettingItem {
14
+ id: string;
15
+ label: string;
16
+ currentValue: string;
17
+ }
18
+
19
+ type Tab = "features" | "icons" | "segments" | "telemetry";
20
+
21
+ const TABS: Tab[] = ["features", "icons", "segments", "telemetry"];
22
+
23
+ const COPY = {
24
+ en: {
25
+ title: "Open TUI Settings",
26
+ tabs: { features: "General", icons: "Icons", segments: "Footer", telemetry: "Telemetry" },
27
+ hint: "Tab/Shift+Tab/←/→: tabs · ↑/↓: move · Enter/Space: change · Esc/q: close",
28
+ labels: {
29
+ enabled: "Enabled",
30
+ language: "Language",
31
+ iconMode: "Icon mode",
32
+ cwd: "CWD",
33
+ sessionName: "Session name",
34
+ gitBranch: "Git branch",
35
+ gitStatus: "Git status",
36
+ gitCommit: "Git commit (detached)",
37
+ runtime: "Runtime",
38
+ context: "Context bar",
39
+ tokens: "Tokens",
40
+ cost: "Cost",
41
+ extensionStatuses: "Extension status line",
42
+ totalDuration: "Total duration",
43
+ tokenCounts: "Token counts",
44
+ stallDetails: "Stall details",
45
+ costRate: "Cost rate",
46
+ },
47
+ values: {
48
+ on: "On",
49
+ off: "Off",
50
+ languages: { en: "English", zh: "简体中文" },
51
+ icons: { auto: "Auto", nerd: "Nerd", ascii: "ASCII" },
52
+ },
53
+ },
54
+ zh: {
55
+ title: "Open TUI 设置",
56
+ tabs: { features: "常规", icons: "图标", segments: "Footer", telemetry: "遥测" },
57
+ hint: "Tab/Shift+Tab/←/→:切页 · ↑/↓:移动 · Enter/Space:更改 · Esc/q:关闭",
58
+ labels: {
59
+ enabled: "启用",
60
+ language: "语言",
61
+ iconMode: "图标模式",
62
+ cwd: "当前目录",
63
+ sessionName: "会话名",
64
+ gitBranch: "Git 分支",
65
+ gitStatus: "Git 状态",
66
+ gitCommit: "Git 提交(分离 HEAD)",
67
+ runtime: "运行环境",
68
+ context: "上下文栏",
69
+ tokens: "Token",
70
+ cost: "费用",
71
+ extensionStatuses: "扩展状态行",
72
+ totalDuration: "总耗时",
73
+ tokenCounts: "Token 数量",
74
+ stallDetails: "停顿详情",
75
+ costRate: "费用速率",
76
+ },
77
+ values: {
78
+ on: "开启",
79
+ off: "关闭",
80
+ languages: { en: "English", zh: "简体中文" },
81
+ icons: { auto: "自动", nerd: "Nerd", ascii: "ASCII" },
82
+ },
83
+ },
84
+ } as const;
85
+
86
+ type SettingsCopy = (typeof COPY)[SettingsLanguage];
87
+
88
+ function toggleSetting(config: OpenTuiConfig, key: keyof OpenTuiConfig["footerSegments"]): OpenTuiConfig {
89
+ return {
90
+ ...config,
91
+ footerSegments: {
92
+ ...config.footerSegments,
93
+ [key]: !config.footerSegments[key],
94
+ },
95
+ };
96
+ }
97
+
98
+ function cycleIconMode(config: OpenTuiConfig): OpenTuiConfig {
99
+ const order: IconMode[] = ["auto", "nerd", "ascii"];
100
+ const currentIdx = order.indexOf(config.icons.mode);
101
+ const next = order[(currentIdx + 1) % order.length]!;
102
+ return { ...config, icons: { mode: next } };
103
+ }
104
+
105
+ function toggleEnabled(config: OpenTuiConfig): OpenTuiConfig {
106
+ return { ...config, enabled: !config.enabled };
107
+ }
108
+
109
+ function toggleLanguage(config: OpenTuiConfig): OpenTuiConfig {
110
+ return { ...config, settingsLanguage: config.settingsLanguage === "en" ? "zh" : "en" };
111
+ }
112
+
113
+ function toggleTelemetry(config: OpenTuiConfig, key: keyof OpenTuiConfig["telemetry"]): OpenTuiConfig {
114
+ return {
115
+ ...config,
116
+ telemetry: { ...config.telemetry, [key]: !config.telemetry[key] },
117
+ };
118
+ }
119
+
120
+ function buildFeaturesItems(config: OpenTuiConfig, copy: SettingsCopy): SettingItem[] {
121
+ return [
122
+ { id: "enabled", label: copy.labels.enabled, currentValue: config.enabled ? copy.values.on : copy.values.off },
123
+ { id: "settingsLanguage", label: copy.labels.language, currentValue: copy.values.languages[config.settingsLanguage] },
124
+ ];
125
+ }
126
+
127
+ function buildIconsItems(config: OpenTuiConfig, copy: SettingsCopy): SettingItem[] {
128
+ return [{ id: "mode", label: copy.labels.iconMode, currentValue: copy.values.icons[config.icons.mode] }];
129
+ }
130
+
131
+ function buildSegmentsItems(config: OpenTuiConfig, copy: SettingsCopy): SettingItem[] {
132
+ const segs = config.footerSegments;
133
+ const flag = (value: boolean) => value ? copy.values.on : copy.values.off;
134
+ return [
135
+ { id: "cwd", label: copy.labels.cwd, currentValue: flag(segs.cwd) },
136
+ { id: "sessionName", label: copy.labels.sessionName, currentValue: flag(segs.sessionName) },
137
+ { id: "gitBranch", label: copy.labels.gitBranch, currentValue: flag(segs.gitBranch) },
138
+ { id: "gitStatus", label: copy.labels.gitStatus, currentValue: flag(segs.gitStatus) },
139
+ { id: "gitCommit", label: copy.labels.gitCommit, currentValue: flag(segs.gitCommit) },
140
+ { id: "runtime", label: copy.labels.runtime, currentValue: flag(segs.runtime) },
141
+ { id: "context", label: copy.labels.context, currentValue: flag(segs.context) },
142
+ { id: "tokens", label: copy.labels.tokens, currentValue: flag(segs.tokens) },
143
+ { id: "cost", label: copy.labels.cost, currentValue: flag(segs.cost) },
144
+ { id: "extensionStatuses", label: copy.labels.extensionStatuses, currentValue: flag(segs.extensionStatuses) },
145
+ ];
146
+ }
147
+
148
+ function buildTelemetryItems(config: OpenTuiConfig, copy: SettingsCopy): SettingItem[] {
149
+ const telemetry = config.telemetry;
150
+ const flag = (value: boolean) => value ? copy.values.on : copy.values.off;
151
+ return [
152
+ { id: "enabled", label: copy.labels.enabled, currentValue: flag(telemetry.enabled) },
153
+ { id: "tps", label: "TPS", currentValue: flag(telemetry.tps) },
154
+ { id: "ttft", label: "TTFT", currentValue: flag(telemetry.ttft) },
155
+ { id: "duration", label: copy.labels.totalDuration, currentValue: flag(telemetry.duration) },
156
+ { id: "tokens", label: copy.labels.tokenCounts, currentValue: flag(telemetry.tokens) },
157
+ { id: "stalls", label: copy.labels.stallDetails, currentValue: flag(telemetry.stalls) },
158
+ { id: "cost", label: copy.labels.costRate, currentValue: flag(telemetry.cost) },
159
+ ];
160
+ }
161
+
162
+ function buildItems(tab: Tab, config: OpenTuiConfig): SettingItem[] {
163
+ const copy = COPY[config.settingsLanguage];
164
+ switch (tab) {
165
+ case "features": return buildFeaturesItems(config, copy);
166
+ case "icons": return buildIconsItems(config, copy);
167
+ case "segments": return buildSegmentsItems(config, copy);
168
+ case "telemetry": return buildTelemetryItems(config, copy);
169
+ }
170
+ }
171
+
172
+ function handleSettingChange(
173
+ tab: Tab,
174
+ itemId: string,
175
+ config: OpenTuiConfig,
176
+ ): OpenTuiConfig {
177
+ if (tab === "features") {
178
+ if (itemId === "enabled") return toggleEnabled(config);
179
+ if (itemId === "settingsLanguage") return toggleLanguage(config);
180
+ }
181
+ if (tab === "icons" && itemId === "mode") return cycleIconMode(config);
182
+ if (tab === "segments") {
183
+ return toggleSetting(config, itemId as keyof OpenTuiConfig["footerSegments"]);
184
+ }
185
+ if (tab === "telemetry") {
186
+ return toggleTelemetry(config, itemId as keyof OpenTuiConfig["telemetry"]);
187
+ }
188
+ return config;
189
+ }
190
+
191
+ interface SettingsUiHandle {
192
+ render: (width: number) => string[];
193
+ invalidate: () => void;
194
+ handleInput: (data: string) => void;
195
+ }
196
+
197
+ class SettingsUi implements SettingsUiHandle {
198
+ private tab: Tab = "features";
199
+ private config: OpenTuiConfig;
200
+ private selectList: SelectList;
201
+ private readonly selectedItemByTab: Partial<Record<Tab, string>> = {};
202
+ private readonly container: Box;
203
+ private readonly theme: Theme;
204
+ private readonly onChange: (config: OpenTuiConfig) => void;
205
+ private readonly onClose: () => void;
206
+ private cachedWidth: number | undefined;
207
+ private cachedLines: string[] | undefined;
208
+ private compact = false;
209
+
210
+ constructor(
211
+ theme: Theme,
212
+ config: OpenTuiConfig,
213
+ onChange: (config: OpenTuiConfig) => void,
214
+ onClose: () => void,
215
+ ) {
216
+ this.theme = theme;
217
+ this.config = config;
218
+ this.onChange = onChange;
219
+ this.onClose = onClose;
220
+ this.container = new Box(1, 1, (s: string) => theme.bg("customMessageBg", s));
221
+ this.selectList = new SelectList([], 12, {
222
+ selectedPrefix: (t) => theme.fg("accent", t),
223
+ selectedText: (t) => theme.fg("accent", t),
224
+ description: (t) => theme.fg("muted", t),
225
+ scrollInfo: (t) => theme.fg("dim", t),
226
+ noMatch: (t) => theme.fg("warning", t),
227
+ });
228
+ this.rebuild();
229
+ }
230
+
231
+ private applySetting(itemId: string): void {
232
+ this.selectedItemByTab[this.tab] = itemId;
233
+ this.config = handleSettingChange(this.tab, itemId, this.config);
234
+ this.onChange(this.config);
235
+ this.rebuild(itemId);
236
+ }
237
+
238
+ private switchTab(offset: number): void {
239
+ const idx = TABS.indexOf(this.tab);
240
+ this.tab = TABS[(idx + offset + TABS.length) % TABS.length]!;
241
+ this.rebuild();
242
+ }
243
+
244
+ private rebuild(preferredItemId = this.selectedItemByTab[this.tab]): void {
245
+ const copy = COPY[this.config.settingsLanguage];
246
+ this.container.clear();
247
+ this.container.addChild(new Text(this.theme.bold(this.theme.fg("accent", copy.title)), 1, 0));
248
+
249
+ const tabBar = TABS.map((tab) => {
250
+ const active = tab === this.tab;
251
+ const label = active ? `[${copy.tabs[tab]}]` : ` ${copy.tabs[tab]} `;
252
+ return active ? this.theme.fg("accent", label) : this.theme.fg("dim", label);
253
+ }).join(" ");
254
+ this.container.addChild(new Text(tabBar, 1, 0));
255
+ this.container.addChild(new Text(this.theme.fg("dim", copy.hint), 1, 0));
256
+
257
+ const items = buildItems(this.tab, this.config).map((item) => ({
258
+ value: item.id,
259
+ label: this.compact ? `${item.label}: ${item.currentValue}` : item.label,
260
+ description: this.compact ? undefined : item.currentValue,
261
+ } as SelectItem));
262
+ this.selectList = new SelectList(items, Math.min(items.length, 10), {
263
+ selectedPrefix: (t) => this.theme.fg("accent", t),
264
+ selectedText: (t) => this.theme.fg("accent", t),
265
+ description: (t) => this.theme.fg("muted", t),
266
+ scrollInfo: (t) => this.theme.fg("dim", t),
267
+ noMatch: (t) => this.theme.fg("warning", t),
268
+ });
269
+ const selectedIndex = items.findIndex((item) => item.value === preferredItemId);
270
+ if (selectedIndex >= 0) {
271
+ this.selectList.setSelectedIndex(selectedIndex);
272
+ }
273
+ this.selectedItemByTab[this.tab] = this.selectList.getSelectedItem()?.value;
274
+ this.selectList.onSelectionChange = (item) => {
275
+ this.selectedItemByTab[this.tab] = item.value;
276
+ };
277
+ this.selectList.onSelect = (item) => {
278
+ this.applySetting(item.value);
279
+ };
280
+ this.selectList.onCancel = () => {
281
+ this.onClose();
282
+ };
283
+ this.container.addChild(this.selectList);
284
+
285
+ this.cachedWidth = undefined;
286
+ this.cachedLines = undefined;
287
+ }
288
+
289
+ handleInput(data: string): void {
290
+ if (matchesKey(data, Key.tab) || matchesKey(data, Key.right)) {
291
+ this.switchTab(1);
292
+ this.invalidate();
293
+ return;
294
+ }
295
+ if (matchesKey(data, Key.shift("tab")) || matchesKey(data, Key.left)) {
296
+ this.switchTab(-1);
297
+ this.invalidate();
298
+ return;
299
+ }
300
+ if (matchesKey(data, Key.escape) || matchesKey(data, "q")) {
301
+ this.onClose();
302
+ return;
303
+ }
304
+ if (matchesKey(data, Key.space) || data === " ") {
305
+ const selected = this.selectList.getSelectedItem();
306
+ if (selected) this.applySetting(selected.value);
307
+ } else {
308
+ this.selectList.handleInput?.(data);
309
+ }
310
+ this.invalidate();
311
+ }
312
+
313
+ render(width: number): string[] {
314
+ const compact = width <= 60;
315
+ if (compact !== this.compact) {
316
+ this.compact = compact;
317
+ this.rebuild();
318
+ }
319
+ if (this.cachedLines && this.cachedWidth === width) return this.cachedLines;
320
+ this.cachedWidth = width;
321
+ this.cachedLines = this.container.render(width);
322
+ return this.cachedLines;
323
+ }
324
+
325
+ invalidate(): void {
326
+ this.cachedWidth = undefined;
327
+ this.cachedLines = undefined;
328
+ this.container.invalidate();
329
+ }
330
+ }
331
+
332
+ export function registerSettingsCommand(
333
+ pi: ExtensionAPI,
334
+ hooks: {
335
+ getConfig: () => OpenTuiConfig;
336
+ onConfigChanged: (config: OpenTuiConfig) => void;
337
+ },
338
+ ): void {
339
+ pi.registerCommand("open-tui", {
340
+ description: "Open the open-tui settings UI",
341
+ handler: async (_args, ctx: ExtensionContext) => {
342
+ if (!ctx.hasUI) return;
343
+ await ctx.ui.custom<void>((tui: TUI, theme, _kb, done) => {
344
+ const ui = new SettingsUi(
345
+ theme,
346
+ hooks.getConfig(),
347
+ (config) => hooks.onConfigChanged(config),
348
+ () => done(undefined),
349
+ );
350
+ return {
351
+ render: (w: number) => ui.render(w),
352
+ invalidate: () => ui.invalidate(),
353
+ handleInput: (data: string) => {
354
+ ui.handleInput(data);
355
+ tui.requestRender();
356
+ },
357
+ };
358
+ }, { overlay: true });
359
+ },
360
+ });
361
+ }