@marcosgilf/pi-modal 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +12 -2
- package/package.json +26 -4
- package/src/index.ts +218 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Marcos Gil
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
#
|
|
1
|
+
# pi-modal
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Shared modal component for Pi extensions. Supports refresh, plain-text copy, and styled rows.
|
|
4
|
+
|
|
5
|
+
Feature packages provide modal content and actions. This package provides the TUI shell, extra top/bottom padding, and outside-click dismissal. Press `c` to copy full modal text or Ctrl/Cmd+C to copy active fullscreen selection.
|
|
6
|
+
|
|
7
|
+
## Use
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { showModal, type ModalContent } from "@marcosgilf/pi-modal";
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`@earendil-works/pi-coding-agent` and `@earendil-works/pi-tui` are Pi-provided peer dependencies.
|
package/package.json
CHANGED
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@marcosgilf/pi-modal",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Shared modal UI components for Pi extension packages.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"private": false,
|
|
8
|
+
"keywords": [
|
|
9
|
+
"pi-library",
|
|
10
|
+
"pi",
|
|
11
|
+
"modal"
|
|
12
|
+
],
|
|
13
|
+
"files": [
|
|
14
|
+
"src",
|
|
15
|
+
"README.md",
|
|
16
|
+
"LICENSE"
|
|
17
|
+
],
|
|
18
|
+
"exports": {
|
|
19
|
+
".": "./src/index.ts"
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"@earendil-works/pi-coding-agent": "*",
|
|
23
|
+
"@earendil-works/pi-tui": "*"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"typecheck": "tsc --noEmit -p tsconfig.json"
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import type { ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import {
|
|
4
|
+
type Component,
|
|
5
|
+
type KeybindingsManager,
|
|
6
|
+
matchesKey,
|
|
7
|
+
type TUI,
|
|
8
|
+
type TuiMouseEvent,
|
|
9
|
+
truncateToWidth,
|
|
10
|
+
visibleWidth,
|
|
11
|
+
} from "@earendil-works/pi-tui";
|
|
12
|
+
|
|
13
|
+
export type ModalRow = { text: string; style?: "accent" | "success" | "warning"; progress?: number };
|
|
14
|
+
export type ModalContent = { rows: ModalRow[]; clipboardText: string };
|
|
15
|
+
export type ModalActions = {
|
|
16
|
+
refresh: () => Promise<ModalContent>;
|
|
17
|
+
copy?: (text: string) => Promise<void>;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export function writeClipboard(text: string, command = "pbcopy"): Promise<void> {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
const child = execFile(command, (error) => (error ? reject(error) : resolve()));
|
|
23
|
+
child.stdin?.on("error", reject);
|
|
24
|
+
child.stdin?.end(text);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function formatProgressBar(percent: number, width: number): string {
|
|
29
|
+
const cells = Math.max(0, Math.floor(width));
|
|
30
|
+
const value = Number.isFinite(percent) ? Math.min(100, Math.max(0, percent)) : 0;
|
|
31
|
+
const filled = Math.round((value / 100) * cells);
|
|
32
|
+
return `${"█".repeat(filled)}${"░".repeat(cells - filled)}`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function borderedRows(rows: ModalRow[], width: number, theme: Theme): string[] {
|
|
36
|
+
if (width < 6) {
|
|
37
|
+
return rows.map(({ text, progress }) =>
|
|
38
|
+
truncateToWidth(progress === undefined ? text : formatProgressBar(progress, width), width, "…"),
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
const contentWidth = width - 4;
|
|
42
|
+
const edge = theme.fg("muted", "│");
|
|
43
|
+
const top = theme.fg("muted", `╭${"─".repeat(contentWidth + 2)}╮`);
|
|
44
|
+
const bottom = theme.fg("muted", `╰${"─".repeat(contentWidth + 2)}╯`);
|
|
45
|
+
const content = rows.map(({ text, style, progress }) => {
|
|
46
|
+
const value = progress === undefined ? text : formatProgressBar(progress, contentWidth);
|
|
47
|
+
const styled = style === "accent" ? theme.fg(style, theme.bold(value)) : style ? theme.fg(style, value) : value;
|
|
48
|
+
const clipped = truncateToWidth(styled, contentWidth, "…");
|
|
49
|
+
const padding = " ".repeat(Math.max(0, contentWidth - visibleWidth(clipped)));
|
|
50
|
+
return `${edge}${theme.bg("selectedBg", ` ${clipped}${padding} `)}${edge}`;
|
|
51
|
+
});
|
|
52
|
+
return [top, ...content, bottom];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function modalScreen(
|
|
56
|
+
rows: ModalRow[],
|
|
57
|
+
width: number,
|
|
58
|
+
height: number,
|
|
59
|
+
theme: Theme,
|
|
60
|
+
): { lines: string[]; bounds: { left: number; top: number; right: number; bottom: number } } {
|
|
61
|
+
const panelWidth = Math.min(width, 74);
|
|
62
|
+
const paddedRows = [{ text: "" }, ...rows, { text: "" }];
|
|
63
|
+
let panel = borderedRows(paddedRows, panelWidth, theme);
|
|
64
|
+
if (panel.length > height) {
|
|
65
|
+
const compactRows = rows.filter(({ text, progress }) => text.length > 0 || progress !== undefined);
|
|
66
|
+
panel = borderedRows(compactRows, panelWidth, theme);
|
|
67
|
+
}
|
|
68
|
+
const left = Math.floor((width - panelWidth) / 2);
|
|
69
|
+
const top = Math.max(0, Math.floor((height - panel.length) / 2));
|
|
70
|
+
const screen = Array.from({ length: height }, () => " ".repeat(width));
|
|
71
|
+
panel.forEach((line, index) => {
|
|
72
|
+
const row = top + index;
|
|
73
|
+
if (row < height) {
|
|
74
|
+
screen[row] = `${" ".repeat(left)}${line}${" ".repeat(Math.max(0, width - left - visibleWidth(line)))}`;
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
return {
|
|
78
|
+
lines: screen,
|
|
79
|
+
bounds: { left, top, right: left + panelWidth, bottom: Math.min(height, top + panel.length) },
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function copyToClipboard(text: string): Promise<void> {
|
|
84
|
+
if (process.platform !== "darwin") throw new Error("Clipboard copy requires macOS.");
|
|
85
|
+
await writeClipboard(text);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function createModalComponent(
|
|
89
|
+
tui: TUI,
|
|
90
|
+
theme: Theme,
|
|
91
|
+
keybindings: KeybindingsManager,
|
|
92
|
+
done: () => void,
|
|
93
|
+
initial: ModalContent,
|
|
94
|
+
actions: ModalActions,
|
|
95
|
+
): Component & { dispose(): void } {
|
|
96
|
+
let content = initial;
|
|
97
|
+
let status: ModalRow | undefined;
|
|
98
|
+
let disposed = false;
|
|
99
|
+
let refreshGeneration = 0;
|
|
100
|
+
let bounds: { left: number; top: number; right: number; bottom: number } | undefined;
|
|
101
|
+
const selectionTui = tui as TUI & {
|
|
102
|
+
hasActiveSelection?: () => boolean;
|
|
103
|
+
copyActiveSelectionToClipboard?: () => Promise<boolean>;
|
|
104
|
+
};
|
|
105
|
+
const requestRender = () => {
|
|
106
|
+
if (!disposed) tui.requestRender();
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
dispose() {
|
|
111
|
+
disposed = true;
|
|
112
|
+
refreshGeneration++;
|
|
113
|
+
},
|
|
114
|
+
render(width: number) {
|
|
115
|
+
const hint = status ?? { text: "r refresh · c copy full summary · Esc/outside closes" };
|
|
116
|
+
const layout = modalScreen([...content.rows, { text: "" }, hint], width, tui.terminal.rows, theme);
|
|
117
|
+
bounds = layout.bounds;
|
|
118
|
+
return layout.lines;
|
|
119
|
+
},
|
|
120
|
+
invalidate() {},
|
|
121
|
+
handleMouse(event: TuiMouseEvent) {
|
|
122
|
+
const inside =
|
|
123
|
+
bounds && event.x >= bounds.left && event.x < bounds.right && event.y >= bounds.top && event.y < bounds.bottom;
|
|
124
|
+
if (event.type !== "click" || inside) return;
|
|
125
|
+
done();
|
|
126
|
+
return { handled: true, render: true };
|
|
127
|
+
},
|
|
128
|
+
handleInput(data: string) {
|
|
129
|
+
if (keybindings.matches(data, "app.interrupt") || keybindings.matches(data, "tui.input.submit")) {
|
|
130
|
+
done();
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const key = data.toLowerCase();
|
|
134
|
+
if (key === "r") {
|
|
135
|
+
const generation = ++refreshGeneration;
|
|
136
|
+
status = { text: "Refreshing usage…" };
|
|
137
|
+
requestRender();
|
|
138
|
+
void actions
|
|
139
|
+
.refresh()
|
|
140
|
+
.then((next) => {
|
|
141
|
+
if (disposed || generation !== refreshGeneration) return;
|
|
142
|
+
content = next;
|
|
143
|
+
status = undefined;
|
|
144
|
+
requestRender();
|
|
145
|
+
})
|
|
146
|
+
.catch((error: unknown) => {
|
|
147
|
+
if (disposed || generation !== refreshGeneration) return;
|
|
148
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
149
|
+
status = { text: `Refresh failed: ${message}`, style: "warning" };
|
|
150
|
+
requestRender();
|
|
151
|
+
});
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const copySelection = matchesKey(data, "super+c") || keybindings.matches(data, "tui.input.copy");
|
|
155
|
+
if (key === "c" || copySelection) {
|
|
156
|
+
if (copySelection && selectionTui.hasActiveSelection?.() && selectionTui.copyActiveSelectionToClipboard) {
|
|
157
|
+
status = { text: "Copying selection…" };
|
|
158
|
+
requestRender();
|
|
159
|
+
void selectionTui
|
|
160
|
+
.copyActiveSelectionToClipboard()
|
|
161
|
+
.then((copied) => {
|
|
162
|
+
if (disposed) return;
|
|
163
|
+
status = copied
|
|
164
|
+
? { text: "Copied selection", style: "success" }
|
|
165
|
+
: { text: "Copy failed", style: "warning" };
|
|
166
|
+
requestRender();
|
|
167
|
+
})
|
|
168
|
+
.catch((error: unknown) => {
|
|
169
|
+
if (disposed) return;
|
|
170
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
171
|
+
status = { text: `Copy failed: ${message}`, style: "warning" };
|
|
172
|
+
requestRender();
|
|
173
|
+
});
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
void (actions.copy ?? copyToClipboard)(content.clipboardText)
|
|
177
|
+
.then(() => {
|
|
178
|
+
if (disposed) return;
|
|
179
|
+
status = { text: "Copied plain text", style: "success" };
|
|
180
|
+
requestRender();
|
|
181
|
+
})
|
|
182
|
+
.catch((error: unknown) => {
|
|
183
|
+
if (disposed) return;
|
|
184
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
185
|
+
status = { text: `Copy failed: ${message}`, style: "warning" };
|
|
186
|
+
requestRender();
|
|
187
|
+
});
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
requestRender();
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export async function showModal(
|
|
196
|
+
ctx: ExtensionContext,
|
|
197
|
+
initial: ModalContent,
|
|
198
|
+
actions: ModalActions,
|
|
199
|
+
unavailableMessage: string,
|
|
200
|
+
): Promise<void> {
|
|
201
|
+
if (ctx.mode !== "tui") {
|
|
202
|
+
ctx.ui.notify(unavailableMessage, "warning");
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
try {
|
|
207
|
+
await ctx.ui.custom(
|
|
208
|
+
(tui, theme, keybindings, done) =>
|
|
209
|
+
createModalComponent(tui, theme, keybindings, () => done(undefined), initial, actions),
|
|
210
|
+
{
|
|
211
|
+
overlay: true,
|
|
212
|
+
overlayOptions: { anchor: "top-left", width: "100%", maxHeight: "100%", margin: 0 },
|
|
213
|
+
},
|
|
214
|
+
);
|
|
215
|
+
} catch (error) {
|
|
216
|
+
ctx.ui.notify(error instanceof Error ? error.message : String(error), "error");
|
|
217
|
+
}
|
|
218
|
+
}
|