@bismawy/pi-arnative 0.16.1 → 0.18.1

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.
Files changed (61) hide show
  1. package/README.md +25 -16
  2. package/extensions/index.ts +41 -0
  3. package/lib/active-clock.ts +91 -0
  4. package/lib/ansi.ts +40 -78
  5. package/lib/box.ts +49 -68
  6. package/lib/chatbox.ts +371 -0
  7. package/lib/color.ts +3 -31
  8. package/lib/compat.ts +0 -60
  9. package/lib/edit-diff.ts +199 -0
  10. package/{extensions → lib/features}/arnative.ts +96 -423
  11. package/{extensions/autocompact.ts → lib/features/auto-compact.ts} +30 -327
  12. package/{extensions → lib/features}/budget.ts +17 -242
  13. package/lib/features/footer.ts +2232 -0
  14. package/{extensions/section-headers.ts → lib/features/headers.ts} +38 -49
  15. package/{extensions → lib/features}/model-heal.ts +5 -28
  16. package/lib/features/progress-group.ts +1691 -0
  17. package/{extensions → lib/features}/render-cache.ts +391 -218
  18. package/{extensions → lib/features}/timestamps.ts +18 -94
  19. package/{extensions → lib/features}/todos.ts +44 -158
  20. package/lib/features/tools.ts +1202 -0
  21. package/lib/features/ui-render-tweaks.ts +606 -0
  22. package/lib/features/usage.ts +786 -0
  23. package/lib/format.ts +22 -4
  24. package/lib/git-status.ts +164 -0
  25. package/lib/list-picker.ts +1 -127
  26. package/lib/model-heal.ts +70 -162
  27. package/lib/patch.ts +0 -63
  28. package/lib/preset-store.ts +17 -133
  29. package/lib/provider-store.ts +573 -0
  30. package/lib/provider.ts +52 -851
  31. package/lib/runtime.ts +209 -0
  32. package/lib/shortcuts.ts +8 -58
  33. package/lib/spinner.ts +88 -162
  34. package/lib/status-widget.ts +4 -30
  35. package/lib/todos.ts +20 -143
  36. package/lib/usage-pricing.ts +143 -0
  37. package/lib/usage-store.ts +44 -378
  38. package/package.json +48 -49
  39. package/themes/arnative-blossom.json +0 -0
  40. package/themes/arnative-crimson.json +0 -0
  41. package/themes/arnative-cyberpunk.json +0 -0
  42. package/themes/arnative-dracula.json +0 -0
  43. package/themes/arnative-earthy.json +0 -0
  44. package/themes/arnative-garden.json +0 -0
  45. package/themes/arnative-gruvbox.json +0 -0
  46. package/themes/arnative-lemonade.json +0 -0
  47. package/themes/arnative-magical.json +0 -0
  48. package/themes/arnative-matrix.json +0 -0
  49. package/themes/arnative-neutral.json +0 -0
  50. package/themes/arnative-nord.json +0 -0
  51. package/themes/arnative-zinc.json +0 -0
  52. package/themes/arnative.json +0 -0
  53. package/CHANGELOG.md +0 -683
  54. package/extensions/animations.ts +0 -242
  55. package/extensions/footer.ts +0 -2617
  56. package/extensions/tools.ts +0 -2231
  57. package/extensions/ui-render-tweaks.ts +0 -1005
  58. package/extensions/usage-export.ts +0 -283
  59. package/extensions/usage.ts +0 -1406
  60. package/lib/check.ts +0 -21
  61. package/themes/gen-themes.mjs +0 -715
@@ -0,0 +1,199 @@
1
+ /**
2
+ * Unified and side-by-side (Split) word-level diff renderer for the `edit` tool box.
3
+ */
4
+ import { visibleWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
5
+ import { EDIT_DIFF_KEY, choiceSetting } from "./preset-store.ts";
6
+ import { clearRenderCache } from "./features/render-cache.ts";
7
+
8
+ const SPLIT_DIFF_MIN_WIDTH = 50;
9
+ const INTRA_LINE_MAX_TOKENS = 200;
10
+
11
+ export type DiffTheme = {
12
+ fg(color: string, text: string): string;
13
+ inverse?(text: string): string;
14
+ };
15
+
16
+ export type DiffResult = {
17
+ content?: Array<{ type: string; text?: string }>;
18
+ details?: { diff?: string };
19
+ };
20
+
21
+ export type EditDiffMode = "Split" | "Unified";
22
+ export const EDIT_DIFF_CHOICES: readonly EditDiffMode[] = ["Split", "Unified"];
23
+ const editDiffSetting = choiceSetting(EDIT_DIFF_KEY, EDIT_DIFF_CHOICES, "Split");
24
+ export const EDIT_DIFF_CONFIG_KEY = editDiffSetting.slot;
25
+
26
+ export const activeEditDiff = (): EditDiffMode => editDiffSetting.get();
27
+
28
+ export function setEditDiff(mode: EditDiffMode): void {
29
+ editDiffSetting.set(mode);
30
+ clearRenderCache();
31
+ }
32
+
33
+ type DiffSide = { styled: string } | null;
34
+
35
+ function unifiedDiffLines(diff: string, th: DiffTheme): string[] {
36
+ return diff.split("\n").map((l) =>
37
+ l.startsWith("+") && !l.startsWith("+++")
38
+ ? th.fg("toolDiffAdded", l)
39
+ : l.startsWith("-") && !l.startsWith("---")
40
+ ? th.fg("toolDiffRemoved", l)
41
+ : th.fg("toolDiffContext", l),
42
+ );
43
+ }
44
+
45
+ function tokenizeDiffLine(s: string): string[] {
46
+ return s.match(/[A-Za-z0-9_]+|\s+|[^\sA-Za-z0-9_]/g) ?? [];
47
+ }
48
+
49
+ /**
50
+ * Highlights changed tokens between paired old/new lines using `inverse`,
51
+ * keeping leading indentation un-highlighted.
52
+ */
53
+ function highlightLinePair(oldText: string, newText: string, th: DiffTheme): { left: string; right: string } {
54
+ const a = tokenizeDiffLine(oldText);
55
+ const b = tokenizeDiffLine(newText);
56
+ if (a.length === 0 || b.length === 0 || a.length > INTRA_LINE_MAX_TOKENS || b.length > INTRA_LINE_MAX_TOKENS) {
57
+ return { left: th.fg("toolDiffRemoved", oldText), right: th.fg("toolDiffAdded", newText) };
58
+ }
59
+
60
+ const m = a.length;
61
+ const n = b.length;
62
+ const dp: Uint16Array[] = Array.from({ length: m + 1 }, () => new Uint16Array(n + 1));
63
+ for (let i = m - 1; i >= 0; i--) {
64
+ for (let j = n - 1; j >= 0; j--) {
65
+ dp[i]![j] = a[i] === b[j] ? dp[i + 1]![j + 1]! + 1 : Math.max(dp[i + 1]![j]!, dp[i]![j + 1]!);
66
+ }
67
+ }
68
+
69
+ const inv = (s: string) => (th.inverse ? th.inverse(s) : `\x1b[7m${s}\x1b[27m`);
70
+ let left = "";
71
+ let right = "";
72
+ let i = 0;
73
+ let j = 0;
74
+ let seenNonWsLeft = false;
75
+ let seenNonWsRight = false;
76
+
77
+ const emitChanged = (tok: string, color: string, seenNonWs: boolean): [string, boolean] => {
78
+ if (!seenNonWs && /^\s+$/.test(tok)) return [th.fg(color, tok), false];
79
+ return [th.fg(color, inv(tok)), true];
80
+ };
81
+
82
+ while (i < m && j < n) {
83
+ if (a[i] === b[j]) {
84
+ const tok = a[i]!;
85
+ if (tok.trim()) {
86
+ seenNonWsLeft = true;
87
+ seenNonWsRight = true;
88
+ }
89
+ left += th.fg("toolDiffRemoved", tok);
90
+ right += th.fg("toolDiffAdded", tok);
91
+ i++;
92
+ j++;
93
+ } else if (dp[i + 1]![j]! >= dp[i]![j + 1]!) {
94
+ const [chunk, seen] = emitChanged(a[i]!, "toolDiffRemoved", seenNonWsLeft);
95
+ left += chunk;
96
+ seenNonWsLeft = seen;
97
+ i++;
98
+ } else {
99
+ const [chunk, seen] = emitChanged(b[j]!, "toolDiffAdded", seenNonWsRight);
100
+ right += chunk;
101
+ seenNonWsRight = seen;
102
+ j++;
103
+ }
104
+ }
105
+ while (i < m) {
106
+ const [chunk, seen] = emitChanged(a[i]!, "toolDiffRemoved", seenNonWsLeft);
107
+ left += chunk;
108
+ seenNonWsLeft = seen;
109
+ i++;
110
+ }
111
+ while (j < n) {
112
+ const [chunk, seen] = emitChanged(b[j]!, "toolDiffAdded", seenNonWsRight);
113
+ right += chunk;
114
+ seenNonWsRight = seen;
115
+ j++;
116
+ }
117
+ return { left, right };
118
+ }
119
+
120
+ function wrapDiffCell(side: DiffSide, width: number): string[] {
121
+ if (!side) return [""];
122
+ const clean = side.styled.replace(/\t/g, " ");
123
+ return clean === "" ? [""] : wrapTextWithAnsi(clean, width);
124
+ }
125
+
126
+ function renderSplitPair(left: DiffSide, right: DiffSide, leftW: number, rightW: number, sep: string): string[] {
127
+ const leftLines = wrapDiffCell(left, leftW);
128
+ const rightLines = wrapDiffCell(right, rightW);
129
+ const count = Math.max(leftLines.length, rightLines.length);
130
+ const rows: string[] = [];
131
+ for (let i = 0; i < count; i++) {
132
+ const l = leftLines[i] ?? "";
133
+ const r = rightLines[i] ?? "";
134
+ const pad = " ".repeat(Math.max(0, leftW - visibleWidth(l)));
135
+ rows.push(`${l}${pad}${sep}${r}`);
136
+ }
137
+ return rows;
138
+ }
139
+
140
+ function fallbackLines(r: DiffResult, th: DiffTheme): string[] {
141
+ const text = (r.content ?? []).filter((c) => c.type === "text").map((c) => c.text ?? "").join("\n");
142
+ return text ? text.split("\n").map((l) => th.fg("toolOutput", l)) : [];
143
+ }
144
+
145
+ /**
146
+ * Formats an `edit` diff side-by-side (Before | After) when the box inner width
147
+ * is at least {@link SPLIT_DIFF_MIN_WIDTH}, falling back to unified diff on
148
+ * narrow terminals or when `details.diff` is absent.
149
+ */
150
+ export function formatEditDiff(r: DiffResult, th: DiffTheme, inner: number, mode: EditDiffMode = activeEditDiff()): string[] {
151
+ const diff: string = r.details?.diff ?? "";
152
+ if (!diff) return fallbackLines(r, th);
153
+ if (mode === "Unified" || inner < SPLIT_DIFF_MIN_WIDTH) return unifiedDiffLines(diff, th);
154
+
155
+ const sep = th.fg("dim", " │ ");
156
+ const leftW = Math.floor((inner - 3) / 2);
157
+ const rightW = inner - 3 - leftW;
158
+ const out: string[] = [
159
+ `${th.fg("dim", "Before")}${" ".repeat(Math.max(0, leftW - 6))}${sep}${th.fg("dim", "After")}`,
160
+ th.fg("dim", `${"─".repeat(leftW)}─┼─${"─".repeat(rightW)}`),
161
+ ];
162
+
163
+ let dels: string[] = [];
164
+ let adds: string[] = [];
165
+ const flushChanges = () => {
166
+ const count = Math.max(dels.length, adds.length);
167
+ for (let i = 0; i < count; i++) {
168
+ const d = dels[i];
169
+ const a = adds[i];
170
+ if (d !== undefined && a !== undefined) {
171
+ const { left, right } = highlightLinePair(d, a, th);
172
+ out.push(...renderSplitPair({ styled: left }, { styled: right }, leftW, rightW, sep));
173
+ } else {
174
+ const left: DiffSide = d !== undefined ? { styled: th.fg("toolDiffRemoved", d) } : null;
175
+ const right: DiffSide = a !== undefined ? { styled: th.fg("toolDiffAdded", a) } : null;
176
+ out.push(...renderSplitPair(left, right, leftW, rightW, sep));
177
+ }
178
+ }
179
+ dels = [];
180
+ adds = [];
181
+ };
182
+
183
+ for (const line of diff.split("\n")) {
184
+ if (line.startsWith("-") && !line.startsWith("---")) {
185
+ dels.push(line.slice(1));
186
+ } else if (line.startsWith("+") && !line.startsWith("+++")) {
187
+ adds.push(line.slice(1));
188
+ } else if (line.startsWith(" ")) {
189
+ flushChanges();
190
+ const side: DiffSide = { styled: th.fg("toolDiffContext", line.slice(1)) };
191
+ out.push(...renderSplitPair(side, side, leftW, rightW, sep));
192
+ } else {
193
+ flushChanges();
194
+ out.push(th.fg("toolDiffContext", line));
195
+ }
196
+ }
197
+ flushChanges();
198
+ return out;
199
+ }