@narumitw/pi-starship 0.51.4 → 0.52.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/src/commands.ts CHANGED
@@ -1,5 +1,15 @@
1
1
  import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
2
2
  import { defineMenu, runMenu } from "@narumitw/pi-tui-kit";
3
+ import {
4
+ type ConfigurationCommandOptions,
5
+ configurationMenuScreen,
6
+ configurationOverviewScreen,
7
+ configurationPresentation,
8
+ effectiveConfigurationScreen,
9
+ reloadConfiguration,
10
+ settingsDocumentScreen,
11
+ type WorkflowOwner,
12
+ } from "./command-configuration.js";
3
13
  import { completeStarshipArguments, STARSHIP_SUBCOMMANDS } from "./command-contract.js";
4
14
  import { formatFooterExplanation } from "./command-inspector.js";
5
15
  import { showPresetPicker } from "./command-preset-picker.js";
@@ -36,26 +46,13 @@ const PREVIEW_ACTIONS = {
36
46
  cancel: "cancel",
37
47
  } as const;
38
48
 
39
- export interface StarshipCommandOptions {
40
- getLoaded(): LoadedStarshipConfig;
49
+ export interface StarshipCommandOptions extends ConfigurationCommandOptions {
41
50
  getInspection?(): StatuslineInspection | undefined;
42
- apply(loaded: LoadedStarshipConfig, ctx: ExtensionCommandContext): void;
43
51
  preview?(loaded: LoadedStarshipConfig | undefined, ctx: ExtensionCommandContext): void;
44
- settingsPath: string;
45
- renderPreview?(
46
- loaded: LoadedStarshipConfig,
47
- width: number,
48
- ctx: ExtensionCommandContext,
49
- ): string[];
50
52
  save?: (settingsPath: string, rawDocument: string) => LoadedStarshipConfig;
51
53
  restore?: (settingsPath: string, rawDocument: string) => void;
52
54
  validate?: (settingsPath: string, rawDocument: string) => LoadedStarshipConfig;
53
- getMenuOwner?(): { signal: AbortSignal; isCurrent(): boolean };
54
- }
55
-
56
- interface WorkflowOwner {
57
- signal: AbortSignal;
58
- isCurrent(): boolean;
55
+ getMenuOwner?(): WorkflowOwner;
59
56
  }
60
57
 
61
58
  type ReviewIntent =
@@ -114,8 +111,16 @@ async function showMainMenu(ctx: ExtensionCommandContext, options: StarshipComma
114
111
  signal: fallbackController.signal,
115
112
  isCurrent: () => !fallbackController.signal.aborted,
116
113
  };
117
- type Screen = "main" | "explain" | "modules" | "configuration" | "help";
118
- type Action = "customize" | "presets" | "restore";
114
+ type Screen =
115
+ | "main"
116
+ | "explain"
117
+ | "modules"
118
+ | "configuration"
119
+ | "configuration-overview"
120
+ | "configuration-effective"
121
+ | "configuration-document"
122
+ | "help";
123
+ type Action = "customize" | "presets" | "configuration-reload" | "restore";
119
124
  const menu = defineMenu<undefined, Screen, Action, ExtensionCommandContext>({
120
125
  start: "main",
121
126
  screens: {
@@ -210,21 +215,12 @@ async function showMainMenu(ctx: ExtensionCommandContext, options: StarshipComma
210
215
  hint: "back",
211
216
  };
212
217
  },
213
- configuration: () => {
214
- const loaded = options.getLoaded();
215
- const presentation = configurationPresentation(loaded);
216
- return {
217
- kind: "detail",
218
- title: "Configuration",
219
- lines: [
220
- `State: ${presentation.state}`,
221
- `Source: ${presentation.source}`,
222
- `Path: ${safeText(options.settingsPath)}`,
223
- ...diagnosticLines(loaded, true),
224
- ],
225
- hint: "back",
226
- };
227
- },
218
+ configuration: () => configurationMenuScreen(options.getLoaded()),
219
+ "configuration-overview": () =>
220
+ configurationOverviewScreen(options.getLoaded(), options.settingsPath),
221
+ "configuration-effective": () => effectiveConfigurationScreen(options.getLoaded()),
222
+ "configuration-document": () =>
223
+ settingsDocumentScreen(options.getLoaded(), options.settingsPath),
228
224
  help: () => ({
229
225
  kind: "detail",
230
226
  title: "pi-starship help",
@@ -233,7 +229,7 @@ async function showMainMenu(ctx: ExtensionCommandContext, options: StarshipComma
233
229
  "Presets live-previews the cursor in the footer; Enter confirms apply and e customizes first.",
234
230
  "Explain footer breaks down the modules currently showing from the existing snapshot.",
235
231
  "Modules searches every supported module and explains its current read-only state.",
236
- "Configuration explains state, source, path, and warnings without changing the footer.",
232
+ "Configuration separates overview, effective TOML, loaded settings text, and safe disk reload.",
237
233
  `Settings: ${safeText(options.settingsPath)}`,
238
234
  "Docs: https://github.com/narumiruna/pi-extensions/tree/main/packages/pi-starship",
239
235
  ],
@@ -249,6 +245,10 @@ async function showMainMenu(ctx: ExtensionCommandContext, options: StarshipComma
249
245
  const result = await choosePreset(ctx, options, owner);
250
246
  return result === "applied" || result === "close" ? { kind: "close" } : { kind: "stay" };
251
247
  },
248
+ "configuration-reload": async () => {
249
+ const result = await reloadConfiguration(ctx, options, owner);
250
+ return result === "close" ? { kind: "close" } : { kind: "stay" };
251
+ },
252
252
  restore: async () => {
253
253
  const presentation = configurationPresentation(options.getLoaded());
254
254
  if (presentation.restoreDisabled) {
@@ -674,70 +674,6 @@ function selectedPreviewAction<Value extends string>(
674
674
  return result?.kind === "selected" ? result.value : null;
675
675
  }
676
676
 
677
- function diagnosticLines(loaded: LoadedStarshipConfig, includeSummary: boolean): string[] {
678
- const diagnostics = loaded.diagnostics
679
- .slice(0, 8)
680
- .map((item) => `${safeText(item.path || "root")}: ${safeText(item.message)}`);
681
- const remaining = loaded.diagnostics.length - diagnostics.length;
682
- return [
683
- ...(includeSummary ? [`Health: ${configurationHealth(loaded)}`] : []),
684
- ...(diagnostics.length > 0 ? diagnostics : ["No configuration warnings."]),
685
- ...(remaining > 0 ? [`${remaining} additional warnings not shown.`] : []),
686
- ];
687
- }
688
-
689
- interface ConfigurationPresentation {
690
- state: string;
691
- source: string;
692
- health: string;
693
- restoreDisabled: boolean;
694
- restoreDescription: string;
695
- }
696
-
697
- function configurationPresentation(loaded: LoadedStarshipConfig): ConfigurationPresentation {
698
- const healthyMissing =
699
- loaded.source === "built-in" &&
700
- loaded.rawDocument === undefined &&
701
- loaded.diagnostics.length === 0;
702
- const savedBuiltIn = loaded.source === "user" && loaded.rawDocument === BUILT_IN_EXAMPLE;
703
- const activePreset = presetForDocument(loaded.rawDocument);
704
- const fallback = loaded.source === "built-in" && loaded.diagnostics.length > 0;
705
- return {
706
- state: healthyMissing
707
- ? "Built-in defaults"
708
- : savedBuiltIn
709
- ? "Saved built-in configuration"
710
- : activePreset
711
- ? `${activePreset.label} preset`
712
- : fallback
713
- ? "Built-in fallback"
714
- : "Custom configuration",
715
- source: healthyMissing
716
- ? "No settings file"
717
- : activePreset
718
- ? "Bundled preset"
719
- : loaded.source === "user"
720
- ? "User file"
721
- : "Built-in fallback",
722
- health: configurationHealth(loaded),
723
- restoreDisabled: healthyMissing || savedBuiltIn,
724
- restoreDescription: healthyMissing
725
- ? "Already using defaults · no file to replace"
726
- : savedBuiltIn
727
- ? "Built-in configuration already saved"
728
- : fallback
729
- ? "Preview before replacing invalid settings"
730
- : "Preview before replacing the document",
731
- };
732
- }
733
-
734
- function configurationHealth(loaded: LoadedStarshipConfig): string {
735
- const errors = loaded.diagnostics.filter((item) => item.severity === "error").length;
736
- if (errors > 0) return `${errors} error${errors === 1 ? "" : "s"}`;
737
- const warnings = loaded.diagnostics.length;
738
- return warnings === 0 ? "Healthy" : `${warnings} warning${warnings === 1 ? "" : "s"}`;
739
- }
740
-
741
677
  function showStatus(ctx: ExtensionCommandContext, options: StarshipCommandOptions) {
742
678
  if (!canNotify(ctx)) return;
743
679
  const loaded = options.getLoaded();
@@ -0,0 +1,65 @@
1
+ import { stringify, type TomlTable, type TomlValue } from "smol-toml";
2
+ import type { StarshipConfig } from "./config.js";
3
+ import { MODULE_DEFINITIONS } from "./modules/catalog.js";
4
+ import type { ModuleOptionValue } from "./modules/types.js";
5
+
6
+ export function projectEffectiveConfig(config: StarshipConfig): TomlTable {
7
+ const projected: TomlTable = {
8
+ format: config.format,
9
+ };
10
+ if (config.palette !== undefined) projected.palette = config.palette;
11
+ projected.palettes = Object.fromEntries(
12
+ sortedEntries(config.palettes).map(([name, colors]) => [name, sortedRecord(colors)]),
13
+ );
14
+ for (const definition of MODULE_DEFINITIONS) {
15
+ const module = config.modules[definition.name];
16
+ const table: TomlTable = {
17
+ format: module.format,
18
+ symbol: module.symbol,
19
+ };
20
+ if (definition.styleDefaults) {
21
+ for (const field of Object.keys(definition.styleDefaults)) {
22
+ table[field] = module.styles[field] ?? "";
23
+ }
24
+ } else if (!definition.displayDefaults) {
25
+ table.style = module.style;
26
+ }
27
+ if (definition.displayDefaults) {
28
+ table.display = module.display.map((entry) => ({ ...entry }));
29
+ }
30
+ table.disabled = module.disabled;
31
+ for (const key of Object.keys(definition.options ?? {})) {
32
+ table[key] = cloneOptionValue(module.options[key]);
33
+ }
34
+ if (definition.name === "extension_status") {
35
+ table.separator = config.extensionStatus.separator;
36
+ table.max_statuses = config.extensionStatus.maxStatuses;
37
+ table.icons = sortedRecord(config.extensionStatus.icons);
38
+ }
39
+ projected[definition.name] = table;
40
+ }
41
+ return projected;
42
+ }
43
+
44
+ export function serializeEffectiveConfig(config: StarshipConfig): string {
45
+ return stringify(projectEffectiveConfig(config));
46
+ }
47
+
48
+ function cloneOptionValue(value: ModuleOptionValue | undefined): TomlValue {
49
+ if (value === undefined) throw new Error("Missing normalized module option");
50
+ if (Array.isArray(value)) return [...(value as readonly string[])];
51
+ if (typeof value === "object") {
52
+ return sortedRecord(value as Readonly<Record<string, string>>);
53
+ }
54
+ return value;
55
+ }
56
+
57
+ function sortedRecord(values: Readonly<Record<string, string>>): Record<string, string> {
58
+ return Object.fromEntries(sortedEntries(values));
59
+ }
60
+
61
+ function sortedEntries<Value>(values: Readonly<Record<string, Value>>): Array<[string, Value]> {
62
+ return Object.entries(values).sort(([left], [right]) =>
63
+ left < right ? -1 : left > right ? 1 : 0,
64
+ );
65
+ }
@@ -85,6 +85,7 @@ interface PiStarshipOptions {
85
85
 
86
86
  export default function piStarship(pi: ExtensionAPI, options: PiStarshipOptions = {}) {
87
87
  let loaded: LoadedStarshipConfig | undefined;
88
+ let loadedRevision = 0;
88
89
  let previewLoaded: LoadedStarshipConfig | undefined;
89
90
  const runtime: RuntimeState = {
90
91
  activeTools: new Map(),
@@ -348,6 +349,7 @@ export default function piStarship(pi: ExtensionAPI, options: PiStarshipOptions
348
349
  const commandOptions: StarshipCommandOptions = {
349
350
  settingsPath: configPath,
350
351
  getLoaded: () => loaded ?? loadStarshipConfig(configPath),
352
+ getLoadedRevision: () => loadedRevision,
351
353
  getInspection: () => {
352
354
  const current = loaded ?? loadStarshipConfig(configPath);
353
355
  return runtime.inspect?.(current);
@@ -365,6 +367,7 @@ export default function piStarship(pi: ExtensionAPI, options: PiStarshipOptions
365
367
  }
366
368
  previewLoaded = undefined;
367
369
  loaded = next;
370
+ loadedRevision += 1;
368
371
  const target = activeTarget;
369
372
  if (target) {
370
373
  restartLocalControllers(target);
@@ -399,6 +402,7 @@ export default function piStarship(pi: ExtensionAPI, options: PiStarshipOptions
399
402
 
400
403
  pi.on("session_start", (_event, ctx) => {
401
404
  loaded = loadStarshipConfig(configPath);
405
+ loadedRevision += 1;
402
406
  if (loaded.diagnostics.length > 0 && (ctx.mode === "tui" || ctx.hasUI)) {
403
407
  ctx.ui.notify(formatDiagnostics(loaded), "warning");
404
408
  }