@lyrumu/pi-diff 0.0.0-stage → 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,103 @@
1
+ import type { Theme } from "@earendil-works/pi-coding-agent";
2
+ import type { FilePatch } from "../diff-engine.ts";
3
+ import {
4
+ allExpandedFileTreeDirectories, buildFileTree, fileTreeFileSelection,
5
+ flattenFileTree, movePatchFileIndex, orderedPatchFileIndexes, toggleFileTreeDirectory, type FileTreeRow,
6
+ } from "./file-tree-utils.ts";
7
+ import { displayText, fit, fitEllipsis, safeFg } from "./text.ts";
8
+ import { messages, type Language } from "../language.ts";
9
+
10
+ /** Tree state and rendering; patch selection remains owned by DiffScreen. */
11
+ export class FileTree {
12
+ private readonly files: readonly FilePatch[];
13
+ private readonly tree: ReturnType<typeof buildFileTree>;
14
+ private expanded: ReadonlySet<number>;
15
+ private rows: FileTreeRow[];
16
+ private readonly fileIndexes: number[];
17
+ private selected = 0;
18
+ private top = 0;
19
+ private revealSelection = true;
20
+
21
+ constructor(files: readonly FilePatch[]) {
22
+ this.files = files;
23
+ this.tree = buildFileTree(files);
24
+ this.expanded = allExpandedFileTreeDirectories(this.tree);
25
+ this.rows = flattenFileTree(this.tree, this.expanded);
26
+ this.fileIndexes = orderedPatchFileIndexes(this.rows);
27
+ this.selected = Math.max(0, this.rows.findIndex((row) => row.fileIndex !== undefined));
28
+ }
29
+
30
+ selectedFile(): number | undefined {
31
+ return this.rows[this.selected]?.fileIndex;
32
+ }
33
+
34
+ moveFile(current: number | undefined, offset: number): number | undefined {
35
+ return movePatchFileIndex(this.fileIndexes, current, offset);
36
+ }
37
+
38
+ move(offset: number): number | undefined {
39
+ this.selected = Math.max(0, Math.min(this.rows.length - 1, this.selected + offset));
40
+ this.revealSelection = true;
41
+ return this.selectedFile();
42
+ }
43
+
44
+ toggle(): number | undefined {
45
+ const row = this.rows[this.selected];
46
+ if (!row) return undefined;
47
+ this.expanded = toggleFileTreeDirectory(this.tree, this.expanded, row.id);
48
+ this.rows = flattenFileTree(this.tree, this.expanded);
49
+ this.selected = Math.max(0, this.rows.findIndex((item) => item.id === row.id));
50
+ this.revealSelection = true;
51
+ return this.selectedFile();
52
+ }
53
+
54
+ selectFile(index: number): void {
55
+ const selection = fileTreeFileSelection(this.tree, index);
56
+ if (!selection) return;
57
+ this.expanded = new Set([...this.expanded, ...selection.expandedNodes]);
58
+ this.rows = flattenFileTree(this.tree, this.expanded);
59
+ this.selected = Math.max(0, this.rows.findIndex((row) => row.fileIndex === index));
60
+ this.revealSelection = true;
61
+ }
62
+
63
+ clickRow(offset: number): number | undefined {
64
+ const index = this.top + offset;
65
+ if (offset < 0 || !this.rows[index]) return undefined;
66
+ this.selected = index;
67
+ this.revealSelection = true;
68
+ return this.rows[index]!.kind === "directory" ? this.toggle() : this.selectedFile();
69
+ }
70
+
71
+ scroll(offset: number, height: number): void {
72
+ this.top = Math.max(0, Math.min(this.rows.length - Math.max(1, height), this.top + offset));
73
+ this.revealSelection = false;
74
+ }
75
+
76
+ render(width: number, height: number, theme: Theme, focused: boolean, fileIndex: number | undefined, language: Language = "en"): string[] {
77
+ this.top = Math.max(0, Math.min(this.top, this.rows.length - height));
78
+ if (this.revealSelection) {
79
+ this.top = Math.min(this.top, this.selected);
80
+ if (this.selected >= this.top + height) this.top = this.selected - height + 1;
81
+ this.revealSelection = false;
82
+ }
83
+ const rail = safeFg(theme, "borderMuted", "muted", "│ ");
84
+ return Array.from({ length: height }, (_, offset) => {
85
+ const index = this.top + offset;
86
+ const row = this.rows[index];
87
+ if (!row) return fit("", width);
88
+ const status = row.fileIndex === undefined ? undefined : this.files[row.fileIndex]?.status;
89
+ const color = status === "added" ? "success" : status === "deleted" ? "error" : "muted";
90
+ const selected = focused ? index === this.selected : row.fileIndex !== undefined && row.fileIndex === fileIndex;
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;
98
+ const badge = theme.fg(color, status === "added" ? "A" : status === "deleted" ? "D" : status === "modified" ? "M" : " ");
99
+ const line = fitEllipsis(prefix + badge + " " + indent + name, width);
100
+ return selected ? theme.bg("selectedBg", line) : line;
101
+ });
102
+ }
103
+ }