@narumitw/pi-starship 0.55.1 → 0.55.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/dist/chunks/{chunk-UX6PBXVK.js → chunk-JO5PH6FN.js} +28 -110
- package/dist/chunks/chunk-JO5PH6FN.js.map +7 -0
- package/dist/chunks/{chunk-P4XMFSQW.js → chunk-YWY5RIDQ.js} +3 -7
- package/dist/chunks/chunk-YWY5RIDQ.js.map +7 -0
- package/dist/chunks/{chunk-EOGXAMTA.js → chunk-ZQZSXXPI.js} +1 -1
- package/dist/chunks/chunk-ZQZSXXPI.js.map +7 -0
- package/dist/chunks/{cloud-BKHYWCTA.js → cloud-C4VU3TX3.js} +7 -14
- package/dist/chunks/cloud-C4VU3TX3.js.map +7 -0
- package/dist/chunks/{commands-53ZO2JUK.js → commands-ATILSUVK.js} +13 -42
- package/dist/chunks/commands-ATILSUVK.js.map +7 -0
- package/dist/chunks/{deployment-RKKOEQQ2.js → deployment-FMNTYRBW.js} +6 -21
- package/dist/chunks/deployment-FMNTYRBW.js.map +7 -0
- package/dist/chunks/{development-BERD42HW.js → development-OUYXDPBS.js} +4 -8
- package/dist/chunks/development-OUYXDPBS.js.map +7 -0
- package/dist/chunks/{execution-42JTEY3N.js → execution-HJOGLMWD.js} +3 -3
- package/dist/chunks/execution-HJOGLMWD.js.map +7 -0
- package/dist/chunks/{languages-RMP6DW7H.js → languages-WXMF4GOF.js} +6 -22
- package/dist/chunks/languages-WXMF4GOF.js.map +7 -0
- package/dist/chunks/{package-LG5H7BBM.js → package-YXRZQAOV.js} +5 -14
- package/dist/chunks/package-YXRZQAOV.js.map +7 -0
- package/dist/index.ts +20 -55
- package/dist/index.ts.map +2 -2
- package/package.json +60 -61
- package/skills/configuring-pi-starship/scripts/apply.mjs +43 -47
- package/skills/configuring-pi-starship/scripts/config-path.mjs +3 -3
- package/skills/configuring-pi-starship/scripts/script-support.mjs +6 -6
- package/skills/configuring-pi-starship/scripts/validate.mjs +18 -18
- package/src/command-configuration.ts +267 -286
- package/src/command-contract.ts +6 -6
- package/src/command-inspector.ts +27 -27
- package/src/command-preset-picker.ts +45 -50
- package/src/command-preview.ts +153 -166
- package/src/commands.ts +588 -618
- package/src/config.ts +580 -660
- package/src/effective-config.ts +46 -48
- package/src/format/formatter.ts +211 -227
- package/src/format/style.ts +205 -216
- package/src/modules/activity.ts +74 -74
- package/src/modules/brand.ts +9 -9
- package/src/modules/cache.ts +18 -18
- package/src/modules/catalog.ts +79 -83
- package/src/modules/cloud.ts +54 -54
- package/src/modules/context.ts +27 -27
- package/src/modules/cost.ts +23 -23
- package/src/modules/deployment.ts +45 -45
- package/src/modules/development.ts +80 -81
- package/src/modules/directory.ts +89 -113
- package/src/modules/display.ts +9 -9
- package/src/modules/execution.ts +62 -69
- package/src/modules/extension-status.ts +59 -64
- package/src/modules/fill.ts +10 -10
- package/src/modules/git/branch.ts +26 -26
- package/src/modules/git/commit.ts +21 -23
- package/src/modules/git/metrics.ts +22 -22
- package/src/modules/git/runtime.ts +299 -334
- package/src/modules/git/state.ts +17 -17
- package/src/modules/git/status.ts +77 -79
- package/src/modules/git/worktree.ts +10 -12
- package/src/modules/github-pr.ts +20 -20
- package/src/modules/helpers.ts +3 -3
- package/src/modules/index.ts +21 -21
- package/src/modules/inspection.ts +101 -108
- package/src/modules/languages.ts +52 -59
- package/src/modules/model.ts +57 -67
- package/src/modules/package.ts +12 -12
- package/src/modules/provider.ts +22 -24
- package/src/modules/render.ts +124 -140
- package/src/modules/thinking.ts +27 -26
- package/src/modules/time.ts +10 -13
- package/src/modules/tokens.ts +13 -13
- package/src/modules/truncation.ts +14 -17
- package/src/modules/turn.ts +9 -9
- package/src/modules/types.ts +125 -132
- package/src/modules/workspace-helpers.ts +4 -7
- package/src/pi-starship.ts +567 -603
- package/src/presets/catalog.ts +113 -113
- package/src/runtime/cloud.ts +122 -150
- package/src/runtime/command.ts +106 -108
- package/src/runtime/deployment.ts +150 -179
- package/src/runtime/development.ts +107 -124
- package/src/runtime/execution.ts +110 -124
- package/src/runtime/github-pr.ts +223 -252
- package/src/runtime/helpers.ts +186 -199
- package/src/runtime/languages.ts +149 -180
- package/src/runtime/package.ts +74 -87
- package/src/runtime/refresh-controller.ts +82 -86
- package/src/runtime/types.ts +38 -38
- package/src/runtime/workspace.ts +108 -116
- package/src/usage.ts +40 -40
- package/dist/chunks/chunk-EOGXAMTA.js.map +0 -7
- package/dist/chunks/chunk-P4XMFSQW.js.map +0 -7
- package/dist/chunks/chunk-UX6PBXVK.js.map +0 -7
- package/dist/chunks/cloud-BKHYWCTA.js.map +0 -7
- package/dist/chunks/commands-53ZO2JUK.js.map +0 -7
- package/dist/chunks/deployment-RKKOEQQ2.js.map +0 -7
- package/dist/chunks/development-BERD42HW.js.map +0 -7
- package/dist/chunks/execution-42JTEY3N.js.map +0 -7
- package/dist/chunks/languages-RMP6DW7H.js.map +0 -7
- package/dist/chunks/package-LG5H7BBM.js.map +0 -7
package/src/config.ts
CHANGED
|
@@ -1,76 +1,59 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
-
import {
|
|
3
|
-
existsSync,
|
|
4
|
-
lstatSync,
|
|
5
|
-
mkdirSync,
|
|
6
|
-
readFileSync,
|
|
7
|
-
renameSync,
|
|
8
|
-
rmSync,
|
|
9
|
-
writeFileSync,
|
|
10
|
-
} from "node:fs";
|
|
2
|
+
import { existsSync, lstatSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
11
3
|
import { createRequire } from "node:module";
|
|
12
4
|
import { dirname, join } from "node:path";
|
|
13
5
|
import type { TomlTable } from "smol-toml";
|
|
14
|
-
import {
|
|
15
|
-
type FormatNode,
|
|
16
|
-
formatVariables,
|
|
17
|
-
parseFormat,
|
|
18
|
-
styleVariables,
|
|
19
|
-
} from "./format/formatter.js";
|
|
6
|
+
import { type FormatNode, formatVariables, parseFormat, styleVariables } from "./format/formatter.js";
|
|
20
7
|
import { type ColorPalette, isValidStyle, parseColor } from "./format/style.js";
|
|
21
8
|
import { MODULE_DEFINITIONS, MODULE_NAMES, type ModuleName } from "./modules/catalog.js";
|
|
22
|
-
import type {
|
|
23
|
-
ModuleDisplayConfig,
|
|
24
|
-
ModuleOptionSchema,
|
|
25
|
-
ModuleOptionValue,
|
|
26
|
-
} from "./modules/types.js";
|
|
9
|
+
import type { ModuleDisplayConfig, ModuleOptionSchema, ModuleOptionValue } from "./modules/types.js";
|
|
27
10
|
|
|
28
11
|
export const CONFIG_FILE_NAME = "pi-starship.toml";
|
|
29
12
|
export { MODULE_NAMES, type ModuleName } from "./modules/catalog.js";
|
|
30
13
|
|
|
31
14
|
const MODULE_CONTENT_VARIABLES = Object.fromEntries(
|
|
32
|
-
|
|
15
|
+
MODULE_DEFINITIONS.map((definition) => [definition.name, definition.variables]),
|
|
33
16
|
) as Record<ModuleName, readonly string[]>;
|
|
34
17
|
|
|
35
18
|
export interface ModuleConfig {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
19
|
+
format: string;
|
|
20
|
+
formatAst: FormatNode[];
|
|
21
|
+
symbol: string;
|
|
22
|
+
style: string;
|
|
23
|
+
styles: Record<string, string>;
|
|
24
|
+
display: ModuleDisplayConfig[];
|
|
25
|
+
disabled: boolean;
|
|
26
|
+
options: Record<string, ModuleOptionValue>;
|
|
44
27
|
}
|
|
45
28
|
|
|
46
29
|
export interface ExtensionStatusConfig {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
30
|
+
separator: string;
|
|
31
|
+
maxStatuses: number;
|
|
32
|
+
icons: Record<string, string>;
|
|
50
33
|
}
|
|
51
34
|
|
|
52
35
|
export interface StarshipConfig {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
36
|
+
format: string;
|
|
37
|
+
formatAst: FormatNode[];
|
|
38
|
+
palette?: string;
|
|
39
|
+
palettes: Record<string, Record<string, string>>;
|
|
40
|
+
modules: Record<ModuleName, ModuleConfig>;
|
|
41
|
+
extensionStatus: ExtensionStatusConfig;
|
|
59
42
|
}
|
|
60
43
|
|
|
61
44
|
export interface ConfigDiagnostic {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
45
|
+
severity: "warning" | "error";
|
|
46
|
+
path: string;
|
|
47
|
+
message: string;
|
|
65
48
|
}
|
|
66
49
|
|
|
67
50
|
export interface LoadedStarshipConfig {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
51
|
+
config: StarshipConfig;
|
|
52
|
+
source: "built-in" | "user";
|
|
53
|
+
settingsPath: string;
|
|
54
|
+
rawDocument?: string;
|
|
55
|
+
fileIdentity?: { dev: number; ino: number };
|
|
56
|
+
diagnostics: ConfigDiagnostic[];
|
|
74
57
|
}
|
|
75
58
|
|
|
76
59
|
const BUILT_IN_FORMAT_DOCUMENT = String.raw`format = """
|
|
@@ -84,34 +67,30 @@ $activity\
|
|
|
84
67
|
$context\
|
|
85
68
|
$time"""`;
|
|
86
69
|
|
|
87
|
-
const BUILT_IN_FORMAT =
|
|
88
|
-
"$brand$model$thinking$directory$git_branch$git_status$activity$context$time";
|
|
70
|
+
const BUILT_IN_FORMAT = "$brand$model$thinking$directory$git_branch$git_status$activity$context$time";
|
|
89
71
|
|
|
90
72
|
const BUILT_IN_MODULES = Object.fromEntries(
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
),
|
|
104
|
-
},
|
|
105
|
-
]),
|
|
73
|
+
MODULE_DEFINITIONS.map(({ name, defaults, styleDefaults, displayDefaults, options }) => [
|
|
74
|
+
name,
|
|
75
|
+
{
|
|
76
|
+
...defaults,
|
|
77
|
+
formatAst: parseFormat(defaults.format),
|
|
78
|
+
styles: { ...styleDefaults },
|
|
79
|
+
display: structuredClone(displayDefaults ?? []),
|
|
80
|
+
options: Object.fromEntries(
|
|
81
|
+
Object.entries(options ?? {}).map(([key, schema]) => [key, cloneOptionValue(schema.default)]),
|
|
82
|
+
),
|
|
83
|
+
},
|
|
84
|
+
]),
|
|
106
85
|
) as Record<ModuleName, ModuleConfig>;
|
|
107
86
|
|
|
108
87
|
export const BUILT_IN_CONFIG: StarshipConfig = {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
88
|
+
format: BUILT_IN_FORMAT,
|
|
89
|
+
formatAst: parseFormat(BUILT_IN_FORMAT),
|
|
90
|
+
palette: undefined,
|
|
91
|
+
palettes: {},
|
|
92
|
+
modules: BUILT_IN_MODULES,
|
|
93
|
+
extensionStatus: { separator: " • ", maxStatuses: 5, icons: {} },
|
|
115
94
|
};
|
|
116
95
|
|
|
117
96
|
export const BUILT_IN_EXAMPLE = `# Native Pi modules with Starship-compatible format and style syntax.\n${BUILT_IN_FORMAT_DOCUMENT}\n`;
|
|
@@ -120,684 +99,625 @@ const require = createRequire(import.meta.url);
|
|
|
120
99
|
let parseTomlImplementation: typeof import("smol-toml")["parse"] | undefined;
|
|
121
100
|
|
|
122
101
|
function parseToml(document: string): TomlTable {
|
|
123
|
-
|
|
124
|
-
|
|
102
|
+
parseTomlImplementation ??= (require("smol-toml") as typeof import("smol-toml")).parse;
|
|
103
|
+
return parseTomlImplementation(document);
|
|
125
104
|
}
|
|
126
105
|
|
|
127
106
|
export function settingsFilePath(agentDir: string): string {
|
|
128
|
-
|
|
107
|
+
return join(agentDir, CONFIG_FILE_NAME);
|
|
129
108
|
}
|
|
130
109
|
|
|
131
110
|
export function loadStarshipConfig(settingsPath: string): LoadedStarshipConfig {
|
|
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
|
-
|
|
111
|
+
let rawDocument: string;
|
|
112
|
+
try {
|
|
113
|
+
rawDocument = readFileSync(settingsPath, "utf8");
|
|
114
|
+
} catch (error) {
|
|
115
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT" && !existsSync(settingsPath)) {
|
|
116
|
+
return {
|
|
117
|
+
config: cloneBuiltInConfig(),
|
|
118
|
+
source: "built-in",
|
|
119
|
+
settingsPath,
|
|
120
|
+
diagnostics: [],
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
config: cloneBuiltInConfig(),
|
|
125
|
+
source: "built-in",
|
|
126
|
+
settingsPath,
|
|
127
|
+
diagnostics: [diagnostic("error", "", `Unable to read settings: ${formatError(error)}`)],
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
let parsed: TomlTable;
|
|
132
|
+
try {
|
|
133
|
+
parsed = parseToml(rawDocument);
|
|
134
|
+
} catch (error) {
|
|
135
|
+
return {
|
|
136
|
+
config: cloneBuiltInConfig(),
|
|
137
|
+
source: "built-in",
|
|
138
|
+
settingsPath,
|
|
139
|
+
rawDocument,
|
|
140
|
+
diagnostics: [diagnostic("error", "", `Unable to parse TOML: ${formatError(error)}`)],
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
const normalized = normalizeConfig(parsed);
|
|
144
|
+
return {
|
|
145
|
+
...normalized,
|
|
146
|
+
source: "user",
|
|
147
|
+
settingsPath,
|
|
148
|
+
rawDocument,
|
|
149
|
+
};
|
|
171
150
|
}
|
|
172
151
|
|
|
173
152
|
export function normalizeConfig(value: unknown): {
|
|
174
|
-
|
|
175
|
-
|
|
153
|
+
config: StarshipConfig;
|
|
154
|
+
diagnostics: ConfigDiagnostic[];
|
|
176
155
|
} {
|
|
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
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
validateLiteralStyles(module.formatAst, palette, `${name}.format`, diagnostics);
|
|
281
|
-
if (definition.styleDefaults) {
|
|
282
|
-
if (definition.fallbackStyle) {
|
|
283
|
-
validateModuleStyleField(name, "style", module, palette, diagnostics);
|
|
284
|
-
}
|
|
285
|
-
for (const field of Object.keys(definition.styleDefaults)) {
|
|
286
|
-
validateModuleStyleField(name, field, module, palette, diagnostics);
|
|
287
|
-
}
|
|
288
|
-
} else if (!definition.displayDefaults) {
|
|
289
|
-
validateModuleStyleField(name, "style", module, palette, diagnostics);
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
return { config, diagnostics };
|
|
156
|
+
const config = cloneBuiltInConfig();
|
|
157
|
+
const diagnostics: ConfigDiagnostic[] = [];
|
|
158
|
+
if (!isRecord(value)) {
|
|
159
|
+
return {
|
|
160
|
+
config,
|
|
161
|
+
diagnostics: [diagnostic("error", "", "Settings must contain a TOML table")],
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const knownRoot = new Set(["format", "palette", "palettes", ...MODULE_NAMES]);
|
|
166
|
+
for (const key of Object.keys(value)) {
|
|
167
|
+
if (!knownRoot.has(key)) diagnostics.push(unknownDiagnostic(key));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (value.format !== undefined) {
|
|
171
|
+
if (typeof value.format !== "string") {
|
|
172
|
+
diagnostics.push(typeDiagnostic("format", "string"));
|
|
173
|
+
} else {
|
|
174
|
+
try {
|
|
175
|
+
config.formatAst = parseFormat(value.format);
|
|
176
|
+
config.format = value.format;
|
|
177
|
+
} catch (error) {
|
|
178
|
+
diagnostics.push(diagnostic("warning", "format", `Invalid format; using built-in: ${formatError(error)}`));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (value.palettes !== undefined) {
|
|
184
|
+
if (!isRecord(value.palettes)) {
|
|
185
|
+
diagnostics.push(typeDiagnostic("palettes", "table"));
|
|
186
|
+
} else {
|
|
187
|
+
for (const [paletteName, paletteValue] of Object.entries(value.palettes)) {
|
|
188
|
+
if (!isRecord(paletteValue)) {
|
|
189
|
+
diagnostics.push(typeDiagnostic(`palettes.${paletteName}`, "table"));
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
const palette: Record<string, string> = {};
|
|
193
|
+
for (const [name, color] of Object.entries(paletteValue)) {
|
|
194
|
+
if (typeof color !== "string" || !parseColor(color.toLowerCase())) {
|
|
195
|
+
diagnostics.push(
|
|
196
|
+
diagnostic(
|
|
197
|
+
"warning",
|
|
198
|
+
`palettes.${paletteName}.${name}`,
|
|
199
|
+
"Palette colors must be named, ANSI 0-255, or #RRGGBB",
|
|
200
|
+
),
|
|
201
|
+
);
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
setOwn(palette, name, color);
|
|
205
|
+
}
|
|
206
|
+
setOwn(config.palettes, paletteName, palette);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (value.palette !== undefined) {
|
|
212
|
+
if (typeof value.palette !== "string") diagnostics.push(typeDiagnostic("palette", "string"));
|
|
213
|
+
else {
|
|
214
|
+
config.palette = value.palette;
|
|
215
|
+
if (!Object.hasOwn(config.palettes, value.palette)) {
|
|
216
|
+
diagnostics.push(diagnostic("warning", "palette", `Unknown palette ${JSON.stringify(value.palette)}`));
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
for (const name of MODULE_NAMES) {
|
|
222
|
+
const moduleValue = value[name];
|
|
223
|
+
if (moduleValue === undefined) continue;
|
|
224
|
+
if (!isRecord(moduleValue)) {
|
|
225
|
+
diagnostics.push(typeDiagnostic(name, "table"));
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
normalizeModule(name, moduleValue, config, diagnostics);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
validateFormatVariables(config.formatAst, new Set([...MODULE_NAMES, "all"]), "format", diagnostics);
|
|
232
|
+
validateStyleVariables(config.formatAst, new Set(), "format", diagnostics);
|
|
233
|
+
const palette = activePalette(config);
|
|
234
|
+
validateLiteralStyles(config.formatAst, palette, "format", diagnostics);
|
|
235
|
+
for (const definition of MODULE_DEFINITIONS) {
|
|
236
|
+
const name = definition.name;
|
|
237
|
+
const module = config.modules[name];
|
|
238
|
+
validateFormatVariables(module.formatAst, new Set(MODULE_CONTENT_VARIABLES[name]), `${name}.format`, diagnostics);
|
|
239
|
+
validateStyleVariables(
|
|
240
|
+
module.formatAst,
|
|
241
|
+
new Set(definition.styleVariables ?? ["style"]),
|
|
242
|
+
`${name}.format`,
|
|
243
|
+
diagnostics,
|
|
244
|
+
);
|
|
245
|
+
validateLiteralStyles(module.formatAst, palette, `${name}.format`, diagnostics);
|
|
246
|
+
if (definition.styleDefaults) {
|
|
247
|
+
if (definition.fallbackStyle) {
|
|
248
|
+
validateModuleStyleField(name, "style", module, palette, diagnostics);
|
|
249
|
+
}
|
|
250
|
+
for (const field of Object.keys(definition.styleDefaults)) {
|
|
251
|
+
validateModuleStyleField(name, field, module, palette, diagnostics);
|
|
252
|
+
}
|
|
253
|
+
} else if (!definition.displayDefaults) {
|
|
254
|
+
validateModuleStyleField(name, "style", module, palette, diagnostics);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return { config, diagnostics };
|
|
294
259
|
}
|
|
295
260
|
|
|
296
261
|
function normalizeModule(
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
262
|
+
name: ModuleName,
|
|
263
|
+
value: Record<string, unknown>,
|
|
264
|
+
config: StarshipConfig,
|
|
265
|
+
diagnostics: ConfigDiagnostic[],
|
|
301
266
|
) {
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
config.extensionStatus.icons = Object.fromEntries(
|
|
406
|
-
Object.entries(value.icons).flatMap(([key, icon]) => {
|
|
407
|
-
if (typeof icon === "string") return [[key, icon]];
|
|
408
|
-
diagnostics.push(typeDiagnostic(`extension_status.icons.${key}`, "string"));
|
|
409
|
-
return [];
|
|
410
|
-
}),
|
|
411
|
-
);
|
|
412
|
-
}
|
|
413
|
-
}
|
|
267
|
+
const definition = MODULE_DEFINITIONS.find((candidate) => candidate.name === name);
|
|
268
|
+
if (!definition) return;
|
|
269
|
+
const optionSchemas: Readonly<Record<string, ModuleOptionSchema>> = definition.options ?? {};
|
|
270
|
+
const known = new Set(["format", "symbol", "disabled", ...Object.keys(optionSchemas)]);
|
|
271
|
+
if (definition.styleDefaults) {
|
|
272
|
+
if (definition.fallbackStyle) known.add("style");
|
|
273
|
+
for (const field of Object.keys(definition.styleDefaults)) known.add(field);
|
|
274
|
+
} else if (!definition.displayDefaults) known.add("style");
|
|
275
|
+
if (definition.displayDefaults) known.add("display");
|
|
276
|
+
if (name === "extension_status") {
|
|
277
|
+
known.add("separator");
|
|
278
|
+
known.add("max_statuses");
|
|
279
|
+
known.add("icons");
|
|
280
|
+
}
|
|
281
|
+
for (const key of Object.keys(value)) {
|
|
282
|
+
if (!known.has(key)) diagnostics.push(unknownDiagnostic(`${name}.${key}`));
|
|
283
|
+
}
|
|
284
|
+
const module = config.modules[name];
|
|
285
|
+
if (value.format !== undefined) {
|
|
286
|
+
if (typeof value.format !== "string") diagnostics.push(typeDiagnostic(`${name}.format`, "string"));
|
|
287
|
+
else {
|
|
288
|
+
try {
|
|
289
|
+
module.formatAst = parseFormat(value.format);
|
|
290
|
+
module.format = value.format;
|
|
291
|
+
} catch (error) {
|
|
292
|
+
diagnostics.push(
|
|
293
|
+
diagnostic("warning", `${name}.format`, `Invalid format; using module default: ${formatError(error)}`),
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (value.symbol !== undefined) {
|
|
299
|
+
if (typeof value.symbol !== "string") {
|
|
300
|
+
diagnostics.push(typeDiagnostic(`${name}.symbol`, "string"));
|
|
301
|
+
} else module.symbol = value.symbol;
|
|
302
|
+
}
|
|
303
|
+
if (definition.styleDefaults && definition.fallbackStyle && value.style !== undefined) {
|
|
304
|
+
if (typeof value.style !== "string") {
|
|
305
|
+
diagnostics.push(typeDiagnostic(`${name}.style`, "string"));
|
|
306
|
+
} else module.style = value.style;
|
|
307
|
+
}
|
|
308
|
+
if (definition.styleDefaults) {
|
|
309
|
+
for (const field of Object.keys(definition.styleDefaults)) {
|
|
310
|
+
if (value[field] === undefined) continue;
|
|
311
|
+
if (typeof value[field] !== "string") {
|
|
312
|
+
diagnostics.push(typeDiagnostic(`${name}.${field}`, "string"));
|
|
313
|
+
} else module.styles[field] = value[field];
|
|
314
|
+
}
|
|
315
|
+
} else if (!definition.displayDefaults && value.style !== undefined) {
|
|
316
|
+
if (typeof value.style !== "string") {
|
|
317
|
+
diagnostics.push(typeDiagnostic(`${name}.style`, "string"));
|
|
318
|
+
} else module.style = value.style;
|
|
319
|
+
}
|
|
320
|
+
if (definition.displayDefaults && value.display !== undefined) {
|
|
321
|
+
module.display = normalizeDisplay(
|
|
322
|
+
name,
|
|
323
|
+
value.display,
|
|
324
|
+
definition.displayDefaults,
|
|
325
|
+
activePalette(config),
|
|
326
|
+
diagnostics,
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
if (value.disabled !== undefined) {
|
|
330
|
+
if (typeof value.disabled !== "boolean") {
|
|
331
|
+
diagnostics.push(typeDiagnostic(`${name}.disabled`, "boolean"));
|
|
332
|
+
} else module.disabled = value.disabled;
|
|
333
|
+
}
|
|
334
|
+
for (const [key, schema] of Object.entries(optionSchemas)) {
|
|
335
|
+
if (value[key] === undefined) continue;
|
|
336
|
+
const normalized = normalizeModuleOption(value[key], schema);
|
|
337
|
+
if (normalized.ok) module.options[key] = normalized.value;
|
|
338
|
+
else diagnostics.push(diagnostic("warning", `${name}.${key}`, normalized.message));
|
|
339
|
+
}
|
|
340
|
+
if (name !== "extension_status") return;
|
|
341
|
+
if (value.separator !== undefined) {
|
|
342
|
+
if (typeof value.separator !== "string") {
|
|
343
|
+
diagnostics.push(typeDiagnostic("extension_status.separator", "string"));
|
|
344
|
+
} else config.extensionStatus.separator = value.separator;
|
|
345
|
+
}
|
|
346
|
+
if (value.max_statuses !== undefined) {
|
|
347
|
+
if (
|
|
348
|
+
typeof value.max_statuses !== "number" ||
|
|
349
|
+
!Number.isInteger(value.max_statuses) ||
|
|
350
|
+
value.max_statuses < 0 ||
|
|
351
|
+
value.max_statuses > 100
|
|
352
|
+
) {
|
|
353
|
+
diagnostics.push(
|
|
354
|
+
diagnostic("warning", "extension_status.max_statuses", "Expected an integer from 0 through 100"),
|
|
355
|
+
);
|
|
356
|
+
} else config.extensionStatus.maxStatuses = value.max_statuses;
|
|
357
|
+
}
|
|
358
|
+
if (value.icons !== undefined) {
|
|
359
|
+
if (!isRecord(value.icons)) diagnostics.push(typeDiagnostic("extension_status.icons", "table"));
|
|
360
|
+
else {
|
|
361
|
+
config.extensionStatus.icons = Object.fromEntries(
|
|
362
|
+
Object.entries(value.icons).flatMap(([key, icon]) => {
|
|
363
|
+
if (typeof icon === "string") return [[key, icon]];
|
|
364
|
+
diagnostics.push(typeDiagnostic(`extension_status.icons.${key}`, "string"));
|
|
365
|
+
return [];
|
|
366
|
+
}),
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
414
370
|
}
|
|
415
371
|
|
|
416
372
|
interface AtomicFileSystem {
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
export function validateConfigDocument(
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
settingsPath,
|
|
441
|
-
rawDocument,
|
|
442
|
-
};
|
|
373
|
+
mkdirSync: typeof mkdirSync;
|
|
374
|
+
writeFileSync: typeof writeFileSync;
|
|
375
|
+
renameSync: typeof renameSync;
|
|
376
|
+
rmSync: typeof rmSync;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
export function validateConfigDocument(settingsPath: string, rawDocument: string): LoadedStarshipConfig {
|
|
380
|
+
let parsed: TomlTable;
|
|
381
|
+
try {
|
|
382
|
+
parsed = parseToml(rawDocument);
|
|
383
|
+
} catch (error) {
|
|
384
|
+
throw new Error(`Unable to parse TOML: ${formatError(error)}`);
|
|
385
|
+
}
|
|
386
|
+
const normalized = normalizeConfig(parsed);
|
|
387
|
+
if (normalized.diagnostics.some((item) => item.severity === "error")) {
|
|
388
|
+
throw new Error(normalized.diagnostics.map((item) => item.message).join("\n"));
|
|
389
|
+
}
|
|
390
|
+
return {
|
|
391
|
+
...normalized,
|
|
392
|
+
source: "user",
|
|
393
|
+
settingsPath,
|
|
394
|
+
rawDocument,
|
|
395
|
+
};
|
|
443
396
|
}
|
|
444
397
|
|
|
445
398
|
export function atomicSaveConfigDocument(
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
399
|
+
settingsPath: string,
|
|
400
|
+
rawDocument: string,
|
|
401
|
+
overrides: Partial<AtomicFileSystem> = {},
|
|
449
402
|
): LoadedStarshipConfig {
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
403
|
+
const validated = validateConfigDocument(settingsPath, rawDocument);
|
|
404
|
+
return {
|
|
405
|
+
...validated,
|
|
406
|
+
fileIdentity: atomicWriteConfigDocument(settingsPath, rawDocument, overrides),
|
|
407
|
+
};
|
|
455
408
|
}
|
|
456
409
|
|
|
457
410
|
export function atomicRestoreConfigDocument(
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
411
|
+
settingsPath: string,
|
|
412
|
+
rawDocument: string,
|
|
413
|
+
overrides: Partial<AtomicFileSystem> = {},
|
|
461
414
|
) {
|
|
462
|
-
|
|
415
|
+
atomicWriteConfigDocument(settingsPath, rawDocument, overrides);
|
|
463
416
|
}
|
|
464
417
|
|
|
465
418
|
export function removeConfigDocumentIfMatches(
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
419
|
+
settingsPath: string,
|
|
420
|
+
expectedRawDocument: string,
|
|
421
|
+
expectedIdentity: { dev: number; ino: number },
|
|
422
|
+
overrides: Pick<Partial<AtomicFileSystem>, "rmSync"> = {},
|
|
470
423
|
) {
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
throw new Error("Starship settings changed concurrently; the newer file was preserved");
|
|
424
|
+
const quarantinePath = join(dirname(settingsPath), `.${CONFIG_FILE_NAME}.${randomUUID()}.rollback`);
|
|
425
|
+
const before = lstatSync(settingsPath);
|
|
426
|
+
if (before.dev !== expectedIdentity.dev || before.ino !== expectedIdentity.ino) {
|
|
427
|
+
throw new Error("Starship settings changed concurrently; the newer file was preserved");
|
|
428
|
+
}
|
|
429
|
+
renameSync(settingsPath, quarantinePath);
|
|
430
|
+
const quarantined = lstatSync(quarantinePath);
|
|
431
|
+
const quarantinedSavedFile =
|
|
432
|
+
quarantined.isFile() &&
|
|
433
|
+
!quarantined.isSymbolicLink() &&
|
|
434
|
+
quarantined.dev === expectedIdentity.dev &&
|
|
435
|
+
quarantined.ino === expectedIdentity.ino;
|
|
436
|
+
if (quarantinedSavedFile && readFileSync(quarantinePath, "utf8") === expectedRawDocument) {
|
|
437
|
+
(overrides.rmSync ?? rmSync)(quarantinePath);
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
if (quarantinedSavedFile && !pathEntryExists(settingsPath)) {
|
|
441
|
+
try {
|
|
442
|
+
renameSync(quarantinePath, settingsPath);
|
|
443
|
+
} catch {
|
|
444
|
+
// Keep the quarantine for recovery when its atomic restoration fails.
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
throw new Error("Starship settings changed concurrently; the newer file was preserved");
|
|
498
448
|
}
|
|
499
449
|
|
|
500
450
|
function atomicWriteConfigDocument(
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
451
|
+
settingsPath: string,
|
|
452
|
+
rawDocument: string,
|
|
453
|
+
overrides: Partial<AtomicFileSystem>,
|
|
504
454
|
): { dev: number; ino: number } {
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
455
|
+
const fs = { mkdirSync, writeFileSync, renameSync, rmSync, ...overrides };
|
|
456
|
+
const replaceExisting = pathEntryExists(settingsPath);
|
|
457
|
+
fs.mkdirSync(dirname(settingsPath), { recursive: true });
|
|
458
|
+
const tempPath = join(dirname(settingsPath), `.${CONFIG_FILE_NAME}.${randomUUID()}.tmp`);
|
|
459
|
+
try {
|
|
460
|
+
fs.writeFileSync(tempPath, rawDocument, { encoding: "utf8", flag: "wx" });
|
|
461
|
+
const info = lstatSync(tempPath);
|
|
462
|
+
if (!replaceExisting && pathEntryExists(settingsPath)) {
|
|
463
|
+
throw new Error(`${CONFIG_FILE_NAME} was created concurrently; reopen settings and retry.`);
|
|
464
|
+
}
|
|
465
|
+
fs.renameSync(tempPath, settingsPath);
|
|
466
|
+
return { dev: info.dev, ino: info.ino };
|
|
467
|
+
} finally {
|
|
468
|
+
try {
|
|
469
|
+
fs.rmSync(tempPath, { force: true });
|
|
470
|
+
} catch {
|
|
471
|
+
// Best-effort cleanup must not replace the publication result.
|
|
472
|
+
}
|
|
473
|
+
}
|
|
524
474
|
}
|
|
525
475
|
|
|
526
476
|
function cloneBuiltInConfig(): StarshipConfig {
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
477
|
+
return {
|
|
478
|
+
...BUILT_IN_CONFIG,
|
|
479
|
+
formatAst: structuredClone(BUILT_IN_CONFIG.formatAst),
|
|
480
|
+
palettes: Object.fromEntries(
|
|
481
|
+
Object.entries(BUILT_IN_CONFIG.palettes).map(([name, colors]) => [name, { ...colors }]),
|
|
482
|
+
),
|
|
483
|
+
modules: Object.fromEntries(
|
|
484
|
+
MODULE_NAMES.map((name) => [
|
|
485
|
+
name,
|
|
486
|
+
{
|
|
487
|
+
...BUILT_IN_CONFIG.modules[name],
|
|
488
|
+
formatAst: structuredClone(BUILT_IN_CONFIG.modules[name].formatAst),
|
|
489
|
+
styles: { ...BUILT_IN_CONFIG.modules[name].styles },
|
|
490
|
+
display: structuredClone(BUILT_IN_CONFIG.modules[name].display),
|
|
491
|
+
options: structuredClone(BUILT_IN_CONFIG.modules[name].options),
|
|
492
|
+
},
|
|
493
|
+
]),
|
|
494
|
+
) as Record<ModuleName, ModuleConfig>,
|
|
495
|
+
extensionStatus: {
|
|
496
|
+
...BUILT_IN_CONFIG.extensionStatus,
|
|
497
|
+
icons: { ...BUILT_IN_CONFIG.extensionStatus.icons },
|
|
498
|
+
},
|
|
499
|
+
};
|
|
550
500
|
}
|
|
551
501
|
|
|
552
502
|
function validateFormatVariables(
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
503
|
+
ast: readonly FormatNode[],
|
|
504
|
+
allowed: ReadonlySet<string>,
|
|
505
|
+
path: string,
|
|
506
|
+
diagnostics: ConfigDiagnostic[],
|
|
557
507
|
) {
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
`Unknown variable ${JSON.stringify(variable)} in ${path} was ignored`,
|
|
565
|
-
),
|
|
566
|
-
);
|
|
567
|
-
}
|
|
508
|
+
for (const variable of formatVariables(ast)) {
|
|
509
|
+
if (allowed.has(variable)) continue;
|
|
510
|
+
diagnostics.push(
|
|
511
|
+
diagnostic("warning", path, `Unknown variable ${JSON.stringify(variable)} in ${path} was ignored`),
|
|
512
|
+
);
|
|
513
|
+
}
|
|
568
514
|
}
|
|
569
515
|
|
|
570
516
|
function validateStyleVariables(
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
517
|
+
ast: readonly FormatNode[],
|
|
518
|
+
allowed: ReadonlySet<string>,
|
|
519
|
+
path: string,
|
|
520
|
+
diagnostics: ConfigDiagnostic[],
|
|
575
521
|
) {
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
`Unknown style variable ${JSON.stringify(variable)} in ${path} was ignored`,
|
|
583
|
-
),
|
|
584
|
-
);
|
|
585
|
-
}
|
|
522
|
+
for (const variable of styleVariables(ast)) {
|
|
523
|
+
if (allowed.has(variable)) continue;
|
|
524
|
+
diagnostics.push(
|
|
525
|
+
diagnostic("warning", path, `Unknown style variable ${JSON.stringify(variable)} in ${path} was ignored`),
|
|
526
|
+
);
|
|
527
|
+
}
|
|
586
528
|
}
|
|
587
529
|
|
|
588
530
|
function validateModuleStyleField(
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
531
|
+
name: ModuleName,
|
|
532
|
+
field: string,
|
|
533
|
+
module: ModuleConfig,
|
|
534
|
+
palette: ColorPalette,
|
|
535
|
+
diagnostics: ConfigDiagnostic[],
|
|
594
536
|
) {
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
else {
|
|
606
|
-
const fallback = BUILT_IN_CONFIG.modules[name].styles[field];
|
|
607
|
-
if (fallback !== undefined) module.styles[field] = fallback;
|
|
608
|
-
}
|
|
537
|
+
const style = field === "style" ? module.style : module.styles[field];
|
|
538
|
+
if (style === undefined || isValidStyle(style, palette)) return;
|
|
539
|
+
diagnostics.push(
|
|
540
|
+
diagnostic("warning", `${name}.${field}`, `Invalid style ${JSON.stringify(style)}; using the module default`),
|
|
541
|
+
);
|
|
542
|
+
if (field === "style") module.style = BUILT_IN_CONFIG.modules[name].style;
|
|
543
|
+
else {
|
|
544
|
+
const fallback = BUILT_IN_CONFIG.modules[name].styles[field];
|
|
545
|
+
if (fallback !== undefined) module.styles[field] = fallback;
|
|
546
|
+
}
|
|
609
547
|
}
|
|
610
548
|
|
|
611
549
|
function normalizeDisplay(
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
550
|
+
name: ModuleName,
|
|
551
|
+
value: unknown,
|
|
552
|
+
defaults: readonly ModuleDisplayConfig[],
|
|
553
|
+
palette: ColorPalette,
|
|
554
|
+
diagnostics: ConfigDiagnostic[],
|
|
617
555
|
): ModuleDisplayConfig[] {
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
diagnostics.push(
|
|
659
|
-
diagnostic("warning", `${name}.display`, "Expected at least one valid entry; using defaults"),
|
|
660
|
-
);
|
|
661
|
-
return defaults.map((entry) => ({ ...entry }));
|
|
556
|
+
if (!Array.isArray(value)) {
|
|
557
|
+
diagnostics.push(typeDiagnostic(`${name}.display`, "array of tables"));
|
|
558
|
+
return defaults.map((entry) => ({ ...entry }));
|
|
559
|
+
}
|
|
560
|
+
const result: ModuleDisplayConfig[] = [];
|
|
561
|
+
for (const [index, entry] of value.entries()) {
|
|
562
|
+
const path = `${name}.display.${index}`;
|
|
563
|
+
if (!isRecord(entry)) {
|
|
564
|
+
diagnostics.push(typeDiagnostic(path, "table"));
|
|
565
|
+
continue;
|
|
566
|
+
}
|
|
567
|
+
for (const field of Object.keys(entry)) {
|
|
568
|
+
if (!new Set(["threshold", "style", "hidden"]).has(field)) {
|
|
569
|
+
diagnostics.push(unknownDiagnostic(`${path}.${field}`));
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
let valid = true;
|
|
573
|
+
if (typeof entry.threshold !== "number" || !Number.isFinite(entry.threshold)) {
|
|
574
|
+
diagnostics.push(diagnostic("warning", `${path}.threshold`, "Expected a finite number"));
|
|
575
|
+
valid = false;
|
|
576
|
+
}
|
|
577
|
+
if (typeof entry.style !== "string" || !isValidStyle(entry.style, palette)) {
|
|
578
|
+
diagnostics.push(diagnostic("warning", `${path}.style`, "Expected a valid Starship style string"));
|
|
579
|
+
valid = false;
|
|
580
|
+
}
|
|
581
|
+
if (typeof entry.hidden !== "boolean") {
|
|
582
|
+
diagnostics.push(typeDiagnostic(`${path}.hidden`, "boolean"));
|
|
583
|
+
valid = false;
|
|
584
|
+
}
|
|
585
|
+
if (valid) {
|
|
586
|
+
result.push({
|
|
587
|
+
threshold: entry.threshold as number,
|
|
588
|
+
style: entry.style as string,
|
|
589
|
+
hidden: entry.hidden as boolean,
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
if (result.length > 0) return result;
|
|
594
|
+
diagnostics.push(diagnostic("warning", `${name}.display`, "Expected at least one valid entry; using defaults"));
|
|
595
|
+
return defaults.map((entry) => ({ ...entry }));
|
|
662
596
|
}
|
|
663
597
|
|
|
664
598
|
function validateLiteralStyles(
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
599
|
+
ast: readonly FormatNode[],
|
|
600
|
+
palette: ColorPalette,
|
|
601
|
+
path: string,
|
|
602
|
+
diagnostics: ConfigDiagnostic[],
|
|
669
603
|
) {
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
if (node.type === "group" || node.type === "conditional") {
|
|
684
|
-
validateLiteralStyles(node.children, palette, path, diagnostics);
|
|
685
|
-
}
|
|
686
|
-
}
|
|
604
|
+
for (const node of ast) {
|
|
605
|
+
if (node.type === "group" && node.style.every((part) => part.type === "text")) {
|
|
606
|
+
const style = node.style.map((part) => (part.type === "text" ? part.value : "")).join("");
|
|
607
|
+
if (!isValidStyle(style, palette)) {
|
|
608
|
+
diagnostics.push(
|
|
609
|
+
diagnostic("warning", path, `Invalid literal style ${JSON.stringify(style)}; rendered unstyled`),
|
|
610
|
+
);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
if (node.type === "group" || node.type === "conditional") {
|
|
614
|
+
validateLiteralStyles(node.children, palette, path, diagnostics);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
687
617
|
}
|
|
688
618
|
|
|
689
619
|
function normalizeModuleOption(
|
|
690
|
-
|
|
691
|
-
|
|
620
|
+
value: unknown,
|
|
621
|
+
schema: ModuleOptionSchema,
|
|
692
622
|
): { ok: true; value: ModuleOptionValue } | { ok: false; message: string } {
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
}
|
|
740
|
-
const result: Record<string, string> = {};
|
|
741
|
-
for (const [key, item] of Object.entries(value)) setOwn(result, key, item as string);
|
|
742
|
-
return { ok: true, value: result };
|
|
743
|
-
}
|
|
744
|
-
}
|
|
623
|
+
switch (schema.kind) {
|
|
624
|
+
case "string":
|
|
625
|
+
return typeof value === "string" && (schema.allowEmpty !== false || value.length > 0)
|
|
626
|
+
? { ok: true, value }
|
|
627
|
+
: { ok: false, message: "Expected a non-empty string; using the default value" };
|
|
628
|
+
case "string-enum":
|
|
629
|
+
return typeof value === "string" && schema.values.includes(value)
|
|
630
|
+
? { ok: true, value }
|
|
631
|
+
: {
|
|
632
|
+
ok: false,
|
|
633
|
+
message: `Expected one of: ${schema.values.join(", ")}; using the default value`,
|
|
634
|
+
};
|
|
635
|
+
case "boolean":
|
|
636
|
+
return typeof value === "boolean"
|
|
637
|
+
? { ok: true, value }
|
|
638
|
+
: { ok: false, message: "Expected boolean; using the default value" };
|
|
639
|
+
case "integer":
|
|
640
|
+
return typeof value === "number" && Number.isInteger(value) && value >= schema.minimum && value <= schema.maximum
|
|
641
|
+
? { ok: true, value }
|
|
642
|
+
: {
|
|
643
|
+
ok: false,
|
|
644
|
+
message: `Expected an integer from ${schema.minimum} through ${schema.maximum}; using the default value`,
|
|
645
|
+
};
|
|
646
|
+
case "string-array": {
|
|
647
|
+
if (
|
|
648
|
+
!Array.isArray(value) ||
|
|
649
|
+
value.some(
|
|
650
|
+
(item) => typeof item !== "string" || item.length === 0 || (!schema.allowNegative && item.startsWith("!")),
|
|
651
|
+
)
|
|
652
|
+
) {
|
|
653
|
+
return {
|
|
654
|
+
ok: false,
|
|
655
|
+
message: "Expected an array of valid strings; using the default value",
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
return { ok: true, value: [...value] as string[] };
|
|
659
|
+
}
|
|
660
|
+
case "string-map": {
|
|
661
|
+
if (!isRecord(value) || Object.values(value).some((item) => typeof item !== "string")) {
|
|
662
|
+
return { ok: false, message: "Expected a table of strings; using the default value" };
|
|
663
|
+
}
|
|
664
|
+
const result: Record<string, string> = {};
|
|
665
|
+
for (const [key, item] of Object.entries(value)) setOwn(result, key, item as string);
|
|
666
|
+
return { ok: true, value: result };
|
|
667
|
+
}
|
|
668
|
+
}
|
|
745
669
|
}
|
|
746
670
|
|
|
747
671
|
function cloneOptionValue(value: ModuleOptionValue): ModuleOptionValue {
|
|
748
|
-
|
|
672
|
+
return typeof value === "object" ? structuredClone(value) : value;
|
|
749
673
|
}
|
|
750
674
|
|
|
751
675
|
function setOwn<T>(record: Record<string, T>, key: string, value: T) {
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
676
|
+
Object.defineProperty(record, key, {
|
|
677
|
+
value,
|
|
678
|
+
writable: true,
|
|
679
|
+
enumerable: true,
|
|
680
|
+
configurable: true,
|
|
681
|
+
});
|
|
758
682
|
}
|
|
759
683
|
|
|
760
684
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
761
|
-
|
|
685
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
762
686
|
}
|
|
763
687
|
|
|
764
688
|
function typeDiagnostic(path: string, type: string): ConfigDiagnostic {
|
|
765
|
-
|
|
689
|
+
return diagnostic("warning", path, `Expected ${type}; using the default value`);
|
|
766
690
|
}
|
|
767
691
|
|
|
768
692
|
function unknownDiagnostic(path: string): ConfigDiagnostic {
|
|
769
|
-
|
|
693
|
+
return diagnostic("warning", path, `Unknown setting ${JSON.stringify(path)} was ignored`);
|
|
770
694
|
}
|
|
771
695
|
|
|
772
|
-
function diagnostic(
|
|
773
|
-
|
|
774
|
-
path: string,
|
|
775
|
-
message: string,
|
|
776
|
-
): ConfigDiagnostic {
|
|
777
|
-
return { severity, path, message };
|
|
696
|
+
function diagnostic(severity: ConfigDiagnostic["severity"], path: string, message: string): ConfigDiagnostic {
|
|
697
|
+
return { severity, path, message };
|
|
778
698
|
}
|
|
779
699
|
|
|
780
700
|
function pathEntryExists(path: string): boolean {
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
701
|
+
try {
|
|
702
|
+
lstatSync(path);
|
|
703
|
+
return true;
|
|
704
|
+
} catch (error) {
|
|
705
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return false;
|
|
706
|
+
throw error;
|
|
707
|
+
}
|
|
788
708
|
}
|
|
789
709
|
|
|
790
710
|
function formatError(error: unknown): string {
|
|
791
|
-
|
|
711
|
+
return error instanceof Error ? error.message : String(error);
|
|
792
712
|
}
|
|
793
713
|
|
|
794
714
|
export function activePalette(config: StarshipConfig): ColorPalette {
|
|
795
|
-
|
|
715
|
+
return ownPalette(config.palettes, config.palette) ?? {};
|
|
796
716
|
}
|
|
797
717
|
|
|
798
718
|
function ownPalette(
|
|
799
|
-
|
|
800
|
-
|
|
719
|
+
palettes: Readonly<Record<string, Record<string, string>>>,
|
|
720
|
+
name: string | undefined,
|
|
801
721
|
): Record<string, string> | undefined {
|
|
802
|
-
|
|
722
|
+
return name !== undefined && Object.hasOwn(palettes, name) ? palettes[name] : undefined;
|
|
803
723
|
}
|