|
1 |
+ |
//! `alloy sync` — the file sync view.
|
|
2 |
+ |
//!
|
|
3 |
+ |
//! See docs/CONTINUITY.md: a mesh VPN and a file sync are what make an Alloy
|
|
4 |
+ |
//! machine feel like the same machine as the last one, so the console fronts
|
|
5 |
+ |
//! both. This is the sync half, and the sibling of [`mesh`](crate::mesh) in
|
|
6 |
+ |
//! shape as well as in purpose: a [`Backend`] trait, one real implementation
|
|
7 |
+ |
//! and one mock, a tabbed list, and every string on screen generic except the
|
|
8 |
+ |
//! backend name in the title.
|
|
9 |
+ |
//!
|
|
10 |
+ |
//! # Fronting the CLI, not the REST API
|
|
11 |
+ |
//!
|
|
12 |
+ |
//! Syncthing's documented interface is an HTTP API on `127.0.0.1:8384` behind
|
|
13 |
+ |
//! an API key in `config.xml`. Fronting it directly would mean an HTTP client
|
|
14 |
+ |
//! in a binary that has none, plus reading a key out of a file the daemon owns.
|
|
15 |
+ |
//! `syncthing cli` is a first-party client for that same API which finds the
|
|
16 |
+ |
//! key itself, so the console keeps the shape every other view has: a command
|
|
17 |
+ |
//! front, logged in the pane, teaching a command the user could have typed.
|
|
18 |
+ |
//!
|
|
19 |
+ |
//! Three invocations cover a refresh. `config dump-json` carries every folder
|
|
20 |
+ |
//! and device with its paused flag, `show connections` carries who is actually
|
|
21 |
+ |
//! connected right now, and `show system` names which device is this one.
|
|
22 |
+ |
//! Configuration and liveness are genuinely different endpoints; merging them
|
|
23 |
+ |
//! is this module's job rather than something to wish for from the tool.
|
|
24 |
+ |
//!
|
|
25 |
+ |
//! # The daemon not running is a state, not an error
|
|
26 |
+ |
//!
|
|
27 |
+ |
//! Syncthing ships preset-disabled (docs/CONTINUITY.md: "a running daemon with
|
|
28 |
+ |
//! nothing to do is unjustified surface"), so on a fresh install the expected
|
|
29 |
+ |
//! answer from every command here is `connection refused`. That is not a
|
|
30 |
+ |
//! failure to report, it is the un-enrolled machine this screen exists to
|
|
31 |
+ |
//! enroll, and it renders as an offer rather than as a red line.
|
|
32 |
+ |
//!
|
|
33 |
+ |
//! Enrollment is `systemctl --user enable --now syncthing.service`. User scope
|
|
34 |
+ |
//! on purpose: the unit is a user service, the files it syncs are the user's,
|
|
35 |
+ |
//! and nothing here needs or asks for root. That is the whole of the
|
|
36 |
+ |
//! difference from [`mesh`](crate::mesh), whose `tailscale up` is a system
|
|
37 |
+ |
//! daemon and a privileged action.
|
|
38 |
+ |
//!
|
|
39 |
+ |
//! <!-- wiki: alloy-console -->
|
|
40 |
+ |
|
|
41 |
+ |
use std::collections::HashMap;
|
|
42 |
+ |
|
|
43 |
+ |
use alloy_tui::{AlloyBlock, AlloyList, AlloyTabs, Cursor, Hint, Severity, Theme, hint, text};
|
|
44 |
+ |
use anyhow::Result;
|
|
45 |
+ |
use ratatui::Frame;
|
|
46 |
+ |
use ratatui::crossterm::event::{KeyCode, KeyEvent};
|
|
47 |
+ |
use ratatui::layout::{Constraint, Layout, Rect};
|
|
48 |
+ |
use ratatui::text::{Line, Span};
|
|
49 |
+ |
use serde::Deserialize;
|
|
50 |
+ |
|
|
51 |
+ |
use crate::cli::{CommandLog, Invocation};
|
|
52 |
+ |
use crate::shell::{Flow, View, block_title, truncate};
|
|
53 |
+ |
|
|
54 |
+ |
/// Ticks between background refreshes.
|
|
55 |
+ |
///
|
|
56 |
+ |
/// Matching [`mesh`](crate::mesh): a folder finishing a scan or a peer
|
|
57 |
+ |
/// connecting happens on the scale of seconds-to-minutes, and three processes
|
|
58 |
+ |
/// per second to learn nothing would be worse than a clock that is five
|
|
59 |
+ |
/// seconds stale.
|
|
60 |
+ |
const POLL_TICKS: u64 = 5;
|
|
61 |
+ |
|
|
62 |
+ |
/// The user unit enrollment enables.
|
|
63 |
+ |
const UNIT: &str = "syncthing.service";
|
|
64 |
+ |
|
|
65 |
+ |
// ---------------------------------------------------------------------------
|
|
66 |
+ |
// What the screen shows
|
|
67 |
+ |
// ---------------------------------------------------------------------------
|
|
68 |
+ |
|
|
69 |
+ |
/// A synchronized directory.
|
|
70 |
+ |
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
71 |
+ |
pub(crate) struct Folder {
|
|
72 |
+ |
pub id: String,
|
|
73 |
+ |
/// Human label, falling back to the id when unset. Syncthing allows an
|
|
74 |
+ |
/// empty label and shows the id in that case; so does this.
|
|
75 |
+ |
pub label: String,
|
|
76 |
+ |
pub path: String,
|
|
77 |
+ |
/// `sendreceive`, `sendonly`, `receiveonly`, as Syncthing spells them.
|
|
78 |
+ |
pub kind: String,
|
|
79 |
+ |
pub paused: bool,
|
|
80 |
+ |
/// How many devices this folder is shared with, this machine included.
|
|
81 |
+ |
pub shared_with: usize,
|
|
82 |
+ |
}
|
|
83 |
+ |
|
|
84 |
+ |
impl Folder {
|
|
85 |
+ |
fn severity(&self) -> Severity {
|
|
86 |
+ |
if self.paused {
|
|
87 |
+ |
Severity::Warn
|
|
88 |
+ |
} else {
|
|
89 |
+ |
Severity::Healthy
|
|
90 |
+ |
}
|
|
91 |
+ |
}
|
|
92 |
+ |
|
|
93 |
+ |
fn state_label(&self) -> &'static str {
|
|
94 |
+ |
if self.paused { "paused" } else { "syncing" }
|
|
95 |
+ |
}
|
|
96 |
+ |
}
|
|
97 |
+ |
|
|
98 |
+ |
/// A peer this machine syncs with, or this machine itself.
|
|
99 |
+ |
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
100 |
+ |
pub(crate) struct Device {
|
|
101 |
+ |
pub id: String,
|
|
102 |
+ |
pub name: String,
|
|
103 |
+ |
pub paused: bool,
|
|
104 |
+ |
/// Connected right now. Always false for this machine, which does not
|
|
105 |
+ |
/// connect to itself, so [`Device::state_label`] special-cases it.
|
|
106 |
+ |
pub connected: bool,
|
|
107 |
+ |
pub is_self: bool,
|
|
108 |
+ |
}
|
|
109 |
+ |
|
|
110 |
+ |
impl Device {
|
|
111 |
+ |
fn severity(&self) -> Severity {
|
|
112 |
+ |
// Info rather than a health colour for this machine: it is a
|
|
113 |
+ |
// notable row, not a good or bad one. Matches how `mesh` tints the
|
|
114 |
+ |
// exit node.
|
|
115 |
+ |
if self.is_self {
|
|
116 |
+ |
return Severity::Info;
|
|
117 |
+ |
}
|
|
118 |
+ |
// Paused and disconnected share a tint and differ in label, as
|
|
119 |
+ |
// online/offline do in `mesh`. The distinction worth colouring is
|
|
120 |
+ |
// "reaching this device" against "not", and neither is reaching it.
|
|
121 |
+ |
if self.paused || !self.connected {
|
|
122 |
+ |
return Severity::Warn;
|
|
123 |
+ |
}
|
|
124 |
+ |
Severity::Healthy
|
|
125 |
+ |
}
|
|
126 |
+ |
|
|
127 |
+ |
fn state_label(&self) -> &'static str {
|
|
128 |
+ |
if self.is_self {
|
|
129 |
+ |
return "this machine";
|
|
130 |
+ |
}
|
|
131 |
+ |
if self.paused {
|
|
132 |
+ |
return "paused";
|
|
133 |
+ |
}
|
|
134 |
+ |
if self.connected {
|
|
135 |
+ |
"connected"
|
|
136 |
+ |
} else {
|
|
137 |
+ |
"disconnected"
|
|
138 |
+ |
}
|
|
139 |
+ |
}
|
|
140 |
+ |
|
|
141 |
+ |
/// The first block of the device id, which is how Syncthing's own UI
|
|
142 |
+ |
/// abbreviates it and enough to tell two devices apart by eye.
|
|
143 |
+ |
fn short_id(&self) -> &str {
|
|
144 |
+ |
self.id.split('-').next().unwrap_or(&self.id)
|
|
145 |
+ |
}
|
|
146 |
+ |
}
|
|
147 |
+ |
|
|
148 |
+ |
/// Everything one refresh gathers.
|
|
149 |
+ |
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
|
150 |
+ |
pub(crate) struct SyncState {
|
|
151 |
+ |
pub folders: Vec<Folder>,
|
|
152 |
+ |
pub devices: Vec<Device>,
|
|
153 |
+ |
}
|
|
154 |
+ |
|
|
155 |
+ |
/// Whether the daemon is there to talk to.
|
|
156 |
+ |
///
|
|
157 |
+ |
/// Two states rather than a `Result`, because "not running" is the ordinary
|
|
158 |
+ |
/// condition of a machine that has not enrolled yet: it wants an offer, not an
|
|
159 |
+ |
/// error. There is deliberately no "syncthing is absent" arm — [`detect`]
|
|
160 |
+ |
/// answers that by handing back [`Mock`], the same way [`mesh`](crate::mesh)
|
|
161 |
+ |
/// does, so a machine without the tool gets a demonstrable screen labelled
|
|
162 |
+ |
/// `mock` rather than a dead end.
|
|
163 |
+ |
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
164 |
+ |
pub(crate) enum Reach {
|
|
165 |
+ |
/// The daemon answered.
|
|
166 |
+ |
Running(SyncState),
|
|
167 |
+ |
/// Syncthing is installed and the daemon is not answering.
|
|
168 |
+ |
NotRunning,
|
|
169 |
+ |
}
|
|
170 |
+ |
|
|
171 |
+ |
// ---------------------------------------------------------------------------
|
|
172 |
+ |
// The backend seam
|
|
173 |
+ |
// ---------------------------------------------------------------------------
|
|
174 |
+ |
|
|
175 |
+ |
pub(crate) trait Backend {
|
|
176 |
+ |
fn name(&self) -> &'static str;
|
|
177 |
+ |
|
|
178 |
+ |
/// Read the whole screen, or say why there is nothing to read.
|
|
179 |
+ |
fn reach(&self, log: &mut CommandLog) -> Result<Reach>;
|
|
180 |
+ |
|
|
181 |
+ |
/// Start the daemon and have it start at every login.
|
|
182 |
+ |
fn enroll(&self, log: &mut CommandLog) -> Result<()>;
|
|
183 |
+ |
|
|
184 |
+ |
/// Pause or resume a folder.
|
|
185 |
+ |
fn set_folder_paused(&self, folder: &Folder, paused: bool, log: &mut CommandLog) -> Result<()>;
|
|
186 |
+ |
|
|
187 |
+ |
/// Pause or resume a device.
|
|
188 |
+ |
fn set_device_paused(&self, device: &Device, paused: bool, log: &mut CommandLog) -> Result<()>;
|
|
189 |
+ |
}
|
|
190 |
+ |
|
|
191 |
+ |
/// Pick a backend: `syncthing` when it answers, the mock otherwise.
|
|
192 |
+ |
pub(crate) fn detect() -> Box<dyn Backend> {
|
|
193 |
+ |
if Invocation::new("syncthing").arg("--version").probe() {
|
|
194 |
+ |
Box::new(Syncthing)
|
|
195 |
+ |
} else {
|
|
196 |
+ |
Box::new(Mock)
|
|
197 |
+ |
}
|
|
198 |
+ |
}
|
|
199 |
+ |
|
|
200 |
+ |
pub(crate) struct Syncthing;
|
|
201 |
+ |
|
|
202 |
+ |
impl Backend for Syncthing {
|
|
203 |
+ |
fn name(&self) -> &'static str {
|
|
204 |
+ |
"syncthing"
|
|
205 |
+ |
}
|
|
206 |
+ |
|
|
207 |
+ |
fn reach(&self, log: &mut CommandLog) -> Result<Reach> {
|
|
208 |
+ |
// Unlogged and first, because on an un-enrolled machine this fails
|
|
209 |
+ |
// every five seconds and a log pane filling with the same connection
|
|
210 |
+ |
// refused would bury the commands worth reading.
|
|
211 |
+ |
let Ok(config) = Invocation::new("syncthing")
|
|
212 |
+ |
.args(["cli", "config", "dump-json"])
|
|
213 |
+ |
.capture_quiet()
|
|
214 |
+ |
else {
|
|
215 |
+ |
return Ok(Reach::NotRunning);
|
|
216 |
+ |
};
|
|
217 |
+ |
// Now that the daemon is known to answer, the same read runs logged,
|
|
218 |
+ |
// so the pane teaches the command rather than hiding it.
|
|
219 |
+ |
let config = Invocation::new("syncthing")
|
|
220 |
+ |
.args(["cli", "config", "dump-json"])
|
|
221 |
+ |
.run(log)
|
|
222 |
+ |
.unwrap_or(config);
|
|
223 |
+ |
let connections = Invocation::new("syncthing")
|
|
224 |
+ |
.args(["cli", "show", "connections"])
|
|
225 |
+ |
.run(log)?;
|
|
226 |
+ |
let system = Invocation::new("syncthing")
|
|
227 |
+ |
.args(["cli", "show", "system"])
|
|
228 |
+ |
.run(log)?;
|
|
229 |
+ |
Ok(Reach::Running(parse(&config, &connections, &system)?))
|
|
230 |
+ |
}
|
|
231 |
+ |
|
|
232 |
+ |
/// `--now` so enrolling starts the daemon as well as arranging for it to
|
|
233 |
+ |
/// start next time. A user who pressed enroll and then had to log out
|
|
234 |
+ |
/// before anything happened would reasonably read that as broken.
|
|
235 |
+ |
fn enroll(&self, log: &mut CommandLog) -> Result<()> {
|
|
236 |
+ |
Invocation::new("systemctl")
|
|
237 |
+ |
.args(["--user", "enable", "--now", UNIT])
|
|
238 |
+ |
.run(log)
|
|
239 |
+ |
.map(drop)
|
|
240 |
+ |
}
|
|
241 |
+ |
|
|
242 |
+ |
fn set_folder_paused(&self, folder: &Folder, paused: bool, log: &mut CommandLog) -> Result<()> {
|
|
243 |
+ |
Invocation::new("syncthing")
|
|
244 |
+ |
.args(["cli", "config", "folders", &folder.id, "paused", "set"])
|
|
245 |
+ |
.arg(paused.to_string())
|
|
246 |
+ |
.run(log)
|
|
247 |
+ |
.map(drop)
|
|
248 |
+ |
}
|
|
249 |
+ |
|
|
250 |
+ |
fn set_device_paused(&self, device: &Device, paused: bool, log: &mut CommandLog) -> Result<()> {
|
|
251 |
+ |
Invocation::new("syncthing")
|
|
252 |
+ |
.args(["cli", "config", "devices", &device.id, "paused", "set"])
|
|
253 |
+ |
.arg(paused.to_string())
|
|
254 |
+ |
.run(log)
|
|
255 |
+ |
.map(drop)
|
|
256 |
+ |
}
|
|
257 |
+ |
}
|
|
258 |
+ |
|
|
259 |
+ |
/// Fixed sample state, for machines without Syncthing.
|
|
260 |
+ |
///
|
|
261 |
+ |
/// Same purpose as [`mesh::Mock`](crate::mesh::Mock): the screen is
|
|
262 |
+ |
/// developable and demonstrable on a box that will never run the daemon, and
|
|
263 |
+ |
/// the title says which backend is answering so nobody mistakes it for real.
|
|
264 |
+ |
pub(crate) struct Mock;
|
|
265 |
+ |
|
|
266 |
+ |
impl Backend for Mock {
|
|
267 |
+ |
fn name(&self) -> &'static str {
|
|
268 |
+ |
"mock"
|
|
269 |
+ |
}
|
|
270 |
+ |
|
|
271 |
+ |
fn reach(&self, _log: &mut CommandLog) -> Result<Reach> {
|
|
272 |
+ |
Ok(Reach::Running(SyncState {
|
|
273 |
+ |
folders: vec![
|
|
274 |
+ |
Folder {
|
|
275 |
+ |
id: "documents".into(),
|
|
276 |
+ |
label: "Documents".into(),
|
|
277 |
+ |
path: "~/Documents".into(),
|
|
278 |
+ |
kind: "sendreceive".into(),
|
|
279 |
+ |
paused: false,
|
|
280 |
+ |
shared_with: 2,
|
|
281 |
+ |
},
|
|
282 |
+ |
Folder {
|
|
283 |
+ |
id: "photos".into(),
|
|
284 |
+ |
label: "Pictures".into(),
|
|
285 |
+ |
path: "~/Pictures".into(),
|
|
286 |
+ |
kind: "sendonly".into(),
|
|
287 |
+ |
paused: true,
|
|
288 |
+ |
shared_with: 1,
|
|
289 |
+ |
},
|
|
290 |
+ |
],
|
|
291 |
+ |
devices: vec![
|
|
292 |
+ |
Device {
|
|
293 |
+ |
id: "AAAAAAA-BBBBBBB".into(),
|
|
294 |
+ |
name: "fw13".into(),
|
|
295 |
+ |
paused: false,
|
|
296 |
+ |
connected: false,
|
|
297 |
+ |
is_self: true,
|
|
298 |
+ |
},
|
|
299 |
+ |
Device {
|
|
300 |
+ |
id: "CCCCCCC-DDDDDDD".into(),
|
|
301 |
+ |
name: "astra".into(),
|
|
302 |
+ |
paused: false,
|
|
303 |
+ |
connected: true,
|
|
304 |
+ |
is_self: false,
|
|
305 |
+ |
},
|
|
306 |
+ |
],
|
|
307 |
+ |
}))
|
|
308 |
+ |
}
|
|
309 |
+ |
|
|
310 |
+ |
fn enroll(&self, _log: &mut CommandLog) -> Result<()> {
|
|
311 |
+ |
Ok(())
|
|
312 |
+ |
}
|
|
313 |
+ |
|
|
314 |
+ |
fn set_folder_paused(&self, _f: &Folder, _p: bool, _log: &mut CommandLog) -> Result<()> {
|
|
315 |
+ |
Ok(())
|
|
316 |
+ |
}
|
|
317 |
+ |
|
|
318 |
+ |
fn set_device_paused(&self, _d: &Device, _p: bool, _log: &mut CommandLog) -> Result<()> {
|
|
319 |
+ |
Ok(())
|
|
320 |
+ |
}
|
|
321 |
+ |
}
|
|
322 |
+ |
|
|
323 |
+ |
// ---------------------------------------------------------------------------
|
|
324 |
+ |
// Parsing
|
|
325 |
+ |
// ---------------------------------------------------------------------------
|
|
326 |
+ |
|
|
327 |
+ |
#[derive(Deserialize)]
|
|
328 |
+ |
struct StConfig {
|
|
329 |
+ |
#[serde(default)]
|
|
330 |
+ |
folders: Vec<StFolder>,
|
|
331 |
+ |
#[serde(default)]
|
|
332 |
+ |
devices: Vec<StDevice>,
|
|
333 |
+ |
}
|
|
334 |
+ |
|
|
335 |
+ |
#[derive(Deserialize)]
|
|
336 |
+ |
#[serde(rename_all = "camelCase")]
|
|
337 |
+ |
struct StFolder {
|
|
338 |
+ |
id: String,
|
|
339 |
+ |
#[serde(default)]
|
|
340 |
+ |
label: String,
|
|
341 |
+ |
#[serde(default)]
|
|
342 |
+ |
path: String,
|
|
343 |
+ |
#[serde(rename = "type", default)]
|
|
344 |
+ |
kind: String,
|
|
345 |
+ |
#[serde(default)]
|
|
346 |
+ |
paused: bool,
|
|
347 |
+ |
#[serde(default)]
|
|
348 |
+ |
devices: Vec<StFolderDevice>,
|
|
349 |
+ |
}
|
|
350 |
+ |
|
|
351 |
+ |
#[derive(Deserialize)]
|
|
352 |
+ |
struct StFolderDevice {}
|
|
353 |
+ |
|
|
354 |
+ |
#[derive(Deserialize)]
|
|
355 |
+ |
struct StDevice {
|
|
356 |
+ |
// Syncthing spells it `deviceID`, not `deviceId`, so camelCase renaming
|
|
357 |
+ |
// does not reach it and the field has to be named outright. Caught by a
|
|
358 |
+ |
// fixture taken from real output rather than by reading the code.
|
|
359 |
+ |
#[serde(rename = "deviceID")]
|
|
360 |
+ |
device_id: String,
|
|
361 |
+ |
#[serde(default)]
|
|
362 |
+ |
name: String,
|
|
363 |
+ |
#[serde(default)]
|
|
364 |
+ |
paused: bool,
|
|
365 |
+ |
}
|
|
366 |
+ |
|
|
367 |
+ |
#[derive(Deserialize)]
|
|
368 |
+ |
struct StConnections {
|
|
369 |
+ |
#[serde(default)]
|
|
370 |
+ |
connections: HashMap<String, StConnection>,
|
|
371 |
+ |
}
|
|
372 |
+ |
|
|
373 |
+ |
#[derive(Deserialize)]
|
|
374 |
+ |
struct StConnection {
|
|
375 |
+ |
#[serde(default)]
|
|
376 |
+ |
connected: bool,
|
|
377 |
+ |
}
|
|
378 |
+ |
|
|
379 |
+ |
#[derive(Deserialize)]
|
|
380 |
+ |
struct StSystem {
|
|
381 |
+ |
/// `myID`, with the same capitalisation quirk as `deviceID`.
|
|
382 |
+ |
#[serde(rename = "myID", default)]
|
|
383 |
+ |
my_id: String,
|
|
384 |
+ |
}
|
|
385 |
+ |
|
|
386 |
+ |
/// Merge the three reads into one screen's worth of state.
|
|
387 |
+ |
fn parse(config: &str, connections: &str, system: &str) -> Result<SyncState> {
|
|
388 |
+ |
let config: StConfig = serde_json::from_str(config)?;
|
|
389 |
+ |
// Liveness and identity are best-effort on purpose. A folder list that
|
|
390 |
+ |
// renders without connection state is worth more than an error, and the
|
|
391 |
+ |
// only cost of losing either is a row reading "disconnected" or no row
|
|
392 |
+ |
// being marked as this machine.
|
|
393 |
+ |
let connections: StConnections = serde_json::from_str(connections).unwrap_or(StConnections {
|
|
394 |
+ |
connections: HashMap::new(),
|
|
395 |
+ |
});
|
|
396 |
+ |
let my_id = serde_json::from_str::<StSystem>(system)
|
|
397 |
+ |
.map(|system| system.my_id)
|
|
398 |
+ |
.unwrap_or_default();
|
|
399 |
+ |
|
|
400 |
+ |
let mut folders: Vec<Folder> = config
|
|
401 |
+ |
.folders
|
|
402 |
+ |
.into_iter()
|
|
403 |
+ |
.map(|folder| Folder {
|
|
404 |
+ |
label: if folder.label.is_empty() {
|
|
405 |
+ |
folder.id.clone()
|
|
406 |
+ |
} else {
|
|
407 |
+ |
folder.label
|
|
408 |
+ |
},
|
|
409 |
+ |
id: folder.id,
|
|
410 |
+ |
path: folder.path,
|
|
411 |
+ |
kind: folder.kind,
|
|
412 |
+ |
paused: folder.paused,
|
|
413 |
+ |
shared_with: folder.devices.len(),
|
|
414 |
+ |
})
|
|
415 |
+ |
.collect();
|
|
416 |
+ |
// Syncthing returns folders in config order, which is insertion order.
|
|
417 |
+ |
// Sorting by label keeps a list stable across a refresh that reordered
|
|
418 |
+ |
// nothing the user can see, and keeps the cursor over the same row.
|
|
419 |
+ |
folders.sort_by(|a, b| a.label.cmp(&b.label));
|
|
420 |
+ |
|
|
421 |
+ |
let mut devices: Vec<Device> = config
|
|
422 |
+ |
.devices
|
|
423 |
+ |
.into_iter()
|
|
424 |
+ |
.map(|device| {
|
|
425 |
+ |
let is_self = !my_id.is_empty() && device.device_id == my_id;
|
|
426 |
+ |
Device {
|
|
427 |
+ |
connected: connections
|
|
428 |
+ |
.connections
|
|
429 |
+ |
.get(&device.device_id)
|
|
430 |
+ |
.is_some_and(|connection| connection.connected),
|
|
431 |
+ |
name: if device.name.is_empty() {
|
|
432 |
+ |
device.device_id.split('-').next().unwrap_or("").to_string()
|
|
433 |
+ |
} else {
|
|
434 |
+ |
device.name
|
|
435 |
+ |
},
|
|
436 |
+ |
id: device.device_id,
|
|
437 |
+ |
paused: device.paused,
|
|
438 |
+ |
is_self,
|
|
439 |
+ |
}
|
|
440 |
+ |
})
|
|
441 |
+ |
.collect();
|
|
442 |
+ |
// This machine first, then connected peers, then the rest by name. Same
|
|
443 |
+ |
// rule as the peer list in `mesh`, and for the same reason: a map-backed
|
|
444 |
+ |
// source reshuffling under the cursor is the bug it prevents.
|
|
445 |
+ |
devices.sort_by(|a, b| {
|
|
446 |
+ |
b.is_self
|
|
447 |
+ |
.cmp(&a.is_self)
|
|
448 |
+ |
.then(b.connected.cmp(&a.connected))
|
|
449 |
+ |
.then(a.name.cmp(&b.name))
|
|
450 |
+ |
});
|
|
451 |
+ |
|
|
452 |
+ |
Ok(SyncState { folders, devices })
|
|
453 |
+ |
}
|
|
454 |
+ |
|
|
455 |
+ |
// ---------------------------------------------------------------------------
|
|
456 |
+ |
// The view
|
|
457 |
+ |
// ---------------------------------------------------------------------------
|
|
458 |
+ |
|
|
459 |
+ |
/// The two tabs, in bar order.
|
|
460 |
+ |
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
461 |
+ |
pub(crate) enum Tab {
|
|
462 |
+ |
Folders,
|
|
463 |
+ |
Devices,
|
|
464 |
+ |
}
|
|
465 |
+ |
|
|
466 |
+ |
impl Tab {
|
|
467 |
+ |
const ALL: [Tab; 2] = [Tab::Folders, Tab::Devices];
|
|
468 |
+ |
|
|
469 |
+ |
const fn label(self) -> &'static str {
|
|
470 |
+ |
match self {
|
|
471 |
+ |
Tab::Folders => "folders",
|
|
472 |
+ |
Tab::Devices => "devices",
|
|
473 |
+ |
}
|
|
474 |
+ |
}
|
|
475 |
+ |
|
|
476 |
+ |
const fn slot(self) -> usize {
|
|
477 |
+ |
match self {
|
|
478 |
+ |
Tab::Folders => 0,
|
|
479 |
+ |
Tab::Devices => 1,
|
|
480 |
+ |
}
|
|
481 |
+ |
}
|
|
482 |
+ |
|
|
483 |
+ |
const fn from_slot(slot: usize) -> Self {
|
|
484 |
+ |
match slot {
|
|
485 |
+ |
0 => Tab::Folders,
|
|
486 |
+ |
_ => Tab::Devices,
|
|
487 |
+ |
}
|
|
488 |
+ |
}
|
|
489 |
+ |
}
|
|
490 |
+ |
|
|
491 |
+ |
pub(crate) struct SyncView {
|
|
492 |
+ |
backend: Box<dyn Backend>,
|
|
493 |
+ |
reach: Reach,
|
|
494 |
+ |
tab: Tab,
|
|
495 |
+ |
/// One cursor per tab, so moving between them does not reset the other.
|
|
496 |
+ |
folders: Cursor,
|
|
497 |
+ |
devices: Cursor,
|
|
498 |
+ |
error: Option<String>,
|
|
499 |
+ |
ticks: u64,
|
|
500 |
+ |
}
|