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