@agnishc/edb-context-viewer 0.4.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/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +41 -0
- package/package.json +44 -0
- package/src/index.ts +225 -0
- package/src/scrollable-overlay.ts +273 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [Unreleased]
|
|
4
|
+
|
|
5
|
+
## [0.4.0] - 2026-04-30
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
- `/system-prompt-data` command — scrollable overlay showing the full system prompt with line numbers, search, and clipboard copy
|
|
9
|
+
- `/total-context-data` command — scrollable overlay showing the complete LLM context (system prompt + all messages) with search and clipboard copy
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Agnish Chakraborty
|
|
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
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# @agnishc/edb-context-viewer
|
|
2
|
+
|
|
3
|
+
Pi CLI extension for inspecting the LLM context. Two commands let you see exactly what the model sees:
|
|
4
|
+
|
|
5
|
+
- **`/system-prompt-data`** — full system prompt with line numbers
|
|
6
|
+
- **`/total-context-data`** — complete LLM context (system prompt + all messages + usage stats)
|
|
7
|
+
|
|
8
|
+
Both open as scrollable overlay popups with search and clipboard copy.
|
|
9
|
+
|
|
10
|
+
## Commands
|
|
11
|
+
|
|
12
|
+
| Command | What it shows |
|
|
13
|
+
|---------|--------------|
|
|
14
|
+
| `/system-prompt-data` | The full system prompt (includes tools, skills, guidelines, AGENTS.md, etc.) |
|
|
15
|
+
| `/total-context-data` | System prompt + all messages (user, assistant, tool calls/results) + context usage stats |
|
|
16
|
+
|
|
17
|
+
## Controls
|
|
18
|
+
|
|
19
|
+
| Key | Action |
|
|
20
|
+
|-----|--------|
|
|
21
|
+
| `↑` / `k` | Scroll up |
|
|
22
|
+
| `↓` / `j` | Scroll down |
|
|
23
|
+
| `Page Up` / `Ctrl+B` | Scroll up one page |
|
|
24
|
+
| `Page Down` / `Ctrl+F` | Scroll down one page |
|
|
25
|
+
| `Ctrl+U` / `Ctrl+D` | Scroll half page |
|
|
26
|
+
| `Home` / `g` | Jump to top |
|
|
27
|
+
| `End` / `G` | Jump to bottom |
|
|
28
|
+
| `/` | Search (live matching as you type) |
|
|
29
|
+
| `n` / `N` | Next / previous match |
|
|
30
|
+
| `y` | Copy full content to clipboard |
|
|
31
|
+
| `Escape` / `q` | Close popup |
|
|
32
|
+
|
|
33
|
+
## Install
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pi install npm:@agnishc/edb-context-viewer
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## License
|
|
40
|
+
|
|
41
|
+
[MIT](LICENSE) © Agnish Chakraborty
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@agnishc/edb-context-viewer",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Pi extension: inspect the system prompt and full LLM context in scrollable overlay popups",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"pi-package",
|
|
7
|
+
"pi-extension",
|
|
8
|
+
"edb"
|
|
9
|
+
],
|
|
10
|
+
"type": "module",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"author": "Agnish Chakraborty",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/agnishcc/pi-extention-monorepo.git",
|
|
16
|
+
"directory": "packages/edb-context-viewer"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://github.com/agnishcc/pi-extention-monorepo/tree/main/packages/edb-context-viewer#readme",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/agnishcc/pi-extention-monorepo/issues"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"test": "vitest run"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"src",
|
|
30
|
+
"!src/**/*.test.ts",
|
|
31
|
+
"README.md",
|
|
32
|
+
"LICENSE",
|
|
33
|
+
"CHANGELOG.md"
|
|
34
|
+
],
|
|
35
|
+
"pi": {
|
|
36
|
+
"extensions": [
|
|
37
|
+
"./src/index.ts"
|
|
38
|
+
]
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"@mariozechner/pi-coding-agent": "*",
|
|
42
|
+
"@mariozechner/pi-tui": "*"
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pi-context-viewer
|
|
3
|
+
*
|
|
4
|
+
* Two commands for inspecting what the LLM sees:
|
|
5
|
+
*
|
|
6
|
+
* /system-prompt-data — shows the full system prompt in a scrollable overlay
|
|
7
|
+
* /total-context-data — shows the complete LLM context (system prompt + all messages)
|
|
8
|
+
*
|
|
9
|
+
* Both overlays support: line numbers, scroll, live search (/), clipboard copy (y).
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
buildSessionContext,
|
|
14
|
+
type ContextUsage,
|
|
15
|
+
type ExtensionAPI,
|
|
16
|
+
type ExtensionCommandContext,
|
|
17
|
+
type SessionContext,
|
|
18
|
+
type Theme,
|
|
19
|
+
} from "@mariozechner/pi-coding-agent";
|
|
20
|
+
import { ScrollableOverlay } from "./scrollable-overlay";
|
|
21
|
+
|
|
22
|
+
// ── Helpers ────────────────────────────────────────────────────────────────────
|
|
23
|
+
|
|
24
|
+
/** Build display lines with line numbers from raw text. */
|
|
25
|
+
export function buildNumberedLines(text: string, theme: Theme): string[] {
|
|
26
|
+
const rawLines = text.split("\n");
|
|
27
|
+
const numWidth = String(rawLines.length).length;
|
|
28
|
+
return rawLines.map((line, i) => {
|
|
29
|
+
const num = String(i + 1).padStart(numWidth, " ");
|
|
30
|
+
return `${theme.fg("dim", num)} ${theme.fg("dim", "│")} ${line}`;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function formatImageBlock(block: any): string {
|
|
35
|
+
const label = block.mimeType ?? block.source?.type ?? "unknown";
|
|
36
|
+
return `[Image: ${label}]`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function formatContent(content: unknown): string[] {
|
|
40
|
+
if (typeof content === "string") return [content];
|
|
41
|
+
if (!Array.isArray(content)) return [];
|
|
42
|
+
|
|
43
|
+
const lines: string[] = [];
|
|
44
|
+
for (const block of content) {
|
|
45
|
+
if (!block || typeof block !== "object") {
|
|
46
|
+
lines.push(String(block));
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
switch ((block as any).type) {
|
|
51
|
+
case "text":
|
|
52
|
+
lines.push((block as any).text ?? "");
|
|
53
|
+
break;
|
|
54
|
+
case "thinking":
|
|
55
|
+
lines.push(`[Thinking: ${(block as any).thinking ?? ""}]`);
|
|
56
|
+
break;
|
|
57
|
+
case "toolCall":
|
|
58
|
+
lines.push(`[Tool Call: ${(block as any).name}(${JSON.stringify((block as any).arguments ?? {})})]`);
|
|
59
|
+
break;
|
|
60
|
+
case "image":
|
|
61
|
+
lines.push(formatImageBlock(block));
|
|
62
|
+
break;
|
|
63
|
+
default:
|
|
64
|
+
lines.push(`[${(block as any).type ?? "unknown"}]`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return lines;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function formatUsage(usage: any): string | undefined {
|
|
71
|
+
if (!usage) return undefined;
|
|
72
|
+
|
|
73
|
+
const parts: string[] = [];
|
|
74
|
+
if (usage.input != null) parts.push(`input: ${usage.input}`);
|
|
75
|
+
if (usage.output != null) parts.push(`output: ${usage.output}`);
|
|
76
|
+
if (usage.cacheRead != null) parts.push(`cache-read: ${usage.cacheRead}`);
|
|
77
|
+
if (usage.cacheWrite != null) parts.push(`cache-write: ${usage.cacheWrite}`);
|
|
78
|
+
if (usage.totalTokens != null) parts.push(`total: ${usage.totalTokens}`);
|
|
79
|
+
return parts.length > 0 ? `Tokens: ${parts.join(", ")}` : undefined;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function formatMessageForDisplay(message: SessionContext["messages"][number], index: number): string[] {
|
|
83
|
+
const msg = message as any;
|
|
84
|
+
const lines: string[] = ["", `──── Message ${index + 1} ────`, `Role: ${msg.role ?? "unknown"}`];
|
|
85
|
+
|
|
86
|
+
if (msg.role === "assistant") {
|
|
87
|
+
if (msg.provider || msg.model) lines.push(`Model: ${[msg.provider, msg.model].filter(Boolean).join("/")}`);
|
|
88
|
+
const usage = formatUsage(msg.usage);
|
|
89
|
+
if (usage) lines.push(usage);
|
|
90
|
+
if (msg.stopReason) lines.push(`Stop: ${msg.stopReason}`);
|
|
91
|
+
if (msg.errorMessage) lines.push(`Error: ${msg.errorMessage}`);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (msg.role === "toolResult") {
|
|
95
|
+
lines.push(`Tool: ${msg.toolName ?? "unknown"}`);
|
|
96
|
+
lines.push(`Tool Call ID: ${msg.toolCallId ?? "unknown"}`);
|
|
97
|
+
lines.push(`Error: ${msg.isError ? "yes" : "no"}`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
lines.push(...formatContent(msg.content));
|
|
101
|
+
return lines;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
interface ContextViewerModelInfo {
|
|
105
|
+
provider: string;
|
|
106
|
+
id: string;
|
|
107
|
+
contextWindow?: number;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function buildTotalContextText(
|
|
111
|
+
systemPrompt: string,
|
|
112
|
+
context: SessionContext,
|
|
113
|
+
usage: ContextUsage | undefined,
|
|
114
|
+
model: ContextViewerModelInfo | undefined,
|
|
115
|
+
): string {
|
|
116
|
+
const sections: string[] = [];
|
|
117
|
+
|
|
118
|
+
sections.push("═══════════════════════════════════════════════════════");
|
|
119
|
+
sections.push("SYSTEM PROMPT");
|
|
120
|
+
sections.push("═══════════════════════════════════════════════════════");
|
|
121
|
+
sections.push(systemPrompt);
|
|
122
|
+
sections.push("");
|
|
123
|
+
|
|
124
|
+
sections.push("═══════════════════════════════════════════════════════");
|
|
125
|
+
sections.push("MESSAGES");
|
|
126
|
+
sections.push("═══════════════════════════════════════════════════════");
|
|
127
|
+
|
|
128
|
+
if (context.messages.length > 0) {
|
|
129
|
+
for (let i = 0; i < context.messages.length; i++) {
|
|
130
|
+
sections.push(...formatMessageForDisplay(context.messages[i]!, i));
|
|
131
|
+
}
|
|
132
|
+
} else {
|
|
133
|
+
sections.push("(no messages yet)");
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
sections.push("");
|
|
137
|
+
sections.push("═══════════════════════════════════════════════════════");
|
|
138
|
+
sections.push("CONTEXT USAGE");
|
|
139
|
+
sections.push("═══════════════════════════════════════════════════════");
|
|
140
|
+
if (usage) {
|
|
141
|
+
sections.push(`Tokens: ${usage.tokens?.toLocaleString() ?? "unknown"}`);
|
|
142
|
+
if (model) {
|
|
143
|
+
sections.push(`Model: ${model.provider}/${model.id}`);
|
|
144
|
+
const contextWindow = model.contextWindow ?? usage.contextWindow;
|
|
145
|
+
if (contextWindow) {
|
|
146
|
+
const pct = usage.percent ?? (usage.tokens == null ? null : (usage.tokens / contextWindow) * 100);
|
|
147
|
+
sections.push(
|
|
148
|
+
`Context Window: ${usage.tokens?.toLocaleString() ?? "unknown"} / ${contextWindow.toLocaleString()} (${pct == null ? "unknown" : `${pct.toFixed(1)}%`})`,
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
} else {
|
|
153
|
+
sections.push("(no usage data available)");
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return sections.join("\n");
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Overlay options shared by both commands. */
|
|
160
|
+
const OVERLAY_OPTIONS = {
|
|
161
|
+
overlay: true,
|
|
162
|
+
overlayOptions: {
|
|
163
|
+
anchor: "center" as const,
|
|
164
|
+
width: "90%" as const,
|
|
165
|
+
minWidth: 60,
|
|
166
|
+
maxHeight: "80%" as const,
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
// ── Extension ──────────────────────────────────────────────────────────────────
|
|
171
|
+
|
|
172
|
+
export default function contextViewerExtension(pi: ExtensionAPI): void {
|
|
173
|
+
// ── /system-prompt-data ──
|
|
174
|
+
pi.registerCommand("system-prompt-data", {
|
|
175
|
+
description: "Show the full system prompt in a scrollable popup",
|
|
176
|
+
handler: async (_args: string, ctx: ExtensionCommandContext) => {
|
|
177
|
+
if (!ctx.hasUI) return;
|
|
178
|
+
|
|
179
|
+
const systemPrompt = ctx.getSystemPrompt();
|
|
180
|
+
if (!systemPrompt) {
|
|
181
|
+
ctx.ui.notify("No system prompt available yet. Send a message first.", "warning");
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const lineCount = systemPrompt.split("\n").length;
|
|
186
|
+
const charCount = systemPrompt.length;
|
|
187
|
+
|
|
188
|
+
await ctx.ui.custom<void>((_tui, theme, _keybindings, done) => {
|
|
189
|
+
const displayLines = buildNumberedLines(systemPrompt, theme);
|
|
190
|
+
return new ScrollableOverlay({
|
|
191
|
+
title: "System Prompt",
|
|
192
|
+
subtitle: `${charCount.toLocaleString()} chars · ${lineCount} lines`,
|
|
193
|
+
rawText: systemPrompt,
|
|
194
|
+
displayLines,
|
|
195
|
+
theme,
|
|
196
|
+
done,
|
|
197
|
+
});
|
|
198
|
+
}, OVERLAY_OPTIONS);
|
|
199
|
+
},
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
// ── /total-context-data ──
|
|
203
|
+
pi.registerCommand("total-context-data", {
|
|
204
|
+
description: "Show the complete LLM context (system prompt + all messages)",
|
|
205
|
+
handler: async (_args: string, ctx: ExtensionCommandContext) => {
|
|
206
|
+
if (!ctx.hasUI) return;
|
|
207
|
+
|
|
208
|
+
const systemPrompt = ctx.getSystemPrompt() ?? "";
|
|
209
|
+
const context = buildSessionContext(ctx.sessionManager.getEntries(), ctx.sessionManager.getLeafId());
|
|
210
|
+
const fullText = buildTotalContextText(systemPrompt, context, ctx.getContextUsage(), ctx.model);
|
|
211
|
+
|
|
212
|
+
await ctx.ui.custom<void>((_tui, theme, _keybindings, done) => {
|
|
213
|
+
const displayLines = buildNumberedLines(fullText, theme);
|
|
214
|
+
return new ScrollableOverlay({
|
|
215
|
+
title: "Total Context Data",
|
|
216
|
+
subtitle: `${fullText.length.toLocaleString()} chars · ${displayLines.length} lines`,
|
|
217
|
+
rawText: fullText,
|
|
218
|
+
displayLines,
|
|
219
|
+
theme,
|
|
220
|
+
done,
|
|
221
|
+
});
|
|
222
|
+
}, OVERLAY_OPTIONS);
|
|
223
|
+
},
|
|
224
|
+
});
|
|
225
|
+
}
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ScrollableOverlay — reusable scrollable text viewer rendered as a bordered overlay.
|
|
3
|
+
*
|
|
4
|
+
* Features:
|
|
5
|
+
* - Line numbers
|
|
6
|
+
* - Arrow / Page / Home / End / j-k scrolling
|
|
7
|
+
* - `/` search with live matching, `n`/`N` navigation
|
|
8
|
+
* - `y` copies the raw content to clipboard
|
|
9
|
+
* - `Escape` / `q` to close
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { copyToClipboard as copyTextToClipboard, type Theme } from "@mariozechner/pi-coding-agent";
|
|
13
|
+
import { Key, matchesKey, truncateToWidth, visibleWidth } from "@mariozechner/pi-tui";
|
|
14
|
+
|
|
15
|
+
const CONTENT_HEIGHT = 30;
|
|
16
|
+
|
|
17
|
+
export interface ScrollableOverlayOptions {
|
|
18
|
+
/** Title shown in the top border */
|
|
19
|
+
title: string;
|
|
20
|
+
/** Subtitle / stats line (e.g. "1,234 chars · 56 lines") */
|
|
21
|
+
subtitle: string;
|
|
22
|
+
/** The raw text to display (used for search & clipboard) */
|
|
23
|
+
rawText: string;
|
|
24
|
+
/** The styled display lines (with ANSI formatting, line numbers, etc.) */
|
|
25
|
+
displayLines: string[];
|
|
26
|
+
/** Active theme */
|
|
27
|
+
theme: Theme;
|
|
28
|
+
/** Called when the user closes the overlay */
|
|
29
|
+
done: () => void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export class ScrollableOverlay {
|
|
33
|
+
private scrollOffset = 0;
|
|
34
|
+
|
|
35
|
+
// Search state
|
|
36
|
+
private searchMode = false;
|
|
37
|
+
private searchQuery = "";
|
|
38
|
+
private searchMatches: number[] = [];
|
|
39
|
+
private currentMatchIndex = -1;
|
|
40
|
+
private copyFlash = false;
|
|
41
|
+
|
|
42
|
+
constructor(private opts: ScrollableOverlayOptions) {}
|
|
43
|
+
|
|
44
|
+
private get visibleLines(): number {
|
|
45
|
+
return CONTENT_HEIGHT;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
handleInput(data: string): void {
|
|
49
|
+
if (this.searchMode) {
|
|
50
|
+
if (matchesKey(data, Key.escape)) {
|
|
51
|
+
this.searchMode = false;
|
|
52
|
+
this.searchQuery = "";
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (matchesKey(data, Key.enter)) {
|
|
56
|
+
if (this.searchQuery.length > 0) {
|
|
57
|
+
this.findMatches();
|
|
58
|
+
if (this.searchMatches.length > 0) {
|
|
59
|
+
this.currentMatchIndex = 0;
|
|
60
|
+
this.scrollToMatch();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
this.searchMode = false;
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (matchesKey(data, Key.backspace)) {
|
|
67
|
+
this.searchQuery = this.searchQuery.slice(0, -1);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (data.length === 1 && data.charCodeAt(0) >= 32) {
|
|
71
|
+
this.searchQuery += data;
|
|
72
|
+
this.findMatches();
|
|
73
|
+
if (this.searchMatches.length > 0) {
|
|
74
|
+
this.currentMatchIndex = 0;
|
|
75
|
+
this.scrollToMatch();
|
|
76
|
+
}
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (matchesKey(data, Key.escape) || data === "q") {
|
|
83
|
+
this.opts.done();
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (matchesKey(data, Key.down) || data === "j") {
|
|
88
|
+
this.scrollDown(1);
|
|
89
|
+
} else if (matchesKey(data, Key.up) || data === "k") {
|
|
90
|
+
this.scrollUp(1);
|
|
91
|
+
} else if (matchesKey(data, Key.home) || data === "g") {
|
|
92
|
+
this.scrollOffset = 0;
|
|
93
|
+
} else if (matchesKey(data, Key.end) || data === "G") {
|
|
94
|
+
this.scrollToBottom();
|
|
95
|
+
} else if (matchesKey(data, Key.pageDown) || matchesKey(data, Key.ctrl("f"))) {
|
|
96
|
+
this.scrollDown(this.visibleLines - 2);
|
|
97
|
+
} else if (matchesKey(data, Key.pageUp) || matchesKey(data, Key.ctrl("b"))) {
|
|
98
|
+
this.scrollUp(this.visibleLines - 2);
|
|
99
|
+
} else if (matchesKey(data, Key.ctrl("d"))) {
|
|
100
|
+
this.scrollDown(Math.floor(this.visibleLines / 2));
|
|
101
|
+
} else if (matchesKey(data, Key.ctrl("u"))) {
|
|
102
|
+
this.scrollUp(Math.floor(this.visibleLines / 2));
|
|
103
|
+
} else if (data === "/") {
|
|
104
|
+
this.searchMode = true;
|
|
105
|
+
this.searchQuery = "";
|
|
106
|
+
this.searchMatches = [];
|
|
107
|
+
this.currentMatchIndex = -1;
|
|
108
|
+
} else if (data === "n") {
|
|
109
|
+
this.nextMatch();
|
|
110
|
+
} else if (data === "N") {
|
|
111
|
+
this.prevMatch();
|
|
112
|
+
} else if (data === "y") {
|
|
113
|
+
void this.copyToClipboard();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
private scrollDown(amount: number): void {
|
|
118
|
+
const maxOffset = Math.max(0, this.opts.displayLines.length - this.visibleLines);
|
|
119
|
+
this.scrollOffset = Math.min(this.scrollOffset + amount, maxOffset);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private scrollUp(amount: number): void {
|
|
123
|
+
this.scrollOffset = Math.max(0, this.scrollOffset - amount);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private scrollToBottom(): void {
|
|
127
|
+
this.scrollOffset = Math.max(0, this.opts.displayLines.length - this.visibleLines);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
private findMatches(): void {
|
|
131
|
+
const query = this.searchQuery.toLowerCase();
|
|
132
|
+
const rawLines = this.opts.rawText.split("\n");
|
|
133
|
+
this.searchMatches = [];
|
|
134
|
+
if (query.length === 0) {
|
|
135
|
+
this.currentMatchIndex = -1;
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
for (let i = 0; i < rawLines.length; i++) {
|
|
139
|
+
if (rawLines[i]!.toLowerCase().includes(query)) {
|
|
140
|
+
this.searchMatches.push(i);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private scrollToMatch(): void {
|
|
146
|
+
if (this.currentMatchIndex >= 0 && this.currentMatchIndex < this.searchMatches.length) {
|
|
147
|
+
const targetLine = this.searchMatches[this.currentMatchIndex]!;
|
|
148
|
+
if (targetLine < this.scrollOffset || targetLine >= this.scrollOffset + this.visibleLines) {
|
|
149
|
+
this.scrollOffset = Math.max(0, targetLine - Math.floor(this.visibleLines / 3));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
private nextMatch(): void {
|
|
155
|
+
if (this.searchMatches.length === 0) return;
|
|
156
|
+
this.currentMatchIndex = (this.currentMatchIndex + 1) % this.searchMatches.length;
|
|
157
|
+
this.scrollToMatch();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
private prevMatch(): void {
|
|
161
|
+
if (this.searchMatches.length === 0) return;
|
|
162
|
+
this.currentMatchIndex = (this.currentMatchIndex - 1 + this.searchMatches.length) % this.searchMatches.length;
|
|
163
|
+
this.scrollToMatch();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private async copyToClipboard(): Promise<void> {
|
|
167
|
+
this.copyFlash = true;
|
|
168
|
+
try {
|
|
169
|
+
await copyTextToClipboard(this.opts.rawText);
|
|
170
|
+
} catch {
|
|
171
|
+
// Silently fail if clipboard tools aren't available
|
|
172
|
+
}
|
|
173
|
+
setTimeout(() => {
|
|
174
|
+
this.copyFlash = false;
|
|
175
|
+
}, 1500);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
render(width: number): string[] {
|
|
179
|
+
const th = this.opts.theme;
|
|
180
|
+
const innerW = width - 2;
|
|
181
|
+
const lines: string[] = [];
|
|
182
|
+
|
|
183
|
+
const pad = (s: string, len: number) => {
|
|
184
|
+
const vis = visibleWidth(s);
|
|
185
|
+
return s + " ".repeat(Math.max(0, len - vis));
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
const row = (content: string) => th.fg("border", "│") + pad(content, innerW) + th.fg("border", "│");
|
|
189
|
+
const borderTop = th.fg("border", `╭${"─".repeat(innerW)}╮`);
|
|
190
|
+
const borderSep = th.fg("border", `├${"─".repeat(innerW)}┤`);
|
|
191
|
+
const borderBottom = th.fg("border", `╰${"─".repeat(innerW)}╯`);
|
|
192
|
+
|
|
193
|
+
// ── Title bar ──
|
|
194
|
+
const title = ` ${th.fg("accent", th.bold(this.opts.title))} ${th.fg("dim", `(${this.opts.subtitle})`)}`;
|
|
195
|
+
lines.push(borderTop);
|
|
196
|
+
lines.push(row(title));
|
|
197
|
+
|
|
198
|
+
// ── Search bar or separator ──
|
|
199
|
+
if (this.searchMode) {
|
|
200
|
+
const searchContent = ` ${th.fg("accent", "/")} ${this.searchQuery}${th.fg("dim", "▏")}`;
|
|
201
|
+
lines.push(row(searchContent));
|
|
202
|
+
} else if (this.searchMatches.length > 0) {
|
|
203
|
+
const matchInfo = ` ${th.fg("accent", "/")} ${th.fg("text", this.searchQuery)} ${th.fg("dim", "—")} ${th.fg("accent", `${this.currentMatchIndex + 1}/${this.searchMatches.length}`)}`;
|
|
204
|
+
lines.push(row(matchInfo));
|
|
205
|
+
} else {
|
|
206
|
+
lines.push(borderSep);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// ── Content lines ──
|
|
210
|
+
const maxScroll = Math.max(0, this.opts.displayLines.length - this.visibleLines);
|
|
211
|
+
this.scrollOffset = Math.min(this.scrollOffset, maxScroll);
|
|
212
|
+
this.scrollOffset = Math.max(0, this.scrollOffset);
|
|
213
|
+
|
|
214
|
+
for (let i = 0; i < this.visibleLines; i++) {
|
|
215
|
+
const lineIdx = this.scrollOffset + i;
|
|
216
|
+
if (lineIdx < this.opts.displayLines.length) {
|
|
217
|
+
let line = this.opts.displayLines[lineIdx]!;
|
|
218
|
+
|
|
219
|
+
const isCurrentMatch =
|
|
220
|
+
this.searchMatches.length > 0 &&
|
|
221
|
+
this.currentMatchIndex >= 0 &&
|
|
222
|
+
this.searchMatches[this.currentMatchIndex] === lineIdx;
|
|
223
|
+
const isOtherMatch =
|
|
224
|
+
this.searchMatches.length > 0 && this.searchMatches.includes(lineIdx) && !isCurrentMatch;
|
|
225
|
+
|
|
226
|
+
if (isCurrentMatch) {
|
|
227
|
+
line = th.bg("selectedBg", truncateToWidth(line, innerW));
|
|
228
|
+
} else if (isOtherMatch) {
|
|
229
|
+
line = th.fg("warning", truncateToWidth(line, innerW));
|
|
230
|
+
} else {
|
|
231
|
+
line = truncateToWidth(line, innerW);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
lines.push(row(line));
|
|
235
|
+
} else {
|
|
236
|
+
lines.push(row(th.fg("dim", "~")));
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// ── Footer ──
|
|
241
|
+
lines.push(borderSep);
|
|
242
|
+
|
|
243
|
+
const scrollPercent =
|
|
244
|
+
this.opts.displayLines.length <= this.visibleLines
|
|
245
|
+
? "All"
|
|
246
|
+
: this.scrollOffset === 0
|
|
247
|
+
? "Top"
|
|
248
|
+
: this.scrollOffset >= maxScroll
|
|
249
|
+
? "Bot"
|
|
250
|
+
: `${Math.round(((this.scrollOffset + this.visibleLines) / this.opts.displayLines.length) * 100)}%`;
|
|
251
|
+
|
|
252
|
+
let statusLeft = th.fg(
|
|
253
|
+
"dim",
|
|
254
|
+
` ${this.scrollOffset + 1}-${Math.min(this.scrollOffset + this.visibleLines, this.opts.displayLines.length)} of ${this.opts.displayLines.length} [${scrollPercent}] `,
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
if (this.copyFlash) {
|
|
258
|
+
statusLeft += th.fg("success", "✓ Copied! ");
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const helpItems = ["↑↓ scroll", "/ search", "n/N next", "y copy", "q close"];
|
|
262
|
+
const helpText = th.fg("dim", helpItems.join(" · "));
|
|
263
|
+
|
|
264
|
+
lines.push(row(statusLeft + helpText));
|
|
265
|
+
lines.push(borderBottom);
|
|
266
|
+
|
|
267
|
+
return lines;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
invalidate(): void {
|
|
271
|
+
// No cached state to clear — rebuilds from opts every render
|
|
272
|
+
}
|
|
273
|
+
}
|