@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.
- package/README.md +22 -5
- package/README.zh-CN.md +22 -5
- package/index.ts +42 -20
- package/package.json +20 -7
- package/src/diff-engine.ts +11 -4
- package/src/git.ts +55 -12
- package/src/language.ts +72 -2
- package/src/shell-targets.ts +215 -0
- package/src/store.ts +20 -2
- package/src/tracker.ts +262 -312
- package/src/tui/diff-screen.ts +172 -36
- package/src/tui/file-tree-utils.ts +11 -2
- package/src/tui/file-tree.ts +12 -8
- package/src/tui/patch-diff.ts +556 -44
- package/src/tui/task-summary.ts +63 -0
- package/src/tui/text.ts +60 -1
package/src/tui/diff-screen.ts
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
import { matchesKey, visibleWidth, type Component, type OverlayOptions, type TuiMouseEvent, type TuiMouseEventResult } from "@earendil-works/pi-tui";
|
|
1
|
+
import { matchesKey, visibleWidth, wrapTextWithAnsi, type Component, type OverlayOptions, type TuiMouseEvent, type TuiMouseEventResult } from "@earendil-works/pi-tui";
|
|
2
2
|
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
3
3
|
import type { FilePatch } from "../diff-engine.ts";
|
|
4
4
|
import { localize, messages, type Language, type LocalizedText } from "../language.ts";
|
|
5
5
|
import { FileTree } from "./file-tree.ts";
|
|
6
6
|
import { PatchDiff, type DiffView } from "./patch-diff.ts";
|
|
7
|
-
import { displayText, fit, type TuiHost } from "./text.ts";
|
|
7
|
+
import { displayText, fit, fitEllipsis, safeBg, safeFg, type TuiHost } from "./text.ts";
|
|
8
8
|
|
|
9
9
|
export const MIN_SPLIT_WIDTH = 100;
|
|
10
10
|
export const FILE_TREE_MIN_WIDTH = 10;
|
|
11
11
|
export const FILE_TREE_MAX_WIDTH = 40;
|
|
12
12
|
const MIN_PATCH_WIDTH = 40;
|
|
13
13
|
export const FILE_HEADER_HEIGHT = 2;
|
|
14
|
+
const JUMP_LEAD_LINES = 3;
|
|
15
|
+
const MAX_CACHED_PATCHES = 16;
|
|
14
16
|
export const FULLSCREEN_OPTIONS = {
|
|
15
17
|
overlay: true,
|
|
16
18
|
overlayOptions: { width: "100%", maxHeight: "100%", row: 0, col: 0, margin: 0 } satisfies OverlayOptions,
|
|
@@ -26,6 +28,13 @@ export interface DiffScreenPreferences {
|
|
|
26
28
|
onScopeChange?: (direction: 1 | -1) => void;
|
|
27
29
|
}
|
|
28
30
|
|
|
31
|
+
interface SavedFileState {
|
|
32
|
+
top: number;
|
|
33
|
+
changeJump: boolean;
|
|
34
|
+
activeChange: number | undefined;
|
|
35
|
+
expandedFolds: number[];
|
|
36
|
+
}
|
|
37
|
+
|
|
29
38
|
/** Selected-file view on a fullscreen overlay; all data comes from FilePatch[]. */
|
|
30
39
|
export class DiffScreen implements Component {
|
|
31
40
|
private readonly host: TuiHost;
|
|
@@ -33,6 +42,7 @@ export class DiffScreen implements Component {
|
|
|
33
42
|
private title: LocalizedText;
|
|
34
43
|
private language: Language;
|
|
35
44
|
private files: readonly FilePatch[];
|
|
45
|
+
private totals = { additions: 0, deletions: 0 };
|
|
36
46
|
private readonly close: () => void;
|
|
37
47
|
private readonly preferences: DiffScreenPreferences;
|
|
38
48
|
private tree: FileTree;
|
|
@@ -40,11 +50,16 @@ export class DiffScreen implements Component {
|
|
|
40
50
|
private notice: LocalizedText | undefined;
|
|
41
51
|
private selected: number | undefined;
|
|
42
52
|
private patch: PatchDiff | undefined;
|
|
53
|
+
private readonly patchCache = new Map<number, PatchDiff>();
|
|
54
|
+
private readonly fileStates = new Map<number, SavedFileState>();
|
|
43
55
|
private focus: "tree" | "diff" = "diff";
|
|
44
56
|
private viewOverride: DiffView | undefined;
|
|
45
57
|
private top = 0;
|
|
58
|
+
private changeJump = false;
|
|
59
|
+
private activeChange: number | undefined;
|
|
46
60
|
private viewButton = 0;
|
|
47
61
|
private showFilesButton = 0;
|
|
62
|
+
private contextButton = { start: -1, end: -1 };
|
|
48
63
|
private viewHint: "noFileToSwitch" | "tooNarrow" | undefined;
|
|
49
64
|
private treeWidthOverride: number | undefined;
|
|
50
65
|
private treeCollapsed = false;
|
|
@@ -58,6 +73,7 @@ export class DiffScreen implements Component {
|
|
|
58
73
|
this.theme = theme;
|
|
59
74
|
this.title = title;
|
|
60
75
|
this.files = files;
|
|
76
|
+
this.totals = this.computeTotals(files);
|
|
61
77
|
this.close = close;
|
|
62
78
|
this.preferences = preferences;
|
|
63
79
|
this.language = preferences.language ?? "en";
|
|
@@ -68,16 +84,25 @@ export class DiffScreen implements Component {
|
|
|
68
84
|
this.select(this.tree.selectedFile());
|
|
69
85
|
}
|
|
70
86
|
|
|
87
|
+
private computeTotals(files: readonly FilePatch[]) {
|
|
88
|
+
return files.reduce((sum, file) => ({ additions: sum.additions + file.additions, deletions: sum.deletions + file.deletions }), { additions: 0, deletions: 0 });
|
|
89
|
+
}
|
|
90
|
+
|
|
71
91
|
/** Replace the source inside the existing overlay; never expose the chat between scopes. */
|
|
72
92
|
setData(title: LocalizedText, files: readonly FilePatch[], status: { emptyMessage?: LocalizedText; notice?: LocalizedText } = {}): void {
|
|
73
93
|
this.title = title;
|
|
74
94
|
this.files = files;
|
|
95
|
+
this.totals = this.computeTotals(files);
|
|
75
96
|
this.emptyMessage = status.emptyMessage;
|
|
76
97
|
this.notice = status.notice;
|
|
77
98
|
this.tree = new FileTree(files);
|
|
78
99
|
this.selected = undefined;
|
|
79
100
|
this.patch = undefined;
|
|
101
|
+
this.patchCache.clear();
|
|
102
|
+
this.fileStates.clear();
|
|
80
103
|
this.top = 0;
|
|
104
|
+
this.changeJump = false;
|
|
105
|
+
this.activeChange = undefined;
|
|
81
106
|
this.focus = "diff";
|
|
82
107
|
this.viewHint = undefined;
|
|
83
108
|
this.pointer = undefined;
|
|
@@ -87,17 +112,44 @@ export class DiffScreen implements Component {
|
|
|
87
112
|
this.host.requestRender();
|
|
88
113
|
}
|
|
89
114
|
|
|
115
|
+
private saveCurrentFileState(): void {
|
|
116
|
+
if (this.selected === undefined || !this.patch) return;
|
|
117
|
+
this.fileStates.set(this.selected, {
|
|
118
|
+
top: this.top,
|
|
119
|
+
changeJump: this.changeJump,
|
|
120
|
+
activeChange: this.activeChange,
|
|
121
|
+
expandedFolds: this.patch.getExpandedFoldIndices(),
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
90
125
|
private select(index: number | undefined): void {
|
|
91
126
|
if (index === undefined || index === this.selected) return;
|
|
92
127
|
const file = this.files[index];
|
|
93
128
|
if (!file) return;
|
|
129
|
+
this.saveCurrentFileState();
|
|
94
130
|
this.selected = index;
|
|
95
|
-
|
|
96
|
-
|
|
131
|
+
const saved = this.fileStates.get(index);
|
|
132
|
+
let cached = this.patchCache.get(index);
|
|
133
|
+
if (cached) {
|
|
134
|
+
this.patchCache.delete(index);
|
|
135
|
+
this.patchCache.set(index, cached);
|
|
136
|
+
} else {
|
|
137
|
+
cached = new PatchDiff(file, this.language);
|
|
138
|
+
if (saved?.expandedFolds.length) cached.restoreExpandedFoldIndices(saved.expandedFolds);
|
|
139
|
+
this.patchCache.set(index, cached);
|
|
140
|
+
if (this.patchCache.size > MAX_CACHED_PATCHES) {
|
|
141
|
+
const oldest = this.patchCache.keys().next().value;
|
|
142
|
+
if (oldest !== undefined) this.patchCache.delete(oldest);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
this.patch = cached;
|
|
146
|
+
this.top = saved?.top ?? 0;
|
|
147
|
+
this.changeJump = saved?.changeJump ?? false;
|
|
148
|
+
this.activeChange = saved?.activeChange;
|
|
97
149
|
this.viewHint = undefined;
|
|
98
150
|
}
|
|
99
151
|
|
|
100
|
-
private layout(width: number) {
|
|
152
|
+
private layout(width: number, rows = this.host.terminal.rows) {
|
|
101
153
|
const inset = width < 90 ? 0 : 2;
|
|
102
154
|
const maxTreeWidth = Math.min(Math.floor(width / 4), width - inset * 2 - MIN_PATCH_WIDTH);
|
|
103
155
|
const defaultTreeWidth = Math.max(30, Math.min(FILE_TREE_MAX_WIDTH, Math.floor(width / 4)));
|
|
@@ -105,8 +157,21 @@ export class DiffScreen implements Component {
|
|
|
105
157
|
? Math.min(maxTreeWidth, Math.max(FILE_TREE_MIN_WIDTH, this.treeWidthOverride ?? defaultTreeWidth)) : 0;
|
|
106
158
|
const patchWidth = Math.max(1, width - treeWidth - inset * 2);
|
|
107
159
|
const view: DiffView = patchWidth >= MIN_SPLIT_WIDTH ? this.viewOverride ?? "split" : "unified";
|
|
108
|
-
const
|
|
109
|
-
|
|
160
|
+
const notice = this.notice ? wrapTextWithAnsi(localize(this.notice, this.language), Math.max(1, width))
|
|
161
|
+
.slice(0, Math.max(0, rows - 3 - FILE_HEADER_HEIGHT)) : [];
|
|
162
|
+
const bodyEnd = rows - 1 - notice.length;
|
|
163
|
+
const height = Math.max(0, bodyEnd - 1 - FILE_HEADER_HEIGHT);
|
|
164
|
+
return { inset, treeWidth, maxTreeWidth, patchWidth, view, height, notice, bodyEnd };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private scrollDiff(delta: number): void {
|
|
168
|
+
if (!this.patch) return;
|
|
169
|
+
const { patchWidth, view, height } = this.layout(this.host.terminal.columns);
|
|
170
|
+
this.top = this.patch.reflowTop(patchWidth, view, this.top);
|
|
171
|
+
const bottom = Math.max(0, this.patch.rowCount(patchWidth, view) - Math.max(1, height));
|
|
172
|
+
this.top = Math.max(0, Math.min(this.top + delta, this.changeJump ? Math.max(this.top, bottom) : bottom));
|
|
173
|
+
this.changeJump &&= this.top > bottom;
|
|
174
|
+
this.activeChange = undefined;
|
|
110
175
|
}
|
|
111
176
|
|
|
112
177
|
handleInput(data: string): void {
|
|
@@ -120,7 +185,7 @@ export class DiffScreen implements Component {
|
|
|
120
185
|
}
|
|
121
186
|
if (matchesKey(data, "l") || matchesKey(data, "shift+l")) {
|
|
122
187
|
this.language = this.language === "en" ? "zh-CN" : "en";
|
|
123
|
-
this.
|
|
188
|
+
for (const cached of this.patchCache.values()) cached.setLanguage(this.language);
|
|
124
189
|
this.preferences.onLanguageChange?.(this.language);
|
|
125
190
|
this.host.requestRender();
|
|
126
191
|
return;
|
|
@@ -156,16 +221,38 @@ export class DiffScreen implements Component {
|
|
|
156
221
|
if (file !== undefined) this.focus = "diff";
|
|
157
222
|
}
|
|
158
223
|
} else {
|
|
159
|
-
if (
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
224
|
+
if (this.patch && (matchesKey(data, "n") || matchesKey(data, "p"))) {
|
|
225
|
+
this.top = this.patch.reflowTop(patchWidth, view, this.top);
|
|
226
|
+
const direction = matchesKey(data, "n") ? 1 : -1;
|
|
227
|
+
const count = this.patch.changeCount();
|
|
228
|
+
let targetIndex: number | undefined;
|
|
229
|
+
if (this.activeChange !== undefined && this.activeChange < count) {
|
|
230
|
+
const candidate = this.activeChange + direction;
|
|
231
|
+
if (candidate >= 0 && candidate < count) targetIndex = candidate;
|
|
232
|
+
} else {
|
|
233
|
+
targetIndex = this.patch.adjacentChangeIndex(this.top, direction);
|
|
234
|
+
}
|
|
235
|
+
if (targetIndex !== undefined) {
|
|
236
|
+
const row = this.patch.changeRow(targetIndex);
|
|
237
|
+
if (row !== undefined) {
|
|
238
|
+
this.activeChange = targetIndex;
|
|
239
|
+
this.top = Math.max(0, row - JUMP_LEAD_LINES);
|
|
240
|
+
this.changeJump = true;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
} else if (matchesKey(data, "up") || matchesKey(data, "down") || matchesKey(data, "j") || matchesKey(data, "k")) this.scrollDiff(matchesKey(data, "up") || matchesKey(data, "k") ? -1 : 1);
|
|
244
|
+
else if (matchesKey(data, "pageUp") || matchesKey(data, "pageDown")) this.scrollDiff(matchesKey(data, "pageUp") ? -Math.max(1, height) : Math.max(1, height));
|
|
245
|
+
else if (matchesKey(data, "home") || matchesKey(data, "end")) {
|
|
246
|
+
this.changeJump = false;
|
|
247
|
+
this.activeChange = undefined;
|
|
248
|
+
this.top = matchesKey(data, "home") ? 0 : Math.max(0, (this.patch?.rowCount(patchWidth, view) ?? 0) - Math.max(1, height));
|
|
249
|
+
}
|
|
163
250
|
}
|
|
164
251
|
this.host.requestRender();
|
|
165
252
|
}
|
|
166
253
|
|
|
167
|
-
private isOnDivider(treeWidth: number,
|
|
168
|
-
return treeWidth > 0 && this.pointer !== undefined && this.pointer.x >= treeWidth - 1 && this.pointer.x <= treeWidth && this.pointer.y >= 1 && this.pointer.y <
|
|
254
|
+
private isOnDivider(treeWidth: number, bodyEnd: number): boolean {
|
|
255
|
+
return treeWidth > 0 && this.pointer !== undefined && this.pointer.x >= treeWidth - 1 && this.pointer.x <= treeWidth && this.pointer.y >= 1 && this.pointer.y < bodyEnd;
|
|
169
256
|
}
|
|
170
257
|
|
|
171
258
|
private resizeTree(x: number, maxWidth: number): number {
|
|
@@ -180,10 +267,13 @@ export class DiffScreen implements Component {
|
|
|
180
267
|
|
|
181
268
|
handleMouse(event: TuiMouseEvent): TuiMouseEventResult {
|
|
182
269
|
// Captured event bounds can predate a terminal resize.
|
|
183
|
-
const
|
|
184
|
-
const
|
|
270
|
+
const dragging = this.dividerGesture === "dragging";
|
|
271
|
+
const width = dragging ? this.host.terminal.columns : event.width;
|
|
272
|
+
const rows = dragging ? this.host.terminal.rows : event.height;
|
|
273
|
+
const { treeWidth, maxTreeWidth, patchWidth, view, bodyEnd } = this.layout(width, rows);
|
|
274
|
+
const wasHovered = this.isOnDivider(treeWidth, bodyEnd);
|
|
185
275
|
this.pointer = { x: event.x, y: event.y };
|
|
186
|
-
const hovered = this.isOnDivider(treeWidth,
|
|
276
|
+
const hovered = this.isOnDivider(treeWidth, bodyEnd);
|
|
187
277
|
if (event.type === "move") return { handled: true, render: wasHovered !== hovered };
|
|
188
278
|
if (event.type === "press") {
|
|
189
279
|
this.dividerGesture = undefined;
|
|
@@ -228,14 +318,14 @@ export class DiffScreen implements Component {
|
|
|
228
318
|
const treeOnly = !treeWidth && this.focus === "tree" && this.files.length > 0;
|
|
229
319
|
const inTree = treeOnly || (treeWidth > 0 && event.x < treeWidth - 1);
|
|
230
320
|
const treeTop = treeOnly ? 2 : 3;
|
|
231
|
-
const treeHeight =
|
|
321
|
+
const treeHeight = Math.max(0, bodyEnd - treeTop);
|
|
232
322
|
if (event.type === "wheel") {
|
|
233
323
|
if (event.y >= treeTop && event.y < treeTop + treeHeight && inTree) {
|
|
234
324
|
this.focus = "tree";
|
|
235
325
|
this.tree.scroll(event.wheelDelta ?? 0, treeHeight);
|
|
236
|
-
} else if (!inTree && event.y >= 3 && event.y <
|
|
326
|
+
} else if (!inTree && event.y >= 3 && event.y < bodyEnd) {
|
|
237
327
|
this.focus = "diff";
|
|
238
|
-
this.
|
|
328
|
+
this.scrollDiff(event.wheelDelta ?? 0);
|
|
239
329
|
}
|
|
240
330
|
return { handled: true };
|
|
241
331
|
}
|
|
@@ -247,7 +337,17 @@ export class DiffScreen implements Component {
|
|
|
247
337
|
const selected = this.tree.clickRow(event.y - treeTop);
|
|
248
338
|
this.select(selected);
|
|
249
339
|
if (treeOnly && selected !== undefined) this.focus = "diff";
|
|
250
|
-
} else if (!inTree && event.y
|
|
340
|
+
} else if (!inTree && event.y === 2 && this.patch && event.x >= this.contextButton.start && event.x < this.contextButton.end) {
|
|
341
|
+
this.focus = "diff";
|
|
342
|
+
this.patch.toggleContext();
|
|
343
|
+
this.top = this.patch.reflowTop(patchWidth, view, this.top);
|
|
344
|
+
} else if (!inTree && event.y > 0 && event.y < bodyEnd) {
|
|
345
|
+
this.focus = "diff";
|
|
346
|
+
if (event.y >= 3 && this.patch) {
|
|
347
|
+
this.top = this.patch.reflowTop(patchWidth, view, this.top);
|
|
348
|
+
this.patch.expandFoldAt(this.top + (event.y - 3));
|
|
349
|
+
}
|
|
350
|
+
}
|
|
251
351
|
return { handled: true, focus: true };
|
|
252
352
|
}
|
|
253
353
|
// Consume overlay mouse events so they cannot scroll the conversation underneath.
|
|
@@ -256,30 +356,32 @@ export class DiffScreen implements Component {
|
|
|
256
356
|
|
|
257
357
|
render(width: number): string[] {
|
|
258
358
|
const rows = Math.max(1, this.host.terminal.rows);
|
|
259
|
-
const { inset, treeWidth, patchWidth, view, height } = this.layout(width);
|
|
359
|
+
const { inset, treeWidth, patchWidth, view, height, notice, bodyEnd } = this.layout(width, rows);
|
|
260
360
|
if ((width < 90 || !this.files.length) && this.dividerGesture === "dragging") {
|
|
261
361
|
this.dividerGesture = "released";
|
|
262
362
|
this.lastDividerClick = 0;
|
|
263
363
|
}
|
|
264
|
-
const dividerActive = treeWidth > 0 && (this.dividerGesture === "dragging" || this.isOnDivider(treeWidth,
|
|
364
|
+
const dividerActive = treeWidth > 0 && (this.dividerGesture === "dragging" || this.isOnDivider(treeWidth, bodyEnd));
|
|
265
365
|
if (patchWidth >= MIN_SPLIT_WIDTH && this.viewHint === "tooNarrow") this.viewHint = undefined;
|
|
266
366
|
this.top = this.patch?.reflowTop(patchWidth, view, this.top) ?? 0;
|
|
267
|
-
|
|
367
|
+
const totalRows = this.patch?.rowCount(patchWidth, view) ?? 0;
|
|
368
|
+
// A jump can place the final change near the top even when fewer rows remain.
|
|
369
|
+
this.top = Math.max(0, Math.min(this.top, totalRows - (this.changeJump ? 1 : Math.max(1, height))));
|
|
268
370
|
const theme = this.theme;
|
|
269
371
|
const text = messages[this.language];
|
|
270
372
|
const file = this.selected === undefined ? undefined : this.files[this.selected];
|
|
271
373
|
const fixedUnified = file !== undefined && file.status !== "modified";
|
|
272
|
-
const
|
|
273
|
-
|
|
274
|
-
+ theme.fg("success", `+${totals.additions}`) + " " + theme.fg("error", `-${totals.deletions}`) + " ");
|
|
374
|
+
const heading = theme.bold(theme.fg("accent", `${displayText(localize(this.title, this.language))} · ${text.fileCount(this.files.length)} `)
|
|
375
|
+
+ theme.fg("success", `+${this.totals.additions}`) + " " + theme.fg("error", `-${this.totals.deletions}`) + " ");
|
|
275
376
|
const showFiles = this.treeCollapsed && this.files.length ? theme.bold(theme.fg("accent", text.showFiles)) + " " : "";
|
|
276
377
|
this.showFilesButton = showFiles ? visibleWidth(text.showFiles) : 0;
|
|
277
378
|
this.viewButton = fixedUnified ? -1 : visibleWidth(showFiles + heading);
|
|
278
379
|
const header = showFiles + heading + theme.bold(theme.fg("accent", fixedUnified ? text.unifiedOnly : text.switchView));
|
|
279
|
-
const lines = [fit(header, width)];
|
|
280
|
-
const bodyHeight = Math.max(0,
|
|
380
|
+
const lines = [safeBg(theme, "customMessageBg", fit(header, width))];
|
|
381
|
+
const bodyHeight = Math.max(0, bodyEnd - 1);
|
|
382
|
+
this.contextButton = { start: -1, end: -1 };
|
|
281
383
|
if (!treeWidth && this.focus === "tree" && this.files.length) {
|
|
282
|
-
lines.push(fit(theme.bold(text.files), width), ...this.tree.render(width, Math.max(0, bodyHeight - 1), theme, true, this.selected));
|
|
384
|
+
lines.push(fit(theme.bold(theme.fg("accent", text.files)), width), ...this.tree.render(width, Math.max(0, bodyHeight - 1), theme, true, this.selected, this.language));
|
|
283
385
|
} else {
|
|
284
386
|
const label = file ? displayText(file.file) : localize(this.emptyMessage ?? text.noChanges, this.language);
|
|
285
387
|
const note = file?.cumulativeFallback ? text.cumulativeFallback
|
|
@@ -288,23 +390,57 @@ export class DiffScreen implements Component {
|
|
|
288
390
|
const effectiveView = this.patch?.effectiveView(view) ?? view;
|
|
289
391
|
const layoutLabel = fixedUnified ? text.fixedUnified : effectiveView === "split" ? text.split : text.unified;
|
|
290
392
|
const hint = this.viewHint ? text[this.viewHint] : note;
|
|
291
|
-
const
|
|
292
|
-
const
|
|
393
|
+
const changeCount = this.patch?.changeCount() ?? 0;
|
|
394
|
+
const changeLabel = changeCount > 0 ? ` · ${text.changeProgress(this.activeChange !== undefined ? this.activeChange + 1 : undefined, changeCount)}` : "";
|
|
395
|
+
const scrollPct = totalRows > height && height > 0 ? ` · ${Math.min(100, Math.round((this.top / Math.max(1, totalRows - height)) * 100))}%` : "";
|
|
396
|
+
const context = this.patch?.contextState();
|
|
397
|
+
const detailPrefix = counts + theme.fg("muted", ` · ${layoutLabel}${hint ? ` · ${hint}` : ""}${changeLabel}${scrollPct}`);
|
|
398
|
+
let detail = detailPrefix;
|
|
399
|
+
if (context) {
|
|
400
|
+
const buttonLabel = text[context];
|
|
401
|
+
const sep = theme.fg("muted", " · ");
|
|
402
|
+
const paneStart = treeWidth + inset;
|
|
403
|
+
const start = paneStart + visibleWidth(detailPrefix) + 3;
|
|
404
|
+
const end = Math.min(paneStart + patchWidth, start + visibleWidth(buttonLabel));
|
|
405
|
+
if (start < paneStart + patchWidth) this.contextButton = { start, end };
|
|
406
|
+
detail += sep + theme.bold(theme.fg("accent", buttonLabel));
|
|
407
|
+
}
|
|
408
|
+
detail += theme.fg("muted", ` · ${text.wrap}`);
|
|
409
|
+
const titleLine = this.focus === "diff" && file ? theme.bold(theme.fg("accent", label)) : theme.bold(label);
|
|
410
|
+
const metadata = [fitEllipsis(titleLine, patchWidth), fit(detail, patchWidth)];
|
|
293
411
|
const content = this.patch?.renderViewport(patchWidth, height, this.top, view, theme) ?? Array.from({ length: height }, () => fit("", patchWidth));
|
|
294
412
|
const pane = [...metadata, ...content];
|
|
295
|
-
const
|
|
413
|
+
const treeRule = treeWidth ? fit(safeFg(theme, "borderMuted", "muted", "─".repeat(Math.max(0, treeWidth - 1))), treeWidth - 1) : "";
|
|
414
|
+
const left = treeWidth ? [fit(theme.bold(theme.fg(this.focus === "tree" ? "accent" : "muted", text.files)), treeWidth - 1), treeRule, ...this.tree.render(treeWidth - 1, height, theme, this.focus === "tree", this.selected, this.language)] : [];
|
|
296
415
|
const divider = theme.fg(dividerActive ? "accent" : "borderMuted", "│");
|
|
297
416
|
const separator = dividerActive ? theme.bg("selectedBg", divider) : divider;
|
|
417
|
+
const showScrollbar = inset >= 1 && totalRows > height && height >= 2;
|
|
418
|
+
const thumbSize = showScrollbar ? Math.max(1, Math.round((height / totalRows) * height)) : 0;
|
|
419
|
+
const thumbStart = showScrollbar ? Math.min(height - thumbSize, Math.round((this.top / Math.max(1, totalRows - height)) * (height - thumbSize))) : 0;
|
|
420
|
+
const thumbGlyph = showScrollbar ? safeFg(theme, "scrollbarThumb", "muted", "┃") : "";
|
|
421
|
+
const trackGlyph = showScrollbar ? safeFg(theme, "scrollbarTrack", "borderMuted", "│") : "";
|
|
298
422
|
for (let i = 0; i < bodyHeight; i++) {
|
|
299
423
|
const tree = treeWidth ? fit(left[i] ?? "", treeWidth - 1) + separator : "";
|
|
300
|
-
|
|
424
|
+
let rightPad = " ".repeat(inset);
|
|
425
|
+
if (showScrollbar && i >= FILE_HEADER_HEIGHT) {
|
|
426
|
+
const contentRow = i - FILE_HEADER_HEIGHT;
|
|
427
|
+
const bar = contentRow >= thumbStart && contentRow < thumbStart + thumbSize ? thumbGlyph : trackGlyph;
|
|
428
|
+
rightPad = " ".repeat(inset - 1) + bar;
|
|
429
|
+
}
|
|
430
|
+
lines.push(fit(tree + " ".repeat(inset) + fit(pane[i] ?? "", patchWidth) + rightPad, width));
|
|
301
431
|
}
|
|
302
432
|
}
|
|
303
|
-
|
|
433
|
+
for (const line of notice) lines.push(fit(theme.fg("muted", line), width));
|
|
434
|
+
if (rows > 1) {
|
|
435
|
+
const dragPrefix = dividerActive ? `${text.dragDivider} · ` : "";
|
|
436
|
+
const footerText = text.footer(fixedUnified, this.focus, Math.max(0, width - visibleWidth(dragPrefix)));
|
|
437
|
+
const footer = (dividerActive ? theme.fg("accent", dragPrefix) : "") + theme.fg("muted", footerText);
|
|
438
|
+
lines.push(safeBg(theme, "customMessageBg", fit(footer, width)));
|
|
439
|
+
}
|
|
304
440
|
return lines.slice(0, rows);
|
|
305
441
|
}
|
|
306
442
|
|
|
307
443
|
invalidate(): void {
|
|
308
|
-
this.
|
|
444
|
+
for (const cached of this.patchCache.values()) cached.invalidate();
|
|
309
445
|
}
|
|
310
446
|
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { isAbsolute } from "node:path"
|
|
2
|
+
|
|
1
3
|
export type FileTreeItem = {
|
|
2
4
|
readonly file: string
|
|
3
5
|
readonly status?: "added" | "deleted" | "modified"
|
|
@@ -11,6 +13,7 @@ export type FileTreeNode = {
|
|
|
11
13
|
readonly depth: number
|
|
12
14
|
readonly kind: "directory" | "file"
|
|
13
15
|
readonly fileIndex?: number
|
|
16
|
+
readonly externalRoot?: boolean
|
|
14
17
|
}
|
|
15
18
|
|
|
16
19
|
export type FileTree = {
|
|
@@ -24,6 +27,7 @@ export type FileTreeRow = {
|
|
|
24
27
|
readonly kind: "directory" | "file"
|
|
25
28
|
readonly name: string
|
|
26
29
|
readonly fileIndex?: number
|
|
30
|
+
readonly externalRoot?: boolean
|
|
27
31
|
}
|
|
28
32
|
|
|
29
33
|
export function buildFileTree(files: readonly FileTreeItem[]): FileTree {
|
|
@@ -32,8 +36,10 @@ export function buildFileTree(files: readonly FileTreeItem[]): FileTree {
|
|
|
32
36
|
const directoryByPath = new Map<string, number>()
|
|
33
37
|
|
|
34
38
|
files.forEach((file, fileIndex) => {
|
|
35
|
-
const
|
|
39
|
+
const external = isAbsolute(file.file)
|
|
40
|
+
const segments = (external && process.platform === "win32" ? file.file.replaceAll("\\", "/") : file.file).split("/").filter(Boolean)
|
|
36
41
|
if (segments.length === 0) return
|
|
42
|
+
if (external) segments.unshift("External files")
|
|
37
43
|
|
|
38
44
|
const parent = segments.slice(0, -1).reduce(
|
|
39
45
|
(state, segment) => {
|
|
@@ -46,11 +52,12 @@ export function buildFileTree(files: readonly FileTreeItem[]): FileTree {
|
|
|
46
52
|
parent: state.id,
|
|
47
53
|
depth: state.depth,
|
|
48
54
|
kind: "directory",
|
|
55
|
+
...(external && state.depth === 0 ? { externalRoot: true } : {}),
|
|
49
56
|
})
|
|
50
57
|
directoryByPath.set(directoryPath, id)
|
|
51
58
|
return { id, path: directoryPath, depth: state.depth + 1 }
|
|
52
59
|
},
|
|
53
|
-
{ id: undefined as number | undefined, path: "", depth: 0 },
|
|
60
|
+
{ id: undefined as number | undefined, path: external ? "\0external" : "", depth: 0 },
|
|
54
61
|
)
|
|
55
62
|
|
|
56
63
|
addFileTreeNode(nodes, roots, {
|
|
@@ -91,6 +98,7 @@ export function flattenFileTree(tree: FileTree, expanded?: ReadonlySet<number>):
|
|
|
91
98
|
kind: node.kind,
|
|
92
99
|
name: chain.map((item) => item.name).join("/"),
|
|
93
100
|
fileIndex: node.fileIndex,
|
|
101
|
+
...(node.externalRoot ? { externalRoot: true } : {}),
|
|
94
102
|
})
|
|
95
103
|
if (!expanded || expanded.has(node.id)) last.children.forEach((child) => visit(child, depth + 1))
|
|
96
104
|
}
|
|
@@ -100,6 +108,7 @@ export function flattenFileTree(tree: FileTree, expanded?: ReadonlySet<number>):
|
|
|
100
108
|
|
|
101
109
|
function collapsedFileTreeDirectoryChain(tree: FileTree, id: number): FileTreeNode[] {
|
|
102
110
|
const node = tree.nodes[id]!
|
|
111
|
+
if (node.externalRoot) return [node]
|
|
103
112
|
const child = node.children.length === 1 ? tree.nodes[node.children[0]!] : undefined
|
|
104
113
|
if (child?.kind !== "directory") return [node]
|
|
105
114
|
return [node, ...collapsedFileTreeDirectoryChain(tree, child.id)]
|
package/src/tui/file-tree.ts
CHANGED
|
@@ -4,7 +4,8 @@ import {
|
|
|
4
4
|
allExpandedFileTreeDirectories, buildFileTree, fileTreeFileSelection,
|
|
5
5
|
flattenFileTree, movePatchFileIndex, orderedPatchFileIndexes, toggleFileTreeDirectory, type FileTreeRow,
|
|
6
6
|
} from "./file-tree-utils.ts";
|
|
7
|
-
import { displayText, fit } from "./text.ts";
|
|
7
|
+
import { displayText, fit, fitEllipsis, safeFg } from "./text.ts";
|
|
8
|
+
import { messages, type Language } from "../language.ts";
|
|
8
9
|
|
|
9
10
|
/** Tree state and rendering; patch selection remains owned by DiffScreen. */
|
|
10
11
|
export class FileTree {
|
|
@@ -72,13 +73,14 @@ export class FileTree {
|
|
|
72
73
|
this.revealSelection = false;
|
|
73
74
|
}
|
|
74
75
|
|
|
75
|
-
render(width: number, height: number, theme: Theme, focused: boolean, fileIndex: number | undefined): string[] {
|
|
76
|
+
render(width: number, height: number, theme: Theme, focused: boolean, fileIndex: number | undefined, language: Language = "en"): string[] {
|
|
76
77
|
this.top = Math.max(0, Math.min(this.top, this.rows.length - height));
|
|
77
78
|
if (this.revealSelection) {
|
|
78
79
|
this.top = Math.min(this.top, this.selected);
|
|
79
80
|
if (this.selected >= this.top + height) this.top = this.selected - height + 1;
|
|
80
81
|
this.revealSelection = false;
|
|
81
82
|
}
|
|
83
|
+
const rail = safeFg(theme, "borderMuted", "muted", "│ ");
|
|
82
84
|
return Array.from({ length: height }, (_, offset) => {
|
|
83
85
|
const index = this.top + offset;
|
|
84
86
|
const row = this.rows[index];
|
|
@@ -86,13 +88,15 @@ export class FileTree {
|
|
|
86
88
|
const status = row.fileIndex === undefined ? undefined : this.files[row.fileIndex]?.status;
|
|
87
89
|
const color = status === "added" ? "success" : status === "deleted" ? "error" : "muted";
|
|
88
90
|
const selected = focused ? index === this.selected : row.fileIndex !== undefined && row.fileIndex === fileIndex;
|
|
89
|
-
const prefix = theme.fg(selected ? "accent" : "muted", selected ? "› " : " ");
|
|
90
|
-
const indent =
|
|
91
|
-
const marker = row.kind === "directory" ? (this.expanded.has(row.id) ? "▾ " : "▸ ") : "
|
|
92
|
-
const
|
|
93
|
-
const name = selected
|
|
91
|
+
const prefix = theme.fg(selected && focused ? "accent" : "muted", selected ? "› " : " ");
|
|
92
|
+
const indent = rail.repeat(Math.max(0, Math.min(row.depth, Math.floor((width - 6) / 2))));
|
|
93
|
+
const marker = row.kind === "directory" ? (this.expanded.has(row.id) ? "▾ " : "▸ ") : "";
|
|
94
|
+
const raw = `${marker}${displayText(row.externalRoot ? messages[language].externalFiles : row.name)}${row.kind === "directory" ? "/" : ""}`;
|
|
95
|
+
const name = selected
|
|
96
|
+
? (focused ? theme.bold(theme.fg("accent", raw)) : raw)
|
|
97
|
+
: row.kind === "directory" ? theme.fg("muted", raw) : raw;
|
|
94
98
|
const badge = theme.fg(color, status === "added" ? "A" : status === "deleted" ? "D" : status === "modified" ? "M" : " ");
|
|
95
|
-
const line =
|
|
99
|
+
const line = fitEllipsis(prefix + badge + " " + indent + name, width);
|
|
96
100
|
return selected ? theme.bg("selectedBg", line) : line;
|
|
97
101
|
});
|
|
98
102
|
}
|