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