@lune_99/pi-core-theme 0.1.0 → 0.2.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/src/ui/preview.ts DELETED
@@ -1,195 +0,0 @@
1
- import {
2
- copyToClipboard,
3
- getMarkdownTheme,
4
- type ExtensionContext,
5
- } from "@earendil-works/pi-coding-agent";
6
- import { transformMarkdown } from "../renderer/markdown-enhance.ts";
7
- import {
8
- Key,
9
- Markdown,
10
- matchesKey,
11
- truncateToWidth,
12
- visibleWidth,
13
- type TuiMouseEvent,
14
- } from "@earendil-works/pi-tui";
15
-
16
- export interface InspectorItem {
17
- label: string;
18
- content: string;
19
- detail?: string;
20
- }
21
- const MAX_PREVIEW_CHARACTERS = 512_000;
22
- function plain(value: string): string {
23
- return value
24
- .replace(/\r\n?/g, "\n")
25
- .replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/g, "");
26
- }
27
- const pad = (value: string, width: number) => {
28
- const text = truncateToWidth(value, Math.max(0, width), "…");
29
- return text + " ".repeat(Math.max(0, width - visibleWidth(text)));
30
- };
31
-
32
- /** One inspector surface serves tool previews and context categories without stacked custom screens. */
33
- export async function showInspector(
34
- ctx: Pick<ExtensionContext, "ui">,
35
- title: string,
36
- items: readonly InspectorItem[],
37
- subtitle = "",
38
- ) {
39
- if (!items.length) return;
40
- await ctx.ui.custom<void>(
41
- (tui, theme, _keys, done) => {
42
- let selected = 0;
43
- let offset = 0;
44
- let pageSize = 1;
45
- let total = 0;
46
- let sidebar = 0;
47
- let width = 0;
48
- let copyHovered = false;
49
- let closeHovered = false;
50
- let markdown: Markdown | undefined;
51
- let cacheKey = "";
52
- let lines: string[] = [];
53
- const select = (index: number) => {
54
- selected = (index + items.length) % items.length;
55
- offset = 0;
56
- markdown = undefined;
57
- tui.requestRender();
58
- };
59
- const scroll = (amount: number) => {
60
- offset = Math.max(0, Math.min(Math.max(0, total - pageSize), offset + amount));
61
- tui.requestRender();
62
- };
63
- const copy = () => {
64
- void copyToClipboard(items[selected]!.content).then(
65
- () => ctx.ui.notify("Copied preview", "info"),
66
- (error) => ctx.ui.notify(String(error), "error"),
67
- );
68
- };
69
- return {
70
- invalidate() {
71
- cacheKey = "";
72
- markdown = undefined;
73
- },
74
- handleInput(data) {
75
- if (matchesKey(data, Key.escape)) {
76
- done();
77
- return;
78
- }
79
- if (matchesKey(data, Key.tab) || matchesKey(data, Key.right))
80
- select(selected + 1);
81
- else if (matchesKey(data, Key.shift("tab")) || matchesKey(data, Key.left))
82
- select(selected - 1);
83
- else if (matchesKey(data, Key.down)) scroll(1);
84
- else if (matchesKey(data, Key.up)) scroll(-1);
85
- else if (matchesKey(data, Key.pageDown)) scroll(pageSize);
86
- else if (matchesKey(data, Key.pageUp)) scroll(-pageSize);
87
- else if (matchesKey(data, Key.home)) scroll(-total);
88
- else if (matchesKey(data, Key.end)) scroll(total);
89
- else if (data === "c" || matchesKey(data, Key.ctrl("c"))) copy();
90
- },
91
- handleMouse(event: TuiMouseEvent) {
92
- if (event.type === "wheel") {
93
- scroll(event.wheelDelta ?? 0);
94
- return { handled: true };
95
- }
96
- if (event.type === "move") {
97
- const nextClose = event.y === 1 && event.x >= width - 8;
98
- const nextCopy =
99
- event.y === 1 && event.x >= width - 17 && event.x < width - 8;
100
- const changed = nextClose !== closeHovered || nextCopy !== copyHovered;
101
- closeHovered = nextClose;
102
- copyHovered = nextCopy;
103
- return { handled: true, render: changed };
104
- }
105
- if (event.type !== "click" || event.button !== "left") return undefined;
106
- if (event.y === 1 && event.x >= width - 8) done();
107
- else if (event.y === 1 && event.x >= width - 17) copy();
108
- else if (
109
- sidebar &&
110
- event.x < sidebar &&
111
- event.y >= 4 &&
112
- event.y < 4 + items.length
113
- )
114
- select(event.y - 4);
115
- return { handled: true };
116
- },
117
- render(available) {
118
- width = available;
119
- const height = Math.max(7, Math.min(40, tui.terminal.rows - 4));
120
- const inner = Math.max(1, width - 4);
121
- sidebar =
122
- items.length > 1 && width >= 72 ? Math.min(26, Math.floor(width / 4)) : 0;
123
- const bodyWidth = Math.max(1, inner - (sidebar ? sidebar + 2 : 0));
124
- pageSize = Math.max(1, height - 7);
125
- const item = items[selected]!;
126
- const key = `${selected}:${bodyWidth}`;
127
- if (!markdown || key !== cacheKey) {
128
- let content = plain(item.content);
129
- if (content.length > MAX_PREVIEW_CHARACTERS)
130
- content =
131
- content.slice(0, MAX_PREVIEW_CHARACTERS) +
132
- "\n\n*Preview truncated; copy retains the complete content.*";
133
- markdown = new Markdown(content, 0, 0, getMarkdownTheme(), undefined, {
134
- transform: (value, availableWidth) =>
135
- transformMarkdown(value, {
136
- messageType: "assistant",
137
- isStreaming: false,
138
- availableWidth,
139
- }),
140
- });
141
- lines = markdown.render(bodyWidth);
142
- total = lines.length;
143
- cacheKey = key;
144
- }
145
- offset = Math.min(offset, Math.max(0, total - pageSize));
146
- const border = (text: string) => theme.fg("borderMuted", text);
147
- const row = (text: string) => border("│ ") + pad(text, inner) + border(" │");
148
- const actions = `${theme.fg(copyHovered ? "accent" : "muted", "[copy]")} ${theme.fg(closeHovered ? "accent" : "dim", "[esc]")}`;
149
- const heading = theme.bold(theme.fg("accent", title));
150
- const header =
151
- pad(heading, Math.max(0, inner - visibleWidth(actions) - 1)) +
152
- " " +
153
- actions;
154
- const description =
155
- subtitle || `${item.label}${item.detail ? ` · ${item.detail}` : ""}`;
156
- const tabs = items
157
- .map((entry, index) =>
158
- theme.fg(index === selected ? "accent" : "dim", entry.label),
159
- )
160
- .join(" · ");
161
- const body = Array.from({ length: pageSize }, (_, index) => {
162
- const line = lines[offset + index] ?? "";
163
- if (!sidebar) return row(line);
164
- const entry = items[index];
165
- const label = entry
166
- ? `${index === selected ? "›" : " "} ${entry.label}`
167
- : "";
168
- const nav = theme.fg(
169
- index === selected ? "accent" : "muted",
170
- pad(label, sidebar),
171
- );
172
- return row(`${nav}${border("│ ")}${line}`);
173
- });
174
- const range = total
175
- ? `${offset + 1}–${Math.min(total, offset + pageSize)} / ${total}`
176
- : "0 / 0";
177
- return [
178
- border(`╭${"─".repeat(Math.max(0, width - 2))}╮`),
179
- row(header),
180
- row(theme.fg("muted", description)),
181
- row(sidebar ? "" : tabs),
182
- ...body,
183
- row(theme.fg("dim", `Tab categories · ↑↓ scroll · c copy ${range}`)),
184
- border(`╰${"─".repeat(Math.max(0, width - 2))}╯`),
185
- ].map((line) => truncateToWidth(line, width, ""));
186
- },
187
- };
188
- },
189
- { overlay: true, overlayOptions: { width: "94%", maxHeight: "94%", anchor: "center" } },
190
- );
191
- }
192
-
193
- export function showTextPreview(ctx: Pick<ExtensionContext, "ui">, title: string, content: string) {
194
- return showInspector(ctx, title, [{ label: "Preview", content }]);
195
- }