xonecas.io
725
Ds

diff-scope / commit 61e2e72

feat(ui): add auto-refresh and status pills

xonecas2026-04-16 61e2e727be7fba005ea7a846443641c55d454894 parent a4cb69d

3 files changed

AGENTS.md+3−6
@@ -24,16 +24,13 @@ The current app already has:
2424 - a scrollable git diff view
2525 - staged and unstaged diff rendering in one snapshot
2626 - diff highlighting and wrapped long lines
27-- a bottom status bar with cwd, branch, and git status summary
27+- a bottom status bar with cwd, branch, git status summary, and refresh mode
2828 - pending-change detection every 5 seconds
2929 - manual refresh with `r`
30+- auto-refresh toggle with `ctrl+r`
31+- adjustable auto-refresh interval controls with `pageup` / `pagedown`
3032 - a help/about modal toggled with `?` and closed with `?` or `Esc`
3133
32-Not implemented yet:
33-
34-- auto-refresh toggle
35-- adjustable auto-refresh interval controls
36-
3734 ## Commands
3835
3936 ```bash
README.md+6−1
@@ -8,9 +8,11 @@ A small terminal git diff viewer built with `cel-tui` and Bun.
88 - staged and unstaged diff snapshots
99 - diff highlighting
1010 - wrapped long lines
11-- status bar with current directory, branch, and git status summary
11+- status bar with current directory, branch, git status summary, and refresh mode
1212 - pending-change detection every 5 seconds
1313 - manual refresh with `r`
14+- auto-refresh toggle with `Ctrl+R`
15+- adjustable auto-refresh interval with `PageUp` / `PageDown`
1416 - help/about modal with `?`
1517
1618 ## Run
@@ -30,6 +32,9 @@ bun run typecheck
3032 ## Current keybinds
3133
3234 - `r` — refresh snapshot
35+- `Ctrl+R` — toggle auto-refresh
36+- `PageUp` — slow auto-refresh by 5 seconds
37+- `PageDown` — speed auto-refresh up by 5 seconds
3338 - `?` — open or close help
3439 - `Esc` — close help
3540 - `Ctrl+Q` / `Ctrl+C` — quit
src/index.ts+191−28
@@ -20,12 +20,29 @@ type RepoState = {
2020 fingerprint: string;
2121 };
2222
23+type StatusPillColor =
24+ | "color00"
25+ | "color01"
26+ | "color02"
27+ | "color03"
28+ | "color04"
29+ | "color05"
30+ | "color06"
31+ | "color07"
32+ | "color08";
33+
2334 const APP_NAME = packageJson.name;
2435 const APP_VERSION = packageJson.version;
2536 const CHANGE_DETECTION_INTERVAL_MS = 5_000;
37+const MIN_AUTO_REFRESH_INTERVAL_SECONDS = 5;
38+const MAX_AUTO_REFRESH_INTERVAL_SECONDS = 60;
39+const AUTO_REFRESH_STEP_SECONDS = 5;
2640 const textDecoder = new TextDecoder();
2741
2842 let helpOpen = false;
43+let autoRefreshEnabled = false;
44+let autoRefreshIntervalSeconds = MIN_AUTO_REFRESH_INTERVAL_SECONDS;
45+let autoRefreshTimer: ReturnType<typeof setInterval> | null = null;
2946 let displayedState = readRepoState();
3047 let snapshot = displayedState.snapshot;
3148 let displayedFingerprint = displayedState.fingerprint;
@@ -243,14 +260,49 @@ function readRepoState(): RepoState {
243260 };
244261 }
245262
246-function refreshSnapshot() {
247- displayedState = readRepoState();
248- snapshot = displayedState.snapshot;
249- displayedFingerprint = displayedState.fingerprint;
263+function applyDisplayedState(nextState: RepoState) {
264+ displayedState = nextState;
265+ snapshot = nextState.snapshot;
266+ displayedFingerprint = nextState.fingerprint;
250267 hasPendingChanges = false;
268+}
269+
270+function refreshSnapshot() {
271+ applyDisplayedState(readRepoState());
272+ cel.render();
273+}
274+
275+function runAutoRefreshTick() {
276+ const current = readRepoState();
277+
278+ if (current.fingerprint === displayedFingerprint) {
279+ if (hasPendingChanges) {
280+ hasPendingChanges = false;
281+ cel.render();
282+ }
283+ return;
284+ }
285+
286+ applyDisplayedState(current);
251287 cel.render();
252288 }
253289
290+function syncAutoRefreshTimer() {
291+ if (autoRefreshTimer !== null) {
292+ clearInterval(autoRefreshTimer);
293+ autoRefreshTimer = null;
294+ }
295+
296+ if (!autoRefreshEnabled) {
297+ return;
298+ }
299+
300+ autoRefreshTimer = setInterval(
301+ runAutoRefreshTick,
302+ autoRefreshIntervalSeconds * 1_000,
303+ );
304+}
305+
254306 function checkForPendingChanges() {
255307 const current = readRepoState();
256308 const nextHasPendingChanges = current.fingerprint !== displayedFingerprint;
@@ -304,6 +356,86 @@ function renderDiffLine(line: string) {
304356 ]);
305357 }
306358
359+function renderStatusPill(
360+ text: string,
361+ options: {
362+ bgColor: StatusPillColor;
363+ fgColor?: StatusPillColor;
364+ bold?: boolean;
365+ },
366+) {
367+ return HStack(
368+ {
369+ bgColor: options.bgColor,
370+ fgColor: options.fgColor ?? "color00",
371+ padding: { x: 1 },
372+ },
373+ [Text(text, { bold: options.bold ?? true })],
374+ );
375+}
376+
377+function statusSummaryPillColors(statusSummary: string) {
378+ if (statusSummary === "clean") {
379+ return { bgColor: "color02" as const, fgColor: "color00" as const };
380+ }
381+
382+ if (statusSummary.includes("modified")) {
383+ return { bgColor: "color03" as const, fgColor: "color00" as const };
384+ }
385+
386+ if (statusSummary.includes("staged")) {
387+ return { bgColor: "color06" as const, fgColor: "color00" as const };
388+ }
389+
390+ if (statusSummary.includes("untracked")) {
391+ return { bgColor: "color05" as const, fgColor: "color00" as const };
392+ }
393+
394+ return { bgColor: "color08" as const, fgColor: "color07" as const };
395+}
396+
397+function renderStatusBar() {
398+ return HStack(
399+ {
400+ padding: { x: 1 },
401+ bgColor: "color00",
402+ fgColor: "color07",
403+ },
404+ [
405+ HStack({ gap: 1 }, [
406+ renderStatusPill(snapshot.cwd, {
407+ bgColor: "color08",
408+ fgColor: "color07",
409+ bold: false,
410+ }),
411+ renderStatusPill(snapshot.branch, {
412+ bgColor: "color06",
413+ fgColor: "color00",
414+ }),
415+ renderStatusPill(snapshot.statusSummary, statusSummaryPillColors(snapshot.statusSummary)),
416+ ]),
417+ Spacer(),
418+ HStack({ gap: 1 }, [
419+ renderStatusPill(autoRefreshModeLabel(), {
420+ bgColor: autoRefreshEnabled ? "color04" : "color08",
421+ fgColor: "color07",
422+ }),
423+ ...(hasPendingChanges
424+ ? [
425+ renderStatusPill("new changes pending", {
426+ bgColor: "color01",
427+ fgColor: "color07",
428+ }),
429+ ]
430+ : []),
431+ Text("r refresh · ctrl+r auto · ? help · ctrl+q quit", {
432+ fgColor: "color08",
433+ }),
434+ ]),
435+ ],
436+ );
437+}
438+
307439 function renderHelpModal() {
308440 return VStack(
309441 {
@@ -324,11 +456,17 @@ function renderHelpModal() {
324456 bold: true,
325457 fgColor: "color06",
326458 }),
459+ Text(`Mode: ${autoRefreshModeLabel()}`, {
460+ fgColor: "color08",
461+ }),
327462 Text(""),
328463 Text("Keybinds", { bold: true }),
329- Text("r refresh snapshot"),
330- Text("? / esc close help"),
331- Text("ctrl+q quit"),
464+ Text("r refresh snapshot"),
465+ Text("ctrl+r toggle auto-refresh"),
466+ Text("pageup slower auto-refresh (+5s)"),
467+ Text("pagedown faster auto-refresh (-5s)"),
468+ Text("? / esc close help"),
469+ Text("ctrl+q quit"),
332470 Text(""),
333471 Text("Mouse wheel scrolls the diff view.", {
334472 fgColor: "color08",
@@ -343,6 +481,36 @@ function isHelpKey(key: string) {
343481 return key === "?" || key === "shift+/";
344482 }
345483
484+function autoRefreshModeLabel() {
485+ return autoRefreshEnabled
486+ ? `auto ${autoRefreshIntervalSeconds}s`
487+ : `manual (${autoRefreshIntervalSeconds}s)`;
488+}
489+
490+function toggleAutoRefresh() {
491+ autoRefreshEnabled = !autoRefreshEnabled;
492+ syncAutoRefreshTimer();
493+ cel.render();
494+}
495+
496+function adjustAutoRefreshInterval(deltaSeconds: number) {
497+ const nextInterval = Math.min(
498+ MAX_AUTO_REFRESH_INTERVAL_SECONDS,
499+ Math.max(
500+ MIN_AUTO_REFRESH_INTERVAL_SECONDS,
501+ autoRefreshIntervalSeconds + deltaSeconds,
502+ ),
503+ );
504+
505+ if (nextInterval === autoRefreshIntervalSeconds) {
506+ return;
507+ }
508+
509+ autoRefreshIntervalSeconds = nextInterval;
510+ syncAutoRefreshTimer();
511+ cel.render();
512+}
513+
346514 function handleKeyPress(key: string) {
347515 if (key === "ctrl+q" || key === "ctrl+c") {
348516 quit();
@@ -361,6 +529,21 @@ function handleKeyPress(key: string) {
361529 return;
362530 }
363531
532+ if (key === "ctrl+r") {
533+ toggleAutoRefresh();
534+ return;
535+ }
536+
537+ if (key === "pageup") {
538+ adjustAutoRefreshInterval(AUTO_REFRESH_STEP_SECONDS);
539+ return;
540+ }
541+
542+ if (key === "pagedown") {
543+ adjustAutoRefreshInterval(-AUTO_REFRESH_STEP_SECONDS);
544+ return;
545+ }
546+
364547 if (key === "r") {
365548 refreshSnapshot();
366549 }
@@ -385,27 +568,7 @@ cel.viewport(() => {
385568 snapshot.diffLines.map(renderDiffLine),
386569 ),
387570 Divider({ fgColor: "color08" }),
388- HStack(
389- {
390- padding: { x: 1 },
391- bgColor: "color07",
392- fgColor: "color00",
393- },
394- [
395- Text(`${snapshot.cwd} · ${snapshot.branch} · ${snapshot.statusSummary}`),
396- Spacer(),
397- ...(hasPendingChanges
398- ? [
399- Text("new changes pending", {
400- bold: true,
401- fgColor: "color01",
402- }),
403- Text(" · "),
404- ]
405- : []),
406- Text("r refresh · ? help · ctrl+q quit"),
407- ],
408- ),
571+ renderStatusBar(),
409572 ],
410573 );