Skip to content

Commit 16a28be

Browse files
committed
[REF] Don't constantly reset the current buffer
For some reason, when I try to refresh the buffer from a separate thread, the deleted code causes newlines between widgets to disappear if and only if the syncthing buffer isn't focused. When I remove this code, everything works correctly. Furthermore, it appears that none of this code is actually necessary. Nor does it do what it appears to do because, e.g., `syncthing-buffer' will be nil unless the current buffer IS the syncthing buffer.
1 parent 3108ee7 commit 16a28be

File tree

2 files changed

+122
-150
lines changed

2 files changed

+122
-150
lines changed

syncthing-draw.el

Lines changed: 115 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -23,58 +23,46 @@
2323
(defun syncthing--draw-folders-header (&optional &key before after)
2424
"Draw folder header with optional BEFORE and AFTER separator."
2525
(syncthing-trace)
26-
(save-window-excursion
27-
(switch-to-buffer
28-
(get-buffer-create (syncthing-buffer-name syncthing-buffer)))
29-
(when before
30-
(widget-insert (syncthing--title "\n")))
31-
(widget-insert
32-
(syncthing--title (format "%s Folders\n"
33-
(syncthing--fallback-ascii "folder"))))
34-
(when after
35-
(widget-insert (syncthing--title "\n")))))
26+
(when before
27+
(widget-insert (syncthing--title "\n")))
28+
(widget-insert
29+
(syncthing--title (format "%s Folders\n"
30+
(syncthing--fallback-ascii "folder"))))
31+
(when after
32+
(widget-insert (syncthing--title "\n"))))
3633

3734
(defun syncthing--draw-devices-header (&optional &key before after)
3835
"Draw device header with optional BEFORE and AFTER separator."
3936
(syncthing-trace)
40-
(save-window-excursion
41-
(switch-to-buffer
42-
(get-buffer-create (syncthing-buffer-name syncthing-buffer)))
43-
(when before
44-
(widget-insert (syncthing--title "\n")))
45-
(widget-insert
46-
(syncthing--title (format "%s Devices\n"
47-
(syncthing--fallback-ascii "laptop"))))
48-
(when after
49-
(widget-insert (syncthing--title "\n")))))
37+
(when before
38+
(widget-insert (syncthing--title "\n")))
39+
(widget-insert
40+
(syncthing--title (format "%s Devices\n"
41+
(syncthing--fallback-ascii "laptop"))))
42+
(when after
43+
(widget-insert (syncthing--title "\n"))))
5044

5145
(defun syncthing--draw-logs-header (&optional &key before after)
5246
"Draw log header with optional BEFORE and AFTER separator."
5347
(syncthing-trace)
54-
(save-window-excursion
55-
(switch-to-buffer
56-
(get-buffer-create (syncthing-buffer-name syncthing-buffer)))
57-
(when before
58-
(widget-insert (syncthing--title "\n")))
59-
(widget-insert
60-
(syncthing--title (format "%s Logs\n"
61-
(syncthing--fallback-ascii "envelope"))))
62-
(when after
63-
(widget-insert (syncthing--title "\n")))))
48+
(when before
49+
(widget-insert (syncthing--title "\n")))
50+
(widget-insert
51+
(syncthing--title (format "%s Logs\n"
52+
(syncthing--fallback-ascii "envelope"))))
53+
(when after
54+
(widget-insert (syncthing--title "\n"))))
6455

6556
(defun syncthing--draw-changes-header (&optional &key before after)
6657
"Draw log header with optional BEFORE and AFTER separator."
6758
(syncthing-trace)
68-
(save-window-excursion
69-
(switch-to-buffer
70-
(get-buffer-create (syncthing-buffer-name syncthing-buffer)))
71-
(when before
72-
(widget-insert (syncthing--title "\n")))
73-
(widget-insert
74-
(syncthing--title (format "%s Recent Changes\n"
75-
(syncthing--fallback-ascii "hourglass"))))
76-
(when after
77-
(widget-insert (syncthing--title "\n")))))
59+
(when before
60+
(widget-insert (syncthing--title "\n")))
61+
(widget-insert
62+
(syncthing--title (format "%s Recent Changes\n"
63+
(syncthing--fallback-ascii "hourglass"))))
64+
(when after
65+
(widget-insert (syncthing--title "\n"))))
7866

7967
(defun syncthing--draw-folders (server)
8068
"Draw folder widget in buffer from SERVER."
@@ -132,43 +120,37 @@
132120
(defun syncthing--list-logs (logs)
133121
"Render LOGS as a widget."
134122
(syncthing-trace)
135-
(save-window-excursion
136-
(switch-to-buffer
137-
(get-buffer-create (syncthing-buffer-name syncthing-buffer)))
138-
(let (text)
139-
(dolist (item (alist-get 'messages logs))
140-
(let-alist item
141-
(push (format "%s\t%s" .when .message) text)))
142-
(widget-insert (string-join (reverse text) "\n")))))
123+
(let (text)
124+
(dolist (item (alist-get 'messages logs))
125+
(let-alist item
126+
(push (format "%s\t%s" .when .message) text)))
127+
(widget-insert (string-join (reverse text) "\n"))))
143128

144129
(defun syncthing--list-changes (change)
145130
"Render CHANGE as a widget."
146131
(syncthing-trace)
147132
(let ((data (syncthing-server-data syncthing-server)))
148-
(save-window-excursion
149-
(switch-to-buffer
150-
(get-buffer-create (syncthing-buffer-name syncthing-buffer)))
151-
(widget-insert
152-
(with-temp-buffer
153-
(let (text)
154-
(push "|Device|Action|Type|Folder|Path|Time|" text)
155-
(push "|-|-|-|-|-|-|" text)
156-
(dolist (item change)
157-
(let-alist (alist-get 'data item)
158-
(push (format
159-
"|%s|%s|%s|%s|%s|%s|"
160-
(alist-get (intern `,.modifiedBy)
161-
(alist-get 'device-map data))
162-
.action .type .label .path
163-
(format-time-string
164-
"%F %T"
165-
(encode-time
166-
(iso8601-parse (alist-get 'time item)))))
167-
text)))
168-
(insert (string-join (reverse text) "\n")))
169-
(orgtbl-mode)
170-
(org-table-align)
171-
(substring-no-properties (buffer-string)))))))
133+
(widget-insert
134+
(with-temp-buffer
135+
(let (text)
136+
(push "|Device|Action|Type|Folder|Path|Time|" text)
137+
(push "|-|-|-|-|-|-|" text)
138+
(dolist (item change)
139+
(let-alist (alist-get 'data item)
140+
(push (format
141+
"|%s|%s|%s|%s|%s|%s|"
142+
(alist-get (intern `,.modifiedBy)
143+
(alist-get 'device-map data))
144+
.action .type .label .path
145+
(format-time-string
146+
"%F %T"
147+
(encode-time
148+
(iso8601-parse (alist-get 'time item)))))
149+
text)))
150+
(insert (string-join (reverse text) "\n")))
151+
(orgtbl-mode)
152+
(org-table-align)
153+
(substring-no-properties (buffer-string))))))
172154

173155
(defun syncthing--list-37-folder (folder)
174156
"Render single FOLDER item in a widget."
@@ -394,40 +376,37 @@
394376
(file-name-base
395377
(alist-get 'filename (alist-get 'lastFile stats)))))))
396378
items)
397-
(save-window-excursion
398-
(switch-to-buffer
399-
(get-buffer-create (syncthing-buffer-name syncthing-buffer)))
400-
(widget-create
401-
'syncthing-collapsible
402-
:value
403-
(not (member id (syncthing-buffer-fold-folders syncthing-buffer)))
404-
:tag
405-
(concat
406-
" "
407-
(syncthing--color-perc perc)
408-
(format "%s%s"
409-
(syncthing--bold
410-
(format " %s" name))
411-
(if paused (syncthing--italic " (Paused)") "")))
412-
:items (nreverse items)
413-
:spacing syncthing-align-folder-values
414-
:action
415-
`(lambda (widget &optional event)
416-
;; "super"
417-
(apply 'widget-toggle-action `(,widget ,event))
379+
(widget-create
380+
'syncthing-collapsible
381+
:value
382+
(not (member id (syncthing-buffer-fold-folders syncthing-buffer)))
383+
:tag
384+
(concat
385+
" "
386+
(syncthing--color-perc perc)
387+
(format "%s%s"
388+
(syncthing--bold
389+
(format " %s" name))
390+
(if paused (syncthing--italic " (Paused)") "")))
391+
:items (nreverse items)
392+
:spacing syncthing-align-folder-values
393+
:action
394+
`(lambda (widget &optional event)
395+
;; "super"
396+
(apply 'widget-toggle-action `(,widget ,event))
418397

419-
(if (member ,id (syncthing-buffer-fold-folders syncthing-buffer))
420-
(progn
421-
(setf (syncthing-buffer-fold-folders syncthing-buffer)
422-
(delete ,id (syncthing-buffer-fold-folders
423-
syncthing-buffer)))
424-
(push ,id (syncthing-buffer-skip-fold-folders
425-
syncthing-buffer)))
426-
(progn
427-
(push ,id (syncthing-buffer-fold-folders syncthing-buffer))
428-
(setf (syncthing-buffer-skip-fold-folders syncthing-buffer)
429-
(delete ,id (syncthing-buffer-skip-fold-folders
430-
syncthing-buffer))))))))))
398+
(if (member ,id (syncthing-buffer-fold-folders syncthing-buffer))
399+
(progn
400+
(setf (syncthing-buffer-fold-folders syncthing-buffer)
401+
(delete ,id (syncthing-buffer-fold-folders
402+
syncthing-buffer)))
403+
(push ,id (syncthing-buffer-skip-fold-folders
404+
syncthing-buffer)))
405+
(progn
406+
(push ,id (syncthing-buffer-fold-folders syncthing-buffer))
407+
(setf (syncthing-buffer-skip-fold-folders syncthing-buffer)
408+
(delete ,id (syncthing-buffer-skip-fold-folders
409+
syncthing-buffer)))))))))
431410

432411
(defun syncthing--list-37-device (device)
433412
"Render single DEVICE item in a widget."
@@ -623,38 +602,35 @@
623602
(format "\n%s "
624603
(syncthing--space syncthing-align-device-values))))
625604
items)
626-
(save-window-excursion
627-
(switch-to-buffer
628-
(get-buffer-create (syncthing-buffer-name syncthing-buffer)))
629-
(widget-create
630-
'syncthing-collapsible
631-
:value (not (member id (syncthing-buffer-fold-devices
632-
syncthing-buffer)))
633-
:tag
634-
(concat
635-
" "
636-
(syncthing--color-perc (alist-get 'completion completion))
637-
(format "%s%s" (syncthing--bold (format " %s" name))
638-
(if paused (syncthing--italic " (Paused)") "")))
639-
:items (nreverse items)
640-
:spacing syncthing-align-device-values
641-
:action
642-
`(lambda (widget &optional event)
643-
;; "super"
644-
(apply 'widget-toggle-action `(,widget ,event))
605+
(widget-create
606+
'syncthing-collapsible
607+
:value (not (member id (syncthing-buffer-fold-devices
608+
syncthing-buffer)))
609+
:tag
610+
(concat
611+
" "
612+
(syncthing--color-perc (alist-get 'completion completion))
613+
(format "%s%s" (syncthing--bold (format " %s" name))
614+
(if paused (syncthing--italic " (Paused)") "")))
615+
:items (nreverse items)
616+
:spacing syncthing-align-device-values
617+
:action
618+
`(lambda (widget &optional event)
619+
;; "super"
620+
(apply 'widget-toggle-action `(,widget ,event))
645621

646-
(if (member ,id (syncthing-buffer-fold-devices syncthing-buffer))
647-
(progn
648-
(setf (syncthing-buffer-fold-devices syncthing-buffer)
649-
(delete ,id (syncthing-buffer-fold-devices
650-
syncthing-buffer)))
651-
(push ,id (syncthing-buffer-skip-fold-devices
652-
syncthing-buffer)))
622+
(if (member ,id (syncthing-buffer-fold-devices syncthing-buffer))
653623
(progn
654-
(push ,id (syncthing-buffer-fold-devices syncthing-buffer))
655-
(setf (syncthing-buffer-skip-fold-devices syncthing-buffer)
656-
(delete ,id (syncthing-buffer-skip-fold-devices
657-
syncthing-buffer))))))))))
624+
(setf (syncthing-buffer-fold-devices syncthing-buffer)
625+
(delete ,id (syncthing-buffer-fold-devices
626+
syncthing-buffer)))
627+
(push ,id (syncthing-buffer-skip-fold-devices
628+
syncthing-buffer)))
629+
(progn
630+
(push ,id (syncthing-buffer-fold-devices syncthing-buffer))
631+
(setf (syncthing-buffer-skip-fold-devices syncthing-buffer)
632+
(delete ,id (syncthing-buffer-skip-fold-devices
633+
syncthing-buffer)))))))))
658634

659635
(defun syncthing--header-line (server)
660636
"Return SERVER `header-line-format' string."
@@ -792,13 +768,10 @@
792768
(defun syncthing--draw-buffer (server)
793769
"Setup widgets and draw other buffer items for SERVER."
794770
(syncthing-trace)
795-
(save-window-excursion
796-
(switch-to-buffer
797-
(get-buffer-create (syncthing-buffer-name syncthing-buffer)))
798-
(widget-setup)
799-
(setq header-line-format (syncthing--header-line server))
800-
;; header drawing messes up with cursor position, reset to 0,0
801-
(goto-char (or (syncthing-buffer-point syncthing-buffer) 0))))
771+
(widget-setup)
772+
(setq header-line-format (syncthing--header-line server))
773+
;; header drawing messes up with cursor position, reset to 0,0
774+
(goto-char (or (syncthing-buffer-point syncthing-buffer) 0)))
802775

803776
(defun syncthing--draw (server)
804777
"Setup buffer and draw widgets for SERVER."

syncthing-update.el

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@
1818
(defun syncthing--update (&rest _)
1919
"Update function for every refresh iteration."
2020
(syncthing-trace)
21-
(save-window-excursion
22-
(switch-to-buffer
23-
(get-buffer-create (syncthing-buffer-name syncthing-buffer)))
24-
(syncthing--ping syncthing-server)
25-
(syncthing--server-update syncthing-server)
26-
(syncthing--init-state)
27-
(syncthing--draw syncthing-server)
28-
(setf (syncthing-buffer-collapse-after-start syncthing-buffer) nil)))
21+
(when-let* ((buf (syncthing-buffer-name syncthing-buffer)))
22+
(with-current-buffer buf
23+
(syncthing--ping syncthing-server)
24+
(syncthing--server-update syncthing-server)
25+
(syncthing--init-state)
26+
(syncthing--draw syncthing-server)
27+
(setf (syncthing-buffer-collapse-after-start syncthing-buffer) nil))))
2928

3029
(defun syncthing--calc-speed (server data)
3130
"Calculate upload and download rate DATA for SERVER."

0 commit comments

Comments
 (0)