@narumitw/pi-starship 0.60.1 → 0.63.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.
Files changed (115) hide show
  1. package/README.md +67 -9
  2. package/dist/chunks/chunk-63R6R5DH.ts +679 -0
  3. package/dist/chunks/chunk-63R6R5DH.ts.map +7 -0
  4. package/dist/chunks/{chunk-KZZKGGFW.ts → chunk-A65IOGL6.ts} +858 -1007
  5. package/dist/chunks/chunk-A65IOGL6.ts.map +7 -0
  6. package/dist/chunks/chunk-IYDZ3R2T.ts +69 -0
  7. package/dist/chunks/chunk-IYDZ3R2T.ts.map +7 -0
  8. package/dist/chunks/chunk-KP33I6BR.ts +579 -0
  9. package/dist/chunks/chunk-KP33I6BR.ts.map +7 -0
  10. package/dist/chunks/{chunk-PD457PX5.ts → chunk-S2KBLHE2.ts} +75 -10
  11. package/dist/chunks/chunk-S2KBLHE2.ts.map +7 -0
  12. package/dist/chunks/chunk-XQUIQO2G.ts +20 -0
  13. package/dist/chunks/chunk-XQUIQO2G.ts.map +7 -0
  14. package/dist/chunks/{chunk-ADX4CY27.ts → chunk-XTLQ4WUL.ts} +1 -1
  15. package/dist/chunks/chunk-XTLQ4WUL.ts.map +7 -0
  16. package/dist/chunks/chunk-Y24TJYG6.ts +1586 -0
  17. package/dist/chunks/chunk-Y24TJYG6.ts.map +7 -0
  18. package/dist/chunks/{cloud-EPAEV4ZB.ts → cloud-J2ZOE3HE.ts} +6 -2
  19. package/dist/chunks/cloud-J2ZOE3HE.ts.map +7 -0
  20. package/dist/chunks/command-configuration-menu-YOLN2SPR.ts +147 -0
  21. package/dist/chunks/command-configuration-menu-YOLN2SPR.ts.map +7 -0
  22. package/dist/chunks/command-settings-chat-5AHP3JGV.ts +1124 -0
  23. package/dist/chunks/command-settings-chat-5AHP3JGV.ts.map +7 -0
  24. package/dist/chunks/commands-IOG2PL67.ts +1026 -0
  25. package/dist/chunks/commands-IOG2PL67.ts.map +7 -0
  26. package/dist/chunks/{deployment-7J54EHNR.ts → deployment-NOJ2KCLI.ts} +85 -5
  27. package/dist/chunks/deployment-NOJ2KCLI.ts.map +7 -0
  28. package/dist/chunks/{development-IWJAAAEV.ts → development-T3XXVVDP.ts} +67 -15
  29. package/dist/chunks/development-T3XXVVDP.ts.map +7 -0
  30. package/dist/chunks/directory-OX6BWJZA.ts +31 -0
  31. package/dist/chunks/directory-OX6BWJZA.ts.map +7 -0
  32. package/dist/chunks/{execution-P4WZALW2.ts → execution-R2OHIP2L.ts} +8 -4
  33. package/dist/chunks/execution-R2OHIP2L.ts.map +7 -0
  34. package/dist/chunks/jujutsu-XLDYKSX2.ts +174 -0
  35. package/dist/chunks/jujutsu-XLDYKSX2.ts.map +7 -0
  36. package/dist/chunks/languages-CBQNNYXM.ts +262 -0
  37. package/dist/chunks/languages-CBQNNYXM.ts.map +7 -0
  38. package/dist/chunks/{package-H24VJEX3.ts → package-7KVAK3TC.ts} +6 -2
  39. package/dist/chunks/package-7KVAK3TC.ts.map +7 -0
  40. package/dist/chunks/system-574MHB4R.ts +165 -0
  41. package/dist/chunks/system-574MHB4R.ts.map +7 -0
  42. package/dist/index.ts +670 -206
  43. package/dist/index.ts.map +4 -4
  44. package/package.json +5 -1
  45. package/skills/configure-pi-starship/SKILL.md +17 -1
  46. package/skills/configure-pi-starship/references/configuration.md +119 -32
  47. package/skills/configure-pi-starship/references/module-catalog.md +278 -25
  48. package/skills/configure-pi-starship/references/modules.md +213 -36
  49. package/skills/configure-pi-starship/references/runtime-and-security.md +88 -28
  50. package/src/command-commit.ts +41 -7
  51. package/src/command-configuration-menu.ts +155 -0
  52. package/src/command-configuration.ts +15 -94
  53. package/src/command-preview.ts +3 -3
  54. package/src/command-settings-chat.ts +168 -0
  55. package/src/commands.ts +76 -124
  56. package/src/config/module-options.ts +112 -0
  57. package/src/config.ts +23 -59
  58. package/src/effective-config.ts +5 -4
  59. package/src/format/bounded-regex.ts +66 -0
  60. package/src/format/version.ts +24 -0
  61. package/src/modules/catalog.ts +10 -0
  62. package/src/modules/cloud.ts +1 -0
  63. package/src/modules/deployment.ts +8 -0
  64. package/src/modules/development.ts +20 -2
  65. package/src/modules/directory.ts +171 -51
  66. package/src/modules/execution.ts +2 -0
  67. package/src/modules/git/branch.ts +19 -4
  68. package/src/modules/git/commit.ts +9 -2
  69. package/src/modules/git/metrics.ts +9 -4
  70. package/src/modules/git/runtime.ts +122 -33
  71. package/src/modules/git/state.ts +23 -2
  72. package/src/modules/git/status-templates.ts +58 -0
  73. package/src/modules/git/status.ts +96 -30
  74. package/src/modules/jujutsu.ts +225 -0
  75. package/src/modules/languages.ts +26 -2
  76. package/src/modules/package.ts +1 -0
  77. package/src/modules/render.ts +42 -16
  78. package/src/modules/system.ts +54 -0
  79. package/src/modules/time-format.ts +188 -0
  80. package/src/modules/time.ts +11 -5
  81. package/src/modules/types.ts +42 -4
  82. package/src/pi-starship.ts +198 -88
  83. package/src/profile-store.ts +5 -0
  84. package/src/runtime/cloud.ts +3 -0
  85. package/src/runtime/command.ts +1 -1
  86. package/src/runtime/deployment.ts +120 -4
  87. package/src/runtime/development.ts +86 -13
  88. package/src/runtime/directory.ts +33 -0
  89. package/src/runtime/execution.ts +12 -2
  90. package/src/runtime/helpers.ts +57 -8
  91. package/src/runtime/jujutsu.ts +225 -0
  92. package/src/runtime/languages.ts +106 -19
  93. package/src/runtime/package.ts +3 -0
  94. package/src/runtime/system.ts +202 -0
  95. package/src/runtime/types.ts +3 -0
  96. package/src/runtime/version-requirements.ts +30 -0
  97. package/src/runtime/workspace.ts +12 -1
  98. package/src/settings-agent.ts +80 -0
  99. package/src/settings-chat-transcript.ts +192 -0
  100. package/src/settings-chat-ui.ts +526 -0
  101. package/src/settings-draft.ts +260 -0
  102. package/src/settings-snapshots.ts +41 -0
  103. package/dist/chunks/chunk-ADX4CY27.ts.map +0 -7
  104. package/dist/chunks/chunk-KZZKGGFW.ts.map +0 -7
  105. package/dist/chunks/chunk-PD457PX5.ts.map +0 -7
  106. package/dist/chunks/cloud-EPAEV4ZB.ts.map +0 -7
  107. package/dist/chunks/commands-C2VJ6ZBN.ts +0 -3374
  108. package/dist/chunks/commands-C2VJ6ZBN.ts.map +0 -7
  109. package/dist/chunks/deployment-7J54EHNR.ts.map +0 -7
  110. package/dist/chunks/development-IWJAAAEV.ts.map +0 -7
  111. package/dist/chunks/execution-P4WZALW2.ts.map +0 -7
  112. package/dist/chunks/languages-JO6R2PHL.ts +0 -168
  113. package/dist/chunks/languages-JO6R2PHL.ts.map +0 -7
  114. package/dist/chunks/package-H24VJEX3.ts.map +0 -7
  115. package/src/command-inspector.ts +0 -41
@@ -1,6 +1,9 @@
1
1
  import { existsSync, readFileSync } from "node:fs";
2
2
  import { basename, isAbsolute, join, resolve } from "node:path";
3
3
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
4
+ import { execWorkspaceCommand } from "../../runtime/command.js";
5
+ import { MAX_COMMAND_OUTPUT_BYTES } from "../../runtime/helpers.js";
6
+ import type { WorkspaceExec } from "../../runtime/types.js";
4
7
  import type {
5
8
  GitBranchSnapshot,
6
9
  GitCommitSnapshot,
@@ -16,6 +19,11 @@ const GIT_TIMEOUT_MS = 3_000;
16
19
  export interface ReadGitSnapshotOptions {
17
20
  includeMetrics: boolean;
18
21
  includeTag: boolean;
22
+ statusIgnoreSubmodules?: boolean;
23
+ metricsIgnoreSubmodules?: boolean;
24
+ tagMaxCandidates?: number;
25
+ inspectBare?: boolean;
26
+ exec?: WorkspaceExec;
19
27
  signal?: AbortSignal;
20
28
  }
21
29
 
@@ -24,30 +32,36 @@ export async function readGitSnapshot(
24
32
  cwd: string,
25
33
  options: ReadGitSnapshotOptions,
26
34
  ): Promise<GitSnapshot | undefined> {
27
- const statusPromise = pi.exec(
28
- "git",
29
- ["--no-optional-locks", "status", "--porcelain=v2", "--branch", "--show-stash", "--untracked-files=normal"],
30
- { cwd, signal: options.signal, timeout: GIT_TIMEOUT_MS },
31
- );
32
- const metadataPromise = pi.exec(
33
- "git",
34
- ["rev-parse", "--path-format=absolute", "--show-toplevel", "--git-common-dir", "--git-dir"],
35
- { cwd, signal: options.signal, timeout: GIT_TIMEOUT_MS },
36
- );
37
- const metricsPromise = options.includeMetrics
38
- ? pi.exec("git", ["--no-optional-locks", "diff", "--shortstat", "HEAD", "--"], {
39
- cwd,
40
- signal: options.signal,
41
- timeout: GIT_TIMEOUT_MS,
42
- })
43
- : Promise.resolve(undefined);
44
- const tagPromise = options.includeTag
45
- ? pi.exec("git", ["describe", "--tags", "--exact-match", "HEAD"], {
46
- cwd,
47
- signal: options.signal,
48
- timeout: GIT_TIMEOUT_MS,
49
- })
50
- : Promise.resolve(undefined);
35
+ if (options.signal?.aborted) return undefined;
36
+ const run = (args: string[]) => runGit(pi, cwd, args, options);
37
+ const statusArgs = [
38
+ "--no-optional-locks",
39
+ "status",
40
+ "--porcelain=v2",
41
+ "--branch",
42
+ "--show-stash",
43
+ "--untracked-files=normal",
44
+ ];
45
+ // Upstream's status control ignores dirt inside submodules, not changed gitlinks.
46
+ if (options.statusIgnoreSubmodules) statusArgs.push("--ignore-submodules=dirty");
47
+ const statusPromise = run(statusArgs);
48
+ const metadataPromise = run([
49
+ "rev-parse",
50
+ "--path-format=absolute",
51
+ "--show-toplevel",
52
+ "--git-common-dir",
53
+ "--git-dir",
54
+ ]);
55
+ const metricsArgs = ["--no-optional-locks", "diff", "--shortstat"];
56
+ if (options.metricsIgnoreSubmodules) metricsArgs.push("--ignore-submodules=all", "--no-ext-diff", "--no-textconv");
57
+ metricsArgs.push("HEAD", "--");
58
+ const metricsPromise = options.includeMetrics ? run(metricsArgs) : Promise.resolve(undefined);
59
+ const candidates = options.tagMaxCandidates;
60
+ const tagArgs =
61
+ Number.isSafeInteger(candidates) && (candidates ?? 0) > 0 && (candidates ?? 0) <= 100
62
+ ? ["describe", "--tags", "--first-parent", "--abbrev=0", `--candidates=${candidates}`, "HEAD"]
63
+ : ["describe", "--tags", "--exact-match", "HEAD"];
64
+ const tagPromise = options.includeTag ? run(tagArgs) : Promise.resolve(undefined);
51
65
 
52
66
  const [statusResult, metadataResult, metricsResult, tagResult] = await Promise.all([
53
67
  statusPromise,
@@ -55,7 +69,10 @@ export async function readGitSnapshot(
55
69
  metricsPromise,
56
70
  tagPromise,
57
71
  ]);
58
- if (statusResult.code !== 0 || statusResult.killed) return undefined;
72
+ if (options.signal?.aborted || statusResult.killed) return undefined;
73
+ if (statusResult.code !== 0) {
74
+ return options.inspectBare ? readBareSnapshot(pi, cwd, options, tagResult) : undefined;
75
+ }
59
76
 
60
77
  const parsed = parseGitStatusPorcelainV2(statusResult.stdout);
61
78
  const metadata =
@@ -76,21 +93,88 @@ export async function readGitSnapshot(
76
93
  };
77
94
  }
78
95
 
96
+ type GitCommandResult = Awaited<ReturnType<ExtensionAPI["exec"]>>;
97
+
98
+ async function runGit(
99
+ pi: ExtensionAPI,
100
+ cwd: string,
101
+ args: string[],
102
+ options: Pick<ReadGitSnapshotOptions, "signal" | "exec">,
103
+ ): Promise<GitCommandResult> {
104
+ const failed = { stdout: "", stderr: "", code: 1, killed: options.signal?.aborted ?? false };
105
+ if (options.signal?.aborted) return failed;
106
+ try {
107
+ const result = options.exec
108
+ ? await options.exec("git", args, {
109
+ cwd,
110
+ timeout: GIT_TIMEOUT_MS,
111
+ maxOutputBytes: MAX_COMMAND_OUTPUT_BYTES,
112
+ signal: options.signal,
113
+ environment: process.env,
114
+ })
115
+ : await pi.exec("git", args, { cwd, signal: options.signal, timeout: GIT_TIMEOUT_MS });
116
+ if (options.signal?.aborted) return { ...failed, killed: true };
117
+ if (Buffer.byteLength(result.stdout) + Buffer.byteLength(result.stderr) > MAX_COMMAND_OUTPUT_BYTES) {
118
+ return { ...failed, killed: true };
119
+ }
120
+ return result;
121
+ } catch {
122
+ return failed;
123
+ }
124
+ }
125
+
126
+ async function readBareSnapshot(
127
+ pi: ExtensionAPI,
128
+ cwd: string,
129
+ options: ReadGitSnapshotOptions,
130
+ tagResult: GitCommandResult | undefined,
131
+ ): Promise<GitSnapshot | undefined> {
132
+ const boundedOptions = { ...options, exec: options.exec ?? execWorkspaceCommand };
133
+ const run = (args: string[]) => runGit(pi, cwd, args, boundedOptions);
134
+ const bare = await run(["rev-parse", "--is-bare-repository"]);
135
+ if (options.signal?.aborted || bare.killed || bare.code !== 0 || bare.stdout.trim() !== "true") return undefined;
136
+ const [head, hash] = await Promise.all([
137
+ run(["symbolic-ref", "--short", "HEAD"]),
138
+ run(["rev-parse", "--verify", "HEAD"]),
139
+ ]);
140
+ if (options.signal?.aborted || head.killed || hash.killed) return undefined;
141
+ const name = head.code === 0 ? head.stdout.trim() : "";
142
+ const commitHash = hash.code === 0 ? hash.stdout.trim() : "";
143
+ const validHash = /^[a-fA-F0-9]{4,64}$/u.test(commitHash);
144
+ if (!name && !validHash) return undefined;
145
+ const detached = name === "";
146
+ const reference = `refs/heads/${name}`;
147
+ const upstreamResult = name
148
+ ? await run(["for-each-ref", "--count=1", "--format=%(refname)%00%(upstream:short)", reference])
149
+ : undefined;
150
+ if (options.signal?.aborted) return undefined;
151
+ const upstreamFields =
152
+ upstreamResult?.code === 0 && !upstreamResult.killed ? upstreamResult.stdout.trimEnd().split("\0") : [];
153
+ const remote = upstreamFields[0] === reference ? splitUpstream(upstreamFields[1]) : {};
154
+ return {
155
+ branch: { name: name || "HEAD", detached, bare: true, ...remote },
156
+ commit: validHash ? withTag({ hash: commitHash, detached }, tagResult) : undefined,
157
+ status: emptyGitStatus(),
158
+ };
159
+ }
160
+
79
161
  export async function readGitStatus(pi: ExtensionAPI, cwd: string): Promise<GitStatusSnapshot | undefined> {
80
- const result = await pi.exec(
81
- "git",
162
+ const result = await runGit(
163
+ pi,
164
+ cwd,
82
165
  ["--no-optional-locks", "status", "--porcelain=v2", "--branch", "--show-stash", "--untracked-files=normal"],
83
- { cwd, timeout: GIT_TIMEOUT_MS },
166
+ {},
84
167
  );
85
168
  if (result.code !== 0 || result.killed) return undefined;
86
169
  return parseGitStatusPorcelainV2(result.stdout).status;
87
170
  }
88
171
 
89
172
  export async function readGitWorktree(pi: ExtensionAPI, cwd: string): Promise<GitWorktreeSnapshot | undefined> {
90
- const result = await pi.exec(
91
- "git",
173
+ const result = await runGit(
174
+ pi,
175
+ cwd,
92
176
  ["rev-parse", "--path-format=absolute", "--show-toplevel", "--git-common-dir", "--git-dir"],
93
- { cwd, timeout: GIT_TIMEOUT_MS },
177
+ {},
94
178
  );
95
179
  if (result.code !== 0 || result.killed) return undefined;
96
180
  return parseGitRepositoryMetadata(result.stdout)?.worktree;
@@ -125,8 +209,13 @@ export function parseGitStatusPorcelainV2(output: string): ParsedGitStatus {
125
209
  }
126
210
  if (line.startsWith("# branch.ab ")) {
127
211
  const match = /^# branch\.ab \+(\d+) -(\d+)$/u.exec(line);
128
- status.ahead = match?.[1] ? Number(match[1]) : 0;
129
- status.behind = match?.[2] ? Number(match[2]) : 0;
212
+ const ahead = Number(match?.[1]);
213
+ const behind = Number(match?.[2]);
214
+ const valid = Number.isSafeInteger(ahead) && Number.isSafeInteger(behind);
215
+ status.ahead = valid ? ahead : 0;
216
+ status.behind = valid ? behind : 0;
217
+ if (valid && ahead === 0 && behind === 0) status.upToDate = true;
218
+ else delete status.upToDate;
130
219
  continue;
131
220
  }
132
221
  if (line.startsWith("# stash ")) {
@@ -1,5 +1,15 @@
1
1
  import { defineModule } from "../types.js";
2
2
 
3
+ const STATE_OPTIONS: Readonly<Record<string, string>> = {
4
+ REBASING: "rebase",
5
+ MERGING: "merge",
6
+ REVERTING: "revert",
7
+ "CHERRY-PICKING": "cherry_pick",
8
+ BISECTING: "bisect",
9
+ AM: "am",
10
+ "AM/REBASE": "am_or_rebase",
11
+ };
12
+
3
13
  export const gitStateModule = defineModule({
4
14
  name: "git_state",
5
15
  variables: ["symbol", "state", "progress_current", "progress_total"],
@@ -9,11 +19,22 @@ export const gitStateModule = defineModule({
9
19
  style: "bold yellow",
10
20
  disabled: false,
11
21
  },
12
- values: ({ runtime }) => {
22
+ options: {
23
+ rebase: { kind: "string", default: "REBASING" },
24
+ merge: { kind: "string", default: "MERGING" },
25
+ revert: { kind: "string", default: "REVERTING" },
26
+ cherry_pick: { kind: "string", default: "CHERRY-PICKING" },
27
+ bisect: { kind: "string", default: "BISECTING" },
28
+ am: { kind: "string", default: "AM" },
29
+ am_or_rebase: { kind: "string", default: "AM/REBASE" },
30
+ },
31
+ values: ({ runtime, options }) => {
13
32
  const state = runtime.gitState;
14
33
  if (!state) return undefined;
34
+ const key = STATE_OPTIONS[state.state];
35
+ const label = typeof key === "string" ? options[key] : undefined;
15
36
  return {
16
- state: state.state,
37
+ state: typeof label === "string" ? label : state.state,
17
38
  progress_current: state.progressCurrent?.toString() ?? "",
18
39
  progress_total: state.progressTotal?.toString() ?? "",
19
40
  };
@@ -0,0 +1,58 @@
1
+ import { sanitizeTerminalText } from "@narumitw/pi-tui-kit/terminal-text";
2
+ import { type FormatNode, formatVariables, parseFormat, renderFormat, styleVariables } from "../../format/formatter.js";
3
+ import { type ColorPalette, isFillChunk, type StyledChunk } from "../../format/style.js";
4
+
5
+ const MAX_TEMPLATE_LENGTH = 4_096;
6
+ const MAX_TEMPLATE_DEPTH = 16;
7
+
8
+ /** Status templates use the normal formatter, but cannot resolve other module data. */
9
+ export function renderStatusTemplate(
10
+ template: string,
11
+ variables: Readonly<Record<string, string>>,
12
+ palette: ColorPalette,
13
+ ): StyledChunk[] {
14
+ const nodes = parseStatusTemplate(template, Object.keys(variables));
15
+ return nodes
16
+ ? renderFormat(nodes, { variables, palette }).filter(
17
+ (chunk): chunk is StyledChunk => !isFillChunk(chunk) && chunk.text.length > 0,
18
+ )
19
+ : [];
20
+ }
21
+
22
+ export function validStatusTemplate(template: string, variables: readonly string[]): boolean {
23
+ return parseStatusTemplate(template, variables) !== undefined;
24
+ }
25
+
26
+ function parseStatusTemplate(template: string, variables: readonly string[]): FormatNode[] | undefined {
27
+ if (template.length > MAX_TEMPLATE_LENGTH) return undefined;
28
+ const safe = sanitizeTerminalText(template);
29
+ let depth = 0;
30
+ for (let index = 0; index < safe.length; index += 1) {
31
+ const character = safe[index];
32
+ if (character === "\\") {
33
+ index += 1;
34
+ } else if (character === "[" || character === "(") {
35
+ if (++depth > MAX_TEMPLATE_DEPTH) return undefined;
36
+ } else if (character === "]" || character === ")") {
37
+ depth -= 1;
38
+ }
39
+ }
40
+ try {
41
+ const nodes = parseFormat(safe);
42
+ if ([...formatVariables(nodes)].some((variable) => !variables.includes(variable))) return undefined;
43
+ if (styleVariables(nodes).size > 0) return undefined;
44
+ return nodes;
45
+ } catch {
46
+ return undefined;
47
+ }
48
+ }
49
+
50
+ export function joinStatusTemplates(values: readonly (readonly StyledChunk[])[]): StyledChunk[] {
51
+ const result: StyledChunk[] = [];
52
+ for (const value of values) {
53
+ if (value.length === 0) continue;
54
+ if (result.length > 0) result.push({ text: " " });
55
+ result.push(...value);
56
+ }
57
+ return result;
58
+ }
@@ -1,5 +1,33 @@
1
+ import type { ColorPalette, StyledChunk } from "../../format/style.js";
1
2
  import { formatCount } from "../helpers.js";
2
- import { defineModule } from "../types.js";
3
+ import { defineModule, type ModuleOptionValue } from "../types.js";
4
+ import { joinStatusTemplates, renderStatusTemplate } from "./status-templates.js";
5
+
6
+ const COUNT_VARIABLES = ["count"] as const;
7
+ const DEFAULT_TEMPLATES = {
8
+ ahead: "⇡$count",
9
+ behind: "⇣$count",
10
+ up_to_date: "",
11
+ diverged: "⇕⇡$ahead_count⇣$behind_count",
12
+ conflicted: "=$count",
13
+ stashed: "\\$$count",
14
+ deleted: "✘$count",
15
+ renamed: "»$count",
16
+ modified: "!$count",
17
+ typechanged: "T$count",
18
+ staged: "+$count",
19
+ untracked: "?$count",
20
+ worktree_added: "A$count",
21
+ worktree_deleted: "D$count",
22
+ worktree_modified: "M$count",
23
+ worktree_typechanged: "T$count",
24
+ index_added: "A$count",
25
+ index_deleted: "D$count",
26
+ index_modified: "M$count",
27
+ index_typechanged: "T$count",
28
+ } as const;
29
+
30
+ type StatusTemplate = keyof typeof DEFAULT_TEMPLATES;
3
31
 
4
32
  export const gitStatusModule = defineModule({
5
33
  name: "git_status",
@@ -34,37 +62,59 @@ export const gitStatusModule = defineModule({
34
62
  style: "red bold",
35
63
  disabled: false,
36
64
  },
37
- values: ({ runtime }) => {
65
+ options: {
66
+ ...Object.fromEntries(
67
+ Object.entries(DEFAULT_TEMPLATES).map(([name, template]) => [
68
+ name,
69
+ {
70
+ kind: "string" as const,
71
+ default: template,
72
+ templateVariables:
73
+ name === "diverged" ? ["ahead_count", "behind_count"] : name === "up_to_date" ? [] : COUNT_VARIABLES,
74
+ },
75
+ ]),
76
+ ),
77
+ ignore_submodules: { kind: "boolean", default: false },
78
+ },
79
+ values: ({ runtime, options, palette, requestedVariables }) => {
38
80
  if (!runtime.gitBranch || !runtime.gitStatus) return undefined;
39
81
  const status = runtime.gitStatus;
40
- const ahead = count("⇡", status.ahead);
41
- const behind = count("⇣", status.behind);
82
+ const hasAhead = Number.isSafeInteger(status.ahead) && status.ahead > 0;
83
+ const hasBehind = Number.isSafeInteger(status.behind) && status.behind > 0;
84
+ const template = (name: StatusTemplate, variables: Readonly<Record<string, string>>) =>
85
+ renderStatusTemplate(templateOption(options, name), variables, palette);
86
+ const count = (name: StatusTemplate, value: number) => countTemplate(options, name, value, palette);
87
+ const ahead = count("ahead", status.ahead);
88
+ const behind = count("behind", status.behind);
42
89
  const diverged =
43
- status.ahead > 0 && status.behind > 0 ? `⇕⇡${formatCount(status.ahead)}⇣${formatCount(status.behind)}` : "";
90
+ hasAhead && hasBehind
91
+ ? template("diverged", { ahead_count: formatCount(status.ahead), behind_count: formatCount(status.behind) })
92
+ : [];
93
+ const upToDate = status.upToDate && status.ahead === 0 && status.behind === 0 ? template("up_to_date", {}) : [];
44
94
  const values = {
45
95
  ahead,
46
96
  behind,
47
- up_to_date: "",
97
+ up_to_date: upToDate,
48
98
  diverged,
49
- ahead_behind: diverged || ahead || behind,
50
- conflicted: count("=", status.conflicted),
51
- stashed: count("$", status.stashed),
52
- deleted: count("✘", status.deleted),
53
- renamed: count("»", status.renamed),
54
- modified: count("!", status.modified),
55
- typechanged: count("T", status.typechanged),
56
- staged: count("+", status.staged),
57
- untracked: count("?", status.untracked),
58
- worktree_added: count("A", status.worktreeAdded),
59
- worktree_deleted: count("D", status.worktreeDeleted),
60
- worktree_modified: count("M", status.worktreeModified),
61
- worktree_typechanged: count("T", status.worktreeTypechanged),
62
- index_added: count("A", status.indexAdded),
63
- index_deleted: count("D", status.indexDeleted),
64
- index_modified: count("M", status.indexModified),
65
- index_typechanged: count("T", status.indexTypechanged),
99
+ ahead_behind: hasAhead && hasBehind ? diverged : hasAhead ? ahead : hasBehind ? behind : upToDate,
100
+ conflicted: count("conflicted", status.conflicted),
101
+ stashed: count("stashed", status.stashed),
102
+ deleted: count("deleted", status.deleted),
103
+ renamed: count("renamed", status.renamed),
104
+ modified: count("modified", status.modified),
105
+ typechanged: count("typechanged", status.typechanged),
106
+ staged: count("staged", status.staged),
107
+ untracked: count("untracked", status.untracked),
108
+ worktree_added: count("worktree_added", status.worktreeAdded),
109
+ worktree_deleted: count("worktree_deleted", status.worktreeDeleted),
110
+ worktree_modified: count("worktree_modified", status.worktreeModified),
111
+ worktree_typechanged: count("worktree_typechanged", status.worktreeTypechanged),
112
+ index_added: count("index_added", status.indexAdded),
113
+ index_deleted: count("index_deleted", status.indexDeleted),
114
+ index_modified: count("index_modified", status.indexModified),
115
+ index_typechanged: count("index_typechanged", status.indexTypechanged),
66
116
  };
67
- const allStatus = [
117
+ const allStatus = joinStatusTemplates([
68
118
  values.conflicted,
69
119
  values.stashed,
70
120
  values.deleted,
@@ -73,13 +123,29 @@ export const gitStatusModule = defineModule({
73
123
  values.typechanged,
74
124
  values.staged,
75
125
  values.untracked,
76
- ]
77
- .filter(Boolean)
78
- .join(" ");
79
- return allStatus || values.ahead_behind ? { ...values, all_status: allStatus } : undefined;
126
+ ]);
127
+ const available = { ...values, all_status: allStatus };
128
+ const requested = [...(requestedVariables ?? [])].filter((name) => Object.hasOwn(available, name));
129
+ const visible =
130
+ requested.length > 0
131
+ ? requested.map((name) => available[name as keyof typeof available])
132
+ : [allStatus, values.ahead_behind];
133
+ return visible.some((value) => value.length > 0) ? available : undefined;
80
134
  },
81
135
  });
82
136
 
83
- function count(symbol: string, value: number): string {
84
- return value > 0 ? `${symbol}${formatCount(value)}` : "";
137
+ function templateOption(options: Readonly<Record<string, ModuleOptionValue>>, name: StatusTemplate): string {
138
+ const option = options[name];
139
+ return typeof option === "string" ? option : DEFAULT_TEMPLATES[name];
140
+ }
141
+
142
+ function countTemplate(
143
+ options: Readonly<Record<string, ModuleOptionValue>>,
144
+ name: StatusTemplate,
145
+ value: number,
146
+ palette: ColorPalette,
147
+ ): StyledChunk[] {
148
+ return Number.isSafeInteger(value) && value > 0
149
+ ? renderStatusTemplate(templateOption(options, name), { count: formatCount(value) }, palette)
150
+ : [];
85
151
  }