-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathshared.ts
More file actions
230 lines (213 loc) · 5.98 KB
/
Copy pathshared.ts
File metadata and controls
230 lines (213 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import { stdin as input, stdout as output } from "node:process";
import { savePluginConfig } from "../../config.js";
import {
type DashboardDisplaySettings,
type DashboardStatuslineField,
DEFAULT_DASHBOARD_DISPLAY_SETTINGS,
getDashboardSettingsPath,
loadDashboardDisplaySettings,
saveDashboardDisplaySettings,
} from "../../dashboard-settings.js";
import type { PluginConfig } from "../../types.js";
import { getUiRuntimeOptions } from "../../ui/runtime.js";
import { sleep } from "../../utils.js";
import {
buildBackendConfigPatch,
cloneBackendPluginConfig,
} from "../backend-settings-helpers.js";
import type { BackendNumberSettingOption } from "../backend-settings-schema.js";
import type { DashboardDisplaySettingKey } from "../dashboard-display-panel.js";
import {
cloneDashboardSettingsData,
dashboardSettingsDataEqual,
} from "../dashboard-settings-data.js";
import {
resolvePluginConfigSavePathKey,
warnPersistFailure,
} from "../settings-persist-utils.js";
import {
buildAccountListPreview as buildAccountListPreviewBase,
buildSummaryPreviewText as buildSummaryPreviewTextBase,
normalizeStatuslineFields,
} from "../settings-preview.js";
import { withQueuedRetry } from "../settings-write-queue.js";
export type SettingsHubAction =
| { type: "account-list" }
| { type: "summary-fields" }
| { type: "behavior" }
| { type: "theme" }
| { type: "experimental" }
| { type: "backend" }
| { type: "back" };
export type DashboardSettingKey = keyof DashboardDisplaySettings;
export function isTtyInteractive(): boolean {
return Boolean(input.isTTY && output.isTTY);
}
function copyDashboardSettingValue(
target: DashboardDisplaySettings,
source: DashboardDisplaySettings,
key: DashboardSettingKey,
): void {
const value = source[key];
(target as unknown as Record<string, unknown>)[key] = Array.isArray(value)
? [...value]
: value;
}
export function applyDashboardDefaultsForKeys(
draft: DashboardDisplaySettings,
keys: readonly DashboardSettingKey[],
): DashboardDisplaySettings {
const next = cloneDashboardSettings(draft);
const defaults = cloneDashboardSettings(DEFAULT_DASHBOARD_DISPLAY_SETTINGS);
for (const key of keys) {
copyDashboardSettingValue(next, defaults, key);
}
return next;
}
function mergeDashboardSettingsForKeys(
base: DashboardDisplaySettings,
selected: DashboardDisplaySettings,
keys: readonly DashboardSettingKey[],
): DashboardDisplaySettings {
const next = cloneDashboardSettings(base);
for (const key of keys) {
copyDashboardSettingValue(next, selected, key);
}
return cloneDashboardSettings(next);
}
export async function persistDashboardSettingsSelection(
selected: DashboardDisplaySettings,
keys: readonly DashboardSettingKey[],
scope: string,
): Promise<DashboardDisplaySettings> {
const fallback = cloneDashboardSettings(selected);
try {
return await withQueuedRetry(
getDashboardSettingsPath(),
async () => {
const latest = cloneDashboardSettings(
await loadDashboardDisplaySettings(),
);
const merged = mergeDashboardSettingsForKeys(latest, selected, keys);
await saveDashboardDisplaySettings(merged);
return merged;
},
{ sleep },
);
} catch (error) {
warnPersistFailure(scope, error);
return fallback;
}
}
export async function persistBackendConfigSelection(
selected: PluginConfig,
scope: string,
): Promise<PluginConfig> {
const fallback = cloneBackendPluginConfig(selected);
try {
await withQueuedRetry(
resolvePluginConfigSavePathKey(),
async () => {
await savePluginConfig(buildBackendConfigPatch(selected));
},
{ sleep },
);
return fallback;
} catch (error) {
warnPersistFailure(scope, error);
return fallback;
}
}
export function cloneDashboardSettings(
settings: DashboardDisplaySettings,
): DashboardDisplaySettings {
return cloneDashboardSettingsData(settings, {
resolveMenuLayoutMode,
normalizeStatuslineFields,
});
}
export function dashboardSettingsEqual(
left: DashboardDisplaySettings,
right: DashboardDisplaySettings,
): boolean {
return dashboardSettingsDataEqual(left, right, {
resolveMenuLayoutMode,
normalizeStatuslineFields,
});
}
export function buildSummaryPreviewText(
settings: DashboardDisplaySettings,
ui: ReturnType<typeof getUiRuntimeOptions>,
focus:
| DashboardDisplaySettingKey
| DashboardStatuslineField
| "menuSortMode"
| "menuLayoutMode"
| null = null,
): string {
return buildSummaryPreviewTextBase(
settings,
ui,
resolveMenuLayoutMode,
focus,
);
}
export function buildAccountListPreview(
settings: DashboardDisplaySettings,
ui: ReturnType<typeof getUiRuntimeOptions>,
focus:
| DashboardDisplaySettingKey
| DashboardStatuslineField
| "menuSortMode"
| "menuLayoutMode"
| null = null,
): { label: string; hint: string } {
return buildAccountListPreviewBase(
settings,
ui,
resolveMenuLayoutMode,
focus,
);
}
export function clampBackendNumber(
option: BackendNumberSettingOption,
value: number,
): number {
return Math.max(option.min, Math.min(option.max, Math.round(value)));
}
export function resolveMenuLayoutMode(
settings: DashboardDisplaySettings,
): "compact-details" | "expanded-rows" {
if (settings.menuLayoutMode === "expanded-rows") {
return "expanded-rows";
}
if (settings.menuLayoutMode === "compact-details") {
return "compact-details";
}
return settings.menuShowDetailsForUnselectedRows === true
? "expanded-rows"
: "compact-details";
}
export async function withQueuedRetryForTests<T>(
pathKey: string,
task: () => Promise<T>,
): Promise<T> {
return withQueuedRetry(pathKey, task, { sleep });
}
export async function persistDashboardSettingsSelectionForTests(
selected: DashboardDisplaySettings,
keys: ReadonlyArray<keyof DashboardDisplaySettings>,
scope: string,
): Promise<DashboardDisplaySettings> {
return persistDashboardSettingsSelection(
selected,
keys as readonly DashboardSettingKey[],
scope,
);
}
export async function persistBackendConfigSelectionForTests(
selected: PluginConfig,
scope: string,
): Promise<PluginConfig> {
return persistBackendConfigSelection(selected, scope);
}