@lyrumu/pi-diff 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/NOTICE.md +8 -0
- package/README.md +120 -2
- package/README.zh-CN.md +121 -0
- package/THIRD_PARTY_LICENSES.md +33 -0
- package/index.ts +153 -0
- package/package.json +45 -4
- package/src/diff-engine.ts +218 -0
- package/src/git.ts +378 -0
- package/src/language.ts +91 -0
- package/src/preferences.ts +47 -0
- package/src/snapshot.ts +114 -0
- package/src/store.ts +77 -0
- package/src/tracker.ts +508 -0
- package/src/tui/diff-screen.ts +310 -0
- package/src/tui/file-tree-utils.ts +174 -0
- package/src/tui/file-tree.ts +99 -0
- package/src/tui/patch-diff.ts +243 -0
- package/src/tui/scopes.ts +20 -0
- package/src/tui/text.ts +15 -0
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { visibleWidth } from "@earendil-works/pi-tui";
|
|
2
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import type { FilePatch } from "../diff-engine.ts";
|
|
4
|
+
import { messages, type Language } from "../language.ts";
|
|
5
|
+
import { displayText, fit } from "./text.ts";
|
|
6
|
+
|
|
7
|
+
export const VIRTUAL_MIN_LINES = 3000;
|
|
8
|
+
export const VIRTUAL_CHUNK_LINES = 128;
|
|
9
|
+
export type DiffView = "unified" | "split";
|
|
10
|
+
|
|
11
|
+
interface Line {
|
|
12
|
+
sign: " " | "+" | "-" | "\\";
|
|
13
|
+
text: string;
|
|
14
|
+
oldLine?: number;
|
|
15
|
+
newLine?: number;
|
|
16
|
+
}
|
|
17
|
+
type Row = { header: string } | { left?: Line; right?: Line };
|
|
18
|
+
|
|
19
|
+
interface WrappedText {
|
|
20
|
+
text: string;
|
|
21
|
+
width: number;
|
|
22
|
+
count: number;
|
|
23
|
+
starts?: number[];
|
|
24
|
+
}
|
|
25
|
+
interface WrappedRow {
|
|
26
|
+
source: Row;
|
|
27
|
+
start: number;
|
|
28
|
+
height: number;
|
|
29
|
+
left: WrappedText;
|
|
30
|
+
right?: WrappedText;
|
|
31
|
+
}
|
|
32
|
+
const graphemes = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
33
|
+
|
|
34
|
+
/** Word wrapping trims code whitespace; keep graphemes and every space instead. */
|
|
35
|
+
function wrap(text: string, width: number): WrappedText {
|
|
36
|
+
width = Math.max(1, width);
|
|
37
|
+
if (/^[\x20-\x7e]*$/.test(text)) return { text, width, count: Math.max(1, Math.ceil(text.length / width)) };
|
|
38
|
+
const starts = [0];
|
|
39
|
+
let used = 0;
|
|
40
|
+
for (const { segment, index } of graphemes.segment(text)) {
|
|
41
|
+
const cells = visibleWidth(segment);
|
|
42
|
+
if (used > 0 && used + cells > width) { starts.push(index); used = 0; }
|
|
43
|
+
used += cells;
|
|
44
|
+
}
|
|
45
|
+
return { text, width, starts, count: starts.length };
|
|
46
|
+
}
|
|
47
|
+
const partStart = (text: WrappedText, part: number) => text.starts?.[part] ?? part * text.width;
|
|
48
|
+
const partText = (text: WrappedText, part: number) => text.text.slice(partStart(text, part), part + 1 < text.count ? partStart(text, part + 1) : undefined);
|
|
49
|
+
|
|
50
|
+
/** pi-tui has no DiffRenderable: parse once, then paint only viewport chunks. */
|
|
51
|
+
export class PatchDiff {
|
|
52
|
+
private readonly unified: Row[] = [];
|
|
53
|
+
private readonly split: Row[] = [];
|
|
54
|
+
private readonly digits: number;
|
|
55
|
+
private readonly placeholder: "unavailable" | "binary" | "tooLarge" | "deletedUnknown" | "noPatch" | "noTextChanges" | undefined;
|
|
56
|
+
private language: Language;
|
|
57
|
+
private readonly status: FilePatch["status"];
|
|
58
|
+
private cacheSignature = "";
|
|
59
|
+
private readonly chunks = new Map<number, string[]>();
|
|
60
|
+
private wrapped: WrappedRow[] = [];
|
|
61
|
+
private totalRows = 0;
|
|
62
|
+
private cacheTheme: Theme | undefined;
|
|
63
|
+
|
|
64
|
+
constructor(file: FilePatch, language: Language = "en") {
|
|
65
|
+
this.language = language;
|
|
66
|
+
this.status = file.status;
|
|
67
|
+
this.placeholder = file.unavailable ? "unavailable"
|
|
68
|
+
: file.binary ? "binary"
|
|
69
|
+
: file.tooLarge ? "tooLarge"
|
|
70
|
+
: file.baselineUnknown && file.status === "deleted" ? "deletedUnknown"
|
|
71
|
+
: !file.patch ? "noPatch" : undefined;
|
|
72
|
+
let oldLine = 0;
|
|
73
|
+
let newLine = 0;
|
|
74
|
+
let inHunk = false;
|
|
75
|
+
let maxLine = 0;
|
|
76
|
+
if (!this.placeholder) for (const text of file.patch.replace(/\n$/, "").split("\n")) {
|
|
77
|
+
const hunk = /^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@/.exec(text);
|
|
78
|
+
if (hunk) {
|
|
79
|
+
oldLine = Number(hunk[1]);
|
|
80
|
+
newLine = Number(hunk[2]);
|
|
81
|
+
inHunk = true;
|
|
82
|
+
this.unified.push({ header: displayText(text) });
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (text.startsWith("diff --git ")) inHunk = false;
|
|
86
|
+
if (!inHunk || ![" ", "+", "-", "\\"].includes(text[0] ?? "")) continue;
|
|
87
|
+
const sign = text[0] as Line["sign"];
|
|
88
|
+
const line: Line = { sign, text: displayText(text.slice(1)) };
|
|
89
|
+
if (sign === " " || sign === "-") line.oldLine = oldLine++;
|
|
90
|
+
if (sign === " " || sign === "+") line.newLine = newLine++;
|
|
91
|
+
maxLine = Math.max(maxLine, line.oldLine ?? 0, line.newLine ?? 0);
|
|
92
|
+
this.unified.push({ left: line, right: line });
|
|
93
|
+
}
|
|
94
|
+
this.digits = Math.max(1, String(maxLine).length);
|
|
95
|
+
for (let i = 0; i < this.unified.length;) {
|
|
96
|
+
const row = this.unified[i]!;
|
|
97
|
+
if ("header" in row || row.left?.sign === " " || row.left?.sign === "\\") {
|
|
98
|
+
this.split.push(row);
|
|
99
|
+
i++;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
const removed: Line[] = [];
|
|
103
|
+
const added: Line[] = [];
|
|
104
|
+
while (i < this.unified.length) {
|
|
105
|
+
const next = this.unified[i]!;
|
|
106
|
+
if ("header" in next || !next.left || !["-", "+"].includes(next.left.sign)) break;
|
|
107
|
+
(next.left.sign === "-" ? removed : added).push(next.left);
|
|
108
|
+
i++;
|
|
109
|
+
}
|
|
110
|
+
for (let j = 0; j < Math.max(removed.length, added.length); j++) this.split.push({ left: removed[j], right: added[j] });
|
|
111
|
+
}
|
|
112
|
+
if (!this.placeholder && this.unified.length === 0) this.placeholder = "noTextChanges";
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
setLanguage(language: Language): void {
|
|
116
|
+
this.language = language;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
effectiveView(view: DiffView): DiffView {
|
|
120
|
+
return this.status === "modified" ? view : "unified";
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
private prepare(width: number, view: DiffView): void {
|
|
124
|
+
const signature = `${width}:${view}`;
|
|
125
|
+
if (signature === this.cacheSignature) return;
|
|
126
|
+
this.cacheSignature = signature;
|
|
127
|
+
this.chunks.clear();
|
|
128
|
+
this.wrapped = [];
|
|
129
|
+
this.totalRows = 0;
|
|
130
|
+
const leftWidth = Math.floor((width - 1) / 2);
|
|
131
|
+
for (const source of view === "split" ? this.split : this.unified) {
|
|
132
|
+
let left: WrappedText;
|
|
133
|
+
let right: WrappedText | undefined;
|
|
134
|
+
if ("header" in source) left = wrap(source.header, width);
|
|
135
|
+
else if (source.left?.sign === "\\") left = wrap(`\\${source.left.text}`, width);
|
|
136
|
+
else if (view === "unified") left = wrap(source.left!.text, width - 2 * this.digits - 3);
|
|
137
|
+
else {
|
|
138
|
+
left = wrap(source.left?.text ?? "", leftWidth - this.digits - 2);
|
|
139
|
+
right = wrap(source.right?.text ?? "", width - leftWidth - 1 - this.digits - 2);
|
|
140
|
+
}
|
|
141
|
+
const height = Math.max(left.count, right?.count ?? 1);
|
|
142
|
+
this.wrapped.push({ source, start: this.totalRows, height, left, right });
|
|
143
|
+
this.totalRows += height;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
private rowAt(index: number): WrappedRow | undefined {
|
|
148
|
+
let low = 0;
|
|
149
|
+
let high = this.wrapped.length;
|
|
150
|
+
while (low < high) {
|
|
151
|
+
const mid = Math.floor((low + high) / 2);
|
|
152
|
+
if (this.wrapped[mid]!.start <= index) low = mid + 1;
|
|
153
|
+
else high = mid;
|
|
154
|
+
}
|
|
155
|
+
const row = this.wrapped[low - 1];
|
|
156
|
+
return row && index < row.start + row.height ? row : undefined;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
rowCount(width: number, view: DiffView): number {
|
|
160
|
+
if (this.placeholder) return 1;
|
|
161
|
+
this.prepare(width, this.effectiveView(view));
|
|
162
|
+
return this.totalRows;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Keep the same source character visible when width or layout changes. */
|
|
166
|
+
reflowTop(width: number, view: DiffView, top: number): number {
|
|
167
|
+
view = this.effectiveView(view);
|
|
168
|
+
if (this.cacheSignature === `${width}:${view}`) return top;
|
|
169
|
+
const old = this.rowAt(top);
|
|
170
|
+
const part = old ? top - old.start : 0;
|
|
171
|
+
const right = old && part >= old.left.count;
|
|
172
|
+
const text = right ? old?.right : old?.left;
|
|
173
|
+
const line = old && !("header" in old.source) ? right ? old.source.right : old.source.left : undefined;
|
|
174
|
+
const offset = text ? partStart(text, part) : 0;
|
|
175
|
+
this.prepare(width, view);
|
|
176
|
+
if (!old) return top;
|
|
177
|
+
const next = this.wrapped.find((item) => item.source === old.source || (line && !("header" in item.source) && (item.source.left === line || item.source.right === line)));
|
|
178
|
+
if (!next) return top;
|
|
179
|
+
const nextText = line && !("header" in next.source) && next.source.right === line && next.source.left !== line ? next.right! : next.left;
|
|
180
|
+
let nextPart = nextText.starts ? 0 : Math.floor(offset / nextText.width);
|
|
181
|
+
if (nextText.starts) {
|
|
182
|
+
while (nextPart + 1 < nextText.count && nextText.starts[nextPart + 1]! <= offset) nextPart++;
|
|
183
|
+
}
|
|
184
|
+
return next.start + Math.min(nextPart, next.height - 1);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
private paint(wrapped: WrappedRow, part: number, width: number, view: DiffView, theme: Theme): string {
|
|
188
|
+
const row = wrapped.source;
|
|
189
|
+
if ("header" in row || row.left?.sign === "\\") return fit(theme.fg("header" in row ? "accent" : "muted", partText(wrapped.left, part)), width);
|
|
190
|
+
const color = (line: Line) => line.sign === "+" ? "success" : line.sign === "-" ? "error" : "muted";
|
|
191
|
+
const styled = (line: Line, text: string, size: number) => {
|
|
192
|
+
const content = fit(theme.fg(color(line), text), size);
|
|
193
|
+
return line.sign === "+" ? theme.bg("toolSuccessBg", content)
|
|
194
|
+
: line.sign === "-" ? theme.bg("toolErrorBg", content) : content;
|
|
195
|
+
};
|
|
196
|
+
const number = (value: number | undefined) => String(value ?? "").padStart(this.digits);
|
|
197
|
+
if (view === "unified") {
|
|
198
|
+
const line = row.left!;
|
|
199
|
+
const gutter = `${number(part === 0 ? line.oldLine : undefined)} ${number(part === 0 ? line.newLine : undefined)} ${part === 0 ? line.sign : "↳"}`;
|
|
200
|
+
return styled(line, gutter + partText(wrapped.left, part), width);
|
|
201
|
+
}
|
|
202
|
+
const leftWidth = Math.floor((width - 1) / 2);
|
|
203
|
+
const rightWidth = width - leftWidth - 1;
|
|
204
|
+
const side = (line: Line | undefined, text: WrappedText, size: number, old: boolean) => {
|
|
205
|
+
if (!line || part >= text.count) return fit("", size);
|
|
206
|
+
const gutter = `${number(part === 0 ? (old ? line.oldLine : line.newLine) : undefined)} ${part === 0 ? line.sign : "↳"}`;
|
|
207
|
+
return styled(line, gutter + partText(text, part), size);
|
|
208
|
+
};
|
|
209
|
+
return side(row.left, wrapped.left, leftWidth, true) + theme.fg("borderMuted", "│") + side(row.right, wrapped.right!, rightWidth, false);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
renderViewport(width: number, height: number, top: number, view: DiffView, theme: Theme): string[] {
|
|
213
|
+
if (this.placeholder) return [fit(theme.fg("muted", messages[this.language][this.placeholder]), width), ...Array.from({ length: Math.max(0, height - 1) }, () => fit("", width))].slice(0, height);
|
|
214
|
+
view = this.effectiveView(view);
|
|
215
|
+
this.prepare(width, view);
|
|
216
|
+
if (theme !== this.cacheTheme) {
|
|
217
|
+
this.chunks.clear();
|
|
218
|
+
this.cacheTheme = theme;
|
|
219
|
+
}
|
|
220
|
+
return Array.from({ length: height }, (_, offset) => {
|
|
221
|
+
const index = top + offset;
|
|
222
|
+
const row = this.rowAt(index);
|
|
223
|
+
if (!row) return fit("", width);
|
|
224
|
+
if (this.totalRows < VIRTUAL_MIN_LINES) return this.paint(row, index - row.start, width, view, theme);
|
|
225
|
+
const chunkIndex = Math.floor(index / VIRTUAL_CHUNK_LINES);
|
|
226
|
+
let chunk = this.chunks.get(chunkIndex);
|
|
227
|
+
if (!chunk) {
|
|
228
|
+
const start = chunkIndex * VIRTUAL_CHUNK_LINES;
|
|
229
|
+
chunk = Array.from({ length: Math.min(VIRTUAL_CHUNK_LINES, this.totalRows - start) }, (_, offset) => {
|
|
230
|
+
const item = this.rowAt(start + offset)!;
|
|
231
|
+
return this.paint(item, start + offset - item.start, width, view, theme);
|
|
232
|
+
});
|
|
233
|
+
this.chunks.set(chunkIndex, chunk);
|
|
234
|
+
if (this.chunks.size > 4) this.chunks.delete(this.chunks.keys().next().value!);
|
|
235
|
+
}
|
|
236
|
+
return chunk[index % VIRTUAL_CHUNK_LINES]!;
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
invalidate(): void {
|
|
241
|
+
this.chunks.clear();
|
|
242
|
+
}
|
|
243
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { TurnDiffEntryData } from "../store.ts";
|
|
2
|
+
|
|
3
|
+
export type DiffScope =
|
|
4
|
+
| { kind: "turn"; turn: number | undefined }
|
|
5
|
+
| { kind: "session" }
|
|
6
|
+
| { kind: "git"; mode: "unstaged" | "working" | "branch" };
|
|
7
|
+
|
|
8
|
+
export const GIT_SCOPES = [
|
|
9
|
+
{ mode: "unstaged", label: "Unstaged" },
|
|
10
|
+
{ mode: "working", label: "Uncommitted" },
|
|
11
|
+
{ mode: "branch", label: "All branch changes" },
|
|
12
|
+
] as const;
|
|
13
|
+
|
|
14
|
+
/** Cycle the five visible scopes; Git scopes are absent outside a repository. */
|
|
15
|
+
export function cycleScope(scope: DiffScope, direction: 1 | -1, turns: readonly TurnDiffEntryData[], gitAvailable: boolean): DiffScope {
|
|
16
|
+
const scopes: DiffScope[] = [{ kind: "turn", turn: turns.at(-1)?.turn }, { kind: "session" },
|
|
17
|
+
...(gitAvailable ? GIT_SCOPES.map(({ mode }): DiffScope => ({ kind: "git", mode })) : [])];
|
|
18
|
+
const index = scopes.findIndex((item) => item.kind === scope.kind && (item.kind !== "git" || (scope.kind === "git" && item.mode === scope.mode)));
|
|
19
|
+
return scopes[(index + direction + scopes.length) % scopes.length]!;
|
|
20
|
+
}
|
package/src/tui/text.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { truncateToWidth, type TUI } from "@earendil-works/pi-tui";
|
|
2
|
+
|
|
3
|
+
export type TuiHost = Pick<TUI, "terminal" | "requestRender">;
|
|
4
|
+
|
|
5
|
+
/** File names and source text must not become terminal control sequences. */
|
|
6
|
+
export function displayText(text: string): string {
|
|
7
|
+
return text.replace(/[\x00-\x1f\x7f-\x9f]/g, (char) => {
|
|
8
|
+
if (char === "\t") return " ";
|
|
9
|
+
if (char === "\n") return "\\n";
|
|
10
|
+
if (char === "\r") return "\\r";
|
|
11
|
+
return `\\x${char.charCodeAt(0).toString(16).padStart(2, "0")}`;
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const fit = (text: string, width: number) => truncateToWidth(text, Math.max(0, width), "", true);
|