@narumitw/pi-starship 0.50.1 → 0.50.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@narumitw/pi-starship",
3
- "version": "0.50.1",
3
+ "version": "0.50.3",
4
4
  "description": "Native Starship-style TOML statusline for the Pi coding agent.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -34,7 +34,7 @@
34
34
  "typecheck": "tsc --noEmit"
35
35
  },
36
36
  "dependencies": {
37
- "@narumitw/pi-tui-kit": "^0.49.1",
37
+ "@narumitw/pi-tui-kit": "^0.54.0",
38
38
  "smol-toml": "^1.7.1",
39
39
  "yaml": "^2.9.0"
40
40
  },
@@ -1,18 +1,5 @@
1
- import { stripVTControlCharacters } from "node:util";
2
- import {
3
- DynamicBorder,
4
- type ExtensionCommandContext,
5
- type KeybindingsManager,
6
- } from "@earendil-works/pi-coding-agent";
7
- import {
8
- Container,
9
- Key,
10
- matchesKey,
11
- type SelectItem,
12
- SelectList,
13
- Text,
14
- } from "@earendil-works/pi-tui";
15
- import { runCustomInteraction } from "@narumitw/pi-tui-kit";
1
+ import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
2
+ import { runLiveChoice } from "@narumitw/pi-tui-kit";
16
3
  import type { StarshipPreset } from "./presets/catalog.js";
17
4
 
18
5
  export type PresetPickerResult =
@@ -34,170 +21,42 @@ export async function showPresetPicker(
34
21
  ctx: ExtensionCommandContext,
35
22
  options: PresetPickerOptions,
36
23
  ): Promise<PresetPickerResult> {
37
- const items: SelectItem[] = options.presets.map((preset) => ({
38
- value: preset.id,
39
- label: `${preset.id === options.activePresetId ? "[-] " : ""}${preset.label}`,
40
- description:
41
- preset.id === options.activePresetId
42
- ? `Currently applied · ${preset.description}`
43
- : preset.description,
44
- }));
45
- const initialIndex = Math.max(
46
- 0,
47
- options.presets.findIndex(
48
- (preset) => preset.id === (options.initialPresetId ?? options.activePresetId),
49
- ),
50
- );
51
- const result = await runCustomInteraction<PresetPickerResult>(ctx, {
24
+ const current =
25
+ options.presets.find((preset) => preset.id === options.activePresetId)?.label ??
26
+ "Custom configuration";
27
+ const result = await runLiveChoice(ctx, {
28
+ title: `Presets · current: ${current}`,
29
+ items: options.presets.map((preset) => ({
30
+ id: preset.id,
31
+ label: preset.label,
32
+ description:
33
+ preset.id === options.activePresetId
34
+ ? `Currently applied · ${preset.description}`
35
+ : preset.description,
36
+ details: [
37
+ `Selected: ${preset.label} · ${preset.requiresNerdFont ? "requires Nerd Font" : "font-safe"}`,
38
+ ],
39
+ confirmationDisabled: preset.id === options.activePresetId,
40
+ confirmationDisabledReason: "Already applied; press e to customize",
41
+ })),
42
+ currentItemId: options.activePresetId,
43
+ initialItemId: options.initialPresetId ?? options.activePresetId,
44
+ viewportSize: Math.min(options.presets.length, 10),
45
+ hint: "back",
46
+ navigationLabel: "live preview",
47
+ confirmLabel: "apply",
48
+ shortcuts: [{ id: "customize", keys: ["e", "shift+e"], label: "customize" }],
49
+ onSelectionChange: ({ item }) => {
50
+ const preset = options.presets.find((candidate) => candidate.id === item.id);
51
+ if (preset) options.preview(preset);
52
+ },
52
53
  signal: options.signal,
53
54
  isCurrent: options.isCurrent,
54
- create({ tui, theme, keybindings, complete }) {
55
- const container = new Container();
56
- const topBorder = new DynamicBorder((text: string) => theme.fg("accent", text));
57
- const bottomBorder = new DynamicBorder((text: string) => theme.fg("accent", text));
58
- const title = new Text("", 1, 0);
59
- const detail = new Text("", 1, 0);
60
- const hint = new Text("", 1, 0);
61
- const viewportSize = Math.min(items.length, 10);
62
- const list = new SelectList(items, viewportSize, {
63
- selectedPrefix: (text) => theme.fg("accent", text),
64
- selectedText: (text) => theme.fg("accent", text),
65
- description: (text) => theme.fg("muted", text),
66
- scrollInfo: (text) => theme.fg("dim", text),
67
- noMatch: (text) => theme.fg("warning", text),
68
- });
69
- const selectedPreset = () => {
70
- const id = list.getSelectedItem()?.value;
71
- return options.presets.find((preset) => preset.id === id);
72
- };
73
- const updateSelectedText = () => {
74
- const preset = selectedPreset();
75
- if (!preset) return;
76
- detail.setText(
77
- theme.fg(
78
- "muted",
79
- `Selected: ${preset.label} · ${preset.requiresNerdFont ? "requires Nerd Font" : "font-safe"}`,
80
- ),
81
- );
82
- };
83
- const previewSelected = () => {
84
- const preset = selectedPreset();
85
- if (!preset) return;
86
- updateSelectedText();
87
- options.preview(preset);
88
- };
89
- const updateThemedText = () => {
90
- const current =
91
- options.presets.find((preset) => preset.id === options.activePresetId)?.label ??
92
- "Custom configuration";
93
- title.setText(theme.fg("accent", theme.bold(`Presets · current: ${current}`)));
94
- hint.setText(theme.fg("dim", pickerHint(keybindings)));
95
- };
96
-
97
- let selectedIndex = initialIndex;
98
- const select = (index: number) => {
99
- if (items.length === 0) return;
100
- selectedIndex = Math.max(0, Math.min(index, items.length - 1));
101
- list.setSelectedIndex(selectedIndex);
102
- previewSelected();
103
- };
104
- const move = (delta: number) => {
105
- if (items.length === 0) return;
106
- select((selectedIndex + delta + items.length) % items.length);
107
- };
108
- const applySelected = () => {
109
- const preset = selectedPreset();
110
- if (preset && preset.id !== options.activePresetId) {
111
- complete({ kind: "apply", presetId: preset.id });
112
- }
113
- };
114
- list.setSelectedIndex(initialIndex);
115
- container.addChild(topBorder);
116
- container.addChild(title);
117
- container.addChild(list);
118
- container.addChild(detail);
119
- container.addChild(hint);
120
- container.addChild(bottomBorder);
121
- updateThemedText();
122
- previewSelected();
123
-
124
- return {
125
- render: (width: number) => container.render(width),
126
- invalidate() {
127
- container.invalidate();
128
- updateThemedText();
129
- updateSelectedText();
130
- },
131
- handleInput(data: string) {
132
- if (matchesKey(data, Key.ctrl("c"))) {
133
- complete({ kind: "close" });
134
- } else if (keybindings.matches(data, "tui.select.cancel")) {
135
- complete({ kind: "back" });
136
- } else if (keybindings.matches(data, "tui.select.up")) {
137
- move(-1);
138
- } else if (keybindings.matches(data, "tui.select.down")) {
139
- move(1);
140
- } else if (keybindings.matches(data, "tui.select.pageUp")) {
141
- select(selectedIndex - Math.max(1, viewportSize));
142
- } else if (keybindings.matches(data, "tui.select.pageDown")) {
143
- select(selectedIndex + Math.max(1, viewportSize));
144
- } else if (matchesKey(data, Key.home)) {
145
- select(0);
146
- } else if (matchesKey(data, Key.end)) {
147
- select(items.length - 1);
148
- } else if (data === "e" || data === "E") {
149
- const preset = selectedPreset();
150
- if (preset) complete({ kind: "customize", presetId: preset.id });
151
- } else if (keybindings.matches(data, "tui.select.confirm")) {
152
- applySelected();
153
- }
154
- tui.requestRender();
155
- },
156
- };
157
- },
158
55
  });
159
- return result.kind === "completed" ? result.value : { kind: "close" };
160
- }
161
-
162
- function pickerHint(keybindings: Pick<KeybindingsManager, "getKeys">): string {
163
- const up = bindingText(keybindings, "tui.select.up");
164
- const down = bindingText(keybindings, "tui.select.down");
165
- const confirm = bindingText(keybindings, "tui.select.confirm");
166
- const cancel = bindingText(keybindings, "tui.select.cancel", "ctrl+c");
167
- const pageUp = bindingText(keybindings, "tui.select.pageUp");
168
- const pageDown = bindingText(keybindings, "tui.select.pageDown");
169
- return [
170
- ...(up || down ? [`${[up, down].filter(Boolean).join("/")} live preview`] : []),
171
- ...(confirm ? [`${confirm} apply`] : []),
172
- "e customize",
173
- ...(cancel ? [`${cancel} back`] : []),
174
- "ctrl+c close",
175
- ...(pageUp || pageDown ? [`${[pageUp, pageDown].filter(Boolean).join("/")} page preview`] : []),
176
- ].join(" • ");
177
- }
178
-
179
- function bindingText(
180
- keybindings: Pick<KeybindingsManager, "getKeys">,
181
- binding: Parameters<KeybindingsManager["getKeys"]>[0],
182
- excluded?: string,
183
- ): string {
184
- return keybindings
185
- .getKeys(binding)
186
- .filter((key) => key !== excluded)
187
- .map((key) => {
188
- if (key === "up") return "↑";
189
- if (key === "down") return "↓";
190
- if (key === "escape") return "esc";
191
- if (key === "return") return "enter";
192
- return safeDisplayText(key);
193
- })
194
- .join("/");
195
- }
196
-
197
- function safeDisplayText(value: string): string {
198
- return Array.from(stripVTControlCharacters(value), (character) => {
199
- const codePoint = character.codePointAt(0) ?? 0;
200
- const control = codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f);
201
- return control ? "" : character;
202
- }).join("");
56
+ if (result.kind === "selected") return { kind: "apply", presetId: result.itemId };
57
+ if (result.kind === "shortcut") {
58
+ return { kind: "customize", presetId: result.itemId };
59
+ }
60
+ if (result.kind === "closed" && result.reason === "back") return { kind: "back" };
61
+ return { kind: "close" };
203
62
  }
@@ -1,6 +1,7 @@
1
1
  import { stripVTControlCharacters } from "node:util";
2
2
  import type { ExtensionCommandContext, KeybindingsManager } from "@earendil-works/pi-coding-agent";
3
3
  import { Key, matchesKey, truncateToWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
4
+ import { runCustomInteraction } from "@narumitw/pi-tui-kit";
4
5
 
5
6
  const RESERVED_HOST_ROWS = 3;
6
7
 
@@ -20,107 +21,104 @@ export async function showPreviewActionMenu<Value extends string>(
20
21
  body: (width: number) => readonly string[],
21
22
  items: readonly PreviewMenuItem<Value>[],
22
23
  signal?: AbortSignal,
24
+ isCurrent: () => boolean = () => !signal?.aborted,
23
25
  ): 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);
26
+ if (signal?.aborted || !isCurrent()) return { kind: "closed" };
27
+ const result = await runCustomInteraction<PreviewMenuResult<Value>>(ctx, {
28
+ signal,
29
+ isCurrent,
30
+ create: ({ tui, theme, keybindings, complete }) => {
31
+ let selectedIndex = 0;
32
+ let scrollOffset = 0;
33
+ let lastMaximumScroll = 0;
34
+ let lastViewportSize = 1;
35
+ let disposed = false;
40
36
 
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
- };
37
+ const requestRender = () => {
38
+ if (!disposed) tui.requestRender();
39
+ };
40
+ const moveSelection = (delta: number) => {
41
+ if (items.length === 0) return;
42
+ selectedIndex = (selectedIndex + delta + items.length) % items.length;
43
+ requestRender();
44
+ };
45
+ const movePreview = (offset: number) => {
46
+ scrollOffset = Math.max(0, Math.min(offset, lastMaximumScroll));
47
+ requestRender();
48
+ };
53
49
 
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
- };
50
+ return {
51
+ render(width: number): string[] {
52
+ const safeWidth = Math.max(1, width);
53
+ const terminalRows = Number.isFinite(tui.terminal.rows)
54
+ ? Math.floor(tui.terminal.rows)
55
+ : 24;
56
+ const availableRows = Math.max(1, terminalRows - RESERVED_HOST_ROWS);
57
+ const bodyLines = body(safeWidth).flatMap((line) =>
58
+ line ? wrapTextWithAnsi(line, safeWidth) : [""],
59
+ );
60
+ const layout = allocateLayout(availableRows, items.length, bodyLines.length);
61
+ lastViewportSize = layout.previewRows;
62
+ lastMaximumScroll = Math.max(0, bodyLines.length - layout.previewRows);
63
+ scrollOffset = Math.max(0, Math.min(scrollOffset, lastMaximumScroll));
64
+ const actionStart = actionWindowStart(selectedIndex, items.length, layout.actionRows);
65
+ const actionLines = items
66
+ .slice(actionStart, actionStart + layout.actionRows)
67
+ .map((item, index) => {
68
+ const absoluteIndex = actionStart + index;
69
+ const prefix = absoluteIndex === selectedIndex ? "→ " : " ";
70
+ return theme.fg(
71
+ absoluteIndex === selectedIndex ? "accent" : "text",
72
+ `${prefix}${safeDisplayText(item.label)}`,
73
+ );
74
+ });
75
+ const position = layout.positionRows
76
+ ? [theme.fg("dim", previewPosition(scrollOffset, layout.previewRows, bodyLines.length))]
77
+ : [];
78
+ const lines = [
79
+ ...(layout.titleRows ? [theme.fg("accent", theme.bold(safeDisplayText(title)))] : []),
80
+ ...bodyLines.slice(scrollOffset, scrollOffset + layout.previewRows),
81
+ ...position,
82
+ ...actionLines,
83
+ ...(layout.hintRows ? [theme.fg("dim", previewHint(keybindings))] : []),
84
+ ];
85
+ return lines.map((line) => truncateToWidth(line, safeWidth, ""));
86
+ },
87
+ invalidate() {},
88
+ handleInput(data: string) {
89
+ if (disposed) return;
90
+ if (matchesKey(data, Key.ctrl("c"))) {
91
+ complete({ kind: "closed" });
92
+ } else if (keybindings.matches(data, "tui.select.cancel")) {
93
+ complete({ kind: "cancelled" });
94
+ } else if (keybindings.matches(data, "tui.select.up")) {
95
+ moveSelection(-1);
96
+ } else if (keybindings.matches(data, "tui.select.down")) {
97
+ moveSelection(1);
98
+ } else if (keybindings.matches(data, "tui.select.pageUp")) {
99
+ movePreview(scrollOffset - lastViewportSize);
100
+ } else if (keybindings.matches(data, "tui.select.pageDown")) {
101
+ movePreview(scrollOffset + lastViewportSize);
102
+ } else if (matchesKey(data, Key.home)) {
103
+ movePreview(0);
104
+ } else if (matchesKey(data, Key.end)) {
105
+ movePreview(lastMaximumScroll);
106
+ } else if (keybindings.matches(data, "tui.select.confirm")) {
107
+ const item = items[selectedIndex];
108
+ if (item) complete({ kind: "selected", value: item.value });
109
+ }
110
+ },
111
+ dispose() {
112
+ if (disposed) return;
113
+ disposed = true;
114
+ },
115
+ };
116
+ },
123
117
  });
118
+ if (result.kind === "completed") return result.value;
119
+ if (result.kind === "error") throw result.error;
120
+ if (result.kind === "stale" && (signal?.aborted || !isCurrent())) return { kind: "closed" };
121
+ return undefined;
124
122
  }
125
123
 
126
124
  interface PreviewLayout {
package/src/commands.ts CHANGED
@@ -300,6 +300,7 @@ async function editSettings(
300
300
  { value: PREVIEW_ACTIONS.cancel, label: "Discard draft" },
301
301
  ],
302
302
  owner.signal,
303
+ owner.isCurrent,
303
304
  );
304
305
  if (!isCurrentOwner(owner)) return "cancel";
305
306
  if (action?.kind === "closed") return "close";
@@ -384,6 +385,7 @@ async function applyPreset(
384
385
  { value: PREVIEW_ACTIONS.cancel, label: "Choose another preset" },
385
386
  ],
386
387
  owner.signal,
388
+ owner.isCurrent,
387
389
  );
388
390
  if (!isCurrentOwner(owner)) return "cancel";
389
391
  if (action?.kind === "closed") return "close";
@@ -461,6 +463,7 @@ async function reviewAndApply(
461
463
  },
462
464
  ],
463
465
  owner.signal,
466
+ owner.isCurrent,
464
467
  );
465
468
  if (!isCurrentOwner(owner)) return "cancel";
466
469
  if (selection?.kind === "closed") return "close";
@@ -1,5 +1,5 @@
1
1
  import { basename, isAbsolute, relative, sep } from "node:path";
2
- import { sanitizeTerminalText } from "./terminal.js";
2
+ import { sanitizeTerminalText } from "@narumitw/pi-tui-kit";
3
3
  import {
4
4
  graphemes,
5
5
  toSlashPath,
@@ -1,4 +1,4 @@
1
- import { sanitizeTerminalText } from "./terminal.js";
1
+ import { sanitizeTerminalText } from "@narumitw/pi-tui-kit";
2
2
  import { defineModule } from "./types.js";
3
3
 
4
4
  const TRUNCATION_DIRECTIONS = ["start", "middle", "end"] as const;
@@ -1,60 +0,0 @@
1
- export function sanitizeTerminalText(value: string): string {
2
- let result = "";
3
- for (let index = 0; index < value.length; ) {
4
- const codePoint = value.codePointAt(index) ?? 0;
5
- const character = String.fromCodePoint(codePoint);
6
- if (codePoint === 0x1b || codePoint === 0x9b || codePoint === 0x9d) {
7
- index = skipTerminalEscape(value, index, codePoint);
8
- continue;
9
- }
10
- if (
11
- codePoint <= 0x1f ||
12
- (codePoint >= 0x7f && codePoint <= 0x9f) ||
13
- codePoint === 0x2028 ||
14
- codePoint === 0x2029
15
- ) {
16
- if (
17
- codePoint === 0x09 ||
18
- codePoint === 0x0a ||
19
- codePoint === 0x0d ||
20
- codePoint === 0x85 ||
21
- codePoint === 0x2028 ||
22
- codePoint === 0x2029
23
- ) {
24
- result += " ";
25
- }
26
- index += character.length;
27
- continue;
28
- }
29
- result += character;
30
- index += character.length;
31
- }
32
- return result;
33
- }
34
-
35
- function skipTerminalEscape(value: string, start: number, codePoint: number): number {
36
- let index = start + 1;
37
- const next = value.charCodeAt(index);
38
- const isOsc = codePoint === 0x9d || (codePoint === 0x1b && next === 0x5d);
39
- if (isOsc) {
40
- if (codePoint === 0x1b) index += 1;
41
- while (index < value.length) {
42
- const current = value.charCodeAt(index);
43
- if (current === 0x07 || current === 0x9c) return index + 1;
44
- if (current === 0x1b && value.charCodeAt(index + 1) === 0x5c) return index + 2;
45
- index += 1;
46
- }
47
- return index;
48
- }
49
- const isCsi = codePoint === 0x9b || (codePoint === 0x1b && next === 0x5b);
50
- if (isCsi) {
51
- if (codePoint === 0x1b) index += 1;
52
- while (index < value.length) {
53
- const current = value.charCodeAt(index);
54
- index += 1;
55
- if (current >= 0x40 && current <= 0x7e) break;
56
- }
57
- return index;
58
- }
59
- return Math.min(value.length, start + (codePoint === 0x1b ? 2 : 1));
60
- }