@nandoblanco/opencode-local-backup 0.0.0-stage → 1.0.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.
- package/LICENSE +21 -0
- package/README.md +341 -2
- package/assets/screenshot.png +0 -0
- package/index.js +1457 -0
- package/package.json +68 -4
- package/tui.ts +1443 -0
package/tui.ts
ADDED
|
@@ -0,0 +1,1443 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import type { TuiPluginContext } from "@opencode-ai/plugin/tui";
|
|
5
|
+
import { TextAttributes } from "@opentui/core";
|
|
6
|
+
import {
|
|
7
|
+
createComponent,
|
|
8
|
+
createElement,
|
|
9
|
+
createTextNode,
|
|
10
|
+
effect,
|
|
11
|
+
insert,
|
|
12
|
+
insertNode,
|
|
13
|
+
setProp,
|
|
14
|
+
use
|
|
15
|
+
} from "@opentui/solid";
|
|
16
|
+
import {
|
|
17
|
+
createEffect,
|
|
18
|
+
createMemo,
|
|
19
|
+
createSignal,
|
|
20
|
+
For,
|
|
21
|
+
onCleanup,
|
|
22
|
+
onMount,
|
|
23
|
+
Show
|
|
24
|
+
} from "solid-js";
|
|
25
|
+
import {
|
|
26
|
+
listBackups,
|
|
27
|
+
computeUnifiedDiff,
|
|
28
|
+
restoreBackupFile,
|
|
29
|
+
openBackupFile,
|
|
30
|
+
openBackupDiff,
|
|
31
|
+
pruneOldBackups,
|
|
32
|
+
readBackupSettings,
|
|
33
|
+
writeBackupSettings
|
|
34
|
+
} from "./index.js";
|
|
35
|
+
|
|
36
|
+
interface BackupEntry {
|
|
37
|
+
id: string;
|
|
38
|
+
snapshotId: string;
|
|
39
|
+
root: string;
|
|
40
|
+
slug: string;
|
|
41
|
+
tool: string;
|
|
42
|
+
timeMs: number;
|
|
43
|
+
relativePath: string;
|
|
44
|
+
fileName: string;
|
|
45
|
+
backupPath: string;
|
|
46
|
+
originalPath: string;
|
|
47
|
+
size: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface DialogProps {
|
|
51
|
+
context: TuiPluginContext;
|
|
52
|
+
workspaceDir: string;
|
|
53
|
+
initialQuery: string;
|
|
54
|
+
close: () => void;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
type ToolFilter = "all" | "edit" | "write" | "patch" | "pre-restore";
|
|
58
|
+
type DateFilter = "all" | "24h" | "7d" | "30d";
|
|
59
|
+
|
|
60
|
+
const TOOL_CYCLE: ToolFilter[] = ["all", "edit", "write", "patch", "pre-restore"];
|
|
61
|
+
const DATE_CYCLE: DateFilter[] = ["all", "24h", "7d", "30d"];
|
|
62
|
+
const RETENTION_PRESETS = [30, 14, 7, 5, 0];
|
|
63
|
+
|
|
64
|
+
function formatAge(timeMs: number): string {
|
|
65
|
+
if (!timeMs) return "";
|
|
66
|
+
const diffSec = Math.max(0, Math.floor((Date.now() - timeMs) / 1000));
|
|
67
|
+
if (diffSec < 60) return `${diffSec}s ago`;
|
|
68
|
+
const diffMin = Math.floor(diffSec / 60);
|
|
69
|
+
if (diffMin < 60) return `${diffMin}m ago`;
|
|
70
|
+
const diffHr = Math.floor(diffMin / 60);
|
|
71
|
+
if (diffHr < 24) return `${diffHr}h ago`;
|
|
72
|
+
const diffDays = Math.floor(diffHr / 24);
|
|
73
|
+
return `${diffDays}d ago`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function formatBytes(bytes: number): string {
|
|
77
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
78
|
+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
|
79
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function formatParentBackupPath(entry: BackupEntry): string {
|
|
83
|
+
const parentDir = path.dirname(entry.backupPath);
|
|
84
|
+
const rootParent = path.dirname(entry.root);
|
|
85
|
+
const relFromParent = path.relative(rootParent, parentDir);
|
|
86
|
+
if (relFromParent && !relFromParent.startsWith("..")) {
|
|
87
|
+
return relFromParent.replace(/\\/g, "/");
|
|
88
|
+
}
|
|
89
|
+
return parentDir.replace(/\\/g, "/");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function splitHighlightedText(
|
|
93
|
+
text: string,
|
|
94
|
+
term: string
|
|
95
|
+
): Array<{ text: string; match: boolean }> {
|
|
96
|
+
const words = term
|
|
97
|
+
.trim()
|
|
98
|
+
.toLowerCase()
|
|
99
|
+
.split(/\s+/)
|
|
100
|
+
.filter((w) => w.length > 0);
|
|
101
|
+
if (!words.length) {
|
|
102
|
+
return [{ text, match: false }];
|
|
103
|
+
}
|
|
104
|
+
const parts: Array<{ text: string; match: boolean }> = [];
|
|
105
|
+
const lower = text.toLowerCase();
|
|
106
|
+
let cursor = 0;
|
|
107
|
+
|
|
108
|
+
while (cursor < text.length) {
|
|
109
|
+
let bestIdx = -1;
|
|
110
|
+
let bestLen = 0;
|
|
111
|
+
for (const w of words) {
|
|
112
|
+
const idx = lower.indexOf(w, cursor);
|
|
113
|
+
if (idx !== -1 && (bestIdx === -1 || idx < bestIdx || (idx === bestIdx && w.length > bestLen))) {
|
|
114
|
+
bestIdx = idx;
|
|
115
|
+
bestLen = w.length;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (bestIdx === -1) {
|
|
119
|
+
parts.push({ text: text.slice(cursor), match: false });
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
if (bestIdx > cursor) {
|
|
123
|
+
parts.push({ text: text.slice(cursor, bestIdx), match: false });
|
|
124
|
+
}
|
|
125
|
+
parts.push({ text: text.slice(bestIdx, bestIdx + bestLen), match: true });
|
|
126
|
+
cursor = bestIdx + bestLen;
|
|
127
|
+
}
|
|
128
|
+
return parts.length ? parts : [{ text, match: false }];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function createTuiRenderer() {
|
|
132
|
+
function el(
|
|
133
|
+
tag: string,
|
|
134
|
+
props?: Record<string, unknown> | null,
|
|
135
|
+
...children: unknown[]
|
|
136
|
+
) {
|
|
137
|
+
const node = createElement(tag);
|
|
138
|
+
if (props) {
|
|
139
|
+
for (const [key, val] of Object.entries(props)) {
|
|
140
|
+
if (val === undefined) continue;
|
|
141
|
+
if (key === "ref" && typeof val === "function") {
|
|
142
|
+
use(val as (e: unknown) => void, node, undefined);
|
|
143
|
+
} else if (
|
|
144
|
+
typeof val === "function" &&
|
|
145
|
+
!key.startsWith("on") &&
|
|
146
|
+
key !== "renderBefore"
|
|
147
|
+
) {
|
|
148
|
+
effect((prev: unknown) => setProp(node, key, (val as () => unknown)(), prev));
|
|
149
|
+
} else {
|
|
150
|
+
setProp(node, key, val);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
for (const child of children) {
|
|
155
|
+
if (child === undefined || child === null || child === false) continue;
|
|
156
|
+
if (typeof child === "string") {
|
|
157
|
+
insertNode(node, createTextNode(child));
|
|
158
|
+
} else {
|
|
159
|
+
insert(node, child);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return node;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function AutoBackupDialog(props: DialogProps) {
|
|
166
|
+
const [query, setQuery] = createSignal(props.initialQuery || "");
|
|
167
|
+
const [toolFilter, setToolFilter] = createSignal<ToolFilter>("all");
|
|
168
|
+
const [dateFilter, setDateFilter] = createSignal<DateFilter>("all");
|
|
169
|
+
const [focusPane, setFocusPane] = createSignal<"search" | "list" | "preview">("search");
|
|
170
|
+
const [selectedIndex, setSelectedIndex] = createSignal(0);
|
|
171
|
+
const [statusMsg, setStatusMsg] = createSignal("");
|
|
172
|
+
const [refreshTick, setRefreshTick] = createSignal(0);
|
|
173
|
+
const [retentionDays, setRetentionDays] = createSignal(
|
|
174
|
+
readBackupSettings().retentionDays
|
|
175
|
+
);
|
|
176
|
+
const [actionPopupOpen, setActionPopupOpen] = createSignal(false);
|
|
177
|
+
const [actionChoice, setActionChoice] = createSignal<0 | 1 | 2>(0); // 0 = Restore, 1 = Open file, 2 = Open diff in editor
|
|
178
|
+
|
|
179
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
180
|
+
let rootRef: any;
|
|
181
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
182
|
+
let listScrollRef: any;
|
|
183
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
184
|
+
let previewScrollRef: any;
|
|
185
|
+
|
|
186
|
+
// Expand OpenCode's modal container to configured width (defaults to 80% terminal width)
|
|
187
|
+
const resolveDialogWidth = (): string | number => {
|
|
188
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
189
|
+
const opts = (props.context as any)?.options;
|
|
190
|
+
const raw =
|
|
191
|
+
opts?.dialogWidth ??
|
|
192
|
+
opts?.width ??
|
|
193
|
+
process.env.OPENCODE_BACKUP_DIALOG_WIDTH ??
|
|
194
|
+
process.env.OPENCODE_DIALOG_WIDTH ??
|
|
195
|
+
(readBackupSettings() as { dialogWidth?: string | number }).dialogWidth ??
|
|
196
|
+
"80%";
|
|
197
|
+
if (typeof raw === "number" && Number.isFinite(raw) && raw > 0) {
|
|
198
|
+
return raw <= 100 ? `${Math.round(raw)}%` : Math.round(raw);
|
|
199
|
+
}
|
|
200
|
+
if (typeof raw === "string") {
|
|
201
|
+
const trimmed = raw.trim();
|
|
202
|
+
if (/^\d+(\.\d+)?%$/.test(trimmed)) return trimmed;
|
|
203
|
+
const num = Number(trimmed);
|
|
204
|
+
if (Number.isFinite(num) && num > 0) {
|
|
205
|
+
return num <= 100 ? `${Math.round(num)}%` : Math.round(num);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return "80%";
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
const applyDialogWidth = () => {
|
|
212
|
+
if (!rootRef) return;
|
|
213
|
+
try {
|
|
214
|
+
const targetWidth = resolveDialogWidth();
|
|
215
|
+
let cursor = rootRef;
|
|
216
|
+
let dialogBox = rootRef;
|
|
217
|
+
while (cursor.parent && cursor.parent._positionType !== "absolute") {
|
|
218
|
+
cursor = cursor.parent;
|
|
219
|
+
dialogBox = cursor;
|
|
220
|
+
}
|
|
221
|
+
dialogBox.width = targetWidth;
|
|
222
|
+
dialogBox.maxWidth = targetWidth;
|
|
223
|
+
dialogBox.minWidth = 76;
|
|
224
|
+
|
|
225
|
+
let inner = rootRef;
|
|
226
|
+
while (inner && inner !== dialogBox) {
|
|
227
|
+
inner.width = "100%";
|
|
228
|
+
inner.maxWidth = "100%";
|
|
229
|
+
inner = inner.parent;
|
|
230
|
+
}
|
|
231
|
+
rootRef.requestRender?.();
|
|
232
|
+
} catch {
|
|
233
|
+
// ignore layout mutation errors
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
238
|
+
const theme = (): any => props.context.theme.surface("dialog");
|
|
239
|
+
|
|
240
|
+
const allEntries = createMemo<BackupEntry[]>(() => {
|
|
241
|
+
refreshTick();
|
|
242
|
+
return listBackups(props.workspaceDir, {
|
|
243
|
+
includeSubprojects: true
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
const filtered = createMemo<BackupEntry[]>(() => {
|
|
248
|
+
const q = query().trim().toLowerCase();
|
|
249
|
+
const terms = q ? q.split(/\s+/).filter(Boolean) : [];
|
|
250
|
+
const tFilter = toolFilter();
|
|
251
|
+
const dFilter = dateFilter();
|
|
252
|
+
const now = Date.now();
|
|
253
|
+
const maxAgeMs =
|
|
254
|
+
dFilter === "24h"
|
|
255
|
+
? 24 * 60 * 60 * 1000
|
|
256
|
+
: dFilter === "7d"
|
|
257
|
+
? 7 * 24 * 60 * 60 * 1000
|
|
258
|
+
: dFilter === "30d"
|
|
259
|
+
? 30 * 24 * 60 * 60 * 1000
|
|
260
|
+
: 0;
|
|
261
|
+
|
|
262
|
+
return allEntries().filter((item) => {
|
|
263
|
+
if (tFilter !== "all") {
|
|
264
|
+
const itemTool = (item.tool || "").toLowerCase();
|
|
265
|
+
const itemSlug = (item.slug || "").toLowerCase();
|
|
266
|
+
if (tFilter === "pre-restore") {
|
|
267
|
+
if (!itemSlug.includes("pre-restore") && itemTool !== "restore") return false;
|
|
268
|
+
} else if (tFilter === "patch") {
|
|
269
|
+
if (!itemTool.includes("patch") && !itemSlug.includes("patch")) return false;
|
|
270
|
+
} else if (itemTool !== tFilter && !itemSlug.includes(tFilter)) {
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
if (maxAgeMs > 0 && item.timeMs && now - item.timeMs > maxAgeMs) {
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
if (!terms.length) return true;
|
|
278
|
+
const haystack = `${item.relativePath} ${item.backupPath} ${item.originalPath} ${item.root} ${item.snapshotId} ${item.slug}`.toLowerCase();
|
|
279
|
+
return terms.every((t) => haystack.includes(t));
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
createEffect(() => {
|
|
284
|
+
query();
|
|
285
|
+
toolFilter();
|
|
286
|
+
dateFilter();
|
|
287
|
+
setSelectedIndex(0);
|
|
288
|
+
queueMicrotask(() => listScrollRef?.scrollTo?.(0));
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
const selectedEntry = createMemo<BackupEntry | null>(() => {
|
|
292
|
+
const list = filtered();
|
|
293
|
+
if (!list.length) return null;
|
|
294
|
+
const idx = Math.min(selectedIndex(), list.length - 1);
|
|
295
|
+
return list[idx] || null;
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
const diffPreview = createMemo(() => {
|
|
299
|
+
const sel = selectedEntry();
|
|
300
|
+
if (!sel) return null;
|
|
301
|
+
return computeUnifiedDiff(sel.backupPath, sel.originalPath, 42);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
function moveSelection(delta: number) {
|
|
305
|
+
const len = filtered().length;
|
|
306
|
+
if (len === 0) return;
|
|
307
|
+
const next = Math.max(0, Math.min(len - 1, selectedIndex() + delta));
|
|
308
|
+
setSelectedIndex(next);
|
|
309
|
+
queueMicrotask(() => {
|
|
310
|
+
listScrollRef?.scrollChildIntoView?.(`backup-row-${next}`);
|
|
311
|
+
previewScrollRef?.scrollTo?.(0);
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function cycleToolFilter() {
|
|
316
|
+
setToolFilter(
|
|
317
|
+
(prev) => TOOL_CYCLE[(TOOL_CYCLE.indexOf(prev) + 1) % TOOL_CYCLE.length]
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function cycleDateFilter() {
|
|
322
|
+
setDateFilter(
|
|
323
|
+
(prev) => DATE_CYCLE[(DATE_CYCLE.indexOf(prev) + 1) % DATE_CYCLE.length]
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function executeRestore(sel: BackupEntry) {
|
|
328
|
+
try {
|
|
329
|
+
restoreBackupFile(sel);
|
|
330
|
+
setActionPopupOpen(false);
|
|
331
|
+
setStatusMsg(`Restored ${sel.relativePath} from ${sel.snapshotId}`);
|
|
332
|
+
setRefreshTick((t) => t + 1);
|
|
333
|
+
props.context.ui.toast.show({
|
|
334
|
+
title: "Backup Restored",
|
|
335
|
+
message: `${sel.relativePath} (${formatAge(sel.timeMs)})`,
|
|
336
|
+
variant: "success"
|
|
337
|
+
});
|
|
338
|
+
} catch (err) {
|
|
339
|
+
setActionPopupOpen(false);
|
|
340
|
+
setStatusMsg(
|
|
341
|
+
`Restore failed: ${err instanceof Error ? err.message : String(err)}`
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function executeOpenFile(sel: BackupEntry) {
|
|
347
|
+
try {
|
|
348
|
+
const res = openBackupFile(sel.backupPath);
|
|
349
|
+
setActionPopupOpen(false);
|
|
350
|
+
setStatusMsg(`Opened backup in ${res.launcher}: ${sel.backupPath}`);
|
|
351
|
+
props.context.ui.toast.show({
|
|
352
|
+
title: "Opened Backup File",
|
|
353
|
+
message: sel.backupPath,
|
|
354
|
+
variant: "info"
|
|
355
|
+
});
|
|
356
|
+
} catch (err) {
|
|
357
|
+
setActionPopupOpen(false);
|
|
358
|
+
setStatusMsg(
|
|
359
|
+
`Open failed: ${err instanceof Error ? err.message : String(err)}`
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function executeOpenDiff(sel: BackupEntry) {
|
|
365
|
+
try {
|
|
366
|
+
const res = openBackupDiff(sel.backupPath, sel.originalPath);
|
|
367
|
+
setActionPopupOpen(false);
|
|
368
|
+
setStatusMsg(`Opened diff (${res.launcher}): ${sel.relativePath}`);
|
|
369
|
+
props.context.ui.toast.show({
|
|
370
|
+
title: "Opened Backup Diff",
|
|
371
|
+
message: `${sel.relativePath} (${sel.snapshotId} vs disk)`,
|
|
372
|
+
variant: "info"
|
|
373
|
+
});
|
|
374
|
+
} catch (err) {
|
|
375
|
+
setActionPopupOpen(false);
|
|
376
|
+
setStatusMsg(
|
|
377
|
+
`Diff failed: ${err instanceof Error ? err.message : String(err)}`
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function handlePrune() {
|
|
383
|
+
const days = retentionDays();
|
|
384
|
+
if (days <= 0) {
|
|
385
|
+
setStatusMsg(
|
|
386
|
+
"Auto-prune is set to 0 (Never). Press Ctrl+R to change retention days first."
|
|
387
|
+
);
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
const sel = selectedEntry();
|
|
391
|
+
const targetRoot = sel ? sel.root : props.workspaceDir;
|
|
392
|
+
const res = pruneOldBackups(targetRoot, days);
|
|
393
|
+
setStatusMsg(
|
|
394
|
+
`Pruned ${res.removed} snapshot(s) >${days}d in ${path.basename(targetRoot)}`
|
|
395
|
+
);
|
|
396
|
+
setRefreshTick((t) => t + 1);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function handleCycleRetention() {
|
|
400
|
+
const cur = retentionDays();
|
|
401
|
+
const idx = RETENTION_PRESETS.indexOf(cur);
|
|
402
|
+
const next =
|
|
403
|
+
idx === -1 ? 30 : RETENTION_PRESETS[(idx + 1) % RETENTION_PRESETS.length];
|
|
404
|
+
const saved = writeBackupSettings({ retentionDays: next });
|
|
405
|
+
setRetentionDays(saved.settings.retentionDays);
|
|
406
|
+
const label = next === 0 ? "Never (0 days)" : `${next} days`;
|
|
407
|
+
setStatusMsg(`Auto-prune retention saved: ${label}`);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
411
|
+
const handleKeyPress = (evt: any) => {
|
|
412
|
+
if (!evt) return;
|
|
413
|
+
const name = String(evt.name || "").toLowerCase();
|
|
414
|
+
|
|
415
|
+
// When the 3-option Action Popup is open, route keys to it
|
|
416
|
+
if (actionPopupOpen()) {
|
|
417
|
+
if (name === "escape" || name === "q") {
|
|
418
|
+
evt.preventDefault?.();
|
|
419
|
+
evt.stopPropagation?.();
|
|
420
|
+
setActionPopupOpen(false);
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
if (
|
|
424
|
+
name === "up" ||
|
|
425
|
+
name === "left" ||
|
|
426
|
+
(evt.ctrl && (name === "k" || name === "p"))
|
|
427
|
+
) {
|
|
428
|
+
evt.preventDefault?.();
|
|
429
|
+
evt.stopPropagation?.();
|
|
430
|
+
setActionChoice((c) => ((c + 2) % 3) as 0 | 1 | 2);
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
if (
|
|
434
|
+
name === "down" ||
|
|
435
|
+
name === "right" ||
|
|
436
|
+
name === "tab" ||
|
|
437
|
+
(evt.ctrl && (name === "j" || name === "n"))
|
|
438
|
+
) {
|
|
439
|
+
evt.preventDefault?.();
|
|
440
|
+
evt.stopPropagation?.();
|
|
441
|
+
setActionChoice((c) => ((c + 1) % 3) as 0 | 1 | 2);
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
if (name === "1" || name === "r") {
|
|
445
|
+
evt.preventDefault?.();
|
|
446
|
+
evt.stopPropagation?.();
|
|
447
|
+
const sel = selectedEntry();
|
|
448
|
+
if (sel) executeRestore(sel);
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
if (name === "2" || name === "o") {
|
|
452
|
+
evt.preventDefault?.();
|
|
453
|
+
evt.stopPropagation?.();
|
|
454
|
+
const sel = selectedEntry();
|
|
455
|
+
if (sel) executeOpenFile(sel);
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
if (name === "3" || name === "d") {
|
|
459
|
+
evt.preventDefault?.();
|
|
460
|
+
evt.stopPropagation?.();
|
|
461
|
+
const sel = selectedEntry();
|
|
462
|
+
if (sel) executeOpenDiff(sel);
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
if (name === "return" || name === "enter") {
|
|
466
|
+
evt.preventDefault?.();
|
|
467
|
+
evt.stopPropagation?.();
|
|
468
|
+
const sel = selectedEntry();
|
|
469
|
+
if (!sel) {
|
|
470
|
+
setActionPopupOpen(false);
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
if (actionChoice() === 0) {
|
|
474
|
+
executeRestore(sel);
|
|
475
|
+
} else if (actionChoice() === 1) {
|
|
476
|
+
executeOpenFile(sel);
|
|
477
|
+
} else {
|
|
478
|
+
executeOpenDiff(sel);
|
|
479
|
+
}
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
if (name === "escape") {
|
|
486
|
+
evt.preventDefault?.();
|
|
487
|
+
evt.stopPropagation?.();
|
|
488
|
+
props.close();
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
if (name === "tab") {
|
|
493
|
+
evt.preventDefault?.();
|
|
494
|
+
evt.stopPropagation?.();
|
|
495
|
+
const order: Array<"search" | "list" | "preview"> = ["search", "list", "preview"];
|
|
496
|
+
const idx = order.indexOf(focusPane());
|
|
497
|
+
const next = order[(idx + (evt.shift ? -1 : 1) + order.length) % order.length];
|
|
498
|
+
setFocusPane(next);
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
if (
|
|
503
|
+
name === "up" ||
|
|
504
|
+
(evt.ctrl && (name === "k" || name === "p")) ||
|
|
505
|
+
((focusPane() === "list" || focusPane() === "preview") && !evt.ctrl && name === "k")
|
|
506
|
+
) {
|
|
507
|
+
evt.preventDefault?.();
|
|
508
|
+
evt.stopPropagation?.();
|
|
509
|
+
if (focusPane() === "preview") {
|
|
510
|
+
previewScrollRef?.scrollBy?.(-2);
|
|
511
|
+
} else {
|
|
512
|
+
moveSelection(-1);
|
|
513
|
+
}
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
if (
|
|
518
|
+
name === "down" ||
|
|
519
|
+
(evt.ctrl && (name === "j" || name === "n")) ||
|
|
520
|
+
((focusPane() === "list" || focusPane() === "preview") && !evt.ctrl && name === "j")
|
|
521
|
+
) {
|
|
522
|
+
evt.preventDefault?.();
|
|
523
|
+
evt.stopPropagation?.();
|
|
524
|
+
if (focusPane() === "preview") {
|
|
525
|
+
previewScrollRef?.scrollBy?.(2);
|
|
526
|
+
} else {
|
|
527
|
+
moveSelection(1);
|
|
528
|
+
}
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
if (name === "pageup") {
|
|
533
|
+
evt.preventDefault?.();
|
|
534
|
+
evt.stopPropagation?.();
|
|
535
|
+
if (focusPane() === "preview") {
|
|
536
|
+
previewScrollRef?.scrollBy?.(-8);
|
|
537
|
+
} else {
|
|
538
|
+
moveSelection(-6);
|
|
539
|
+
}
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
if (name === "pagedown") {
|
|
544
|
+
evt.preventDefault?.();
|
|
545
|
+
evt.stopPropagation?.();
|
|
546
|
+
if (focusPane() === "preview") {
|
|
547
|
+
previewScrollRef?.scrollBy?.(8);
|
|
548
|
+
} else {
|
|
549
|
+
moveSelection(6);
|
|
550
|
+
}
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
if (evt.ctrl && name === "u") {
|
|
555
|
+
evt.preventDefault?.();
|
|
556
|
+
evt.stopPropagation?.();
|
|
557
|
+
setQuery("");
|
|
558
|
+
setToolFilter("all");
|
|
559
|
+
setDateFilter("all");
|
|
560
|
+
setSelectedIndex(0);
|
|
561
|
+
listScrollRef?.scrollTo?.(0);
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
if (name === "return" || name === "enter") {
|
|
566
|
+
evt.preventDefault?.();
|
|
567
|
+
evt.stopPropagation?.();
|
|
568
|
+
if (selectedEntry()) {
|
|
569
|
+
setActionChoice(0);
|
|
570
|
+
setActionPopupOpen(true);
|
|
571
|
+
}
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
if (evt.ctrl && name === "t") {
|
|
576
|
+
evt.preventDefault?.();
|
|
577
|
+
evt.stopPropagation?.();
|
|
578
|
+
cycleToolFilter();
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
if (evt.ctrl && name === "g") {
|
|
583
|
+
evt.preventDefault?.();
|
|
584
|
+
evt.stopPropagation?.();
|
|
585
|
+
cycleDateFilter();
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
if (evt.ctrl && name === "r") {
|
|
590
|
+
evt.preventDefault?.();
|
|
591
|
+
evt.stopPropagation?.();
|
|
592
|
+
handleCycleRetention();
|
|
593
|
+
return;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
if (evt.ctrl && name === "d") {
|
|
597
|
+
evt.preventDefault?.();
|
|
598
|
+
evt.stopPropagation?.();
|
|
599
|
+
handlePrune();
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
};
|
|
603
|
+
|
|
604
|
+
onMount(() => {
|
|
605
|
+
props.context.renderer.keyInput.prependListener("keypress", handleKeyPress);
|
|
606
|
+
applyDialogWidth();
|
|
607
|
+
queueMicrotask(() => {
|
|
608
|
+
applyDialogWidth();
|
|
609
|
+
});
|
|
610
|
+
});
|
|
611
|
+
|
|
612
|
+
onCleanup(() => {
|
|
613
|
+
props.context.renderer.keyInput.removeListener("keypress", handleKeyPress);
|
|
614
|
+
});
|
|
615
|
+
|
|
616
|
+
const Pill = (pillProps: {
|
|
617
|
+
label: () => string;
|
|
618
|
+
shortcut?: string;
|
|
619
|
+
active: () => boolean;
|
|
620
|
+
onClick: () => void;
|
|
621
|
+
}) =>
|
|
622
|
+
el(
|
|
623
|
+
"box",
|
|
624
|
+
{
|
|
625
|
+
flexDirection: "row",
|
|
626
|
+
flexShrink: 0,
|
|
627
|
+
paddingX: 1,
|
|
628
|
+
backgroundColor: () =>
|
|
629
|
+
pillProps.active()
|
|
630
|
+
? theme().background.action.primary.focused
|
|
631
|
+
: theme().background.raised.base,
|
|
632
|
+
onMouseUp: pillProps.onClick
|
|
633
|
+
},
|
|
634
|
+
el(
|
|
635
|
+
"text",
|
|
636
|
+
{
|
|
637
|
+
fg: () =>
|
|
638
|
+
pillProps.active()
|
|
639
|
+
? theme().text.action.primary.focused
|
|
640
|
+
: theme().text.base,
|
|
641
|
+
attributes: () => (pillProps.active() ? TextAttributes.BOLD : undefined)
|
|
642
|
+
},
|
|
643
|
+
pillProps.label
|
|
644
|
+
),
|
|
645
|
+
pillProps.shortcut
|
|
646
|
+
? el(
|
|
647
|
+
"text",
|
|
648
|
+
{
|
|
649
|
+
fg: () =>
|
|
650
|
+
pillProps.active()
|
|
651
|
+
? theme().text.action.primary.focused
|
|
652
|
+
: theme().text.muted
|
|
653
|
+
},
|
|
654
|
+
` ${pillProps.shortcut}`
|
|
655
|
+
)
|
|
656
|
+
: null
|
|
657
|
+
);
|
|
658
|
+
|
|
659
|
+
return el(
|
|
660
|
+
"box",
|
|
661
|
+
{
|
|
662
|
+
ref: (r: unknown) => {
|
|
663
|
+
rootRef = r;
|
|
664
|
+
queueMicrotask(applyDialogWidth);
|
|
665
|
+
},
|
|
666
|
+
width: "100%",
|
|
667
|
+
height: 28,
|
|
668
|
+
flexDirection: "column",
|
|
669
|
+
backgroundColor: () => theme().background.base
|
|
670
|
+
},
|
|
671
|
+
// 1. Header Bar
|
|
672
|
+
el(
|
|
673
|
+
"box",
|
|
674
|
+
{
|
|
675
|
+
flexDirection: "row",
|
|
676
|
+
justifyContent: "space-between",
|
|
677
|
+
alignItems: "center",
|
|
678
|
+
paddingX: 2,
|
|
679
|
+
paddingTop: 1,
|
|
680
|
+
flexShrink: 0
|
|
681
|
+
},
|
|
682
|
+
el(
|
|
683
|
+
"box",
|
|
684
|
+
{ flexDirection: "row", gap: 2, alignItems: "center" },
|
|
685
|
+
el(
|
|
686
|
+
"text",
|
|
687
|
+
{
|
|
688
|
+
fg: () => theme().text.base,
|
|
689
|
+
attributes: TextAttributes.BOLD
|
|
690
|
+
},
|
|
691
|
+
"Auto-Backup Snapshots"
|
|
692
|
+
),
|
|
693
|
+
el(
|
|
694
|
+
"text",
|
|
695
|
+
{ fg: () => theme().text.muted },
|
|
696
|
+
() =>
|
|
697
|
+
statusMsg() ||
|
|
698
|
+
`${filtered().length} of ${allEntries().length} snapshots`
|
|
699
|
+
)
|
|
700
|
+
),
|
|
701
|
+
el(
|
|
702
|
+
"box",
|
|
703
|
+
{ flexDirection: "row", gap: 2, alignItems: "center" },
|
|
704
|
+
el(
|
|
705
|
+
"text",
|
|
706
|
+
{ fg: () => theme().text.muted },
|
|
707
|
+
() => props.context.ui.format.path(props.workspaceDir)
|
|
708
|
+
),
|
|
709
|
+
el(
|
|
710
|
+
"box",
|
|
711
|
+
{ paddingX: 1, onMouseUp: () => props.close() },
|
|
712
|
+
el("text", { fg: () => theme().text.muted }, "esc")
|
|
713
|
+
)
|
|
714
|
+
)
|
|
715
|
+
),
|
|
716
|
+
|
|
717
|
+
// 2. Search Input + Colored Filter Pills Bar (matching session-search & local-memory)
|
|
718
|
+
el(
|
|
719
|
+
"box",
|
|
720
|
+
{
|
|
721
|
+
flexDirection: "column",
|
|
722
|
+
paddingX: 2,
|
|
723
|
+
paddingY: 1,
|
|
724
|
+
gap: 1,
|
|
725
|
+
flexShrink: 0,
|
|
726
|
+
border: ["bottom"],
|
|
727
|
+
borderColor: () => theme().border.base
|
|
728
|
+
},
|
|
729
|
+
el(
|
|
730
|
+
"box",
|
|
731
|
+
{
|
|
732
|
+
flexDirection: "row",
|
|
733
|
+
alignItems: "center",
|
|
734
|
+
gap: 1
|
|
735
|
+
},
|
|
736
|
+
el(
|
|
737
|
+
"text",
|
|
738
|
+
{
|
|
739
|
+
fg: () =>
|
|
740
|
+
focusPane() === "search" && !actionPopupOpen()
|
|
741
|
+
? theme().text.action.primary.base
|
|
742
|
+
: theme().text.muted,
|
|
743
|
+
attributes: TextAttributes.BOLD
|
|
744
|
+
},
|
|
745
|
+
"›"
|
|
746
|
+
),
|
|
747
|
+
el("input", {
|
|
748
|
+
id: "auto-backup-search-input",
|
|
749
|
+
flexGrow: 1,
|
|
750
|
+
value: () => query(),
|
|
751
|
+
focused: () => focusPane() === "search" && !actionPopupOpen(),
|
|
752
|
+
maxLength: Number.POSITIVE_INFINITY,
|
|
753
|
+
placeholder:
|
|
754
|
+
"Filter backups by file path, parent folder, or snapshot slug... (0 tokens)",
|
|
755
|
+
placeholderColor: () => theme().text.muted,
|
|
756
|
+
textColor: () => theme().text.base,
|
|
757
|
+
focusedTextColor: () => theme().text.formfield.focused,
|
|
758
|
+
cursorColor: () => theme().text.formfield.focused,
|
|
759
|
+
backgroundColor: () => theme().background.base,
|
|
760
|
+
focusedBackgroundColor: () => theme().background.formfield.focused,
|
|
761
|
+
onMouseDown: () => setFocusPane("search"),
|
|
762
|
+
onInput: (val: string) => {
|
|
763
|
+
setQuery(typeof val === "string" ? val : "");
|
|
764
|
+
}
|
|
765
|
+
})
|
|
766
|
+
),
|
|
767
|
+
// Colored Filter Pills Row
|
|
768
|
+
el(
|
|
769
|
+
"box",
|
|
770
|
+
{
|
|
771
|
+
flexDirection: "row",
|
|
772
|
+
flexWrap: "wrap",
|
|
773
|
+
gap: 1,
|
|
774
|
+
alignItems: "center"
|
|
775
|
+
},
|
|
776
|
+
Pill({
|
|
777
|
+
label: () => `tool:${toolFilter()}`,
|
|
778
|
+
shortcut: "^t",
|
|
779
|
+
active: () => toolFilter() !== "all",
|
|
780
|
+
onClick: cycleToolFilter
|
|
781
|
+
}),
|
|
782
|
+
Pill({
|
|
783
|
+
label: () => `date:${dateFilter()}`,
|
|
784
|
+
shortcut: "^g",
|
|
785
|
+
active: () => dateFilter() !== "all",
|
|
786
|
+
onClick: cycleDateFilter
|
|
787
|
+
}),
|
|
788
|
+
Pill({
|
|
789
|
+
label: () =>
|
|
790
|
+
`prune:${
|
|
791
|
+
retentionDays() === 0 ? "never (0d)" : `${retentionDays()}d`
|
|
792
|
+
}`,
|
|
793
|
+
shortcut: "^r",
|
|
794
|
+
active: () => retentionDays() !== 30,
|
|
795
|
+
onClick: handleCycleRetention
|
|
796
|
+
}),
|
|
797
|
+
Pill({
|
|
798
|
+
label: () => "prune now",
|
|
799
|
+
shortcut: "^d",
|
|
800
|
+
active: () => false,
|
|
801
|
+
onClick: handlePrune
|
|
802
|
+
})
|
|
803
|
+
)
|
|
804
|
+
),
|
|
805
|
+
|
|
806
|
+
// 3. Action Confirmation Sub-Popup (wrapped in its own container so @opentui/solid Show cleanup never clears rootRef siblings)
|
|
807
|
+
el(
|
|
808
|
+
"box",
|
|
809
|
+
{ flexDirection: "column", flexShrink: 0 },
|
|
810
|
+
createComponent(Show, {
|
|
811
|
+
get when() {
|
|
812
|
+
return actionPopupOpen() && Boolean(selectedEntry());
|
|
813
|
+
},
|
|
814
|
+
get children() {
|
|
815
|
+
return el(
|
|
816
|
+
"box",
|
|
817
|
+
{
|
|
818
|
+
flexDirection: "column",
|
|
819
|
+
marginX: 2,
|
|
820
|
+
marginTop: 1,
|
|
821
|
+
paddingX: 2,
|
|
822
|
+
paddingY: 1,
|
|
823
|
+
gap: 1,
|
|
824
|
+
border: ["top", "bottom", "left", "right"],
|
|
825
|
+
borderColor: () => theme().text.action.primary.base,
|
|
826
|
+
backgroundColor: () => theme().background.raised.base,
|
|
827
|
+
flexShrink: 0
|
|
828
|
+
},
|
|
829
|
+
el(
|
|
830
|
+
"box",
|
|
831
|
+
{ flexDirection: "row", justifyContent: "space-between" },
|
|
832
|
+
el(
|
|
833
|
+
"text",
|
|
834
|
+
{
|
|
835
|
+
fg: () => theme().text.action.primary.base,
|
|
836
|
+
attributes: TextAttributes.BOLD
|
|
837
|
+
},
|
|
838
|
+
() => `Action for: ${selectedEntry()?.relativePath ?? ""}`
|
|
839
|
+
),
|
|
840
|
+
el(
|
|
841
|
+
"text",
|
|
842
|
+
{ fg: () => theme().text.muted },
|
|
843
|
+
"↑/↓ or 1/2/3 choose · Enter confirm · Esc cancel"
|
|
844
|
+
)
|
|
845
|
+
),
|
|
846
|
+
el(
|
|
847
|
+
"text",
|
|
848
|
+
{ fg: () => theme().text.muted, wrapMode: "none", truncate: true },
|
|
849
|
+
() => `Backup file: ${selectedEntry()?.backupPath ?? ""}`
|
|
850
|
+
),
|
|
851
|
+
el(
|
|
852
|
+
"box",
|
|
853
|
+
{ flexDirection: "row", flexWrap: "wrap", gap: 2, marginTop: 0 },
|
|
854
|
+
// Option 1: Restore
|
|
855
|
+
el(
|
|
856
|
+
"box",
|
|
857
|
+
{
|
|
858
|
+
paddingX: 2,
|
|
859
|
+
paddingY: 0,
|
|
860
|
+
backgroundColor: () =>
|
|
861
|
+
actionChoice() === 0
|
|
862
|
+
? theme().background.action.primary.focused
|
|
863
|
+
: theme().background.base,
|
|
864
|
+
onMouseUp: () => {
|
|
865
|
+
const sel = selectedEntry();
|
|
866
|
+
if (sel) executeRestore(sel);
|
|
867
|
+
}
|
|
868
|
+
},
|
|
869
|
+
el(
|
|
870
|
+
"text",
|
|
871
|
+
{
|
|
872
|
+
fg: () =>
|
|
873
|
+
actionChoice() === 0
|
|
874
|
+
? theme().text.action.primary.focused
|
|
875
|
+
: theme().text.base,
|
|
876
|
+
attributes: () =>
|
|
877
|
+
actionChoice() === 0 ? TextAttributes.BOLD : undefined
|
|
878
|
+
},
|
|
879
|
+
() =>
|
|
880
|
+
`${actionChoice() === 0 ? "› " : " "}[1] Restore file`
|
|
881
|
+
)
|
|
882
|
+
),
|
|
883
|
+
// Option 2: Open File
|
|
884
|
+
el(
|
|
885
|
+
"box",
|
|
886
|
+
{
|
|
887
|
+
paddingX: 2,
|
|
888
|
+
paddingY: 0,
|
|
889
|
+
backgroundColor: () =>
|
|
890
|
+
actionChoice() === 1
|
|
891
|
+
? theme().background.action.primary.focused
|
|
892
|
+
: theme().background.base,
|
|
893
|
+
onMouseUp: () => {
|
|
894
|
+
const sel = selectedEntry();
|
|
895
|
+
if (sel) executeOpenFile(sel);
|
|
896
|
+
}
|
|
897
|
+
},
|
|
898
|
+
el(
|
|
899
|
+
"text",
|
|
900
|
+
{
|
|
901
|
+
fg: () =>
|
|
902
|
+
actionChoice() === 1
|
|
903
|
+
? theme().text.action.primary.focused
|
|
904
|
+
: theme().text.base,
|
|
905
|
+
attributes: () =>
|
|
906
|
+
actionChoice() === 1 ? TextAttributes.BOLD : undefined
|
|
907
|
+
},
|
|
908
|
+
() =>
|
|
909
|
+
`${actionChoice() === 1 ? "› " : " "}[2] Open backup file`
|
|
910
|
+
)
|
|
911
|
+
),
|
|
912
|
+
// Option 3: Open Side-by-Side Diff in Editor
|
|
913
|
+
el(
|
|
914
|
+
"box",
|
|
915
|
+
{
|
|
916
|
+
paddingX: 2,
|
|
917
|
+
paddingY: 0,
|
|
918
|
+
backgroundColor: () =>
|
|
919
|
+
actionChoice() === 2
|
|
920
|
+
? theme().background.action.primary.focused
|
|
921
|
+
: theme().background.base,
|
|
922
|
+
onMouseUp: () => {
|
|
923
|
+
const sel = selectedEntry();
|
|
924
|
+
if (sel) executeOpenDiff(sel);
|
|
925
|
+
}
|
|
926
|
+
},
|
|
927
|
+
el(
|
|
928
|
+
"text",
|
|
929
|
+
{
|
|
930
|
+
fg: () =>
|
|
931
|
+
actionChoice() === 2
|
|
932
|
+
? theme().text.action.primary.focused
|
|
933
|
+
: theme().text.base,
|
|
934
|
+
attributes: () =>
|
|
935
|
+
actionChoice() === 2 ? TextAttributes.BOLD : undefined
|
|
936
|
+
},
|
|
937
|
+
() =>
|
|
938
|
+
`${actionChoice() === 2 ? "› " : " "}[3] Open diff in editor (backup vs current)`
|
|
939
|
+
)
|
|
940
|
+
)
|
|
941
|
+
)
|
|
942
|
+
);
|
|
943
|
+
}
|
|
944
|
+
})
|
|
945
|
+
),
|
|
946
|
+
|
|
947
|
+
// 4. Main Split View (Left: Two-line Snapshot List with Parent Folder, Right: Full Paths + Colored Diff)
|
|
948
|
+
el(
|
|
949
|
+
"box",
|
|
950
|
+
{
|
|
951
|
+
flexDirection: "row",
|
|
952
|
+
flexGrow: 1,
|
|
953
|
+
minHeight: 0
|
|
954
|
+
},
|
|
955
|
+
// Left Pane: Snapshot List (44% width, matching session-search & local-memory)
|
|
956
|
+
el(
|
|
957
|
+
"scrollbox",
|
|
958
|
+
{
|
|
959
|
+
id: "auto-backup-list",
|
|
960
|
+
ref: (r: unknown) => {
|
|
961
|
+
listScrollRef = r;
|
|
962
|
+
},
|
|
963
|
+
width: "44%",
|
|
964
|
+
height: "100%",
|
|
965
|
+
flexShrink: 0,
|
|
966
|
+
border: ["right"],
|
|
967
|
+
borderColor: () =>
|
|
968
|
+
focusPane() === "list" ? theme().text.base : theme().border.base,
|
|
969
|
+
scrollbarOptions: { visible: false },
|
|
970
|
+
onMouseDown: () => setFocusPane("list")
|
|
971
|
+
},
|
|
972
|
+
createComponent(Show, {
|
|
973
|
+
get when() {
|
|
974
|
+
return filtered().length > 0;
|
|
975
|
+
},
|
|
976
|
+
get fallback() {
|
|
977
|
+
return el(
|
|
978
|
+
"box",
|
|
979
|
+
{ paddingX: 2, paddingY: 1 },
|
|
980
|
+
el(
|
|
981
|
+
"text",
|
|
982
|
+
{ fg: () => theme().text.muted },
|
|
983
|
+
"No matching backup snapshots found."
|
|
984
|
+
)
|
|
985
|
+
);
|
|
986
|
+
},
|
|
987
|
+
get children() {
|
|
988
|
+
return createComponent(For, {
|
|
989
|
+
get each() {
|
|
990
|
+
return filtered();
|
|
991
|
+
},
|
|
992
|
+
children: (item: BackupEntry, idx: () => number) => {
|
|
993
|
+
const isSel = () => idx() === selectedIndex();
|
|
994
|
+
return el(
|
|
995
|
+
"box",
|
|
996
|
+
{
|
|
997
|
+
id: () => `backup-row-${idx()}`,
|
|
998
|
+
flexDirection: "column",
|
|
999
|
+
paddingX: 1,
|
|
1000
|
+
paddingY: 0,
|
|
1001
|
+
marginBottom: 1,
|
|
1002
|
+
flexShrink: 0,
|
|
1003
|
+
backgroundColor: () =>
|
|
1004
|
+
isSel()
|
|
1005
|
+
? theme().background.action.primary.focused
|
|
1006
|
+
: undefined,
|
|
1007
|
+
onMouseUp: () => {
|
|
1008
|
+
setSelectedIndex(idx());
|
|
1009
|
+
setActionChoice(0);
|
|
1010
|
+
setActionPopupOpen(true);
|
|
1011
|
+
},
|
|
1012
|
+
onMouseOver: () => {
|
|
1013
|
+
setSelectedIndex(idx());
|
|
1014
|
+
}
|
|
1015
|
+
},
|
|
1016
|
+
// Line 1: Backed-up file relative path + [tool] badge + age
|
|
1017
|
+
el(
|
|
1018
|
+
"box",
|
|
1019
|
+
{
|
|
1020
|
+
flexDirection: "row",
|
|
1021
|
+
justifyContent: "space-between",
|
|
1022
|
+
width: "100%"
|
|
1023
|
+
},
|
|
1024
|
+
el(
|
|
1025
|
+
"text",
|
|
1026
|
+
{
|
|
1027
|
+
wrapMode: "none",
|
|
1028
|
+
truncate: true,
|
|
1029
|
+
flexShrink: 1,
|
|
1030
|
+
minWidth: 0,
|
|
1031
|
+
fg: () =>
|
|
1032
|
+
isSel()
|
|
1033
|
+
? theme().text.action.primary.focused
|
|
1034
|
+
: theme().text.base,
|
|
1035
|
+
attributes: () =>
|
|
1036
|
+
isSel() ? TextAttributes.BOLD : undefined
|
|
1037
|
+
},
|
|
1038
|
+
() => `${isSel() ? "› " : " "}${item.relativePath}`
|
|
1039
|
+
),
|
|
1040
|
+
el(
|
|
1041
|
+
"text",
|
|
1042
|
+
{
|
|
1043
|
+
wrapMode: "none",
|
|
1044
|
+
flexShrink: 0,
|
|
1045
|
+
fg: () =>
|
|
1046
|
+
isSel()
|
|
1047
|
+
? theme().text.action.primary.focused
|
|
1048
|
+
: theme().text.action.primary.base
|
|
1049
|
+
},
|
|
1050
|
+
() =>
|
|
1051
|
+
` [${item.tool || "auto"}] ${formatAge(item.timeMs)}`
|
|
1052
|
+
)
|
|
1053
|
+
),
|
|
1054
|
+
// Line 2: Parent backup folder directly underneath
|
|
1055
|
+
el(
|
|
1056
|
+
"text",
|
|
1057
|
+
{
|
|
1058
|
+
wrapMode: "none",
|
|
1059
|
+
truncate: true,
|
|
1060
|
+
fg: () =>
|
|
1061
|
+
isSel()
|
|
1062
|
+
? theme().text.action.primary.focused
|
|
1063
|
+
: theme().text.muted
|
|
1064
|
+
},
|
|
1065
|
+
() => ` ↳ ${formatParentBackupPath(item)}`
|
|
1066
|
+
)
|
|
1067
|
+
);
|
|
1068
|
+
}
|
|
1069
|
+
});
|
|
1070
|
+
}
|
|
1071
|
+
})
|
|
1072
|
+
),
|
|
1073
|
+
|
|
1074
|
+
// Right Pane: Full Paths + Unified Diff Preview (56% width)
|
|
1075
|
+
el(
|
|
1076
|
+
"scrollbox",
|
|
1077
|
+
{
|
|
1078
|
+
id: "auto-backup-preview",
|
|
1079
|
+
ref: (r: unknown) => {
|
|
1080
|
+
previewScrollRef = r;
|
|
1081
|
+
},
|
|
1082
|
+
flexGrow: 1,
|
|
1083
|
+
minWidth: 0,
|
|
1084
|
+
minHeight: 0,
|
|
1085
|
+
paddingX: 2,
|
|
1086
|
+
paddingY: 1,
|
|
1087
|
+
focused: () => focusPane() === "preview",
|
|
1088
|
+
onMouseDown: () => setFocusPane("preview")
|
|
1089
|
+
},
|
|
1090
|
+
createComponent(Show, {
|
|
1091
|
+
get when() {
|
|
1092
|
+
return Boolean(selectedEntry() && diffPreview());
|
|
1093
|
+
},
|
|
1094
|
+
get fallback() {
|
|
1095
|
+
return el(
|
|
1096
|
+
"text",
|
|
1097
|
+
{ fg: () => theme().text.muted },
|
|
1098
|
+
"Select a snapshot on the left to inspect its full path and diff."
|
|
1099
|
+
);
|
|
1100
|
+
},
|
|
1101
|
+
get children() {
|
|
1102
|
+
return el(
|
|
1103
|
+
"box",
|
|
1104
|
+
{ flexDirection: "column", gap: 1 },
|
|
1105
|
+
// Colored left-border header card
|
|
1106
|
+
el(
|
|
1107
|
+
"box",
|
|
1108
|
+
{
|
|
1109
|
+
flexDirection: "column",
|
|
1110
|
+
paddingLeft: 1,
|
|
1111
|
+
border: ["left"],
|
|
1112
|
+
borderColor: () => theme().text.action.primary.base
|
|
1113
|
+
},
|
|
1114
|
+
el(
|
|
1115
|
+
"box",
|
|
1116
|
+
{
|
|
1117
|
+
flexDirection: "row",
|
|
1118
|
+
justifyContent: "space-between",
|
|
1119
|
+
width: "100%"
|
|
1120
|
+
},
|
|
1121
|
+
el(
|
|
1122
|
+
"text",
|
|
1123
|
+
{
|
|
1124
|
+
fg: () => theme().text.base,
|
|
1125
|
+
attributes: TextAttributes.BOLD
|
|
1126
|
+
},
|
|
1127
|
+
() => selectedEntry()?.relativePath ?? ""
|
|
1128
|
+
),
|
|
1129
|
+
el(
|
|
1130
|
+
"text",
|
|
1131
|
+
{
|
|
1132
|
+
fg: () => theme().text.action.primary.base,
|
|
1133
|
+
attributes: TextAttributes.BOLD
|
|
1134
|
+
},
|
|
1135
|
+
() =>
|
|
1136
|
+
`[${(selectedEntry()?.tool || "auto").toUpperCase()} · ${formatBytes(selectedEntry()?.size ?? 0)} · ${formatAge(selectedEntry()?.timeMs ?? 0)}]`
|
|
1137
|
+
)
|
|
1138
|
+
),
|
|
1139
|
+
el(
|
|
1140
|
+
"text",
|
|
1141
|
+
{ fg: () => theme().text.muted },
|
|
1142
|
+
() => `Snapshot: ${selectedEntry()?.snapshotId ?? ""}`
|
|
1143
|
+
),
|
|
1144
|
+
el(
|
|
1145
|
+
"text",
|
|
1146
|
+
{ fg: () => theme().text.base, wrapMode: "word" },
|
|
1147
|
+
() => `Original: ${selectedEntry()?.originalPath ?? ""}`
|
|
1148
|
+
),
|
|
1149
|
+
el(
|
|
1150
|
+
"text",
|
|
1151
|
+
{ fg: () => theme().text.muted, wrapMode: "word" },
|
|
1152
|
+
() => `Backup: ${selectedEntry()?.backupPath ?? ""}`
|
|
1153
|
+
)
|
|
1154
|
+
),
|
|
1155
|
+
|
|
1156
|
+
// Diff section header
|
|
1157
|
+
el(
|
|
1158
|
+
"text",
|
|
1159
|
+
{
|
|
1160
|
+
fg: () => theme().text.action.primary.base,
|
|
1161
|
+
attributes: TextAttributes.BOLD,
|
|
1162
|
+
marginTop: 1
|
|
1163
|
+
},
|
|
1164
|
+
() => diffPreview()?.summary ?? ""
|
|
1165
|
+
),
|
|
1166
|
+
|
|
1167
|
+
// Colored diff lines inside a subtle left-bordered block
|
|
1168
|
+
el(
|
|
1169
|
+
"box",
|
|
1170
|
+
{
|
|
1171
|
+
flexDirection: "column",
|
|
1172
|
+
paddingLeft: 1,
|
|
1173
|
+
border: ["left"],
|
|
1174
|
+
borderColor: () => theme().border.base
|
|
1175
|
+
},
|
|
1176
|
+
createComponent(For, {
|
|
1177
|
+
get each() {
|
|
1178
|
+
return diffPreview()?.lines ?? [];
|
|
1179
|
+
},
|
|
1180
|
+
children: (line: { type: string; text: string }) =>
|
|
1181
|
+
el(
|
|
1182
|
+
"text",
|
|
1183
|
+
{
|
|
1184
|
+
fg: () =>
|
|
1185
|
+
line.type === "add"
|
|
1186
|
+
? theme().text.feedback?.success?.base ??
|
|
1187
|
+
theme().text.action.primary.base
|
|
1188
|
+
: line.type === "del"
|
|
1189
|
+
? theme().text.feedback?.error?.base ??
|
|
1190
|
+
"#f87171"
|
|
1191
|
+
: line.text.startsWith("@@")
|
|
1192
|
+
? theme().text.action.primary.base
|
|
1193
|
+
: theme().text.muted,
|
|
1194
|
+
attributes: () =>
|
|
1195
|
+
line.type === "add" || line.type === "del"
|
|
1196
|
+
? TextAttributes.BOLD
|
|
1197
|
+
: undefined,
|
|
1198
|
+
wrapMode: "none",
|
|
1199
|
+
truncate: true
|
|
1200
|
+
},
|
|
1201
|
+
createComponent(For, {
|
|
1202
|
+
get each() {
|
|
1203
|
+
return splitHighlightedText(line.text, query());
|
|
1204
|
+
},
|
|
1205
|
+
children: (seg: { text: string; match: boolean }) =>
|
|
1206
|
+
el(
|
|
1207
|
+
"span",
|
|
1208
|
+
{
|
|
1209
|
+
fg: () =>
|
|
1210
|
+
seg.match
|
|
1211
|
+
? theme().text.action.primary.base
|
|
1212
|
+
: undefined,
|
|
1213
|
+
attributes: () =>
|
|
1214
|
+
seg.match ? TextAttributes.BOLD : undefined
|
|
1215
|
+
},
|
|
1216
|
+
seg.text
|
|
1217
|
+
)
|
|
1218
|
+
})
|
|
1219
|
+
)
|
|
1220
|
+
})
|
|
1221
|
+
)
|
|
1222
|
+
);
|
|
1223
|
+
}
|
|
1224
|
+
})
|
|
1225
|
+
)
|
|
1226
|
+
),
|
|
1227
|
+
|
|
1228
|
+
// 5. Footer Shortcut Bar
|
|
1229
|
+
el(
|
|
1230
|
+
"box",
|
|
1231
|
+
{
|
|
1232
|
+
flexDirection: "row",
|
|
1233
|
+
justifyContent: "space-between",
|
|
1234
|
+
gap: 2,
|
|
1235
|
+
paddingX: 2,
|
|
1236
|
+
paddingY: 1,
|
|
1237
|
+
flexShrink: 0,
|
|
1238
|
+
border: ["top"],
|
|
1239
|
+
borderColor: () => theme().border.base
|
|
1240
|
+
},
|
|
1241
|
+
el(
|
|
1242
|
+
"text",
|
|
1243
|
+
{
|
|
1244
|
+
fg: () => theme().text.muted,
|
|
1245
|
+
flexShrink: 1,
|
|
1246
|
+
minWidth: 0,
|
|
1247
|
+
wrapMode: "none",
|
|
1248
|
+
truncate: true
|
|
1249
|
+
},
|
|
1250
|
+
"enter actions (restore / open) · ↑/↓ or ^j/^k move · ^t tool · ^g date · ^r retention · ^d prune · tab pane"
|
|
1251
|
+
),
|
|
1252
|
+
el(
|
|
1253
|
+
"text",
|
|
1254
|
+
{ fg: () => theme().text.muted, flexShrink: 0, wrapMode: "none" },
|
|
1255
|
+
"0 tokens"
|
|
1256
|
+
)
|
|
1257
|
+
)
|
|
1258
|
+
);
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1261
|
+
return (props: DialogProps) => createComponent(AutoBackupDialog, props);
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
async function openFallbackSelectDialog(
|
|
1265
|
+
context: TuiPluginContext,
|
|
1266
|
+
workspaceDir: string,
|
|
1267
|
+
initialQuery: string
|
|
1268
|
+
) {
|
|
1269
|
+
const all = listBackups(workspaceDir, { includeSubprojects: true });
|
|
1270
|
+
const q = initialQuery.trim().toLowerCase();
|
|
1271
|
+
const entries = q
|
|
1272
|
+
? all.filter(
|
|
1273
|
+
(item: BackupEntry) =>
|
|
1274
|
+
item.relativePath.toLowerCase().includes(q) ||
|
|
1275
|
+
item.backupPath.toLowerCase().includes(q) ||
|
|
1276
|
+
item.snapshotId.toLowerCase().includes(q)
|
|
1277
|
+
)
|
|
1278
|
+
: all;
|
|
1279
|
+
|
|
1280
|
+
if (!entries.length) {
|
|
1281
|
+
context.ui.toast.show({
|
|
1282
|
+
title: "Auto-Backup",
|
|
1283
|
+
message: "No backup snapshots found.",
|
|
1284
|
+
variant: "warning"
|
|
1285
|
+
});
|
|
1286
|
+
return;
|
|
1287
|
+
}
|
|
1288
|
+
|
|
1289
|
+
const selectedId = await context.ui.dialog.select<string>({
|
|
1290
|
+
title: "Auto-Backup Snapshots (0 tokens)",
|
|
1291
|
+
placeholder: "Filter backups...",
|
|
1292
|
+
options: entries.map((e: BackupEntry) => ({
|
|
1293
|
+
title: `${e.relativePath} (${formatAge(e.timeMs)})`,
|
|
1294
|
+
value: e.id,
|
|
1295
|
+
description: `Backup: ${e.backupPath}`,
|
|
1296
|
+
footer: `Original: ${e.originalPath} · ${formatBytes(e.size)}`
|
|
1297
|
+
}))
|
|
1298
|
+
});
|
|
1299
|
+
|
|
1300
|
+
if (!selectedId) return;
|
|
1301
|
+
const chosen = entries.find((e: BackupEntry) => e.id === selectedId);
|
|
1302
|
+
if (!chosen) return;
|
|
1303
|
+
|
|
1304
|
+
const action = await context.ui.dialog.select<string>({
|
|
1305
|
+
title: `Action for ${chosen.relativePath}`,
|
|
1306
|
+
placeholder: "Choose action...",
|
|
1307
|
+
options: [
|
|
1308
|
+
{
|
|
1309
|
+
title: "Restore file",
|
|
1310
|
+
value: "restore",
|
|
1311
|
+
description: `Revert ${chosen.originalPath} to this snapshot (creates safety backup first)`
|
|
1312
|
+
},
|
|
1313
|
+
{
|
|
1314
|
+
title: "Open backup file",
|
|
1315
|
+
value: "open",
|
|
1316
|
+
description: `Open ${chosen.backupPath} in your default system editor`
|
|
1317
|
+
}
|
|
1318
|
+
]
|
|
1319
|
+
});
|
|
1320
|
+
|
|
1321
|
+
if (action === "restore") {
|
|
1322
|
+
restoreBackupFile(chosen);
|
|
1323
|
+
context.ui.toast.show({
|
|
1324
|
+
title: "Backup Restored",
|
|
1325
|
+
message: `${chosen.relativePath} restored from ${chosen.snapshotId}`,
|
|
1326
|
+
variant: "success"
|
|
1327
|
+
});
|
|
1328
|
+
} else if (action === "open") {
|
|
1329
|
+
openBackupFile(chosen.backupPath);
|
|
1330
|
+
context.ui.toast.show({
|
|
1331
|
+
title: "Opened Backup File",
|
|
1332
|
+
message: chosen.backupPath,
|
|
1333
|
+
variant: "info"
|
|
1334
|
+
});
|
|
1335
|
+
}
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
export default {
|
|
1339
|
+
id: "opencode-local-backup",
|
|
1340
|
+
async setup(context: TuiPluginContext) {
|
|
1341
|
+
if (!context?.ui) {
|
|
1342
|
+
return;
|
|
1343
|
+
}
|
|
1344
|
+
const workspaceDir = context.location?.directory ?? process.cwd();
|
|
1345
|
+
const renderDialog = createTuiRenderer();
|
|
1346
|
+
|
|
1347
|
+
const openBackupsPopup = async (rawArgs?: unknown) => {
|
|
1348
|
+
const initialQuery =
|
|
1349
|
+
typeof rawArgs === "string"
|
|
1350
|
+
? rawArgs.trim()
|
|
1351
|
+
: typeof (rawArgs as { arguments?: unknown } | undefined)?.arguments === "string"
|
|
1352
|
+
? ((rawArgs as { arguments: string }).arguments).trim()
|
|
1353
|
+
: "";
|
|
1354
|
+
|
|
1355
|
+
try {
|
|
1356
|
+
context.ui.dialog.show(() =>
|
|
1357
|
+
renderDialog({
|
|
1358
|
+
context,
|
|
1359
|
+
workspaceDir,
|
|
1360
|
+
initialQuery,
|
|
1361
|
+
close: () => context.ui.dialog.clear()
|
|
1362
|
+
})
|
|
1363
|
+
);
|
|
1364
|
+
context.ui.dialog.set({
|
|
1365
|
+
size: "xlarge",
|
|
1366
|
+
centered: true
|
|
1367
|
+
});
|
|
1368
|
+
} catch {
|
|
1369
|
+
await openFallbackSelectDialog(context, workspaceDir, initialQuery);
|
|
1370
|
+
}
|
|
1371
|
+
};
|
|
1372
|
+
|
|
1373
|
+
let layerRegistered = false;
|
|
1374
|
+
context.ui.slot({
|
|
1375
|
+
append: "app",
|
|
1376
|
+
render() {
|
|
1377
|
+
if (!layerRegistered) {
|
|
1378
|
+
layerRegistered = true;
|
|
1379
|
+
context.keymap.layer(() => ({
|
|
1380
|
+
mode: "global",
|
|
1381
|
+
commands: [
|
|
1382
|
+
{
|
|
1383
|
+
id: "auto.backup.browse",
|
|
1384
|
+
title: "Browse & restore file backups (0 tokens)...",
|
|
1385
|
+
description:
|
|
1386
|
+
"Interactive zero-token popup to browse .backups/ snapshots, preview diffs, restore files, or open backup files",
|
|
1387
|
+
category: "Backup",
|
|
1388
|
+
palette: true,
|
|
1389
|
+
slash: {
|
|
1390
|
+
name: "backups",
|
|
1391
|
+
arguments: true
|
|
1392
|
+
},
|
|
1393
|
+
run: (input) => {
|
|
1394
|
+
void openBackupsPopup(input);
|
|
1395
|
+
}
|
|
1396
|
+
},
|
|
1397
|
+
{
|
|
1398
|
+
id: "auto.backup.browse.alias.backup",
|
|
1399
|
+
title: "Browse & restore file backups (/backup)",
|
|
1400
|
+
category: "Backup",
|
|
1401
|
+
palette: false,
|
|
1402
|
+
slash: {
|
|
1403
|
+
name: "backup",
|
|
1404
|
+
arguments: true
|
|
1405
|
+
},
|
|
1406
|
+
run: (input) => {
|
|
1407
|
+
void openBackupsPopup(input);
|
|
1408
|
+
}
|
|
1409
|
+
},
|
|
1410
|
+
{
|
|
1411
|
+
id: "auto.backup.browse.alias.restore",
|
|
1412
|
+
title: "Browse & restore file backups (/restore)",
|
|
1413
|
+
category: "Backup",
|
|
1414
|
+
palette: false,
|
|
1415
|
+
slash: {
|
|
1416
|
+
name: "restore",
|
|
1417
|
+
arguments: true
|
|
1418
|
+
},
|
|
1419
|
+
run: (input) => {
|
|
1420
|
+
void openBackupsPopup(input);
|
|
1421
|
+
}
|
|
1422
|
+
},
|
|
1423
|
+
{
|
|
1424
|
+
id: "auto.backup.browse.alias.snapshots",
|
|
1425
|
+
title: "Browse & restore file backups (/snapshots)",
|
|
1426
|
+
category: "Backup",
|
|
1427
|
+
palette: false,
|
|
1428
|
+
slash: {
|
|
1429
|
+
name: "snapshots",
|
|
1430
|
+
arguments: true
|
|
1431
|
+
},
|
|
1432
|
+
run: (input) => {
|
|
1433
|
+
void openBackupsPopup(input);
|
|
1434
|
+
}
|
|
1435
|
+
}
|
|
1436
|
+
]
|
|
1437
|
+
}));
|
|
1438
|
+
}
|
|
1439
|
+
return null;
|
|
1440
|
+
}
|
|
1441
|
+
});
|
|
1442
|
+
}
|
|
1443
|
+
};
|