@narumitw/pi-starship 0.28.0 → 0.29.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.
@@ -1,16 +1,27 @@
1
+ import { visibleWidth } from "@earendil-works/pi-tui";
1
2
  import { activePalette, type ModuleConfig, type StarshipConfig } from "../config.js";
2
3
  import { type FormatValue, formatVariables, renderFormat } from "../format/formatter.js";
3
- import { renderChunksToAnsi, type StyledChunk } from "../format/style.js";
4
+ import {
5
+ isFillChunk,
6
+ type LayoutChunk,
7
+ renderChunksToAnsi,
8
+ type StyledChunk,
9
+ } from "../format/style.js";
4
10
  import { MODULE_DEFINITIONS, MODULE_NAMES, type ModuleName } from "./catalog.js";
5
11
  import type { ModuleValueContext, RenderedStatusline, StarshipRuntimeSnapshot } from "./types.js";
6
12
 
7
13
  export function renderStatusline(
8
14
  config: StarshipConfig,
9
15
  runtime: StarshipRuntimeSnapshot,
16
+ width = 80,
10
17
  ): RenderedStatusline<ModuleName> {
11
18
  const palette = activePalette(config);
12
19
  const modules = {} as Record<ModuleName, StyledChunk[]>;
13
- for (const name of MODULE_NAMES) modules[name] = [];
20
+ const layoutModules = {} as Record<ModuleName, LayoutChunk[]>;
21
+ for (const name of MODULE_NAMES) {
22
+ modules[name] = [];
23
+ layoutModules[name] = [];
24
+ }
14
25
  const consumedExtensionStatusKeys = new Set<string>();
15
26
 
16
27
  for (const definition of MODULE_DEFINITIONS) {
@@ -19,12 +30,17 @@ export function renderStatusline(
19
30
  valueContext(config, name, runtime, consumedExtensionStatusKeys),
20
31
  );
21
32
  if (!values) continue;
22
- modules[name] = renderModule(config.modules[name], values, palette);
33
+ const rendered = renderModule(config.modules[name], values, palette);
34
+ modules[name] = rendered;
35
+ layoutModules[name] =
36
+ definition.layout === "fill" && !config.modules[name].disabled
37
+ ? [{ type: "fill", pattern: rendered }]
38
+ : rendered;
23
39
  if (
24
40
  name === "git_branch" &&
25
41
  values.pr &&
26
42
  formatVariables(config.modules.git_branch.formatAst).has("pr") &&
27
- modules.git_branch.some((chunk) => chunk.text.includes(values.pr ?? ""))
43
+ rendered.some((chunk) => chunk.text.includes(values.pr ?? ""))
28
44
  ) {
29
45
  consumedExtensionStatusKeys.add("github-pr");
30
46
  }
@@ -32,11 +48,12 @@ export function renderStatusline(
32
48
 
33
49
  const explicitModules = formatVariables(config.formatAst);
34
50
  const all = MODULE_NAMES.flatMap((name) =>
35
- explicitModules.has(name) || config.modules[name].disabled ? [] : modules[name],
51
+ explicitModules.has(name) || config.modules[name].disabled ? [] : layoutModules[name],
36
52
  );
37
53
  const rootVariables: Record<string, FormatValue> = { all };
38
- for (const name of MODULE_NAMES) rootVariables[name] = modules[name];
39
- const chunks = renderFormat(config.formatAst, { variables: rootVariables, palette });
54
+ for (const name of MODULE_NAMES) rootVariables[name] = layoutModules[name];
55
+ const layout = renderFormat(config.formatAst, { variables: rootVariables, palette });
56
+ const chunks = resolveFillLayout(layout, width);
40
57
  return {
41
58
  ansi: renderChunksToAnsi(chunks),
42
59
  chunks,
@@ -66,8 +83,79 @@ function renderModule(
66
83
  ): StyledChunk[] {
67
84
  if (module.disabled) return [];
68
85
  return renderFormat(module.formatAst, {
69
- variables: { ...values, symbol: module.symbol },
86
+ variables: { symbol: module.symbol, ...values },
70
87
  styleVariables: { style: module.style },
71
88
  palette,
72
- });
89
+ }).filter((chunk): chunk is StyledChunk => !isFillChunk(chunk));
90
+ }
91
+
92
+ export function reachableModuleRequirements(
93
+ config: StarshipConfig,
94
+ ): ReadonlyMap<ModuleName, ReadonlySet<string>> {
95
+ const rootVariables = formatVariables(config.formatAst);
96
+ const includeAll = rootVariables.has("all");
97
+ const requirements = new Map<ModuleName, ReadonlySet<string>>();
98
+ for (const definition of MODULE_DEFINITIONS) {
99
+ if (config.modules[definition.name].disabled) continue;
100
+ if (!includeAll && !rootVariables.has(definition.name)) continue;
101
+ requirements.set(definition.name, formatVariables(config.modules[definition.name].formatAst));
102
+ }
103
+ return requirements;
104
+ }
105
+
106
+ function resolveFillLayout(layout: readonly LayoutChunk[], width: number): StyledChunk[] {
107
+ const lines = splitLogicalLines(layout);
108
+ const resolved: StyledChunk[] = [];
109
+ for (const [lineIndex, line] of lines.entries()) {
110
+ const fills = line.filter(isFillChunk);
111
+ const fixedWidth = line.reduce(
112
+ (total, chunk) =>
113
+ total + (isFillChunk(chunk) ? 0 : visibleWidth(renderChunksToAnsi([chunk]))),
114
+ 0,
115
+ );
116
+ const remaining = Math.max(0, width - fixedWidth);
117
+ const base = fills.length > 0 ? Math.floor(remaining / fills.length) : 0;
118
+ let remainder = fills.length > 0 ? remaining % fills.length : 0;
119
+ for (const chunk of line) {
120
+ if (!isFillChunk(chunk)) {
121
+ resolved.push(chunk);
122
+ continue;
123
+ }
124
+ const allocation = base + (remainder > 0 ? 1 : 0);
125
+ if (remainder > 0) remainder -= 1;
126
+ resolved.push(...expandFill(chunk.pattern, allocation));
127
+ }
128
+ if (lineIndex < lines.length - 1) resolved.push({ text: "\n" });
129
+ }
130
+ return resolved;
131
+ }
132
+
133
+ function splitLogicalLines(layout: readonly LayoutChunk[]): LayoutChunk[][] {
134
+ const lines: LayoutChunk[][] = [[]];
135
+ for (const chunk of layout) {
136
+ if (isFillChunk(chunk)) {
137
+ lines.at(-1)?.push(chunk);
138
+ continue;
139
+ }
140
+ const parts = chunk.text.split("\n");
141
+ for (const [index, text] of parts.entries()) {
142
+ if (text) lines.at(-1)?.push({ ...chunk, text });
143
+ if (index < parts.length - 1) lines.push([]);
144
+ }
145
+ }
146
+ return lines;
147
+ }
148
+
149
+ function expandFill(pattern: readonly StyledChunk[], width: number): StyledChunk[] {
150
+ if (width <= 0) return [];
151
+ const patternWidth = visibleWidth(renderChunksToAnsi(pattern));
152
+ if (patternWidth <= 0) return [{ text: " ".repeat(width), style: pattern[0]?.style }];
153
+ const repetitions = Math.floor(width / patternWidth);
154
+ const result: StyledChunk[] = [];
155
+ for (let index = 0; index < repetitions; index += 1) {
156
+ result.push(...pattern.map((chunk) => ({ ...chunk })));
157
+ }
158
+ const remainder = width - repetitions * patternWidth;
159
+ if (remainder > 0) result.push({ text: " ".repeat(remainder), style: pattern.at(-1)?.style });
160
+ return result;
73
161
  }
@@ -1,12 +1,48 @@
1
1
  import type { StyledChunk } from "../format/style.js";
2
2
 
3
+ export interface GitBranchSnapshot {
4
+ name: string;
5
+ remoteName?: string;
6
+ remoteBranch?: string;
7
+ detached: boolean;
8
+ }
9
+
10
+ export interface GitCommitSnapshot {
11
+ hash: string;
12
+ tag?: string;
13
+ detached: boolean;
14
+ }
15
+
16
+ export interface GitStateSnapshot {
17
+ state: string;
18
+ progressCurrent?: number;
19
+ progressTotal?: number;
20
+ }
21
+
22
+ export interface GitMetricsSnapshot {
23
+ added: number;
24
+ deleted: number;
25
+ }
26
+
3
27
  export interface GitStatusSnapshot {
4
28
  ahead: number;
5
29
  behind: number;
6
- staged: number;
30
+ stashed: number;
31
+ conflicted: number;
32
+ deleted: number;
33
+ renamed: number;
7
34
  modified: number;
35
+ staged: number;
36
+ typechanged: number;
8
37
  untracked: number;
9
- conflicted: number;
38
+ worktreeAdded: number;
39
+ worktreeDeleted: number;
40
+ worktreeModified: number;
41
+ worktreeTypechanged: number;
42
+ indexAdded: number;
43
+ indexDeleted: number;
44
+ indexModified: number;
45
+ indexTypechanged: number;
10
46
  }
11
47
 
12
48
  export interface GitWorktreeSnapshot {
@@ -14,8 +50,21 @@ export interface GitWorktreeSnapshot {
14
50
  path: string;
15
51
  }
16
52
 
53
+ export interface GitSnapshot {
54
+ branch?: GitBranchSnapshot;
55
+ commit?: GitCommitSnapshot;
56
+ state?: GitStateSnapshot;
57
+ metrics?: GitMetricsSnapshot;
58
+ status: GitStatusSnapshot;
59
+ worktree?: GitWorktreeSnapshot;
60
+ }
61
+
17
62
  export type ExtensionStatusIconAliasMap = ReadonlyMap<string, readonly string[]>;
18
63
 
64
+ export interface WorkspaceSnapshot {
65
+ modules: Readonly<Record<string, Readonly<Record<string, string>>>>;
66
+ }
67
+
19
68
  export interface StarshipRuntimeSnapshot {
20
69
  cwd: string;
21
70
  model?: { provider: string; id: string };
@@ -31,11 +80,16 @@ export interface StarshipRuntimeSnapshot {
31
80
  };
32
81
  tokenTotals: { input: number; output: number; cost: number };
33
82
  gitBranch: string | null;
83
+ gitBranchDetails?: GitBranchSnapshot;
84
+ gitCommit?: GitCommitSnapshot;
85
+ gitState?: GitStateSnapshot;
86
+ gitMetrics?: GitMetricsSnapshot;
34
87
  gitStatus?: GitStatusSnapshot;
35
88
  gitWorktree?: GitWorktreeSnapshot;
36
89
  extensionStatuses: ReadonlyMap<string, string>;
37
90
  extensionStatusIconAliases: ExtensionStatusIconAliasMap;
38
91
  now: Date;
92
+ workspace?: WorkspaceSnapshot;
39
93
  }
40
94
 
41
95
  export interface ExtensionStatusPresentation {
@@ -51,6 +105,13 @@ export interface ModuleValueContext {
51
105
  hiddenExtensionStatusKeys: ReadonlySet<string>;
52
106
  }
53
107
 
108
+ export type ModuleOptionSchema =
109
+ | { kind: "string"; default: string; allowEmpty?: boolean }
110
+ | { kind: "boolean"; default: boolean }
111
+ | { kind: "integer"; default: number; minimum: number; maximum: number }
112
+ | { kind: "string-array"; default: readonly string[]; allowNegative?: boolean }
113
+ | { kind: "string-map"; default: Readonly<Record<string, string>> };
114
+
54
115
  export interface ModuleDefaults {
55
116
  format: string;
56
117
  symbol: string;
@@ -62,6 +123,8 @@ export interface ModuleDefinition<Name extends string> {
62
123
  name: Name;
63
124
  variables: readonly string[];
64
125
  defaults: ModuleDefaults;
126
+ options?: Readonly<Record<string, ModuleOptionSchema>>;
127
+ layout?: "fill";
65
128
  values(context: ModuleValueContext): Record<string, string> | undefined;
66
129
  }
67
130
 
@@ -0,0 +1,10 @@
1
+ import type { ModuleValueContext } from "./types.js";
2
+
3
+ export function workspaceModuleValues(
4
+ name: string,
5
+ context: ModuleValueContext,
6
+ ): Record<string, string> | undefined {
7
+ const values = context.runtime.workspace?.modules[name];
8
+ if (!values) return undefined;
9
+ return { ...values };
10
+ }