@lyrumu/pi-diff 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.
@@ -0,0 +1,63 @@
1
+ import { Box, Text, truncateToWidth, visibleWidth, type Component } from "@earendil-works/pi-tui";
2
+ import type { Theme } from "@earendil-works/pi-coding-agent";
3
+ import { isTurnDiffData } from "../store.ts";
4
+ import { messages, type Language } from "../language.ts";
5
+ import { displayText } from "./text.ts";
6
+
7
+ export const MAX_SUMMARY_FILES = 12;
8
+
9
+ /** Render the persisted result, never current disk state or tool-operation totals. */
10
+ export function taskSummary(data: unknown, theme: Theme, language: Language = "en"): Component | undefined {
11
+ if (!isTurnDiffData(data)) return undefined;
12
+ const text = messages[data.summary?.language ?? language];
13
+ const title = `${text.taskChanges} · ${text.fileCount(data.fileCount)}${data.summary?.aborted ? ` · ${text.taskAborted}` : ""}`;
14
+ const exact = data.patches.every((file) => !file.baselineUnknown && !file.unavailable && !file.tooLarge && !file.binary);
15
+ const counts = (added: number, deleted: number) => `${theme.fg("success", `+${added}`)} ${theme.fg("error", `-${deleted}`)}`;
16
+ const total = data.fileCount && exact ? counts(
17
+ data.patches.reduce((sum, file) => sum + file.additions, 0),
18
+ data.patches.reduce((sum, file) => sum + file.deletions, 0),
19
+ ) : "";
20
+ const visiblePatches = data.patches.slice(0, MAX_SUMMARY_FILES);
21
+ const hiddenCount = Math.max(0, data.patches.length - visiblePatches.length);
22
+ const rows = visiblePatches.map((file): [string, string] => {
23
+ const color = file.status === "added" ? "success" : file.status === "deleted" ? "error" : "muted";
24
+ const unknown = file.unavailable || file.baselineUnknown;
25
+ const status = unknown ? "?" : file.status === "added" ? "A" : file.status === "deleted" ? "D" : "M";
26
+ const detail = unknown || file.tooLarge ? theme.fg("muted", "—") : file.binary ? theme.fg("muted", text.binary) : counts(file.additions, file.deletions);
27
+ const mode = file.oldMode && file.newMode && file.oldMode !== file.newMode ? ` · ${file.oldMode} → ${file.newMode}` : "";
28
+ return [`${theme.fg(color, status)} ${displayText(file.file)}${mode}`, detail];
29
+ });
30
+ if (hiddenCount > 0) rows.push([theme.fg("muted", text.moreFiles(hiddenCount)), ""]);
31
+ const empty = data.summary?.issues.length ? text.noChangesLimited : text.noChangesDetected;
32
+ const naturalWidth = Math.max(
33
+ visibleWidth(title) + (total ? visibleWidth(total) + 2 : 0),
34
+ visibleWidth(text.taskReview),
35
+ rows.length ? 0 : visibleWidth(empty),
36
+ ...rows.map(([left, right]) => visibleWidth(left) + (right ? visibleWidth(right) + 2 : 0)),
37
+ ) + 4;
38
+ return {
39
+ invalidate() {},
40
+ render(width) {
41
+ if (width < 8) return [truncateToWidth(title, Math.max(0, width), "")];
42
+ const inner = Math.min(width, naturalWidth) - 2;
43
+ const content = Math.max(1, inner - 2);
44
+ const pair = (left: string, right: string) => {
45
+ if (!right) return truncateToWidth(left, content);
46
+ if (visibleWidth(right) + 3 >= content) return `${left}\n${right}`;
47
+ const label = truncateToWidth(left, content - visibleWidth(right) - 2);
48
+ return label + " ".repeat(content - visibleWidth(label) - visibleWidth(right)) + right;
49
+ };
50
+ const header = new Box(1, 0, (line) => theme.bg("selectedBg", line));
51
+ header.addChild(new Text(pair(theme.bold(title), total), 0, 0));
52
+ const body = new Box(1, 0, (line) => theme.bg("customMessageBg", line));
53
+ if (!data.fileCount) body.addChild(new Text(theme.fg("muted", empty), 0, 0));
54
+ for (const [left, right] of rows) body.addChild(new Text(pair(left, right), 0, 0));
55
+ const footer = new Box(1, 0, (line) => theme.bg("customMessageBg", line));
56
+ footer.addChild(new Text(theme.fg("muted", text.taskReview), 0, 0));
57
+ const border = (left: string, right: string) => theme.fg("borderMuted", left + "─".repeat(inner) + right);
58
+ const sides = (line: string) => theme.fg("borderMuted", "│") + line + theme.fg("borderMuted", "│");
59
+ return [border("╭", "╮"), ...header.render(inner).map(sides), border("├", "┤"),
60
+ ...body.render(inner).map(sides), border("├", "┤"), ...footer.render(inner).map(sides), border("╰", "╯")];
61
+ },
62
+ };
63
+ }
package/src/tui/text.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { truncateToWidth, type TUI } from "@earendil-works/pi-tui";
1
+ import { backgroundAnsi, mixColors, truncateToWidth, type TUI } from "@earendil-works/pi-tui";
2
+ import type { Theme, ThemeBg, ThemeColor } from "@earendil-works/pi-coding-agent";
2
3
 
3
4
  export type TuiHost = Pick<TUI, "terminal" | "requestRender">;
4
5
 
@@ -13,3 +14,61 @@ export function displayText(text: string): string {
13
14
  }
14
15
 
15
16
  export const fit = (text: string, width: number) => truncateToWidth(text, Math.max(0, width), "", true);
17
+
18
+ export const fitEllipsis = (text: string, width: number) =>
19
+ width <= 1 ? fit(text, width) : truncateToWidth(text, width, "…", true);
20
+
21
+ /** Resolve an optional theme foreground token with a safe fallback for minimal/mock themes. */
22
+ export function safeFg(theme: Theme, token: ThemeColor, fallback: ThemeColor, text: string): string {
23
+ try {
24
+ return theme.fg(token, text);
25
+ } catch {
26
+ return theme.fg(fallback, text);
27
+ }
28
+ }
29
+
30
+ /** Resolve an optional theme background token, returning plain text if unavailable. */
31
+ export function safeBg(theme: Theme, token: ThemeBg, text: string): string {
32
+ try {
33
+ return theme.bg(token, text);
34
+ } catch {
35
+ return text;
36
+ }
37
+ }
38
+
39
+ export function safeFgAnsi(theme: Theme, token: ThemeColor, fallback?: ThemeColor): string {
40
+ if (typeof theme.getFgAnsi !== "function") return "";
41
+ try {
42
+ return theme.getFgAnsi(token);
43
+ } catch {
44
+ if (!fallback) return "";
45
+ try {
46
+ return theme.getFgAnsi(fallback);
47
+ } catch {
48
+ return "";
49
+ }
50
+ }
51
+ }
52
+
53
+ export function safeBgAnsi(theme: Theme, token: ThemeBg): string {
54
+ if (typeof theme.getBgAnsi !== "function") return "";
55
+ try {
56
+ return theme.getBgAnsi(token);
57
+ } catch {
58
+ return "";
59
+ }
60
+ }
61
+
62
+ /** Mix a diff row background toward its semantic accent to highlight intra-line changed spans. */
63
+ export function emphasisBgAnsi(theme: Theme, bgToken: ThemeBg, accentToken: ThemeColor): string {
64
+ try {
65
+ const bg = theme.colors?.[bgToken];
66
+ const accent = theme.colors?.[accentToken];
67
+ const mode = typeof theme.getColorMode === "function" ? theme.getColorMode() : undefined;
68
+ if (!bg || !accent || !mode) return "";
69
+ const ratio = theme.appearance === "light" ? 0.18 : 0.24;
70
+ return backgroundAnsi(mixColors(bg, accent, ratio, "oklch"), mode);
71
+ } catch {
72
+ return "";
73
+ }
74
+ }