1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
|
;;; sync-ui.el --- Rougier-style git sync dashboard -*- lexical-binding: t -*-
;; Copyright (C) 2026 cashmere
;; Author: cashmere
;; Version: 0.1.0
;; Package-Requires: ((emacs "28.1"))
;; Keywords: vc, tools
;;; Commentary:
;; A keyboard-first, Rougier-style dashboard for keeping a set of git
;; repos committed, rebased and pushed to all of their (redundant)
;; remotes. Sync is manual: `s' runs one cycle.
;;
;; AUTO repos commit everything dirty with a "host: ISO8601" message
;; and push without asking. ASK repos prompt once per cycle; a yes
;; zaps you to magit, and `r' in the dashboard resumes (push) once a
;; new commit exists. Pulls are `git rebase --autostash'; a conflict
;; aborts the rebase, marks the repo CONFLICTED and leaves it alone.
;;
;; Archetype: flat stream with per-item state -> state-glyph rail
;; over two-line cards (framework S3 + S5 + S10). The rail owns the
;; left edge: filled chips carry the dirty count and the state, the
;; `┊' gutter keeps it unbroken between cards. Faces are
;; theme-agnostic (recipe 0); the only fixed hues are the AUTO/ASK
;; category pills, which are brand colors by design.
;;
;; Keys (dashboard):
;; s sync all RET magit r resume x skip
;; t auto/ask a add repo d remove g refresh
;; n/p next/prev q quit
;;
;; Helix: normal state gets j/k plus the same one-key actions.
;;; Code:
(require 'cl-lib)
;;; § 1 Faces (theme-agnostic, recipe 0)
(defface sync-ui-strong '((t :inherit bold))
"Structural text: repo names, title."
:group 'sync-ui)
(defface sync-ui-faded '((t :inherit shadow))
"Secondary text: paths, metadata, hints."
:group 'sync-ui)
(defface sync-ui-salient '((t :inherit link))
"Attention: repo names, unpushed counts."
:group 'sync-ui)
(defface sync-ui-popout '((t :inherit (font-lock-warning-face bold)))
"The second hue: repos that need a decision, incoming commits."
:group 'sync-ui)
(defface sync-ui-critical '((t :inherit (error bold)))
"Conflicts and errors. Used scarcely."
:group 'sync-ui)
(defface sync-ui-chip '((t :inherit bold :inverse-video t))
"Rail chip, structural dark: dirty auto repo, freshly synced."
:group 'sync-ui)
(defface sync-ui-chip-i '((t :inherit shadow :inverse-video t))
"Rail chip, quiet: clean repo."
:group 'sync-ui)
(defface sync-ui-chip-active '((t :inherit link :inverse-video t))
"Rail chip while a sync step runs."
:group 'sync-ui)
(defface sync-ui-chip-popout '((t :inherit (font-lock-warning-face bold) :inverse-video t))
"Rail chip for a repo that needs a decision (dirty ask repo, waiting)."
:group 'sync-ui)
(defface sync-ui-chip-critical '((t :inherit (error bold) :inverse-video t))
"Rail chip for conflict/error."
:group 'sync-ui)
(defface sync-ui-pill-auto '((t :background "#3a7d5d" :foreground "#ffffff"))
"Category pill for AUTO mode. Brand hue, white text, theme-stable."
:group 'sync-ui)
(defface sync-ui-pill-ask '((t :background "#7451a6" :foreground "#ffffff"))
"Category pill for ASK mode. Brand hue, white text, theme-stable."
:group 'sync-ui)
(defface sync-ui-selected '((t :inherit bold :inverse-video t))
"Selected card (hl-line remap target). Pure inverse: always theme-fresh."
:group 'sync-ui)
(defface sync-ui-hairline
'((t :inherit default :strike-through t :overline nil :underline nil
:height 1.0 :extend t))
"Full-width rule drawn by a strike-through newline."
:group 'sync-ui)
;;; § 2 Customs
(defgroup sync-ui nil
"Dashboard for manual autosync/autopush of git repos."
:group 'applications
:prefix "sync-ui-")
(defcustom sync-ui-repos
'(("~/org" . auto)
("~/.claude/skills" . auto)
("~/pass" . auto)
("~/nix" . ask)
("~/.emacs.d" . ask))
"Alist of (PATH . MODE) for managed repos.
MODE `auto' commits and pushes without prompting; MODE `ask' offers a
magit detour first. Edit from the dashboard with `a', `d' and `t'."
:type '(alist :key-type directory
:value-type (choice (const auto) (const ask)))
:group 'sync-ui)
(defcustom sync-ui-hostname
(car (split-string (system-name) "\\."))
"Host label used in auto-commit messages."
:type 'string
:group 'sync-ui)
;;; § 3 Data layer
(cl-defstruct (sync-ui--repo (:constructor sync-ui--repo-create))
id path mode (state 'clean) (dirty 0) branch head head-at-wait upstream
remotes last-sync message process)
(defvar sync-ui--repos nil
"Live list of `sync-ui--repo' structs, in display order.")
(defconst sync-ui--buffer-name "*sync*")
(defun sync-ui--load-registry ()
"Build `sync-ui--repos' from `sync-ui-repos'."
(setq sync-ui--repos
(mapcar (lambda (entry)
(sync-ui--repo-create
:id (expand-file-name (car entry))
:path (abbreviate-file-name (expand-file-name (car entry)))
:mode (cdr entry)))
sync-ui-repos)))
(defun sync-ui--find (id)
"Repo struct with ID, or nil."
(cl-find id sync-ui--repos :key #'sync-ui--repo-id :test #'equal))
(defun sync-ui--save-repos ()
"Persist the live registry back to `sync-ui-repos' via customize."
(customize-save-variable
'sync-ui-repos
(mapcar (lambda (repo)
(cons (sync-ui--repo-path repo) (sync-ui--repo-mode repo)))
sync-ui--repos)))
;;; § 4 Git engine
;; Local operations (status, rev-parse, commit) run synchronously —
;; they are sub-second. Network operations (fetch, rebase onto a
;; fetched ref, push) run as async process chains so the dashboard
;; stays live and shows per-step states.
(defun sync-ui--git (repo args)
"Run git ARGS synchronously in REPO's directory.
Return (EXIT-CODE OUTPUT). Never signals: a missing directory or a
missing git binary becomes a non-zero exit code."
(with-temp-buffer
(list (condition-case nil
(apply #'call-process "git" nil t nil
"-C" (sync-ui--repo-id repo) args)
(error 127))
(buffer-string))))
(defun sync-ui--git-async (repo args on-done)
"Run git ARGS asynchronously in REPO's directory.
ON-DONE is called with (EXIT-CODE OUTPUT). One process per repo at a
time; the caller's state machine guarantees that."
(let ((buf (generate-new-buffer
(format " *sync-ui:%s*" (sync-ui--repo-path repo)))))
(setf (sync-ui--repo-process repo)
(make-process
:name (format "sync-ui:%s" (sync-ui--repo-path repo))
:buffer buf
:command (append (list "git" "-C" (sync-ui--repo-id repo)) args)
:noquery t
:sentinel
(lambda (proc _event)
(when (memq (process-status proc) '(exit signal))
(let ((code (process-exit-status proc))
(out (with-current-buffer (process-buffer proc)
(buffer-string))))
(kill-buffer (process-buffer proc))
(setf (sync-ui--repo-process repo) nil)
(funcall on-done code out))))))))
(defun sync-ui--trim (string)
(if (stringp string) (string-trim string) ""))
(defun sync-ui--remote-names (repo)
"Names of REPO's configured remotes that actually have a URL.
A bare [remote \"x\"] section in any config file (e.g. global gcrypt
keys) makes `git remote' list x in every repository; get-url filters
those phantoms out."
(pcase-let ((`(,code ,out) (sync-ui--git repo '("remote"))))
(if (zerop code)
(cl-remove-if-not
(lambda (name)
(zerop (car (sync-ui--git repo (list "remote" "get-url" name)))))
(cl-remove-if #'string-empty-p (split-string out "\n")))
nil)))
(defun sync-ui--primary-remote (repo)
"\"origin\" when present, else the first remote."
(let ((names (mapcar #'car (sync-ui--repo-remotes repo))))
(if (member "origin" names) "origin" (car names))))
(defun sync-ui--ref-exists-p (repo ref)
"Non-nil when REF resolves in REPO."
(zerop (car (sync-ui--git repo (list "rev-parse" "--verify" "--quiet" ref)))))
(defun sync-ui--probe-remotes (repo names)
"Per-remote (NAME AHEAD BEHIND) triples against the current branch.
AHEAD/BEHIND are nil when the remote has no branch to compare with."
(let ((branch (sync-ui--repo-branch repo)))
(mapcar
(lambda (name)
(if (and branch
(not (string= branch "HEAD"))
(sync-ui--ref-exists-p repo (concat name "/" branch)))
(pcase-let ((`(,code ,out)
(sync-ui--git repo
(list "rev-list" "--left-right" "--count"
(format "%s/%s...HEAD" name branch)))))
(if (zerop code)
(pcase-let ((`(,left ,right) (split-string out)))
(list name (string-to-number right) (string-to-number left)))
(list name nil nil)))
(list name nil nil)))
names)))
(defun sync-ui--probe (repo &optional force)
"Local-only refresh of REPO: dirty count, branch, head, upstream, remotes.
State is recomputed only when FORCE, or when the repo is not waiting
and no sync step is running."
(cond
((not (file-directory-p (sync-ui--repo-id repo)))
(setf (sync-ui--repo-state repo) 'missing
(sync-ui--repo-message repo) "directory gone"))
((not (sync-ui--ref-exists-p repo "HEAD"))
(pcase-let ((`(,code ,_out) (sync-ui--git repo '("rev-parse" "--git-dir"))))
(if (not (zerop code))
(setf (sync-ui--repo-state repo) 'missing
(sync-ui--repo-message repo) "not a git repository")
(setf (sync-ui--repo-state repo) 'missing
(sync-ui--repo-message repo) "no commits yet"))))
(t
(setf (sync-ui--repo-branch repo)
(sync-ui--trim (cadr (sync-ui--git repo '("rev-parse" "--abbrev-ref" "HEAD"))))
(sync-ui--repo-head repo)
(sync-ui--trim (cadr (sync-ui--git repo '("rev-parse" "HEAD"))))
(sync-ui--repo-dirty repo)
(length (cl-remove-if #'string-empty-p
(split-string
(cadr (sync-ui--git repo '("status" "--porcelain")))
"\n")))
(sync-ui--repo-upstream repo)
(pcase-let ((`(,code ,out)
(sync-ui--git repo '("rev-parse" "--abbrev-ref"
"--symbolic-full-name" "@{u}"))))
(and (zerop code) (sync-ui--trim out)))
(sync-ui--repo-remotes repo)
(sync-ui--probe-remotes repo (sync-ui--remote-names repo)))
(when (or force
(not (memq (sync-ui--repo-state repo)
'(waiting committing fetching rebasing pushing))))
(setf (sync-ui--repo-state repo)
(if (> (sync-ui--repo-dirty repo) 0) 'dirty 'clean)
(sync-ui--repo-message repo) nil)))))
(defun sync-ui--set-state (repo state &optional message)
"Move REPO to STATE with MESSAGE and repaint its card."
(setf (sync-ui--repo-state repo) state
(sync-ui--repo-message repo) message)
(sync-ui--update-card repo))
(defun sync-ui--fail (repo step out)
"Mark REPO as failed during STEP; OUT is git's output."
(let* ((lines (cl-remove-if #'string-empty-p (split-string out "\n")))
(line (or (cl-find-if (lambda (l) (string-match-p "error:\|fatal:\|! \\[" l))
lines)
(car lines))))
(sync-ui--set-state
repo 'error
(truncate-string-to-width
(format "%s: %s" step
(replace-regexp-in-string
"\\`\\(fatal\\|error\\): *" "" (or line "failed")))
60 nil nil "…"))))
(defun sync-ui--commit-message ()
"Auto-commit message: \"host: ISO8601 with colon zone\"."
(format "%s: %s" sync-ui-hostname
(format-time-string "%Y-%m-%dT%H:%M:%S%:z")))
(defun sync-ui--commit-all (repo)
"Stage everything in REPO and commit. Return non-nil on success."
(sync-ui--set-state repo 'committing)
(sync-ui--git repo '("add" "-A"))
(pcase-let ((`(,code ,out)
(sync-ui--git repo (list "commit" "-m" (sync-ui--commit-message)))))
(if (or (zerop code) (string-match-p "nothing to commit" out))
(progn
(setf (sync-ui--repo-head repo)
(sync-ui--trim (cadr (sync-ui--git repo '("rev-parse" "HEAD")))))
t)
(sync-ui--fail repo "commit" out)
nil)))
(defun sync-ui--behind-count (repo onto)
"Commits in ONTO that are not in HEAD."
(pcase-let ((`(,code ,out)
(sync-ui--git repo (list "rev-list" "--count"
(format "HEAD..%s" onto)))))
(if (zerop code) (string-to-number (sync-ui--trim out)) 0)))
(defun sync-ui--fetch-step (repo remotes on-done &optional quiet)
"Fetch each of REMOTES in turn, then call ON-DONE.
Fetching explicitly by name (never `fetch --all') keeps phantom
remotes from global config out of the cycle. In QUIET mode a failed
fetch is skipped instead of failing the repo."
(if (null remotes)
(funcall on-done)
(unless quiet
(sync-ui--set-state repo 'fetching (format "fetching %s…" (car remotes))))
(sync-ui--git-async
repo (list "fetch" "--quiet" (car remotes))
(lambda (code out)
(if (and (not quiet) (not (zerop code)))
(sync-ui--fail repo "fetch" out)
(sync-ui--fetch-step repo (cdr remotes) on-done quiet))))))
(defun sync-ui--network-sync (repo)
"Fetch, rebase if behind, then push every remote of REPO."
(cond
((or (null (sync-ui--repo-branch repo))
(string= (sync-ui--repo-branch repo) "HEAD"))
(sync-ui--set-state repo 'error "detached HEAD — push skipped"))
((null (sync-ui--repo-remotes repo))
(sync-ui--set-state repo 'clean "no remotes — nothing to push"))
(t
(sync-ui--fetch-step repo (mapcar #'car (sync-ui--repo-remotes repo))
(lambda ()
(sync-ui--probe repo)
(sync-ui--update-card repo)
(sync-ui--rebase-step repo))))))
(defun sync-ui--rebase-step (repo)
"Rebase REPO onto its upstream (or primary remote branch) when behind.
A conflict aborts the rebase and marks the repo conflicted; the
working tree is left exactly as the fetch found it."
(let* ((branch (sync-ui--repo-branch repo))
(primary (sync-ui--primary-remote repo))
(onto (or (sync-ui--repo-upstream repo)
(and primary
(sync-ui--ref-exists-p repo (concat primary "/" branch))
(concat primary "/" branch))))
(behind (and onto (sync-ui--behind-count repo onto))))
(if (and onto (> behind 0))
(progn
(sync-ui--set-state repo 'rebasing (format "rebasing onto %s…" onto))
(sync-ui--git-async
repo (list "rebase" "--autostash" onto)
(lambda (code out)
(if (not (zerop code))
(progn
(sync-ui--git repo '("rebase" "--abort"))
(if (string-match-p "[Cc]onflict" out)
(sync-ui--set-state
repo 'conflicted
(format "conflict onto %s — resolve in magit, then s" onto))
(sync-ui--fail repo "rebase" out)))
(sync-ui--probe repo)
(sync-ui--push-step repo (mapcar #'car (sync-ui--repo-remotes repo)))))))
(sync-ui--push-step repo (mapcar #'car (sync-ui--repo-remotes repo))))))
(defun sync-ui--push-step (repo remotes)
"Push the current branch to each of REMOTES, in order.
A repo without upstream gets `push --set-upstream' on the primary
remote first. A rejection stops the chain; nothing is forced."
(if (null remotes)
(sync-ui--finish repo)
(let* ((remote (car remotes))
(branch (sync-ui--repo-branch repo))
(args (if (and (null (sync-ui--repo-upstream repo))
(string= remote (sync-ui--primary-remote repo)))
(list "push" "--set-upstream" remote (format "HEAD:%s" branch))
(list "push" remote (format "HEAD:%s" branch)))))
(sync-ui--set-state repo 'pushing (format "pushing %s…" remote))
(sync-ui--git-async
repo args
(lambda (code out)
(if (not (zerop code))
(sync-ui--fail repo (format "push %s" remote) out)
(sync-ui--probe repo)
(sync-ui--push-step repo (cdr remotes))))))))
(defun sync-ui--finish (repo)
"End of a sync cycle for REPO: re-probe and mark the outcome."
(sync-ui--probe repo t)
(when (eq (sync-ui--repo-state repo) 'clean)
(setf (sync-ui--repo-state repo) 'synced
(sync-ui--repo-last-sync repo) (current-time)))
(sync-ui--update-card repo))
(defun sync-ui--sync-one (repo)
"One sync cycle for REPO, honouring its mode."
(cond
((or (sync-ui--repo-process repo)
(memq (sync-ui--repo-state repo) '(waiting missing)))
nil)
((and (eq (sync-ui--repo-mode repo) 'ask)
(> (sync-ui--repo-dirty repo) 0))
(if (y-or-n-p (format "%s: %d unstaged — commit something in magit? "
(sync-ui--repo-path repo)
(sync-ui--repo-dirty repo)))
(progn
(setf (sync-ui--repo-head-at-wait repo) (sync-ui--repo-head repo))
(sync-ui--set-state repo 'waiting "commit in magit — r resumes · x skips")
(sync-ui--open-magit repo))
(sync-ui--set-state repo 'dirty "skipped this cycle")))
((and (> (sync-ui--repo-dirty repo) 0)
(not (sync-ui--commit-all repo)))
nil)
(t
(sync-ui--network-sync repo))))
(defun sync-ui-sync-all ()
"Run one sync cycle over every repo."
(interactive)
(unless sync-ui--repos (sync-ui--load-registry))
(mapc #'sync-ui--probe sync-ui--repos)
(mapc #'sync-ui--update-card sync-ui--repos)
(mapc #'sync-ui--sync-one sync-ui--repos))
(defun sync-ui-resume ()
"Resume the waiting repo at point: push once a new commit exists."
(interactive)
(let ((repo (sync-ui--repo-at-point)))
(cond
((null repo)
(user-error "No repo at point"))
((not (eq (sync-ui--repo-state repo) 'waiting))
(user-error "%s is not waiting" (sync-ui--repo-path repo)))
(t
(sync-ui--probe repo)
(sync-ui--update-card repo)
(if (equal (sync-ui--repo-head repo) (sync-ui--repo-head-at-wait repo))
(message "%s: no new commit yet — commit in magit, then r"
(sync-ui--repo-path repo))
(sync-ui--network-sync repo))))))
(defun sync-ui-skip ()
"Take the waiting repo at point out of the cycle; it will ask again."
(interactive)
(let ((repo (sync-ui--repo-at-point)))
(cond
((null repo)
(user-error "No repo at point"))
((not (eq (sync-ui--repo-state repo) 'waiting))
(user-error "%s is not waiting" (sync-ui--repo-path repo)))
(t
(sync-ui--set-state
repo (if (> (sync-ui--repo-dirty repo) 0) 'dirty 'clean)
"skipped — asks again next sync")))))
(defun sync-ui-refresh ()
"Re-probe every repo and fetch in the background to update ↑/↓ counts."
(interactive)
(unless sync-ui--repos (sync-ui--load-registry))
(mapc #'sync-ui--probe sync-ui--repos)
(sync-ui--render)
(dolist (repo sync-ui--repos)
(when (and (not (sync-ui--repo-process repo))
(not (eq (sync-ui--repo-state repo) 'missing)))
(sync-ui--fetch-step repo (mapcar #'car (sync-ui--repo-remotes repo))
(lambda ()
(sync-ui--probe repo)
(sync-ui--update-card repo))
t))))
;;; § 5 Layout primitives
(defconst sync-ui--symbols
'((ok . ("ok" . "✓"))
(bad . ("XX" . "✗"))
(void . ("--" . "∅"))
(gutter . ("|" . "┊"))
(up . ("^" . "↑"))
(down . ("v" . "↓"))
(new . ("+" . "⁺"))
(busy . ("~" . "…")))
"State glyphs: (ASCII-fallback . preferred).")
(defun sync-ui--symbol (name)
"Glyph for NAME, degrading to ASCII when undisplayable."
(let ((pair (alist-get name sync-ui--symbols)))
(if (and (cdr pair) (char-displayable-p (aref (cdr pair) 0)))
(cdr pair)
(car pair))))
(defun sync-ui--width ()
"Render width: the dashboard window when visible, else 80."
(if-let* ((win (get-buffer-window sync-ui--buffer-name)))
(window-width win)
80))
(defun sync-ui--justify-space (right-width &optional face)
"Elastic space aligning the next RIGHT-WIDTH columns to the right edge."
(propertize " " 'display `(space :align-to (- right ,(+ right-width 1)))
'face (or face 'default)))
(defun sync-ui--hairline ()
"A full-width rule."
(propertize "\n" 'face 'sync-ui-hairline))
(defun sync-ui--button (text action help)
"Clickable TEXT running ACTION, with HELP echo."
(propertize text 'pointer 'hand 'mouse-face 'highlight 'follow-link t
'help-echo help
'keymap (let ((map (make-sparse-keymap)))
(define-key map [mouse-2] action)
(define-key map (kbd "RET") action)
map)))
(defun sync-ui--chip (repo)
"The rail chip for REPO: fixed width 6, face computed from state."
(pcase (sync-ui--repo-state repo)
('clean
(propertize (format " %-4s " (sync-ui--symbol 'ok)) 'face 'sync-ui-chip-i))
('synced
(propertize (format " %-4s " (sync-ui--symbol 'ok)) 'face 'sync-ui-chip))
('dirty
(propertize (format " %-4s " (format "%02d" (sync-ui--repo-dirty repo)))
'face (if (eq (sync-ui--repo-mode repo) 'ask)
'sync-ui-chip-popout
'sync-ui-chip)))
('waiting
(propertize (format " %-4s " "??") 'face 'sync-ui-chip-popout))
((or 'committing 'fetching 'rebasing 'pushing)
(propertize (format " %-4s " (sync-ui--symbol 'busy)) 'face 'sync-ui-chip-active))
('conflicted
(propertize (format " %-4s " (sync-ui--symbol 'bad)) 'face 'sync-ui-chip-critical))
('error
(propertize (format " %-4s " "!!") 'face 'sync-ui-chip-critical))
('missing
(propertize (format " %-4s " (sync-ui--symbol 'void)) 'face 'sync-ui-chip-i))
(_
(propertize (format " %-4s " "?") 'face 'sync-ui-chip-i))))
(defun sync-ui--state-word (repo)
"The faded status text beside REPO's name."
(pcase (sync-ui--repo-state repo)
('clean "clean")
('synced (format "synced · %s"
(format-time-string "%H:%M" (sync-ui--repo-last-sync repo))))
('dirty (if (eq (sync-ui--repo-mode repo) 'ask)
"unstaged — s asks"
"unstaged"))
('waiting (or (sync-ui--repo-message repo) "waiting"))
('committing "committing…")
('fetching "fetching…")
('rebasing (or (sync-ui--repo-message repo) "rebasing…"))
('pushing (or (sync-ui--repo-message repo) "pushing…"))
('conflicted (or (sync-ui--repo-message repo) "conflicted"))
('error (or (sync-ui--repo-message repo) "error"))
('missing (or (sync-ui--repo-message repo) "missing"))
(_ "?")))
(defun sync-ui--pill (repo)
"The AUTO/ASK category pill for REPO (click toggles the mode).
On rows where the rail chip is already loud (dirty ask, waiting,
conflict, error) the pill drops to the inactive chip face: one
saturated fill per row."
(let* ((auto-p (eq (sync-ui--repo-mode repo) 'auto))
(loud-p (or (memq (sync-ui--repo-state repo) '(waiting conflicted error))
(and (eq (sync-ui--repo-state repo) 'dirty) (not auto-p))))
(face (cond (loud-p 'sync-ui-chip-i)
(auto-p 'sync-ui-pill-auto)
(t 'sync-ui-pill-ask))))
(sync-ui--button
(propertize (if auto-p " AUTO " " ASK ") 'face face)
(lambda (&rest _)
(interactive)
(sync-ui--toggle repo))
"mouse-2 / RET: toggle auto/ask")))
;;; § 6 Dashboard rendering
(defun sync-ui--insert-title ()
"Title line."
(insert (propertize " sync"
'face (if (display-graphic-p)
'(:inherit sync-ui-strong :height 1.25)
'sync-ui-strong))
(propertize " local & redundant repos" 'face 'sync-ui-faded)
"\n"))
(defun sync-ui--hint (key label)
"One hint: bracketed KEY in faded, LABEL in default."
(concat (propertize "[" 'face 'sync-ui-faded)
(propertize key 'face 'sync-ui-strong)
(propertize "]" 'face 'sync-ui-faded)
" " label))
(defun sync-ui--insert-hints ()
"Literal key hints, maintained with the keymap."
(insert "\n "
(mapconcat #'identity
(list (sync-ui--hint "s" "sync")
(sync-ui--hint "RET" "magit")
(sync-ui--hint "r" "resume")
(sync-ui--hint "x" "skip")
(sync-ui--hint "t" "auto/ask"))
" ")
"\n "
(mapconcat #'identity
(list (sync-ui--hint "a" "add")
(sync-ui--hint "d" "remove")
(sync-ui--hint "g" "fetch")
(sync-ui--hint "n/p" "move")
(sync-ui--hint "q" "quit"))
" ")
"\n"))
(defun sync-ui--insert-card (repo)
"One two-line card for REPO: rail chip + name + pill on top,
gutter + path + per-remote ↑/↓ counts below. One logical line, so
navigation and hl-line treat the card as a unit."
(let* ((id (sync-ui--repo-id repo))
(band (face-background 'highlight nil t))
(top `(:background ,band))
(name-face `(:inherit (sync-ui-strong sync-ui-salient) :background ,band))
(faded-band `(:inherit sync-ui-faded :background ,band))
(pill (sync-ui--pill repo))
(left (concat (propertize " " 'face top)
(sync-ui--chip repo)
(propertize " " 'face top)
(sync-ui--button
(propertize (sync-ui--repo-path repo) 'face name-face)
(lambda (&rest _)
(interactive)
(sync-ui--open-magit repo))
"mouse-2 / RET: magit")
(propertize (concat " " (sync-ui--state-word repo))
'face faded-band)))
(remotes (sync-ui--remotes-summary repo band))
(bottom (concat (propertize " " 'face top)
(propertize (sync-ui--symbol 'gutter) 'face faded-band)
(propertize " " 'face top)
(propertize (format "%s · %s"
(sync-ui--repo-path repo)
(or (sync-ui--repo-branch repo) "?"))
'face faded-band)))
(left-width (string-width left))
(budget (- (sync-ui--width) (string-width pill) 3))
(left (if (> left-width budget)
(concat (truncate-string-to-width left (max 8 budget) nil nil "…"))
left)))
(insert
(propertize
(concat left
(sync-ui--justify-space (string-width pill) top)
pill
(propertize " " 'face top)
(propertize " " 'face top 'display "\n")
bottom
(sync-ui--justify-space (string-width remotes) top)
remotes
(propertize " " 'face top))
'sync-ui-id id)
"\n")))
(defun sync-ui--remotes-summary (repo band)
"Faded per-remote status: \"origin ↑2 · backup\", arrows only when non-zero.
A ⁺ marks a remote that has no copy of the branch yet."
(let ((faded-band `(:inherit sync-ui-faded :background ,band))
(salient-band `(:inherit sync-ui-salient :background ,band))
(popout-band `(:inherit sync-ui-popout :background ,band)))
(if (null (sync-ui--repo-remotes repo))
(propertize "no remotes" 'face faded-band)
(mapconcat
(lambda (status)
(pcase-let ((`(,name ,ahead ,behind) status))
(concat (propertize name 'face faded-band)
(cond
((null ahead)
(propertize (concat " " (sync-ui--symbol 'new)) 'face salient-band))
((> ahead 0)
(propertize (format " %s%d" (sync-ui--symbol 'up) ahead) 'face salient-band))
(t ""))
(if (and behind (> behind 0))
(propertize (format " %s%d" (sync-ui--symbol 'down) behind) 'face popout-band)
""))))
(sync-ui--repo-remotes repo)
(propertize " " 'face faded-band)))))
(defun sync-ui--header-line ()
"Four-slot status bar: [ SYNC | repos (n) ... counts ]."
(let* ((total (length sync-ui--repos))
(clean (cl-count-if (lambda (r)
(memq (sync-ui--repo-state r) '(clean synced)))
sync-ui--repos))
(dirty (cl-count-if (lambda (r) (eq (sync-ui--repo-state r) 'dirty))
sync-ui--repos))
(waiting (cl-count-if (lambda (r) (eq (sync-ui--repo-state r) 'waiting))
sync-ui--repos))
(busy (cl-count-if (lambda (r)
(memq (sync-ui--repo-state r)
'(committing fetching rebasing pushing)))
sync-ui--repos))
(attention (cl-count-if (lambda (r)
(memq (sync-ui--repo-state r) '(conflicted error missing)))
sync-ui--repos))
(chip (propertize " SYNC " 'face (if (> busy 0)
'sync-ui-chip-active
'sync-ui-chip)))
(name (propertize " repos " 'face 'sync-ui-strong))
(prim (propertize (format "(%d repo%s)" total (if (= total 1) "" "s"))
'face 'sync-ui-faded))
(parts (delq nil
(list (format "%d/%d clean" clean total)
(and (> dirty 0) (format "%d dirty" dirty))
(and (> waiting 0) (format "%d waiting" waiting))
(and (> attention 0) (format "%d ⚠" attention)))))
(right (propertize (concat (mapconcat #'identity parts " · ") " ")
'face 'sync-ui-faded)))
(concat (propertize " " 'display '(raise 0.15))
chip name
(propertize " " 'display '(raise -0.20))
prim
(propertize " " 'display `(space :align-to (- right ,(string-width right))))
right)))
(defun sync-ui--render ()
"Re-render the dashboard, restoring point by repo id."
(when-let* ((buf (get-buffer sync-ui--buffer-name)))
(with-current-buffer buf
(when (eq major-mode 'sync-ui-mode)
(let ((id (get-text-property (point) 'sync-ui-id))
(inhibit-read-only t))
(erase-buffer)
(sync-ui--insert-title)
(sync-ui--insert-hints)
(insert (sync-ui--hairline) "\n")
(if (null sync-ui--repos)
(insert (propertize " no repos — a adds one" 'face 'sync-ui-faded)
"\n")
(dolist (repo sync-ui--repos)
(sync-ui--insert-card repo)
(unless (eq repo (car (last sync-ui--repos)))
(insert (propertize (concat " " (sync-ui--symbol 'gutter))
'face 'sync-ui-faded)
"\n"))))
(if-let* ((match (and id
(progn (goto-char (point-min))
(text-property-search-forward
'sync-ui-id id #'equal)))))
(goto-char (prop-match-beginning match))
(goto-char (point-min))
(sync-ui-next)))))))
(defun sync-ui--update-card (repo)
"In-place rewrite of REPO's card; the buffer never reflows."
(when-let* ((buf (get-buffer sync-ui--buffer-name)))
(with-current-buffer buf
(when (eq major-mode 'sync-ui-mode)
(save-excursion
(goto-char (point-min))
(when-let* ((match (text-property-search-forward
'sync-ui-id (sync-ui--repo-id repo) #'equal)))
(let ((inhibit-read-only t)
(beg (prop-match-beginning match)))
(goto-char beg)
(let ((end (min (point-max) (1+ (line-end-position)))))
(delete-region beg end)
(goto-char beg)
(sync-ui--insert-card repo)))))))))
;;; § 7 Mode and navigation
(defun sync-ui--repo-at-point ()
"Repo struct for the card at point."
(when-let* ((id (get-text-property (point) 'sync-ui-id)))
(sync-ui--find id)))
(defun sync-ui-next ()
"Move to the next card."
(interactive)
(let ((match (text-property-search-forward 'sync-ui-id nil nil t)))
(if match
(goto-char (prop-match-beginning match))
(goto-char (point-min))
(when-let* ((first (text-property-search-forward 'sync-ui-id nil nil)))
(goto-char (prop-match-beginning first))))))
(defun sync-ui-previous ()
"Move to the previous card, wrapping to the last."
(interactive)
(let ((starts nil))
(save-excursion
(goto-char (point-min))
(cl-loop for m = (text-property-search-forward 'sync-ui-id nil nil)
while m
do (push (prop-match-beginning m) starts)
(goto-char (prop-match-end m))))
(let ((target (or (car (cl-remove-if-not (lambda (s) (< s (point))) starts))
(car starts))))
(if target
(goto-char target)
(message "sync-ui: no cards")))))
(defun sync-ui--card-range ()
"hl-line range: the whole card (both visual lines), or nothing."
(if (get-text-property (point) 'sync-ui-id)
(cons (line-beginning-position)
(min (point-max) (1+ (line-end-position))))
(cons (point) (point))))
(defun sync-ui--open-magit (repo)
"Open REPO in magit, falling back to dired when magit is absent."
(if (require 'magit nil t)
(progn
(declare-function magit-status "magit")
(magit-status (sync-ui--repo-id repo)))
(dired (sync-ui--repo-id repo))))
(defun sync-ui-magit ()
"Open the repo at point in magit."
(interactive)
(if-let* ((repo (sync-ui--repo-at-point)))
(sync-ui--open-magit repo)
(user-error "No repo at point")))
(defun sync-ui--toggle (repo)
"Flip REPO between auto and ask, persist, repaint."
(setf (sync-ui--repo-mode repo)
(if (eq (sync-ui--repo-mode repo) 'auto) 'ask 'auto))
(sync-ui--save-repos)
(sync-ui--update-card repo)
(message "%s: %s" (sync-ui--repo-path repo) (sync-ui--repo-mode repo)))
(defun sync-ui-toggle ()
"Toggle auto/ask for the repo at point."
(interactive)
(when-let* ((repo (sync-ui--repo-at-point)))
(sync-ui--toggle repo)))
(defun sync-ui-add-repo (dir mode)
"Register DIR as a managed repo with MODE (`auto' or `ask')."
(interactive
(list (read-directory-name "Repo: ")
(intern (completing-read "Mode: " '("ask" "auto") nil t nil nil "ask"))))
(unless sync-ui--repos (sync-ui--load-registry))
(let* ((id (expand-file-name dir))
(repo (sync-ui--repo-create
:id id
:path (abbreviate-file-name id)
:mode mode)))
(sync-ui--probe repo)
(if (eq (sync-ui--repo-state repo) 'missing)
(user-error "%s: %s" (sync-ui--repo-path repo)
(sync-ui--repo-message repo))
(when (sync-ui--find id)
(user-error "%s is already managed" (sync-ui--repo-path repo)))
(setq sync-ui--repos (append sync-ui--repos (list repo)))
(sync-ui--save-repos)
(sync-ui--render)
(message "%s added (%s)" (sync-ui--repo-path repo) mode))))
(defun sync-ui-remove-repo ()
"Remove the repo at point from the managed set (the repo stays on disk)."
(interactive)
(when-let* ((repo (sync-ui--repo-at-point)))
(when (y-or-n-p (format "Stop managing %s? " (sync-ui--repo-path repo)))
(setq sync-ui--repos (delq repo sync-ui--repos))
(sync-ui--save-repos)
(sync-ui--render))))
(defvar sync-ui-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "s") #'sync-ui-sync-all)
(define-key map (kbd "RET") #'sync-ui-magit)
(define-key map (kbd "<return>") #'sync-ui-magit)
(define-key map (kbd "r") #'sync-ui-resume)
(define-key map (kbd "x") #'sync-ui-skip)
(define-key map (kbd "t") #'sync-ui-toggle)
(define-key map (kbd "a") #'sync-ui-add-repo)
(define-key map (kbd "d") #'sync-ui-remove-repo)
(define-key map (kbd "g") #'sync-ui-refresh)
(define-key map (kbd "n") #'sync-ui-next)
(define-key map (kbd "p") #'sync-ui-previous)
(define-key map (kbd "q") #'quit-window)
map)
"Keymap for `sync-ui-mode'.")
(define-derived-mode sync-ui-mode special-mode "sync"
"NΛNO-style dashboard for git repo sync."
(setq-local cursor-type nil
truncate-lines t
left-fringe-width 1
right-fringe-width 8
header-line-format '(:eval (sync-ui--header-line))
mode-line-format ""
hl-line-range-function #'sync-ui--card-range)
(face-remap-set-base 'hl-line 'sync-ui-selected)
(hl-line-mode 1))
(with-eval-after-load 'helix
(helix-define-key 'normal "j" #'sync-ui-next 'sync-ui-mode)
(helix-define-key 'normal "k" #'sync-ui-previous 'sync-ui-mode)
(helix-define-key 'normal "RET" #'sync-ui-magit 'sync-ui-mode)
(helix-define-key 'normal (kbd "<return>") #'sync-ui-magit 'sync-ui-mode))
;;; § 8 Entry points
;;;###autoload
(defun sync-ui ()
"Open the sync dashboard."
(interactive)
(unless sync-ui--repos (sync-ui--load-registry))
(mapc #'sync-ui--probe sync-ui--repos)
(let ((buf (get-buffer-create sync-ui--buffer-name)))
(with-current-buffer buf
(unless (eq major-mode 'sync-ui-mode)
(sync-ui-mode))
(sync-ui--render))
(pop-to-buffer buf)))
(provide 'sync-ui)
;;; sync-ui.el ends here
|