@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
package/README.md CHANGED
@@ -26,7 +26,7 @@ A native Pi footer configured with Starship-style TOML. It parses and renders fo
26
26
  pi install npm:@narumitw/pi-starship
27
27
  ```
28
28
 
29
- Try it from a checkout; the recipe prepares the generated runtime before loading it:
29
+ Try it directly from a checkout:
30
30
 
31
31
  ```bash
32
32
  just try starship
@@ -630,9 +630,7 @@ Keep `extension_status` last in the catalog so arbitrary third-party statuses fo
630
630
 
631
631
  ## 🗂️ Package layout
632
632
 
633
- - `src/index.ts` — thin Pi package entrypoint forwarding to the generated runtime.
634
- - `dist/` — generated split TypeScript runtime and source maps loaded through Pi's Jiti aliases.
635
- - `scripts/build-runtime.mjs` — deterministic runtime bundler and eager-import boundary validator.
633
+ - `src/index.ts` — thin Pi package entrypoint forwarding to the source runtime.
636
634
  - `src/pi-starship.ts` — authoritative extension lifecycle, cached refresh binding, live preview, and footer.
637
635
  - `src/usage.ts` — native-aligned session usage and cache aggregation.
638
636
  - `src/command-contract.ts` — lightweight command routes and completions loaded at startup.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@narumitw/pi-starship",
3
- "version": "0.49.3",
3
+ "version": "0.49.4",
4
4
  "description": "Native Starship-style TOML statusline for the Pi coding agent.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -15,8 +15,7 @@
15
15
  "toml"
16
16
  ],
17
17
  "files": [
18
- "src/index.ts",
19
- "dist",
18
+ "src",
20
19
  "README.md",
21
20
  "LICENSE",
22
21
  "NOTICES.md"
@@ -27,10 +26,8 @@
27
26
  ]
28
27
  },
29
28
  "scripts": {
30
- "build": "node --test scripts/build-runtime.test.mjs && node scripts/build-runtime.mjs",
31
- "check": "npm run build && biome check --vcs-use-ignore-file=false src test scripts package.json tsconfig.json README.md NOTICES.md && npm run typecheck",
32
- "format": "biome check --write --vcs-use-ignore-file=false src test scripts package.json tsconfig.json README.md NOTICES.md",
33
- "prepack": "npm run build",
29
+ "check": "biome check --vcs-use-ignore-file=false src test package.json tsconfig.json README.md NOTICES.md && npm run typecheck",
30
+ "format": "biome check --write --vcs-use-ignore-file=false src test package.json tsconfig.json README.md NOTICES.md",
34
31
  "typecheck": "tsc --noEmit"
35
32
  },
36
33
  "dependencies": {
@@ -47,7 +44,6 @@
47
44
  "@earendil-works/pi-coding-agent": "0.83.0",
48
45
  "@earendil-works/pi-tui": "0.83.0",
49
46
  "@types/node": "26.1.2",
50
- "esbuild": "0.28.1",
51
47
  "typescript": "7.0.2"
52
48
  },
53
49
  "repository": {
@@ -0,0 +1,13 @@
1
+ import type { AutocompleteItem } from "@earendil-works/pi-tui";
2
+
3
+ export const STARSHIP_SUBCOMMANDS: readonly AutocompleteItem[] = [
4
+ { value: "settings", label: "settings", description: "Customize the footer TOML" },
5
+ { value: "status", label: "status", description: "Show configuration health and source" },
6
+ { value: "help", label: "help", description: "Show configuration help" },
7
+ ];
8
+
9
+ export function completeStarshipArguments(prefix: string): AutocompleteItem[] | null {
10
+ const normalized = prefix.trim().toLowerCase();
11
+ const matches = STARSHIP_SUBCOMMANDS.filter((item) => item.value.startsWith(normalized));
12
+ return matches.length > 0 ? [...matches] : null;
13
+ }
@@ -0,0 +1,198 @@
1
+ import { stripVTControlCharacters } from "node:util";
2
+ import type { ExtensionCommandContext, KeybindingsManager } from "@earendil-works/pi-coding-agent";
3
+ import { Key, matchesKey, truncateToWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
4
+ import type { StatuslineInspection } from "./modules/inspection.js";
5
+
6
+ const RESERVED_HOST_ROWS = 3;
7
+
8
+ export type InspectorResult = { kind: "back" } | { kind: "close" };
9
+
10
+ export function showFooterExplanation(
11
+ ctx: ExtensionCommandContext,
12
+ inspection: StatuslineInspection | undefined,
13
+ signal?: AbortSignal,
14
+ ): Promise<InspectorResult | undefined> {
15
+ if (signal?.aborted) return Promise.resolve({ kind: "close" });
16
+ return ctx.ui.custom<InspectorResult | undefined>((tui, theme, keybindings, done) => {
17
+ let scrollOffset = 0;
18
+ let lastMaximumScroll = 0;
19
+ let lastViewportSize = 1;
20
+ let disposed = false;
21
+ let settled = false;
22
+ let deferredAbort: ReturnType<typeof setImmediate> | undefined;
23
+ const finish = (result: InspectorResult) => {
24
+ if (disposed || settled) return;
25
+ settled = true;
26
+ done(result);
27
+ };
28
+ const closeForAbort = () => finish({ kind: "close" });
29
+ signal?.addEventListener("abort", closeForAbort, { once: true });
30
+ if (signal?.aborted) deferredAbort = setImmediate(closeForAbort);
31
+
32
+ return {
33
+ focused: false,
34
+ render(width: number): string[] {
35
+ const safeWidth = Math.max(1, width);
36
+ const availableRows = componentRows(tui.terminal.rows);
37
+ const content = explanationLines(inspection, safeWidth);
38
+ const layout = detailLayout(availableRows, content.length);
39
+ lastViewportSize = layout.contentRows;
40
+ lastMaximumScroll = Math.max(0, content.length - layout.contentRows);
41
+ scrollOffset = clamp(scrollOffset, 0, lastMaximumScroll);
42
+ const lines = [
43
+ ...(layout.titleRows ? [theme.fg("accent", theme.bold("Explain footer"))] : []),
44
+ ...content.slice(scrollOffset, scrollOffset + layout.contentRows),
45
+ ...(layout.positionRows
46
+ ? [theme.fg("dim", positionText(scrollOffset, layout.contentRows, content.length))]
47
+ : []),
48
+ ...(layout.hintRows ? [theme.fg("dim", detailHint(keybindings, "back"))] : []),
49
+ ];
50
+ return boundedLines(lines, safeWidth, availableRows);
51
+ },
52
+ invalidate() {},
53
+ handleInput(data: string) {
54
+ if (disposed || settled) return;
55
+ if (matchesKey(data, Key.ctrl("c"))) finish({ kind: "close" });
56
+ else if (keybindings.matches(data, "tui.select.cancel")) finish({ kind: "back" });
57
+ else if (keybindings.matches(data, "tui.select.up")) {
58
+ scrollOffset = clamp(scrollOffset - 1, 0, lastMaximumScroll);
59
+ } else if (keybindings.matches(data, "tui.select.down")) {
60
+ scrollOffset = clamp(scrollOffset + 1, 0, lastMaximumScroll);
61
+ } else if (keybindings.matches(data, "tui.select.pageUp")) {
62
+ scrollOffset = clamp(scrollOffset - lastViewportSize, 0, lastMaximumScroll);
63
+ } else if (keybindings.matches(data, "tui.select.pageDown")) {
64
+ scrollOffset = clamp(scrollOffset + lastViewportSize, 0, lastMaximumScroll);
65
+ } else if (matchesKey(data, Key.home)) scrollOffset = 0;
66
+ else if (matchesKey(data, Key.end)) scrollOffset = lastMaximumScroll;
67
+ tui.requestRender();
68
+ },
69
+ dispose() {
70
+ if (disposed) return;
71
+ disposed = true;
72
+ if (deferredAbort) clearImmediate(deferredAbort);
73
+ deferredAbort = undefined;
74
+ signal?.removeEventListener("abort", closeForAbort);
75
+ },
76
+ };
77
+ });
78
+ }
79
+
80
+ function explanationLines(inspection: StatuslineInspection | undefined, width: number): string[] {
81
+ if (!inspection) {
82
+ return wrapLines(
83
+ [
84
+ "Footer inspection is unavailable until the TUI footer is ready.",
85
+ "No collection work was started.",
86
+ ],
87
+ width,
88
+ );
89
+ }
90
+ if (inspection.showing.length === 0) {
91
+ return wrapLines(
92
+ [
93
+ "No modules are currently showing.",
94
+ "Open Modules to inspect empty, disabled, or unreachable modules.",
95
+ ],
96
+ width,
97
+ );
98
+ }
99
+ const lines = inspection.showing.flatMap((module, index) => [
100
+ ...(index > 0 ? [""] : []),
101
+ module.name,
102
+ ...previewLines(module.preview),
103
+ module.description,
104
+ ]);
105
+ return wrapLines(lines, width);
106
+ }
107
+
108
+ function previewLines(preview: string): string[] {
109
+ const lines = preview ? preview.split("\n") : ["(no text)"];
110
+ return lines.map((line, index) => `${index === 0 ? "Value: " : " "}${line}`);
111
+ }
112
+
113
+ function wrapLines(lines: readonly string[], width: number): string[] {
114
+ return lines.flatMap((line) => {
115
+ const safe = safeDisplayText(line);
116
+ return safe ? wrapTextWithAnsi(safe, width) : [""];
117
+ });
118
+ }
119
+
120
+ interface DetailLayout {
121
+ titleRows: number;
122
+ contentRows: number;
123
+ positionRows: number;
124
+ hintRows: number;
125
+ }
126
+
127
+ function detailLayout(availableRows: number, contentLength: number): DetailLayout {
128
+ if (availableRows === 1) {
129
+ return { titleRows: 0, contentRows: 1, positionRows: 0, hintRows: 0 };
130
+ }
131
+ const titleRows = availableRows >= 4 ? 1 : 0;
132
+ const hintRows = availableRows >= 3 ? 1 : 0;
133
+ let contentRows = Math.max(1, availableRows - titleRows - hintRows);
134
+ const positionRows = contentLength > contentRows && contentRows >= 2 ? 1 : 0;
135
+ contentRows -= positionRows;
136
+ return { titleRows, contentRows, positionRows, hintRows };
137
+ }
138
+
139
+ function componentRows(rows: number): number {
140
+ const terminalRows = Number.isFinite(rows) ? Math.floor(rows) : 24;
141
+ return Math.max(1, terminalRows - RESERVED_HOST_ROWS);
142
+ }
143
+
144
+ function positionText(offset: number, viewportSize: number, itemCount: number): string {
145
+ if (itemCount === 0) return "0/0";
146
+ return `${offset + 1}-${Math.min(itemCount, offset + viewportSize)}/${itemCount}`;
147
+ }
148
+
149
+ function boundedLines(lines: readonly string[], width: number, rows: number): string[] {
150
+ return lines.slice(0, rows).map((line) => truncateToWidth(line, width, ""));
151
+ }
152
+
153
+ function detailHint(
154
+ keybindings: Pick<KeybindingsManager, "getKeys">,
155
+ destination: "back" | "close",
156
+ ): string {
157
+ const up = bindingText(keybindings, "tui.select.up");
158
+ const down = bindingText(keybindings, "tui.select.down");
159
+ const cancel = bindingText(keybindings, "tui.select.cancel", "ctrl+c");
160
+ const pageUp = bindingText(keybindings, "tui.select.pageUp");
161
+ const pageDown = bindingText(keybindings, "tui.select.pageDown");
162
+ return [
163
+ ...(up || down ? [`${[up, down].filter(Boolean).join("/")} scroll`] : []),
164
+ ...(pageUp || pageDown ? [`${[pageUp, pageDown].filter(Boolean).join("/")} page`] : []),
165
+ ...(cancel ? [`${cancel} ${destination}`] : []),
166
+ "ctrl+c close",
167
+ ].join(" • ");
168
+ }
169
+
170
+ function bindingText(
171
+ keybindings: Pick<KeybindingsManager, "getKeys">,
172
+ binding: Parameters<KeybindingsManager["getKeys"]>[0],
173
+ excluded?: string,
174
+ ): string {
175
+ return keybindings
176
+ .getKeys(binding)
177
+ .filter((key) => key !== excluded)
178
+ .map((key) => {
179
+ if (key === "up") return "↑";
180
+ if (key === "down") return "↓";
181
+ if (key === "escape") return "esc";
182
+ if (key === "return") return "enter";
183
+ return safeDisplayText(key);
184
+ })
185
+ .filter(Boolean)
186
+ .join("/");
187
+ }
188
+
189
+ function safeDisplayText(value: string): string {
190
+ return Array.from(stripVTControlCharacters(value), (character) => {
191
+ const codePoint = character.codePointAt(0) ?? 0;
192
+ return codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f) ? "" : character;
193
+ }).join("");
194
+ }
195
+
196
+ function clamp(value: number, minimum: number, maximum: number): number {
197
+ return Math.max(minimum, Math.min(value, maximum));
198
+ }
@@ -0,0 +1,206 @@
1
+ import { stripVTControlCharacters } from "node:util";
2
+ import type { ExtensionCommandContext, KeybindingsManager } from "@earendil-works/pi-coding-agent";
3
+ import { Key, matchesKey, truncateToWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
4
+
5
+ const RESERVED_HOST_ROWS = 3;
6
+
7
+ export interface PreviewMenuItem<Value extends string> {
8
+ value: Value;
9
+ label: string;
10
+ }
11
+
12
+ export type PreviewMenuResult<Value extends string> =
13
+ | { kind: "selected"; value: Value }
14
+ | { kind: "cancelled" }
15
+ | { kind: "closed" };
16
+
17
+ export async function showPreviewActionMenu<Value extends string>(
18
+ ctx: ExtensionCommandContext,
19
+ title: string,
20
+ body: (width: number) => readonly string[],
21
+ items: readonly PreviewMenuItem<Value>[],
22
+ signal?: AbortSignal,
23
+ ): Promise<PreviewMenuResult<Value> | undefined> {
24
+ if (signal?.aborted) return { kind: "closed" };
25
+ return ctx.ui.custom<PreviewMenuResult<Value> | undefined>((tui, theme, keybindings, done) => {
26
+ let selectedIndex = 0;
27
+ let scrollOffset = 0;
28
+ let lastMaximumScroll = 0;
29
+ let lastViewportSize = 1;
30
+ let disposed = false;
31
+ let deferredAbort: ReturnType<typeof setImmediate> | undefined;
32
+ const closeForAbort = () => {
33
+ if (disposed) return;
34
+ if (deferredAbort) clearImmediate(deferredAbort);
35
+ deferredAbort = undefined;
36
+ done({ kind: "closed" });
37
+ };
38
+ signal?.addEventListener("abort", closeForAbort, { once: true });
39
+ if (signal?.aborted) deferredAbort = setImmediate(closeForAbort);
40
+
41
+ const requestRender = () => {
42
+ if (!disposed) tui.requestRender();
43
+ };
44
+ const moveSelection = (delta: number) => {
45
+ if (items.length === 0) return;
46
+ selectedIndex = (selectedIndex + delta + items.length) % items.length;
47
+ requestRender();
48
+ };
49
+ const movePreview = (offset: number) => {
50
+ scrollOffset = Math.max(0, Math.min(offset, lastMaximumScroll));
51
+ requestRender();
52
+ };
53
+
54
+ return {
55
+ render(width: number): string[] {
56
+ const safeWidth = Math.max(1, width);
57
+ const terminalRows = Number.isFinite(tui.terminal.rows)
58
+ ? Math.floor(tui.terminal.rows)
59
+ : 24;
60
+ const availableRows = Math.max(1, terminalRows - RESERVED_HOST_ROWS);
61
+ const bodyLines = body(safeWidth).flatMap((line) =>
62
+ line ? wrapTextWithAnsi(line, safeWidth) : [""],
63
+ );
64
+ const layout = allocateLayout(availableRows, items.length, bodyLines.length);
65
+ lastViewportSize = layout.previewRows;
66
+ lastMaximumScroll = Math.max(0, bodyLines.length - layout.previewRows);
67
+ scrollOffset = Math.max(0, Math.min(scrollOffset, lastMaximumScroll));
68
+ const actionStart = actionWindowStart(selectedIndex, items.length, layout.actionRows);
69
+ const actionLines = items
70
+ .slice(actionStart, actionStart + layout.actionRows)
71
+ .map((item, index) => {
72
+ const absoluteIndex = actionStart + index;
73
+ const prefix = absoluteIndex === selectedIndex ? "→ " : " ";
74
+ return theme.fg(
75
+ absoluteIndex === selectedIndex ? "accent" : "text",
76
+ `${prefix}${safeDisplayText(item.label)}`,
77
+ );
78
+ });
79
+ const position = layout.positionRows
80
+ ? [theme.fg("dim", previewPosition(scrollOffset, layout.previewRows, bodyLines.length))]
81
+ : [];
82
+ const lines = [
83
+ ...(layout.titleRows ? [theme.fg("accent", theme.bold(safeDisplayText(title)))] : []),
84
+ ...bodyLines.slice(scrollOffset, scrollOffset + layout.previewRows),
85
+ ...position,
86
+ ...actionLines,
87
+ ...(layout.hintRows ? [theme.fg("dim", previewHint(keybindings))] : []),
88
+ ];
89
+ return lines.map((line) => truncateToWidth(line, safeWidth, ""));
90
+ },
91
+ invalidate() {},
92
+ handleInput(data: string) {
93
+ if (disposed) return;
94
+ if (matchesKey(data, Key.ctrl("c"))) {
95
+ done({ kind: "closed" });
96
+ } else if (keybindings.matches(data, "tui.select.cancel")) {
97
+ done({ kind: "cancelled" });
98
+ } else if (keybindings.matches(data, "tui.select.up")) {
99
+ moveSelection(-1);
100
+ } else if (keybindings.matches(data, "tui.select.down")) {
101
+ moveSelection(1);
102
+ } else if (keybindings.matches(data, "tui.select.pageUp")) {
103
+ movePreview(scrollOffset - lastViewportSize);
104
+ } else if (keybindings.matches(data, "tui.select.pageDown")) {
105
+ movePreview(scrollOffset + lastViewportSize);
106
+ } else if (matchesKey(data, Key.home)) {
107
+ movePreview(0);
108
+ } else if (matchesKey(data, Key.end)) {
109
+ movePreview(lastMaximumScroll);
110
+ } else if (keybindings.matches(data, "tui.select.confirm")) {
111
+ const item = items[selectedIndex];
112
+ if (item) done({ kind: "selected", value: item.value });
113
+ }
114
+ },
115
+ dispose() {
116
+ if (disposed) return;
117
+ disposed = true;
118
+ if (deferredAbort) clearImmediate(deferredAbort);
119
+ deferredAbort = undefined;
120
+ signal?.removeEventListener("abort", closeForAbort);
121
+ },
122
+ };
123
+ });
124
+ }
125
+
126
+ interface PreviewLayout {
127
+ titleRows: number;
128
+ previewRows: number;
129
+ positionRows: number;
130
+ actionRows: number;
131
+ hintRows: number;
132
+ }
133
+
134
+ function allocateLayout(
135
+ availableRows: number,
136
+ actionCount: number,
137
+ bodyLineCount: number,
138
+ ): PreviewLayout {
139
+ if (availableRows === 1) {
140
+ return { titleRows: 0, previewRows: 0, positionRows: 0, actionRows: 1, hintRows: 0 };
141
+ }
142
+ const titleRows = availableRows >= 3 ? 1 : 0;
143
+ const hintRows = availableRows >= 3 ? 1 : 0;
144
+ const minimumPreviewRows = bodyLineCount > 0 && availableRows >= 4 ? 1 : 0;
145
+ const availableActionRows = Math.max(
146
+ 1,
147
+ availableRows - titleRows - hintRows - minimumPreviewRows,
148
+ );
149
+ const actionRows = Math.min(Math.max(1, actionCount), availableActionRows);
150
+ let previewRows = Math.max(0, availableRows - titleRows - hintRows - actionRows);
151
+ const positionRows = previewRows >= 2 && bodyLineCount > previewRows ? 1 : 0;
152
+ previewRows -= positionRows;
153
+ return { titleRows, previewRows, positionRows, actionRows, hintRows };
154
+ }
155
+
156
+ function actionWindowStart(selectedIndex: number, itemCount: number, viewportSize: number): number {
157
+ if (itemCount <= viewportSize) return 0;
158
+ return Math.max(0, Math.min(selectedIndex, itemCount - viewportSize));
159
+ }
160
+
161
+ function previewPosition(offset: number, viewportSize: number, lineCount: number): string {
162
+ if (lineCount === 0) return "0/0";
163
+ return `${offset + 1}-${Math.min(lineCount, offset + viewportSize)}/${lineCount}`;
164
+ }
165
+
166
+ function previewHint(keybindings: Pick<KeybindingsManager, "getKeys">): string {
167
+ const up = bindingText(keybindings, "tui.select.up");
168
+ const down = bindingText(keybindings, "tui.select.down");
169
+ const confirm = bindingText(keybindings, "tui.select.confirm");
170
+ const cancel = bindingText(keybindings, "tui.select.cancel", "ctrl+c");
171
+ const pageUp = bindingText(keybindings, "tui.select.pageUp");
172
+ const pageDown = bindingText(keybindings, "tui.select.pageDown");
173
+ return [
174
+ ...(up || down ? [`${[up, down].filter(Boolean).join("/")} navigate`] : []),
175
+ ...(confirm ? [`${confirm} select`] : []),
176
+ ...(cancel ? [`${cancel} discard`] : []),
177
+ "ctrl+c close",
178
+ ...(pageUp || pageDown ? [`${[pageUp, pageDown].filter(Boolean).join("/")} preview`] : []),
179
+ ].join(" • ");
180
+ }
181
+
182
+ function bindingText(
183
+ keybindings: Pick<KeybindingsManager, "getKeys">,
184
+ binding: Parameters<KeybindingsManager["getKeys"]>[0],
185
+ excluded?: string,
186
+ ): string {
187
+ return keybindings
188
+ .getKeys(binding)
189
+ .filter((key) => key !== excluded)
190
+ .map((key) => {
191
+ if (key === "up") return "↑";
192
+ if (key === "down") return "↓";
193
+ if (key === "escape") return "esc";
194
+ if (key === "return") return "enter";
195
+ return safeDisplayText(key);
196
+ })
197
+ .join("/");
198
+ }
199
+
200
+ function safeDisplayText(value: string): string {
201
+ return Array.from(stripVTControlCharacters(value), (character) => {
202
+ const codePoint = character.codePointAt(0) ?? 0;
203
+ const control = codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f);
204
+ return control ? "" : character;
205
+ }).join("");
206
+ }