@@ -1,38 +1,345 @@
1-// Panel for displaying the result of the last action
1+// Panel for displaying the result of the last action with enhanced visuals
22
3-import { PALETTE, THEME } from "../theme";
3+import type { ActionHistoryEntry } from "../../types";
4+import { PALETTE, SYMBOLS, THEME } from "../theme";
45 import { applyBold, applyColor } from "../widgets";
56
6-export function renderActionResultPanel(
7- lastActionResult: string | null | undefined,
7+type ActionCategory = "navigation" | "resource" | "combat" | "info" | "social";
8+
9+/**
10+ * Strip blessed color tags to measure visual width
11+ * Example: "{red-fg}hello{/red-fg}" -> "hello"
12+ */
13+function stripBlessedTags(text: string): string {
14+ return text.replace(/\{[^}]*\}/g, "");
15+}
16+
17+/**
18+ * Calculate visual width (actual displayed characters)
19+ */
20+function getVisualWidth(text: string): number {
21+ return stripBlessedTags(text).length;
22+}
23+
24+/**
25+ * Pad text to target width by adding spaces on right
26+ * Preserves blessed tags but ensures visual width matches target
27+ */
28+function padToWidth(text: string, targetWidth: number): string {
29+ const visualWidth = getVisualWidth(text);
30+ const spacesNeeded = Math.max(0, targetWidth - visualWidth);
31+ return text + " ".repeat(spacesNeeded);
32+}
33+
34+/**
35+ * Create a content line with colored borders
36+ * Pads content first, then adds colored borders
37+ */
38+function createBorderedLine(
39+ content: string,
40+ contentWidth: number,
41+ borderColor: string,
42+ indent: string,
843 ): string {
9- if (!lastActionResult || !lastActionResult.trim()) {
10- return applyColor("No recent actions.", THEME.INACTIVE);
44+ const paddedContent = padToWidth(content, contentWidth);
45+ return (
46+ indent +
47+ applyColor("║", borderColor) +
48+ paddedContent +
49+ applyColor("║", borderColor)
50+ );
51+}
52+
53+function detectActionCategory(actionLine: string): ActionCategory {
54+ const lower = actionLine.toLowerCase();
55+
56+ if (
57+ lower.includes("travel") ||
58+ lower.includes("jump") ||
59+ lower.includes("dock") ||
60+ lower.includes("undock") ||
61+ lower.includes("arrived") ||
62+ lower.includes("jumped")
63+ ) {
64+ return "navigation";
1165 }
1266
67+ if (
68+ lower.includes("mine") ||
69+ lower.includes("buy") ||
70+ lower.includes("sell") ||
71+ lower.includes("refuel") ||
72+ lower.includes("repair")
73+ ) {
74+ return "resource";
75+ }
76+
77+ if (
78+ lower.includes("attack") ||
79+ lower.includes("scan") ||
80+ lower.includes("combat")
81+ ) {
82+ return "combat";
83+ }
84+
85+ if (
86+ lower.includes("forum") ||
87+ lower.includes("chat") ||
88+ lower.includes("say") ||
89+ lower.includes("faction") ||
90+ lower.includes("msg")
91+ ) {
92+ return "social";
93+ }
94+
95+ return "info";
96+}
97+
98+function getCategoryLabel(category: ActionCategory): string {
99+ switch (category) {
100+ case "navigation":
101+ return "NAVIGATION";
102+ case "resource":
103+ return "TRADE";
104+ case "combat":
105+ return "COMBAT";
106+ case "social":
107+ return "SOCIAL";
108+ case "info":
109+ return "STATUS";
110+ }
111+}
112+
113+function getCategoryBlock(category: ActionCategory): string {
114+ switch (category) {
115+ case "navigation":
116+ return SYMBOLS.NAV_BLOCK;
117+ case "resource":
118+ return SYMBOLS.RESOURCE_BLOCK;
119+ case "combat":
120+ return SYMBOLS.COMBAT_BLOCK;
121+ case "social":
122+ return SYMBOLS.INFO_BLOCK;
123+ case "info":
124+ return SYMBOLS.INFO_BLOCK;
125+ }
126+}
127+
128+function getCategoryColor(category: ActionCategory): string {
129+ switch (category) {
130+ case "navigation":
131+ return PALETTE.AMBER;
132+ case "resource":
133+ return PALETTE.MOSS;
134+ case "combat":
135+ return PALETTE.BRICK;
136+ case "social":
137+ return PALETTE.ORANGE;
138+ case "info":
139+ return PALETTE.WHITE;
140+ }
141+}
142+
143+function hasError(lines: string[]): boolean {
144+ return lines.some((line) => line.trim().startsWith("- ERROR"));
145+}
146+
147+function buildFrame(
148+ category: ActionCategory,
149+ hasError: boolean,
150+ panelWidth: number,
151+): { top: string; bottom: string; contentWidth: number; indent: string } {
152+ const block = getCategoryBlock(category);
153+ const label = getCategoryLabel(category);
154+ const color = hasError ? PALETTE.BRICK : getCategoryColor(category);
155+
156+ // Calculate frame width with padding from panel borders
157+ // panelWidth includes blessed borders (2 chars) + scrollbar (1 char when active)
158+ // We need: left padding (1) + our borders (2) + content + scrollbar (1) + blessed border (implied)
159+ const BLESSED_BORDERS = 2;
160+ const SCROLLBAR_WIDTH = 1;
161+ const LEFT_PADDING = 1;
162+ const RIGHT_MARGIN = 2; // Extra safety margin
163+ const frameWidth = Math.max(
164+ 20,
165+ panelWidth -
166+ BLESSED_BORDERS -
167+ SCROLLBAR_WIDTH -
168+ LEFT_PADDING -
169+ RIGHT_MARGIN,
170+ );
171+
172+ // Content width is what's available inside our custom borders
173+ const contentWidth = frameWidth - 2; // Minus our ║║ borders
174+ const indent = " "; // 1 space indent for left padding
175+
176+ // Top: ╔═══ LABEL ═══...╗
177+ const labelPart = ` ${label} `;
178+ const blockCount = 3;
179+ const leftBlocks = block.repeat(blockCount);
180+
181+ // Calculate remaining space for right blocks
182+ const innerWidth = frameWidth - 2; // Space between ╔ and ╗
183+ const usedWidth = blockCount + labelPart.length; // Left blocks + label
184+ const remainingWidth = Math.max(0, innerWidth - usedWidth); // Space for right blocks
185+ const rightBlocks = block.repeat(remainingWidth);
186+
187+ const topLine = `╔${leftBlocks}${labelPart}${rightBlocks}╗`;
188+ const bottomLine = `╚${"═".repeat(frameWidth - 2)}╝`;
189+
190+ return {
191+ top: applyColor(topLine, color),
192+ bottom: applyColor(bottomLine, color),
193+ contentWidth,
194+ indent,
195+ };
196+}
197+
198+function renderSingleResult(
199+ result: string,
200+ category: ActionCategory,
201+ hasErr: boolean,
202+ panelWidth: number,
203+ isLatest = false,
204+): string[] {
13205 const lines: string[] = [];
14- const raw = lastActionResult.trim();
206+ const frame = buildFrame(category, hasErr, panelWidth);
207+ const { contentWidth, indent } = frame;
208+ const borderColor = hasErr ? PALETTE.BRICK : getCategoryColor(category);
15209
16- // Try to parse standard format "Action: ... \n Result: ..."
17- // If it matches, we can style it better.
210+ // Add top border with indent
211+ lines.push(indent + frame.top);
18212
19- const parts = raw.split("\n");
213+ // Add top padding (blank line WITH borders)
214+ lines.push(createBorderedLine("", contentWidth, borderColor, indent));
20215
216+ // Process content lines
217+ const parts = result.trim().split("\n");
21218 for (const line of parts) {
22219 if (line.startsWith("Action:")) {
23- // Highlight the action header
24- lines.push(applyColor(applyBold(line), PALETTE.AMBER));
220+ // Action header - extract just the action part
221+ const actionText = line.replace(/^Action:\s*/, "");
222+ const symbol = hasErr ? SYMBOLS.ERROR : SYMBOLS.SUCCESS;
223+ const symbolColor = hasErr ? PALETTE.BRICK : PALETTE.MOSS;
224+ const latestBadge = isLatest ? " [LATEST]" : "";
225+
226+ // Calculate available space: " ✓ " = 5 chars, latestBadge = 9 chars (if present)
227+ const prefixLen = 5; // " ✓ "
228+ const badgeLen = latestBadge.length;
229+ const breathingRoom = 2; // Minimum spaces before closing ║
230+ const maxActionLen = contentWidth - prefixLen - badgeLen - breathingRoom;
231+
232+ // Truncate actionText if needed (subtract 3 for "...")
233+ const truncatedAction =
234+ actionText.length > maxActionLen
235+ ? actionText.substring(0, maxActionLen - 3) + "..."
236+ : actionText;
237+
238+ const content = ` ${applyColor(symbol, symbolColor)} ${applyColor(truncatedAction, THEME.VALUE)}${isLatest ? applyColor(applyBold(latestBadge), PALETTE.AMBER) : ""}`;
239+ lines.push(
240+ createBorderedLine(content, contentWidth, borderColor, indent),
241+ );
25242 } else if (line.startsWith("Result:")) {
26- lines.push(applyColor(line, THEME.LABEL));
243+ // Skip "Result:" header
27244 } else if (line.trim().startsWith("- ERROR")) {
28- lines.push(applyColor(line, PALETTE.BRICK));
29- } else if (line.trim().startsWith("-")) {
245+ const errorText = line.replace(/^\s*-\s*ERROR:?\s*/, "").trim();
246+ const prefixLen = 5; // " "
247+ const maxLen = contentWidth - prefixLen;
248+ const truncated =
249+ errorText.length > maxLen
250+ ? errorText.substring(0, maxLen - 3) + "..."
251+ : errorText;
252+ const content = ` ${applyColor(truncated, PALETTE.BRICK)}`;
253+ lines.push(
254+ createBorderedLine(content, contentWidth, borderColor, indent),
255+ );
256+ } else if (line.trim().startsWith("- ")) {
30257 // Bullet points
31- lines.push(applyColor(line, PALETTE.WHITE));
32- } else {
33- lines.push(applyColor(line, THEME.VALUE));
258+ const bulletText = line.replace(/^\s*-\s*/, "");
259+ const prefixLen = 5; // " "
260+ const maxLen = contentWidth - prefixLen;
261+ const truncated =
262+ bulletText.length > maxLen
263+ ? bulletText.substring(0, maxLen - 3) + "..."
264+ : bulletText;
265+ const content = ` ${applyColor(truncated, THEME.VALUE)}`;
266+ lines.push(
267+ createBorderedLine(content, contentWidth, borderColor, indent),
268+ );
269+ } else if (line.trim().startsWith("->")) {
270+ // Solution/guidance
271+ const guideText = line.replace(/^\s*->\s*/, "");
272+ // Visual layout: " → text" = 5 spaces + arrow + space + text
273+ // We build: " " + applyColor("→ " + text)
274+ // So: contentWidth = 5 + 1 + 1 + text.length
275+ const INDENT = 5; // " "
276+ const ARROW_AND_SPACE = 2; // "→ "
277+ const maxTextLen = contentWidth - INDENT - ARROW_AND_SPACE;
278+ const truncated =
279+ guideText.length > maxTextLen
280+ ? guideText.substring(0, maxTextLen - 3) + "..."
281+ : guideText;
282+ const content = ` ${applyColor(`→ ${truncated}`, PALETTE.MOSS)}`;
283+ lines.push(
284+ createBorderedLine(content, contentWidth, borderColor, indent),
285+ );
286+ } else if (line.trim()) {
287+ // Other content
288+ const text = line.trim();
289+ const prefixLen = 5; // " "
290+ const maxLen = contentWidth - prefixLen;
291+ const truncated =
292+ text.length > maxLen ? text.substring(0, maxLen - 3) + "..." : text;
293+ const content = ` ${applyColor(truncated, THEME.VALUE)}`;
294+ lines.push(
295+ createBorderedLine(content, contentWidth, borderColor, indent),
296+ );
297+ }
298+ }
299+
300+ // Add bottom padding (blank line WITH borders)
301+ lines.push(createBorderedLine("", contentWidth, borderColor, indent));
302+
303+ // Add bottom border with indent
304+ lines.push(indent + frame.bottom);
305+
306+ return lines;
307+}
308+
309+export function renderActionResultPanel(
310+ history: ActionHistoryEntry[],
311+ panelWidth: number,
312+): string {
313+ if (!history || history.length === 0) {
314+ return applyColor("No recent actions.", THEME.INACTIVE);
315+ }
316+
317+ const allLines: string[] = [];
318+
319+ // Render each action in history (most recent first)
320+ for (let i = 0; i < history.length; i++) {
321+ const entry = history[i];
322+ const parts = entry.result.trim().split("\n");
323+ const actionLine = parts.find((line) => line.startsWith("Action:")) || "";
324+ const category = detectActionCategory(actionLine);
325+ const hasErr = hasError(parts);
326+ const isLatest = i === 0;
327+
328+ const resultLines = renderSingleResult(
329+ entry.result,
330+ category,
331+ hasErr,
332+ panelWidth,
333+ isLatest,
334+ );
335+
336+ allLines.push(...resultLines);
337+
338+ // Add single blank line between entries (except after last one)
339+ if (i < history.length - 1) {
340+ allLines.push("");
34341 }
35342 }
36343
37- return lines.join("\n");
344+ return allLines.join("\n");
38345 }