@narumitw/pi-starship 0.31.0 → 0.35.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 -2
- package/package.json +1 -1
- package/src/commands.ts +9 -4
- package/src/config.ts +57 -49
- package/src/installed-packages.ts +1 -1
- package/src/modules/extension-status.ts +1 -1
- package/src/modules/git/runtime.ts +23 -4
- package/src/pi-starship.ts +1 -2
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ A native Pi footer configured with Starship-style TOML. It parses and renders fo
|
|
|
8
8
|
|
|
9
9
|
## ✨ Features
|
|
10
10
|
|
|
11
|
-
-
|
|
11
|
+
- Uses a readable built-in Tokyo Night configuration until the user explicitly saves settings.
|
|
12
12
|
- Starship-style root/module formats, conditional groups, `$all`, styles, and palettes.
|
|
13
13
|
- Pi modules for model, thinking, activity, context, tokens, cost, turn, and extension statuses.
|
|
14
14
|
- Cached Git branch, commit, operation state, line metrics, detailed status, and linked-worktree identity.
|
|
@@ -40,7 +40,7 @@ The only configuration source is:
|
|
|
40
40
|
<getAgentDir()>/pi-starship.toml
|
|
41
41
|
```
|
|
42
42
|
|
|
43
|
-
|
|
43
|
+
When this file is absent, the extension uses its readable Tokyo Night default without creating the file or parent directory. The first successful settings save creates it atomically. Existing malformed documents are never overwritten.
|
|
44
44
|
|
|
45
45
|
The extension does **not** read project overrides, `pi-statusline.json`, `PI_STATUSLINE_PRESET`, or `~/.config/starship.toml`, and does not migrate statusline settings.
|
|
46
46
|
|
package/package.json
CHANGED
package/src/commands.ts
CHANGED
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
atomicSaveConfigDocument,
|
|
12
12
|
BUILT_IN_EXAMPLE,
|
|
13
13
|
type LoadedStarshipConfig,
|
|
14
|
+
removeConfigDocumentIfMatches,
|
|
14
15
|
validateConfigDocument,
|
|
15
16
|
} from "./config.js";
|
|
16
17
|
|
|
@@ -264,7 +265,7 @@ async function reviewAndApply(
|
|
|
264
265
|
try {
|
|
265
266
|
options.apply(saved, ctx);
|
|
266
267
|
} catch (error) {
|
|
267
|
-
const rollbackError = restorePreviousConfiguration(ctx, options, previous);
|
|
268
|
+
const rollbackError = restorePreviousConfiguration(ctx, options, previous, saved);
|
|
268
269
|
ctx.ui.notify(
|
|
269
270
|
rollbackError
|
|
270
271
|
? `Footer settings could not be applied: ${safeText(formatError(error))}. Restoring the previous configuration also failed: ${safeText(formatError(rollbackError))}.`
|
|
@@ -292,12 +293,16 @@ function restorePreviousConfiguration(
|
|
|
292
293
|
ctx: ExtensionCommandContext,
|
|
293
294
|
options: StarshipCommandOptions,
|
|
294
295
|
previous: LoadedStarshipConfig,
|
|
296
|
+
saved: LoadedStarshipConfig,
|
|
295
297
|
): unknown {
|
|
296
298
|
try {
|
|
297
|
-
if (
|
|
298
|
-
throw new Error("The
|
|
299
|
+
if (saved.rawDocument === undefined || saved.fileIdentity === undefined) {
|
|
300
|
+
throw new Error("The saved settings document identity is unavailable");
|
|
301
|
+
}
|
|
302
|
+
removeConfigDocumentIfMatches(options.settingsPath, saved.rawDocument, saved.fileIdentity);
|
|
303
|
+
if (previous.rawDocument !== undefined) {
|
|
304
|
+
(options.restore ?? atomicRestoreConfigDocument)(options.settingsPath, previous.rawDocument);
|
|
299
305
|
}
|
|
300
|
-
(options.restore ?? atomicRestoreConfigDocument)(options.settingsPath, previous.rawDocument);
|
|
301
306
|
options.apply(previous, ctx);
|
|
302
307
|
return undefined;
|
|
303
308
|
} catch (error) {
|
package/src/config.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import {
|
|
3
3
|
existsSync,
|
|
4
|
-
|
|
4
|
+
lstatSync,
|
|
5
5
|
mkdirSync,
|
|
6
6
|
readFileSync,
|
|
7
7
|
renameSync,
|
|
@@ -69,6 +69,7 @@ export interface LoadedStarshipConfig {
|
|
|
69
69
|
source: "built-in" | "user";
|
|
70
70
|
settingsPath: string;
|
|
71
71
|
rawDocument?: string;
|
|
72
|
+
fileIdentity?: { dev: number; ino: number };
|
|
72
73
|
diagnostics: ConfigDiagnostic[];
|
|
73
74
|
}
|
|
74
75
|
|
|
@@ -157,7 +158,7 @@ export function loadStarshipConfig(settingsPath: string): LoadedStarshipConfig {
|
|
|
157
158
|
try {
|
|
158
159
|
rawDocument = readFileSync(settingsPath, "utf8");
|
|
159
160
|
} catch (error) {
|
|
160
|
-
if (!existsSync(settingsPath)) {
|
|
161
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT" && !existsSync(settingsPath)) {
|
|
161
162
|
return {
|
|
162
163
|
config: cloneBuiltInConfig(),
|
|
163
164
|
source: "built-in",
|
|
@@ -194,48 +195,6 @@ export function loadStarshipConfig(settingsPath: string): LoadedStarshipConfig {
|
|
|
194
195
|
};
|
|
195
196
|
}
|
|
196
197
|
|
|
197
|
-
interface InitialFileSystem {
|
|
198
|
-
mkdirSync: typeof mkdirSync;
|
|
199
|
-
writeFileSync: typeof writeFileSync;
|
|
200
|
-
linkSync: typeof linkSync;
|
|
201
|
-
rmSync: typeof rmSync;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
export function loadOrCreateStarshipConfig(
|
|
205
|
-
settingsPath: string,
|
|
206
|
-
overrides: Partial<InitialFileSystem> = {},
|
|
207
|
-
): LoadedStarshipConfig {
|
|
208
|
-
const loaded = loadStarshipConfig(settingsPath);
|
|
209
|
-
if (loaded.source === "user" || loaded.diagnostics.length > 0) return loaded;
|
|
210
|
-
|
|
211
|
-
const fs = { mkdirSync, writeFileSync, linkSync, rmSync, ...overrides };
|
|
212
|
-
const tempPath = join(dirname(settingsPath), `.${CONFIG_FILE_NAME}.${randomUUID()}.tmp`);
|
|
213
|
-
try {
|
|
214
|
-
fs.mkdirSync(dirname(settingsPath), { recursive: true });
|
|
215
|
-
fs.writeFileSync(tempPath, BUILT_IN_EXAMPLE, { encoding: "utf8", flag: "wx" });
|
|
216
|
-
try {
|
|
217
|
-
fs.linkSync(tempPath, settingsPath);
|
|
218
|
-
} catch (error) {
|
|
219
|
-
if (isAlreadyExistsError(error)) return loadStarshipConfig(settingsPath);
|
|
220
|
-
throw error;
|
|
221
|
-
}
|
|
222
|
-
return loadStarshipConfig(settingsPath);
|
|
223
|
-
} catch (error) {
|
|
224
|
-
return {
|
|
225
|
-
...loaded,
|
|
226
|
-
diagnostics: [
|
|
227
|
-
diagnostic("warning", "", `Unable to create default settings: ${formatError(error)}`),
|
|
228
|
-
],
|
|
229
|
-
};
|
|
230
|
-
} finally {
|
|
231
|
-
try {
|
|
232
|
-
fs.rmSync(tempPath, { force: true });
|
|
233
|
-
} catch {
|
|
234
|
-
// Best-effort cleanup must not replace the initialization result.
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
|
|
239
198
|
export function normalizeConfig(value: unknown): {
|
|
240
199
|
config: StarshipConfig;
|
|
241
200
|
diagnostics: ConfigDiagnostic[];
|
|
@@ -472,8 +431,10 @@ export function atomicSaveConfigDocument(
|
|
|
472
431
|
overrides: Partial<AtomicFileSystem> = {},
|
|
473
432
|
): LoadedStarshipConfig {
|
|
474
433
|
const validated = validateConfigDocument(settingsPath, rawDocument);
|
|
475
|
-
|
|
476
|
-
|
|
434
|
+
return {
|
|
435
|
+
...validated,
|
|
436
|
+
fileIdentity: atomicWriteConfigDocument(settingsPath, rawDocument, overrides),
|
|
437
|
+
};
|
|
477
438
|
}
|
|
478
439
|
|
|
479
440
|
export function atomicRestoreConfigDocument(
|
|
@@ -484,17 +445,58 @@ export function atomicRestoreConfigDocument(
|
|
|
484
445
|
atomicWriteConfigDocument(settingsPath, rawDocument, overrides);
|
|
485
446
|
}
|
|
486
447
|
|
|
448
|
+
export function removeConfigDocumentIfMatches(
|
|
449
|
+
settingsPath: string,
|
|
450
|
+
expectedRawDocument: string,
|
|
451
|
+
expectedIdentity: { dev: number; ino: number },
|
|
452
|
+
overrides: Pick<Partial<AtomicFileSystem>, "rmSync"> = {},
|
|
453
|
+
) {
|
|
454
|
+
const quarantinePath = join(
|
|
455
|
+
dirname(settingsPath),
|
|
456
|
+
`.${CONFIG_FILE_NAME}.${randomUUID()}.rollback`,
|
|
457
|
+
);
|
|
458
|
+
const before = lstatSync(settingsPath);
|
|
459
|
+
if (before.dev !== expectedIdentity.dev || before.ino !== expectedIdentity.ino) {
|
|
460
|
+
throw new Error("Starship settings changed concurrently; the newer file was preserved");
|
|
461
|
+
}
|
|
462
|
+
renameSync(settingsPath, quarantinePath);
|
|
463
|
+
const quarantined = lstatSync(quarantinePath);
|
|
464
|
+
const quarantinedSavedFile =
|
|
465
|
+
quarantined.isFile() &&
|
|
466
|
+
!quarantined.isSymbolicLink() &&
|
|
467
|
+
quarantined.dev === expectedIdentity.dev &&
|
|
468
|
+
quarantined.ino === expectedIdentity.ino;
|
|
469
|
+
if (quarantinedSavedFile && readFileSync(quarantinePath, "utf8") === expectedRawDocument) {
|
|
470
|
+
(overrides.rmSync ?? rmSync)(quarantinePath);
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
if (quarantinedSavedFile && !pathEntryExists(settingsPath)) {
|
|
474
|
+
try {
|
|
475
|
+
renameSync(quarantinePath, settingsPath);
|
|
476
|
+
} catch {
|
|
477
|
+
// Keep the quarantine for recovery when its atomic restoration fails.
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
throw new Error("Starship settings changed concurrently; the newer file was preserved");
|
|
481
|
+
}
|
|
482
|
+
|
|
487
483
|
function atomicWriteConfigDocument(
|
|
488
484
|
settingsPath: string,
|
|
489
485
|
rawDocument: string,
|
|
490
486
|
overrides: Partial<AtomicFileSystem>,
|
|
491
|
-
) {
|
|
487
|
+
): { dev: number; ino: number } {
|
|
492
488
|
const fs = { mkdirSync, writeFileSync, renameSync, rmSync, ...overrides };
|
|
489
|
+
const replaceExisting = pathEntryExists(settingsPath);
|
|
493
490
|
fs.mkdirSync(dirname(settingsPath), { recursive: true });
|
|
494
491
|
const tempPath = join(dirname(settingsPath), `.${CONFIG_FILE_NAME}.${randomUUID()}.tmp`);
|
|
495
492
|
try {
|
|
496
493
|
fs.writeFileSync(tempPath, rawDocument, { encoding: "utf8", flag: "wx" });
|
|
494
|
+
const info = lstatSync(tempPath);
|
|
495
|
+
if (!replaceExisting && pathEntryExists(settingsPath)) {
|
|
496
|
+
throw new Error(`${CONFIG_FILE_NAME} was created concurrently; reopen settings and retry.`);
|
|
497
|
+
}
|
|
497
498
|
fs.renameSync(tempPath, settingsPath);
|
|
499
|
+
return { dev: info.dev, ino: info.ino };
|
|
498
500
|
} finally {
|
|
499
501
|
try {
|
|
500
502
|
fs.rmSync(tempPath, { force: true });
|
|
@@ -648,8 +650,14 @@ function diagnostic(
|
|
|
648
650
|
return { severity, path, message };
|
|
649
651
|
}
|
|
650
652
|
|
|
651
|
-
function
|
|
652
|
-
|
|
653
|
+
function pathEntryExists(path: string): boolean {
|
|
654
|
+
try {
|
|
655
|
+
lstatSync(path);
|
|
656
|
+
return true;
|
|
657
|
+
} catch (error) {
|
|
658
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return false;
|
|
659
|
+
throw error;
|
|
660
|
+
}
|
|
653
661
|
}
|
|
654
662
|
|
|
655
663
|
function formatError(error: unknown): string {
|
|
@@ -75,7 +75,7 @@ function packageNameForSource(source: string, baseDirectory: string): string | u
|
|
|
75
75
|
|
|
76
76
|
function npmPackageName(source: string): string {
|
|
77
77
|
const spec = source.slice("npm:".length);
|
|
78
|
-
if (spec.startsWith("@")) return spec.split("@").slice(0, 2).join("@")
|
|
78
|
+
if (spec.startsWith("@")) return spec.split("@").slice(0, 2).join("@");
|
|
79
79
|
return spec.split("@")[0] ?? spec;
|
|
80
80
|
}
|
|
81
81
|
|
|
@@ -179,6 +179,6 @@ export function buildExtensionStatusIconAliases(
|
|
|
179
179
|
|
|
180
180
|
function npmPackageName(source: string): string {
|
|
181
181
|
const spec = source.slice("npm:".length);
|
|
182
|
-
if (spec.startsWith("@")) return spec.split("@").slice(0, 2).join("@")
|
|
182
|
+
if (spec.startsWith("@")) return spec.split("@").slice(0, 2).join("@");
|
|
183
183
|
return spec.split("@")[0] ?? spec;
|
|
184
184
|
}
|
|
@@ -198,14 +198,33 @@ export function parseGitStatusPorcelain(output: string): GitStatusSnapshot {
|
|
|
198
198
|
}
|
|
199
199
|
|
|
200
200
|
export function parseGitDiffShortstat(output: string): GitMetricsSnapshot {
|
|
201
|
-
const added = /(\d+) insertions?\(\+\)/u.exec(output)?.[1];
|
|
202
|
-
const deleted = /(\d+) deletions?\(-\)/u.exec(output)?.[1];
|
|
203
201
|
return {
|
|
204
|
-
added:
|
|
205
|
-
deleted:
|
|
202
|
+
added: parseShortstatMetric(output, "insertion(+)", "insertions(+)") ?? 0,
|
|
203
|
+
deleted: parseShortstatMetric(output, "deletion(-)", "deletions(-)") ?? 0,
|
|
206
204
|
};
|
|
207
205
|
}
|
|
208
206
|
|
|
207
|
+
function parseShortstatMetric(
|
|
208
|
+
output: string,
|
|
209
|
+
singularLabel: string,
|
|
210
|
+
pluralLabel: string,
|
|
211
|
+
): number | undefined {
|
|
212
|
+
for (const field of output.split(",")) {
|
|
213
|
+
const trimmed = field.trim();
|
|
214
|
+
const separator = trimmed.indexOf(" ");
|
|
215
|
+
if (separator <= 0) continue;
|
|
216
|
+
const label = trimmed.slice(separator + 1).trimStart();
|
|
217
|
+
if (label !== singularLabel && label !== pluralLabel) continue;
|
|
218
|
+
const countText = trimmed.slice(0, separator);
|
|
219
|
+
for (const character of countText) {
|
|
220
|
+
if (character < "0" || character > "9") return undefined;
|
|
221
|
+
}
|
|
222
|
+
const count = Number(countText);
|
|
223
|
+
return Number.isSafeInteger(count) ? count : undefined;
|
|
224
|
+
}
|
|
225
|
+
return undefined;
|
|
226
|
+
}
|
|
227
|
+
|
|
209
228
|
export function parseGitState(gitDirectory: string): GitStateSnapshot | undefined {
|
|
210
229
|
const rebaseMerge = join(gitDirectory, "rebase-merge");
|
|
211
230
|
if (existsSync(rebaseMerge))
|
package/src/pi-starship.ts
CHANGED
|
@@ -9,7 +9,6 @@ import { wrapTextWithAnsi } from "@earendil-works/pi-tui";
|
|
|
9
9
|
import { registerStarshipCommand } from "./commands.js";
|
|
10
10
|
import {
|
|
11
11
|
type LoadedStarshipConfig,
|
|
12
|
-
loadOrCreateStarshipConfig,
|
|
13
12
|
loadStarshipConfig,
|
|
14
13
|
type StarshipConfig,
|
|
15
14
|
settingsFilePath,
|
|
@@ -237,7 +236,7 @@ export default function piStarship(pi: ExtensionAPI) {
|
|
|
237
236
|
});
|
|
238
237
|
|
|
239
238
|
pi.on("session_start", (_event, ctx) => {
|
|
240
|
-
loaded =
|
|
239
|
+
loaded = loadStarshipConfig(configPath);
|
|
241
240
|
if (loaded.diagnostics.length > 0 && (ctx.mode === "tui" || ctx.hasUI)) {
|
|
242
241
|
ctx.ui.notify(formatDiagnostics(loaded), "warning");
|
|
243
242
|
}
|