@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.
Files changed (84) hide show
  1. package/README.md +2 -4
  2. package/package.json +4 -8
  3. package/src/command-contract.ts +13 -0
  4. package/src/command-inspector.ts +198 -0
  5. package/src/command-preview.ts +206 -0
  6. package/src/commands.ts +575 -0
  7. package/src/config.ts +794 -0
  8. package/src/format/formatter.ts +275 -0
  9. package/src/format/style.ts +278 -0
  10. package/src/index.ts +1 -1
  11. package/src/modules/activity.ts +38 -0
  12. package/src/modules/brand.ts +13 -0
  13. package/src/modules/cache.ts +23 -0
  14. package/src/modules/catalog.ts +124 -0
  15. package/src/modules/cloud.ts +74 -0
  16. package/src/modules/context.ts +33 -0
  17. package/src/modules/cost.ts +31 -0
  18. package/src/modules/deployment.ts +61 -0
  19. package/src/modules/development.ts +112 -0
  20. package/src/modules/directory.ts +151 -0
  21. package/src/modules/display.ts +14 -0
  22. package/src/modules/execution.ts +84 -0
  23. package/src/modules/extension-status.ts +93 -0
  24. package/src/modules/fill.ts +14 -0
  25. package/src/modules/git/branch.ts +31 -0
  26. package/src/modules/git/commit.ts +29 -0
  27. package/src/modules/git/metrics.ts +26 -0
  28. package/src/modules/git/runtime.ts +415 -0
  29. package/src/modules/git/state.ts +21 -0
  30. package/src/modules/git/status.ts +87 -0
  31. package/src/modules/git/worktree.ts +16 -0
  32. package/src/modules/github-pr.ts +24 -0
  33. package/src/modules/helpers.ts +5 -0
  34. package/src/modules/index.ts +31 -0
  35. package/src/modules/inspection.ts +138 -0
  36. package/src/modules/languages.ts +87 -0
  37. package/src/modules/model.ts +85 -0
  38. package/src/modules/package.ts +17 -0
  39. package/src/modules/provider.ts +13 -0
  40. package/src/modules/render.ts +166 -0
  41. package/src/modules/terminal.ts +60 -0
  42. package/src/modules/thinking.ts +13 -0
  43. package/src/modules/time.ts +20 -0
  44. package/src/modules/tokens.ts +18 -0
  45. package/src/modules/truncation.ts +37 -0
  46. package/src/modules/turn.ts +13 -0
  47. package/src/modules/types.ts +189 -0
  48. package/src/modules/workspace-helpers.ts +10 -0
  49. package/src/pi-starship.ts +609 -0
  50. package/src/runtime/cloud.ts +166 -0
  51. package/src/runtime/command.ts +123 -0
  52. package/src/runtime/deployment.ts +201 -0
  53. package/src/runtime/development.ts +153 -0
  54. package/src/runtime/execution.ts +151 -0
  55. package/src/runtime/github-pr.ts +332 -0
  56. package/src/runtime/helpers.ts +274 -0
  57. package/src/runtime/languages.ts +205 -0
  58. package/src/runtime/package.ts +112 -0
  59. package/src/runtime/refresh-controller.ts +107 -0
  60. package/src/runtime/types.ts +63 -0
  61. package/src/runtime/workspace.ts +147 -0
  62. package/src/usage.ts +53 -0
  63. package/dist/chunks/chunk-CKNSOLSW.ts +0 -10
  64. package/dist/chunks/chunk-CKNSOLSW.ts.map +0 -7
  65. package/dist/chunks/chunk-G2GG2V6Q.ts +0 -218
  66. package/dist/chunks/chunk-G2GG2V6Q.ts.map +0 -7
  67. package/dist/chunks/chunk-Q6DLIM7H.ts +0 -2492
  68. package/dist/chunks/chunk-Q6DLIM7H.ts.map +0 -7
  69. package/dist/chunks/cloud-JBSZWTB6.ts +0 -142
  70. package/dist/chunks/cloud-JBSZWTB6.ts.map +0 -7
  71. package/dist/chunks/commands-BSQZ2R4M.ts +0 -757
  72. package/dist/chunks/commands-BSQZ2R4M.ts.map +0 -7
  73. package/dist/chunks/deployment-ZNYM4B6W.ts +0 -172
  74. package/dist/chunks/deployment-ZNYM4B6W.ts.map +0 -7
  75. package/dist/chunks/development-TNWZ5SRC.ts +0 -132
  76. package/dist/chunks/development-TNWZ5SRC.ts.map +0 -7
  77. package/dist/chunks/execution-QHAP4U33.ts +0 -132
  78. package/dist/chunks/execution-QHAP4U33.ts.map +0 -7
  79. package/dist/chunks/languages-DPWDCH6Q.ts +0 -184
  80. package/dist/chunks/languages-DPWDCH6Q.ts.map +0 -7
  81. package/dist/chunks/package-MXN2YFLX.ts +0 -104
  82. package/dist/chunks/package-MXN2YFLX.ts.map +0 -7
  83. package/dist/pi-starship.ts +0 -1350
  84. package/dist/pi-starship.ts.map +0 -7
@@ -0,0 +1,87 @@
1
+ import { formatCount } from "../helpers.js";
2
+ import { defineModule } from "../types.js";
3
+
4
+ export const gitStatusModule = defineModule({
5
+ name: "git_status",
6
+ variables: [
7
+ "symbol",
8
+ "all_status",
9
+ "ahead_behind",
10
+ "ahead",
11
+ "behind",
12
+ "up_to_date",
13
+ "diverged",
14
+ "conflicted",
15
+ "stashed",
16
+ "deleted",
17
+ "renamed",
18
+ "modified",
19
+ "typechanged",
20
+ "staged",
21
+ "untracked",
22
+ "worktree_added",
23
+ "worktree_deleted",
24
+ "worktree_modified",
25
+ "worktree_typechanged",
26
+ "index_added",
27
+ "index_deleted",
28
+ "index_modified",
29
+ "index_typechanged",
30
+ ],
31
+ defaults: {
32
+ format: "[$all_status( $ahead_behind) ]($style)",
33
+ symbol: "",
34
+ style: "red bold",
35
+ disabled: false,
36
+ },
37
+ values: ({ runtime }) => {
38
+ if (!runtime.gitBranch || !runtime.gitStatus) return undefined;
39
+ const status = runtime.gitStatus;
40
+ const ahead = count("⇡", status.ahead);
41
+ const behind = count("⇣", status.behind);
42
+ const diverged =
43
+ status.ahead > 0 && status.behind > 0
44
+ ? `⇕⇡${formatCount(status.ahead)}⇣${formatCount(status.behind)}`
45
+ : "";
46
+ const values = {
47
+ ahead,
48
+ behind,
49
+ up_to_date: "",
50
+ diverged,
51
+ ahead_behind: diverged || ahead || behind,
52
+ conflicted: count("=", status.conflicted),
53
+ stashed: count("$", status.stashed),
54
+ deleted: count("✘", status.deleted),
55
+ renamed: count("»", status.renamed),
56
+ modified: count("!", status.modified),
57
+ typechanged: count("T", status.typechanged),
58
+ staged: count("+", status.staged),
59
+ untracked: count("?", status.untracked),
60
+ worktree_added: count("A", status.worktreeAdded),
61
+ worktree_deleted: count("D", status.worktreeDeleted),
62
+ worktree_modified: count("M", status.worktreeModified),
63
+ worktree_typechanged: count("T", status.worktreeTypechanged),
64
+ index_added: count("A", status.indexAdded),
65
+ index_deleted: count("D", status.indexDeleted),
66
+ index_modified: count("M", status.indexModified),
67
+ index_typechanged: count("T", status.indexTypechanged),
68
+ };
69
+ const allStatus = [
70
+ values.conflicted,
71
+ values.stashed,
72
+ values.deleted,
73
+ values.renamed,
74
+ values.modified,
75
+ values.typechanged,
76
+ values.staged,
77
+ values.untracked,
78
+ ]
79
+ .filter(Boolean)
80
+ .join(" ");
81
+ return allStatus || values.ahead_behind ? { ...values, all_status: allStatus } : undefined;
82
+ },
83
+ });
84
+
85
+ function count(symbol: string, value: number): string {
86
+ return value > 0 ? `${symbol}${formatCount(value)}` : "";
87
+ }
@@ -0,0 +1,16 @@
1
+ import { defineModule } from "../types.js";
2
+
3
+ export const gitWorktreeModule = defineModule({
4
+ name: "git_worktree",
5
+ variables: ["symbol", "name", "path"],
6
+ defaults: {
7
+ format: "[ $symbol $name ]($style)",
8
+ symbol: "🌳",
9
+ style: "cyan bold",
10
+ disabled: false,
11
+ },
12
+ values: ({ runtime }) =>
13
+ runtime.gitWorktree
14
+ ? { name: runtime.gitWorktree.name, path: runtime.gitWorktree.path }
15
+ : undefined,
16
+ });
@@ -0,0 +1,24 @@
1
+ import { defineModule } from "./types.js";
2
+
3
+ export const githubPrModule = defineModule({
4
+ name: "github_pr",
5
+ variables: ["symbol", "number", "link", "state", "checks", "review", "status"],
6
+ defaults: {
7
+ format: "[ $symbol$link( · $status) ]($style)",
8
+ symbol: "PR ",
9
+ style: "bold blue",
10
+ disabled: false,
11
+ },
12
+ values: ({ runtime }) => {
13
+ const snapshot = runtime.githubPr;
14
+ if (!snapshot) return undefined;
15
+ return {
16
+ number: snapshot.number,
17
+ link: snapshot.link,
18
+ state: snapshot.state,
19
+ checks: snapshot.checks,
20
+ review: snapshot.review,
21
+ status: snapshot.status,
22
+ };
23
+ },
24
+ });
@@ -0,0 +1,5 @@
1
+ export function formatCount(value: number): string {
2
+ if (value < 1000) return `${value}`;
3
+ if (value < 1_000_000) return `${(value / 1000).toFixed(value < 10_000 ? 1 : 0)}k`;
4
+ return `${(value / 1_000_000).toFixed(1)}m`;
5
+ }
@@ -0,0 +1,31 @@
1
+ export {
2
+ type CatalogModuleDefinition,
3
+ MODULE_DEFINITIONS,
4
+ MODULE_NAMES,
5
+ type ModuleName,
6
+ } from "./catalog.js";
7
+ export { formatExtensionStatus } from "./extension-status.js";
8
+ export { formatCount } from "./helpers.js";
9
+ export {
10
+ inspectStatuslineModules,
11
+ inspectUnavailableModules,
12
+ type ModuleInspection,
13
+ type ModuleInspectionState,
14
+ type StatuslineInspection,
15
+ } from "./inspection.js";
16
+ export { shortenModel } from "./model.js";
17
+ export { reachableModuleRequirements, renderStatusline } from "./render.js";
18
+ export type {
19
+ GitBranchSnapshot,
20
+ GitCommitSnapshot,
21
+ GithubPrSnapshot,
22
+ GithubPrState,
23
+ GitMetricsSnapshot,
24
+ GitSnapshot,
25
+ GitStateSnapshot,
26
+ GitStatusSnapshot,
27
+ GitWorktreeSnapshot,
28
+ RenderedStatusline,
29
+ StarshipRuntimeSnapshot,
30
+ WorkspaceSnapshot,
31
+ } from "./types.js";
@@ -0,0 +1,138 @@
1
+ import { stripVTControlCharacters } from "node:util";
2
+ import type { StarshipConfig } from "../config.js";
3
+ import { formatVariables } from "../format/formatter.js";
4
+ import { MODULE_DEFINITIONS, type ModuleName } from "./catalog.js";
5
+ import { reachableModuleRequirements, renderStatusline } from "./render.js";
6
+ import type { StarshipRuntimeSnapshot } from "./types.js";
7
+
8
+ export type ModuleInspectionState =
9
+ | "Showing"
10
+ | "Empty"
11
+ | "Disabled"
12
+ | "Not in format"
13
+ | "Unavailable";
14
+
15
+ export interface ModuleInspection {
16
+ name: ModuleName;
17
+ description: string;
18
+ state: ModuleInspectionState;
19
+ preview: string;
20
+ variables: readonly string[];
21
+ styleFields: readonly string[];
22
+ displayRules: readonly string[];
23
+ rootReferenced: boolean;
24
+ reachable: boolean;
25
+ reason: string;
26
+ }
27
+
28
+ export interface StatuslineInspection {
29
+ modules: readonly ModuleInspection[];
30
+ showing: readonly ModuleInspection[];
31
+ }
32
+
33
+ export function inspectStatuslineModules(
34
+ config: StarshipConfig,
35
+ runtime: StarshipRuntimeSnapshot,
36
+ width = 80,
37
+ ): StatuslineInspection {
38
+ const rendered = renderStatusline(config, runtime, width);
39
+ const requirements = reachableModuleRequirements(config);
40
+ const rootVariables = formatVariables(config.formatAst);
41
+ const includeAll = rootVariables.has("all");
42
+ const modules = MODULE_DEFINITIONS.map((definition): ModuleInspection => {
43
+ const module = config.modules[definition.name];
44
+ const reachable = requirements.has(definition.name);
45
+ const preview = plainPreview(
46
+ rendered.modules[definition.name].map((chunk) => chunk.text).join(""),
47
+ );
48
+ const state = module.disabled
49
+ ? "Disabled"
50
+ : !reachable
51
+ ? "Not in format"
52
+ : preview.length > 0
53
+ ? "Showing"
54
+ : "Empty";
55
+ return moduleInspection(
56
+ config,
57
+ definition,
58
+ state,
59
+ preview,
60
+ includeAll || rootVariables.has(definition.name),
61
+ reachable,
62
+ );
63
+ });
64
+ return {
65
+ modules,
66
+ showing: modules.filter((module) => module.state === "Showing"),
67
+ };
68
+ }
69
+
70
+ export function inspectUnavailableModules(config: StarshipConfig): StatuslineInspection {
71
+ const requirements = reachableModuleRequirements(config);
72
+ const rootVariables = formatVariables(config.formatAst);
73
+ const includeAll = rootVariables.has("all");
74
+ return {
75
+ modules: MODULE_DEFINITIONS.map((definition) => {
76
+ const module = config.modules[definition.name];
77
+ const reachable = requirements.has(definition.name);
78
+ const state = module.disabled ? "Disabled" : reachable ? "Unavailable" : "Not in format";
79
+ return moduleInspection(
80
+ config,
81
+ definition,
82
+ state,
83
+ "",
84
+ includeAll || rootVariables.has(definition.name),
85
+ reachable,
86
+ );
87
+ }),
88
+ showing: [],
89
+ };
90
+ }
91
+
92
+ function moduleInspection(
93
+ config: StarshipConfig,
94
+ definition: (typeof MODULE_DEFINITIONS)[number],
95
+ state: ModuleInspectionState,
96
+ preview: string,
97
+ rootReferenced: boolean,
98
+ reachable: boolean,
99
+ ): ModuleInspection {
100
+ const module = config.modules[definition.name];
101
+ return {
102
+ name: definition.name,
103
+ description: definition.description,
104
+ state,
105
+ preview,
106
+ variables: [...definition.variables],
107
+ styleFields: ["style", ...Object.keys(module.styles)],
108
+ displayRules: module.display.map(
109
+ (rule) => `${rule.threshold}: ${rule.hidden ? "hidden" : rule.style || "unstyled"}`,
110
+ ),
111
+ rootReferenced,
112
+ reachable,
113
+ reason: inspectionReason(state),
114
+ };
115
+ }
116
+
117
+ function inspectionReason(state: ModuleInspectionState): string {
118
+ switch (state) {
119
+ case "Showing":
120
+ return "Rendered in the current footer.";
121
+ case "Empty":
122
+ return "Referenced by the root format, but the current snapshot produced no output.";
123
+ case "Disabled":
124
+ return "Disabled by this module's configuration.";
125
+ case "Not in format":
126
+ return "Not referenced by the root format or $all.";
127
+ case "Unavailable":
128
+ return "Current footer inspection is unavailable; no collector failure was inferred.";
129
+ }
130
+ }
131
+
132
+ function plainPreview(value: string): string {
133
+ return Array.from(stripVTControlCharacters(value), (character) => {
134
+ const codePoint = character.codePointAt(0) ?? 0;
135
+ if (character === "\n") return character;
136
+ return codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f) ? " " : character;
137
+ }).join("");
138
+ }
@@ -0,0 +1,87 @@
1
+ import { defineModule, type ModuleDefinition } from "./types.js";
2
+ import { workspaceModuleValues } from "./workspace-helpers.js";
3
+
4
+ const detectionOptions = {
5
+ version_format: { kind: "string", default: "v$raw" },
6
+ detect_files: { kind: "string-array", default: [], allowNegative: true },
7
+ detect_extensions: { kind: "string-array", default: [], allowNegative: true },
8
+ detect_folders: { kind: "string-array", default: [], allowNegative: true },
9
+ } as const;
10
+
11
+ function languageModule<const Name extends string>(definition: {
12
+ name: Name;
13
+ variables: readonly string[];
14
+ format: string;
15
+ symbol: string;
16
+ style: string;
17
+ }): ModuleDefinition<Name> {
18
+ return defineModule({
19
+ name: definition.name,
20
+ variables: ["symbol", ...definition.variables],
21
+ defaults: {
22
+ format: definition.format,
23
+ symbol: definition.symbol,
24
+ style: definition.style,
25
+ disabled: false,
26
+ },
27
+ options: detectionOptions,
28
+ values: (context) => workspaceModuleValues(definition.name, context),
29
+ });
30
+ }
31
+
32
+ export const nodejsModule = languageModule({
33
+ name: "nodejs",
34
+ variables: ["version", "engines_version"],
35
+ format: "via [$symbol($version )]($style)",
36
+ symbol: " ",
37
+ style: "bold green",
38
+ });
39
+
40
+ export const pythonModule = languageModule({
41
+ name: "python",
42
+ variables: ["version", "virtualenv", "pyenv_prefix"],
43
+ format: "via [$symbol$pyenv_prefix($version )(\\($virtualenv\\) )]($style)",
44
+ symbol: " ",
45
+ style: "yellow bold",
46
+ });
47
+
48
+ export const rustModule = languageModule({
49
+ name: "rust",
50
+ variables: ["version", "numver", "toolchain"],
51
+ format: "via [$symbol($version )]($style)",
52
+ symbol: " ",
53
+ style: "bold red",
54
+ });
55
+
56
+ export const golangModule = languageModule({
57
+ name: "golang",
58
+ variables: ["version", "mod_version"],
59
+ format: "via [$symbol($version )]($style)",
60
+ symbol: " ",
61
+ style: "bold cyan",
62
+ });
63
+
64
+ export const bunModule = languageModule({
65
+ name: "bun",
66
+ variables: ["version"],
67
+ format: "via [$symbol($version )]($style)",
68
+ symbol: "🍞 ",
69
+ style: "bold red",
70
+ });
71
+
72
+ export const denoModule = languageModule({
73
+ name: "deno",
74
+ variables: ["version"],
75
+ format: "via [$symbol($version )]($style)",
76
+ symbol: "🦕 ",
77
+ style: "green bold",
78
+ });
79
+
80
+ export const languageModules = [
81
+ nodejsModule,
82
+ pythonModule,
83
+ rustModule,
84
+ golangModule,
85
+ bunModule,
86
+ denoModule,
87
+ ] as const;
@@ -0,0 +1,85 @@
1
+ import { sanitizeTerminalText } from "./terminal.js";
2
+ import { defineModule } from "./types.js";
3
+
4
+ const TRUNCATION_DIRECTIONS = ["start", "middle", "end"] as const;
5
+ type TruncationDirection = (typeof TRUNCATION_DIRECTIONS)[number];
6
+ const graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
7
+
8
+ export const modelModule = defineModule({
9
+ name: "model",
10
+ variables: ["symbol", "model"],
11
+ defaults: {
12
+ format: "[$symbol $model ]($style)",
13
+ symbol: "🤖",
14
+ style: "bold blue",
15
+ disabled: false,
16
+ },
17
+ options: {
18
+ truncation_length: { kind: "integer", default: 0, minimum: 0, maximum: 1000 },
19
+ truncation_symbol: { kind: "string", default: "…" },
20
+ truncation_direction: {
21
+ kind: "string-enum",
22
+ default: "end",
23
+ values: TRUNCATION_DIRECTIONS,
24
+ },
25
+ model_aliases: { kind: "string-map", default: {} },
26
+ },
27
+ values: ({ runtime, options }) => {
28
+ if (!runtime.model) return undefined;
29
+ const length = typeof options.truncation_length === "number" ? options.truncation_length : 0;
30
+ const symbol = typeof options.truncation_symbol === "string" ? options.truncation_symbol : "…";
31
+ const direction = isTruncationDirection(options.truncation_direction)
32
+ ? options.truncation_direction
33
+ : "end";
34
+ const aliases = options.model_aliases;
35
+ const aliasMap =
36
+ aliases && typeof aliases === "object" && !Array.isArray(aliases)
37
+ ? (aliases as Readonly<Record<string, string>>)
38
+ : undefined;
39
+ const alias =
40
+ aliasMap && Object.hasOwn(aliasMap, runtime.model.id)
41
+ ? aliasMap[runtime.model.id]
42
+ : undefined;
43
+ return {
44
+ model: truncateModel(alias ?? shortenModel(runtime.model.id), length, symbol, direction),
45
+ };
46
+ },
47
+ });
48
+
49
+ export function truncateModel(
50
+ model: string,
51
+ length: number,
52
+ symbol: string,
53
+ direction: TruncationDirection,
54
+ ): string {
55
+ const safeModel = sanitizeTerminalText(model);
56
+ if (length === 0) return safeModel;
57
+ const graphemes = [...graphemeSegmenter.segment(safeModel)].map(({ segment }) => segment);
58
+ if (graphemes.length <= length) return safeModel;
59
+ const safeSymbol = sanitizeTerminalText(symbol);
60
+
61
+ switch (direction) {
62
+ case "start":
63
+ return `${safeSymbol}${graphemes.slice(-length).join("")}`;
64
+ case "middle": {
65
+ const headLength = Math.ceil(length / 2);
66
+ const tailLength = Math.floor(length / 2);
67
+ const tail = tailLength > 0 ? graphemes.slice(-tailLength).join("") : "";
68
+ return `${graphemes.slice(0, headLength).join("")}${safeSymbol}${tail}`;
69
+ }
70
+ case "end":
71
+ return `${graphemes.slice(0, length).join("")}${safeSymbol}`;
72
+ }
73
+ }
74
+
75
+ function isTruncationDirection(value: unknown): value is TruncationDirection {
76
+ return TRUNCATION_DIRECTIONS.includes(value as TruncationDirection);
77
+ }
78
+
79
+ export function shortenModel(model: string): string {
80
+ return model
81
+ .replace(/^claude-/u, "")
82
+ .replace(/^gpt-/u, "gpt ")
83
+ .replace(/-20\d{6}$/u, "")
84
+ .replace(/-latest$/u, "");
85
+ }
@@ -0,0 +1,17 @@
1
+ import { defineModule } from "./types.js";
2
+ import { workspaceModuleValues } from "./workspace-helpers.js";
3
+
4
+ export const packageModule = defineModule({
5
+ name: "package",
6
+ variables: ["symbol", "version", "source"],
7
+ defaults: {
8
+ format: "via [$symbol$version]($style) ",
9
+ symbol: "📦 ",
10
+ style: "bold 208",
11
+ disabled: false,
12
+ },
13
+ options: {
14
+ version_format: { kind: "string", default: "v$raw" },
15
+ },
16
+ values: (context) => workspaceModuleValues("package", context),
17
+ });
@@ -0,0 +1,13 @@
1
+ import { defineModule } from "./types.js";
2
+
3
+ export const providerModule = defineModule({
4
+ name: "provider",
5
+ variables: ["symbol", "provider"],
6
+ defaults: {
7
+ format: "[$symbol $provider ]($style)",
8
+ symbol: "🔌",
9
+ style: "bold blue",
10
+ disabled: false,
11
+ },
12
+ values: ({ runtime }) => (runtime.model ? { provider: runtime.model.provider } : undefined),
13
+ });
@@ -0,0 +1,166 @@
1
+ import { visibleWidth } from "@earendil-works/pi-tui";
2
+ import { activePalette, type ModuleConfig, type StarshipConfig } from "../config.js";
3
+ import { type FormatValue, formatVariables, renderFormat } from "../format/formatter.js";
4
+ import {
5
+ isFillChunk,
6
+ type LayoutChunk,
7
+ renderChunksToAnsi,
8
+ type StyledChunk,
9
+ } from "../format/style.js";
10
+ import { MODULE_DEFINITIONS, MODULE_NAMES, type ModuleName } from "./catalog.js";
11
+ import type { ModuleValueContext, RenderedStatusline, StarshipRuntimeSnapshot } from "./types.js";
12
+
13
+ export function renderStatusline(
14
+ config: StarshipConfig,
15
+ runtime: StarshipRuntimeSnapshot,
16
+ width = 80,
17
+ ): RenderedStatusline<ModuleName> {
18
+ const palette = activePalette(config);
19
+ const modules = {} as Record<ModuleName, StyledChunk[]>;
20
+ const layoutModules = {} as Record<ModuleName, LayoutChunk[]>;
21
+ for (const name of MODULE_NAMES) {
22
+ modules[name] = [];
23
+ layoutModules[name] = [];
24
+ }
25
+ for (const definition of MODULE_DEFINITIONS) {
26
+ const name = definition.name;
27
+ const module = config.modules[name];
28
+ const values = definition.values(valueContext(config, name, runtime));
29
+ if (!values) continue;
30
+ const styleVariables = definition.resolveStyleVariables
31
+ ? definition.resolveStyleVariables({
32
+ runtime,
33
+ values,
34
+ style: module.style,
35
+ styles: module.styles,
36
+ display: module.display,
37
+ })
38
+ : { style: module.style };
39
+ if (!styleVariables) continue;
40
+ const contentValues = Object.fromEntries(
41
+ definition.variables.flatMap((variable) =>
42
+ variable !== "symbol" && Object.hasOwn(values, variable)
43
+ ? [[variable, values[variable]]]
44
+ : [],
45
+ ),
46
+ );
47
+ const rendered = renderModule(module, contentValues, styleVariables, palette);
48
+ modules[name] = rendered;
49
+ layoutModules[name] =
50
+ definition.layout === "fill" && !module.disabled
51
+ ? [{ type: "fill", pattern: rendered }]
52
+ : rendered;
53
+ }
54
+
55
+ const explicitModules = formatVariables(config.formatAst);
56
+ const all = MODULE_NAMES.flatMap((name) =>
57
+ explicitModules.has(name) || config.modules[name].disabled ? [] : layoutModules[name],
58
+ );
59
+ const rootVariables: Record<string, FormatValue> = { all };
60
+ for (const name of MODULE_NAMES) rootVariables[name] = layoutModules[name];
61
+ const layout = renderFormat(config.formatAst, { variables: rootVariables, palette });
62
+ const chunks = resolveFillLayout(layout, width);
63
+ return {
64
+ ansi: renderChunksToAnsi(chunks),
65
+ chunks,
66
+ modules,
67
+ };
68
+ }
69
+
70
+ function valueContext(
71
+ config: StarshipConfig,
72
+ name: ModuleName,
73
+ runtime: StarshipRuntimeSnapshot,
74
+ ): ModuleValueContext {
75
+ return {
76
+ runtime,
77
+ symbol: config.modules[name].symbol,
78
+ options: config.modules[name].options,
79
+ extensionStatus: config.extensionStatus,
80
+ };
81
+ }
82
+
83
+ function renderModule(
84
+ module: ModuleConfig,
85
+ values: Readonly<Record<string, FormatValue>>,
86
+ styleVariables: Readonly<Record<string, string>>,
87
+ palette: Readonly<Record<string, string>>,
88
+ ): StyledChunk[] {
89
+ if (module.disabled) return [];
90
+ return renderFormat(module.formatAst, {
91
+ variables: { symbol: module.symbol, ...values },
92
+ styleVariables,
93
+ palette,
94
+ }).filter((chunk): chunk is StyledChunk => !isFillChunk(chunk));
95
+ }
96
+
97
+ export function reachableModuleRequirements(
98
+ config: StarshipConfig,
99
+ ): ReadonlyMap<ModuleName, ReadonlySet<string>> {
100
+ const rootVariables = formatVariables(config.formatAst);
101
+ const includeAll = rootVariables.has("all");
102
+ const requirements = new Map<ModuleName, ReadonlySet<string>>();
103
+ for (const definition of MODULE_DEFINITIONS) {
104
+ if (config.modules[definition.name].disabled) continue;
105
+ if (!includeAll && !rootVariables.has(definition.name)) continue;
106
+ requirements.set(definition.name, formatVariables(config.modules[definition.name].formatAst));
107
+ }
108
+ return requirements;
109
+ }
110
+
111
+ function resolveFillLayout(layout: readonly LayoutChunk[], width: number): StyledChunk[] {
112
+ const lines = splitLogicalLines(layout);
113
+ const resolved: StyledChunk[] = [];
114
+ for (const [lineIndex, line] of lines.entries()) {
115
+ const fills = line.filter(isFillChunk);
116
+ const fixedWidth = line.reduce(
117
+ (total, chunk) =>
118
+ total + (isFillChunk(chunk) ? 0 : visibleWidth(renderChunksToAnsi([chunk]))),
119
+ 0,
120
+ );
121
+ const remaining = Math.max(0, width - fixedWidth);
122
+ const base = fills.length > 0 ? Math.floor(remaining / fills.length) : 0;
123
+ let remainder = fills.length > 0 ? remaining % fills.length : 0;
124
+ for (const chunk of line) {
125
+ if (!isFillChunk(chunk)) {
126
+ resolved.push(chunk);
127
+ continue;
128
+ }
129
+ const allocation = base + (remainder > 0 ? 1 : 0);
130
+ if (remainder > 0) remainder -= 1;
131
+ resolved.push(...expandFill(chunk.pattern, allocation));
132
+ }
133
+ if (lineIndex < lines.length - 1) resolved.push({ text: "\n" });
134
+ }
135
+ return resolved;
136
+ }
137
+
138
+ function splitLogicalLines(layout: readonly LayoutChunk[]): LayoutChunk[][] {
139
+ const lines: LayoutChunk[][] = [[]];
140
+ for (const chunk of layout) {
141
+ if (isFillChunk(chunk)) {
142
+ lines.at(-1)?.push(chunk);
143
+ continue;
144
+ }
145
+ const parts = chunk.text.split("\n");
146
+ for (const [index, text] of parts.entries()) {
147
+ if (text) lines.at(-1)?.push({ ...chunk, text });
148
+ if (index < parts.length - 1) lines.push([]);
149
+ }
150
+ }
151
+ return lines;
152
+ }
153
+
154
+ function expandFill(pattern: readonly StyledChunk[], width: number): StyledChunk[] {
155
+ if (width <= 0) return [];
156
+ const patternWidth = visibleWidth(renderChunksToAnsi(pattern));
157
+ if (patternWidth <= 0) return [{ text: " ".repeat(width), style: pattern[0]?.style }];
158
+ const repetitions = Math.floor(width / patternWidth);
159
+ const result: StyledChunk[] = [];
160
+ for (let index = 0; index < repetitions; index += 1) {
161
+ result.push(...pattern.map((chunk) => ({ ...chunk })));
162
+ }
163
+ const remainder = width - repetitions * patternWidth;
164
+ if (remainder > 0) result.push({ text: " ".repeat(remainder), style: pattern.at(-1)?.style });
165
+ return result;
166
+ }