@pi-unipi/utility 2.6.0 → 2.6.2
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/package.json +8 -6
- package/src/commands.ts +4 -5
- package/src/herdr-sync.ts +136 -0
- package/src/index.ts +11 -68
- package/src/lifecycle/cleanup.ts +2 -16
- package/src/{diff/settings.ts → settings.ts} +42 -80
- package/src/tools/env.ts +0 -0
- package/src/tui/name-badge-state.ts +18 -2
- package/src/tui/name-badge.ts +5 -4
- package/src/tui/util-settings-tui.ts +41 -224
- package/src/cache/ttl-cache.ts +0 -311
- package/src/diff/highlighter.ts +0 -352
- package/src/diff/parser.ts +0 -191
- package/src/diff/renderer.ts +0 -395
- package/src/diff/theme.ts +0 -316
- package/src/diff/wrapper.ts +0 -339
- package/src/display/capabilities.ts +0 -214
- package/src/display/width.ts +0 -226
- package/src/tools/batch.ts +0 -229
- package/src/tui/badge-settings-tui.ts +0 -388
- package/src/tui/badge-settings.ts +0 -67
- package/src/tui/settings-inspector.ts +0 -303
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @pi-unipi/utility — Settings Inspector
|
|
3
|
-
*
|
|
4
|
-
* Reusable settings inspector overlay pattern.
|
|
5
|
-
* Split-pane layout: list left, editor right.
|
|
6
|
-
* Search/filter, keyboard navigation, JSON editing.
|
|
7
|
-
*
|
|
8
|
-
* Note: This is a data model and rendering helper. The actual TUI
|
|
9
|
-
* rendering depends on the host environment (pi's TUI API).
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import type {
|
|
13
|
-
SettingSchema,
|
|
14
|
-
SettingsInspectorState,
|
|
15
|
-
} from "../types.js";
|
|
16
|
-
|
|
17
|
-
/** Navigation actions */
|
|
18
|
-
export type InspectorAction =
|
|
19
|
-
| { type: "navigate"; direction: "up" | "down" | "first" | "last" }
|
|
20
|
-
| { type: "search"; query: string }
|
|
21
|
-
| { type: "select"; index: number }
|
|
22
|
-
| { type: "edit"; value: unknown }
|
|
23
|
-
| { type: "toggle_edit" }
|
|
24
|
-
| { type: "save" }
|
|
25
|
-
| { type: "cancel" };
|
|
26
|
-
|
|
27
|
-
/** Result of applying an action */
|
|
28
|
-
export interface InspectorUpdate {
|
|
29
|
-
state: SettingsInspectorState;
|
|
30
|
-
changed: boolean;
|
|
31
|
-
saved: boolean;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/** Create initial inspector state */
|
|
35
|
-
export function createSettingsInspector(
|
|
36
|
-
schemas: SettingSchema[],
|
|
37
|
-
initialValues?: Record<string, unknown>,
|
|
38
|
-
): SettingsInspectorState {
|
|
39
|
-
const values: Record<string, unknown> = {};
|
|
40
|
-
for (const schema of schemas) {
|
|
41
|
-
values[schema.key] = initialValues?.[schema.key] ?? schema.default;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return {
|
|
45
|
-
schemas,
|
|
46
|
-
values,
|
|
47
|
-
selectedIndex: 0,
|
|
48
|
-
searchQuery: "",
|
|
49
|
-
editMode: false,
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** Get filtered schemas based on search query */
|
|
54
|
-
export function getFilteredSchemas(
|
|
55
|
-
state: SettingsInspectorState,
|
|
56
|
-
): SettingSchema[] {
|
|
57
|
-
if (!state.searchQuery.trim()) {
|
|
58
|
-
return state.schemas;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const query = state.searchQuery.toLowerCase();
|
|
62
|
-
return state.schemas.filter(
|
|
63
|
-
(s) =>
|
|
64
|
-
s.key.toLowerCase().includes(query) ||
|
|
65
|
-
s.description.toLowerCase().includes(query),
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/** Get the currently selected schema */
|
|
70
|
-
export function getSelectedSchema(
|
|
71
|
-
state: SettingsInspectorState,
|
|
72
|
-
): SettingSchema | undefined {
|
|
73
|
-
const filtered = getFilteredSchemas(state);
|
|
74
|
-
return filtered[state.selectedIndex];
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/** Get current value for a key */
|
|
78
|
-
export function getValue(
|
|
79
|
-
state: SettingsInspectorState,
|
|
80
|
-
key: string,
|
|
81
|
-
): unknown {
|
|
82
|
-
return state.values[key];
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/** Apply an action to the inspector state */
|
|
86
|
-
export function applyAction(
|
|
87
|
-
state: SettingsInspectorState,
|
|
88
|
-
action: InspectorAction,
|
|
89
|
-
): InspectorUpdate {
|
|
90
|
-
const newState: SettingsInspectorState = {
|
|
91
|
-
...state,
|
|
92
|
-
values: { ...state.values },
|
|
93
|
-
};
|
|
94
|
-
let changed = false;
|
|
95
|
-
let saved = false;
|
|
96
|
-
|
|
97
|
-
const filtered = getFilteredSchemas(newState);
|
|
98
|
-
|
|
99
|
-
switch (action.type) {
|
|
100
|
-
case "navigate": {
|
|
101
|
-
const maxIndex = Math.max(0, filtered.length - 1);
|
|
102
|
-
switch (action.direction) {
|
|
103
|
-
case "up":
|
|
104
|
-
newState.selectedIndex = Math.max(0, newState.selectedIndex - 1);
|
|
105
|
-
break;
|
|
106
|
-
case "down":
|
|
107
|
-
newState.selectedIndex = Math.min(maxIndex, newState.selectedIndex + 1);
|
|
108
|
-
break;
|
|
109
|
-
case "first":
|
|
110
|
-
newState.selectedIndex = 0;
|
|
111
|
-
break;
|
|
112
|
-
case "last":
|
|
113
|
-
newState.selectedIndex = maxIndex;
|
|
114
|
-
break;
|
|
115
|
-
}
|
|
116
|
-
changed = newState.selectedIndex !== state.selectedIndex;
|
|
117
|
-
break;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
case "search": {
|
|
121
|
-
newState.searchQuery = action.query;
|
|
122
|
-
newState.selectedIndex = 0;
|
|
123
|
-
changed = true;
|
|
124
|
-
break;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
case "select": {
|
|
128
|
-
const maxIndex = Math.max(0, filtered.length - 1);
|
|
129
|
-
newState.selectedIndex = Math.max(0, Math.min(maxIndex, action.index));
|
|
130
|
-
changed = newState.selectedIndex !== state.selectedIndex;
|
|
131
|
-
break;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
case "edit": {
|
|
135
|
-
const selected = getSelectedSchema(newState);
|
|
136
|
-
if (selected) {
|
|
137
|
-
newState.values[selected.key] = action.value;
|
|
138
|
-
changed = true;
|
|
139
|
-
}
|
|
140
|
-
break;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
case "toggle_edit": {
|
|
144
|
-
newState.editMode = !newState.editMode;
|
|
145
|
-
changed = true;
|
|
146
|
-
break;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
case "save": {
|
|
150
|
-
saved = true;
|
|
151
|
-
changed = true;
|
|
152
|
-
break;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
case "cancel": {
|
|
156
|
-
newState.editMode = false;
|
|
157
|
-
changed = true;
|
|
158
|
-
break;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return { state: newState, changed, saved };
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/** Validate a value against its schema */
|
|
166
|
-
export function validateValue(
|
|
167
|
-
schema: SettingSchema,
|
|
168
|
-
value: unknown,
|
|
169
|
-
): string | undefined {
|
|
170
|
-
if (value === undefined || value === null) {
|
|
171
|
-
if (schema.required) {
|
|
172
|
-
return `Required field: ${schema.key}`;
|
|
173
|
-
}
|
|
174
|
-
return undefined;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
switch (schema.type) {
|
|
178
|
-
case "string":
|
|
179
|
-
if (typeof value !== "string") {
|
|
180
|
-
return `Expected string for ${schema.key}`;
|
|
181
|
-
}
|
|
182
|
-
break;
|
|
183
|
-
case "number":
|
|
184
|
-
if (typeof value !== "number" || Number.isNaN(value)) {
|
|
185
|
-
return `Expected number for ${schema.key}`;
|
|
186
|
-
}
|
|
187
|
-
break;
|
|
188
|
-
case "boolean":
|
|
189
|
-
if (typeof value !== "boolean") {
|
|
190
|
-
return `Expected boolean for ${schema.key}`;
|
|
191
|
-
}
|
|
192
|
-
break;
|
|
193
|
-
case "object":
|
|
194
|
-
if (typeof value !== "object" || Array.isArray(value)) {
|
|
195
|
-
return `Expected object for ${schema.key}`;
|
|
196
|
-
}
|
|
197
|
-
break;
|
|
198
|
-
case "array":
|
|
199
|
-
if (!Array.isArray(value)) {
|
|
200
|
-
return `Expected array for ${schema.key}`;
|
|
201
|
-
}
|
|
202
|
-
break;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
return undefined;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/** Validate all values against schemas */
|
|
209
|
-
export function validateAll(
|
|
210
|
-
state: SettingsInspectorState,
|
|
211
|
-
): Record<string, string> {
|
|
212
|
-
const errors: Record<string, string> = {};
|
|
213
|
-
for (const schema of state.schemas) {
|
|
214
|
-
const error = validateValue(schema, state.values[schema.key]);
|
|
215
|
-
if (error) {
|
|
216
|
-
errors[schema.key] = error;
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
return errors;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
/** Export state values as JSON */
|
|
223
|
-
export function exportToJSON(state: SettingsInspectorState): string {
|
|
224
|
-
return JSON.stringify(state.values, null, 2);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/** Import values from JSON string */
|
|
228
|
-
export function importFromJSON(
|
|
229
|
-
state: SettingsInspectorState,
|
|
230
|
-
json: string,
|
|
231
|
-
): { state: SettingsInspectorState; errors: Record<string, string> } {
|
|
232
|
-
let parsed: Record<string, unknown>;
|
|
233
|
-
try {
|
|
234
|
-
parsed = JSON.parse(json);
|
|
235
|
-
} catch (err) {
|
|
236
|
-
return {
|
|
237
|
-
state,
|
|
238
|
-
errors: { _parse: `Invalid JSON: ${(err as Error).message}` },
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
const newState: SettingsInspectorState = {
|
|
243
|
-
...state,
|
|
244
|
-
values: { ...state.values },
|
|
245
|
-
};
|
|
246
|
-
|
|
247
|
-
const errors: Record<string, string> = {};
|
|
248
|
-
for (const schema of state.schemas) {
|
|
249
|
-
if (parsed[schema.key] !== undefined) {
|
|
250
|
-
const error = validateValue(schema, parsed[schema.key]);
|
|
251
|
-
if (error) {
|
|
252
|
-
errors[schema.key] = error;
|
|
253
|
-
} else {
|
|
254
|
-
newState.values[schema.key] = parsed[schema.key];
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
return { state: newState, errors };
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
/** Format a setting for display */
|
|
263
|
-
export function formatSetting(
|
|
264
|
-
schema: SettingSchema,
|
|
265
|
-
value: unknown,
|
|
266
|
-
): string {
|
|
267
|
-
const displayValue = value === undefined ? "(unset)" : JSON.stringify(value);
|
|
268
|
-
const requiredMark = schema.required ? "*" : "";
|
|
269
|
-
return `${schema.key}${requiredMark}: ${displayValue}\n ${schema.description}`;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
/** Render the inspector as markdown (for non-TUI environments) */
|
|
273
|
-
export function renderAsMarkdown(
|
|
274
|
-
state: SettingsInspectorState,
|
|
275
|
-
): string {
|
|
276
|
-
const filtered = getFilteredSchemas(state);
|
|
277
|
-
const lines = [
|
|
278
|
-
"## ⚙️ Settings",
|
|
279
|
-
"",
|
|
280
|
-
state.searchQuery ? `*Filter: "${state.searchQuery}"*` : "",
|
|
281
|
-
"",
|
|
282
|
-
];
|
|
283
|
-
|
|
284
|
-
for (let i = 0; i < filtered.length; i++) {
|
|
285
|
-
const schema = filtered[i];
|
|
286
|
-
const value = state.values[schema.key];
|
|
287
|
-
const selected = i === state.selectedIndex ? "> " : " ";
|
|
288
|
-
const required = schema.required ? " **(required)**" : "";
|
|
289
|
-
|
|
290
|
-
lines.push(
|
|
291
|
-
`${selected}**${schema.key}**${required} \`${schema.type}\``,
|
|
292
|
-
` ${schema.description}`,
|
|
293
|
-
` Value: \`${JSON.stringify(value)}\``,
|
|
294
|
-
"",
|
|
295
|
-
);
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
if (filtered.length === 0) {
|
|
299
|
-
lines.push("*No settings match your search.*");
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
return lines.join("\n");
|
|
303
|
-
}
|