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
//! Application state and the reducers that mutate it.
//!
//! State is plain data with no networking or rendering concerns, which
//! makes every transition unit-testable without a terminal or a socket.
use std::time::{Duration, Instant};
use crate::config::Settings;
use crate::engine::metrics::Sampler;
use crate::engine::models::{LatencyStats, Server, TestPhase, TestReport, TransferStats};
use crate::engine::EngineEvent;
/// Number of speed samples kept for sparklines.
const HISTORY_CAPACITY: usize = 120;
/// The screen currently in the foreground.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Screen {
/// Startup splash while servers are discovered.
Splash,
/// A test is running.
Testing,
/// Final results of a completed test.
Results,
/// Keyboard shortcut reference.
Help,
/// Read-only view of the active configuration.
Settings,
/// Server selection list.
ServerSelect,
/// Theme selection list.
ThemeSelect,
/// Past results and trends.
Trends,
/// A fatal test error.
Error,
}
impl Screen {
/// Whether this screen is an overlay that returns to a parent screen.
#[must_use]
pub fn is_overlay(self) -> bool {
matches!(
self,
Self::Help | Self::Settings | Self::ServerSelect | Self::ThemeSelect | Self::Trends
)
}
/// Whether this screen animates continuously and needs steady redraws.
#[must_use]
pub fn is_animated(self) -> bool {
matches!(self, Self::Splash | Self::Testing)
}
}
/// Live view of the ping phase.
#[derive(Debug, Clone)]
pub struct PingView {
/// Most recent sample, in milliseconds.
pub last_ms: Option<f64>,
/// Number of samples completed so far.
pub samples_done: u32,
/// Final statistics, once the phase completes.
pub stats: Option<LatencyStats>,
/// Recent samples for the sparkline.
pub history: Sampler,
}
impl PingView {
fn new() -> Self {
Self {
last_ms: None,
samples_done: 0,
stats: None,
history: Sampler::new(HISTORY_CAPACITY),
}
}
fn reset(&mut self) {
self.last_ms = None;
self.samples_done = 0;
self.stats = None;
self.history.clear();
}
}
/// Live view of a download or upload phase.
#[derive(Debug, Clone)]
pub struct TransferView {
/// Bytes transferred so far.
pub bytes: u64,
/// Smoothed instantaneous speed, in bits per second.
pub current_bps: f64,
/// Average speed, in bits per second.
pub average_bps: f64,
/// Peak smoothed speed, in bits per second.
pub peak_bps: f64,
/// Estimated time remaining in this phase.
pub eta: Duration,
/// Phase completion in `0.0..=1.0`.
pub ratio: f64,
/// Recent speed samples for the sparkline.
pub history: Sampler,
/// Final statistics, once the phase completes.
pub stats: Option<TransferStats>,
}
impl TransferView {
fn new() -> Self {
Self {
bytes: 0,
current_bps: 0.0,
average_bps: 0.0,
peak_bps: 0.0,
eta: Duration::ZERO,
ratio: 0.0,
history: Sampler::new(HISTORY_CAPACITY),
stats: None,
}
}
fn reset(&mut self) {
self.bytes = 0;
self.current_bps = 0.0;
self.average_bps = 0.0;
self.peak_bps = 0.0;
self.eta = Duration::ZERO;
self.ratio = 0.0;
self.history.clear();
self.stats = None;
}
}
/// The complete state of the application.
#[derive(Debug)]
pub struct AppState {
/// The screen in the foreground.
pub screen: Screen,
/// Where overlay screens return on [`Screen::is_overlay`] exit.
pub return_to: Screen,
/// The phase currently executing, if a test is running.
pub phase: Option<TestPhase>,
/// Ping phase view.
pub ping: PingView,
/// Download phase view.
pub download: TransferView,
/// Upload phase view.
pub upload: TransferView,
/// The last completed report.
pub report: Option<TestReport>,
/// Discovered servers.
pub servers: Vec<Server>,
/// Index of the active server in [`AppState::servers`].
pub server_index: usize,
/// Highlighted row on the server selection screen.
pub server_cursor: usize,
/// Names of all loaded themes.
pub theme_names: Vec<String>,
/// Index of the active theme.
pub theme_index: usize,
/// Highlighted row on the theme selection screen.
pub theme_cursor: usize,
/// Fatal error message, when on [`Screen::Error`].
pub error: Option<String>,
/// Whether server discovery is still in flight.
pub servers_loading: bool,
/// Whether a test task is currently running.
pub testing: bool,
/// Monotonic UI tick counter, drives spinners.
pub tick: u64,
/// When the current test started.
pub started_at: Option<Instant>,
/// Elapsed time of the current or last test.
pub elapsed: Duration,
/// When the splash screen appeared.
pub splash_since: Instant,
/// The loaded configuration (displayed on the settings screen).
pub settings: Settings,
/// Display name of the active provider.
pub provider_name: &'static str,
/// The client's public IP and ISP, once discovered.
pub client_info: Option<String>,
/// Server query from `--server`; applied when discovery completes.
pub preferred_server: Option<String>,
/// Auto-restart the test this long after completion, when set.
pub repeat_every: Option<Duration>,
/// When the last test completed; drives the repeat countdown.
pub finished_at: Option<Instant>,
/// Past results shown on the trends screen.
pub trends: Vec<crate::app::history::HistoryRecord>,
/// Trends server filter: 0 = all, otherwise index+1 into
/// [`AppState::trend_servers`].
pub trends_filter: usize,
/// Highlighted row on the settings screen.
pub settings_cursor: usize,
/// A transient status message (e.g. clipboard confirmation).
notice: Option<(String, Instant)>,
/// Set when the visible content changed and a redraw is required.
dirty: bool,
}
impl AppState {
/// Creates the initial state on the splash screen.
#[must_use]
pub fn new(settings: Settings, theme_names: Vec<String>, provider_name: &'static str) -> Self {
let theme_index = theme_names
.iter()
.position(|name| name.eq_ignore_ascii_case(&settings.theme))
.unwrap_or(0);
Self {
screen: Screen::Splash,
return_to: Screen::Splash,
phase: None,
ping: PingView::new(),
download: TransferView::new(),
upload: TransferView::new(),
report: None,
servers: Vec::new(),
server_index: 0,
server_cursor: 0,
theme_names,
theme_index,
theme_cursor: theme_index,
error: None,
servers_loading: true,
testing: false,
tick: 0,
started_at: None,
elapsed: Duration::ZERO,
splash_since: Instant::now(),
settings,
provider_name,
client_info: None,
preferred_server: None,
repeat_every: None,
finished_at: None,
trends: Vec::new(),
trends_filter: 0,
settings_cursor: 0,
notice: None,
dirty: true,
}
}
/// Unique server names present in the loaded trends, sorted.
#[must_use]
pub fn trend_servers(&self) -> Vec<String> {
let mut names: Vec<String> = self
.trends
.iter()
.map(|record| record.server.clone())
.collect();
names.sort();
names.dedup();
names
}
/// Cycles the trends server filter left or right.
pub fn cycle_trends_filter(&mut self, delta: i64) {
let options = self.trend_servers().len() + 1;
if options <= 1 {
return;
}
let current = self.trends_filter as i64;
self.trends_filter = (current + delta).rem_euclid(options as i64) as usize;
self.request_redraw();
}
/// The trends records passing the current server filter.
#[must_use]
pub fn filtered_trends(&self) -> Vec<&crate::app::history::HistoryRecord> {
match self.trends_filter.checked_sub(1) {
None => self.trends.iter().collect(),
Some(index) => {
let servers = self.trend_servers();
match servers.get(index) {
Some(name) => self
.trends
.iter()
.filter(|record| &record.server == name)
.collect(),
None => self.trends.iter().collect(),
}
}
}
}
/// Number of editable rows on the settings screen.
pub const SETTINGS_ROWS: usize = 10;
/// Repeat-interval presets cycled on the settings screen (`None`
/// disables auto-repeat).
pub const REPEAT_PRESETS: [Option<u64>; 6] =
[None, Some(30), Some(60), Some(300), Some(900), Some(3600)];
/// The provider kinds selectable on the settings screen. `Custom`
/// only appears when `[[servers]]` entries exist to back it.
#[must_use]
pub fn provider_choices(&self) -> Vec<crate::engine::providers::ProviderKind> {
use crate::engine::providers::ProviderKind;
let mut choices = vec![
ProviderKind::Librespeed,
ProviderKind::Ookla,
ProviderKind::Fast,
];
if !self.settings.servers.is_empty() {
choices.push(ProviderKind::Custom);
}
choices
}
/// Adjusts the setting under the cursor by one step, within the same
/// clamps the config loader enforces. Returns `true` when the
/// provider changed and the engine needs a rebuild.
pub fn adjust_setting(&mut self, delta: i64) -> bool {
let up = delta > 0;
let step_u64 = |value: u64, min: u64, max: u64, step: u64| -> u64 {
if up {
(value + step).min(max)
} else {
value.saturating_sub(step).max(min)
}
};
let mut provider_changed = false;
match self.settings_cursor {
0 => {
// Theme: applies live.
let count = self.theme_names.len().max(1);
let current = self.theme_index as i64;
self.theme_index = (current + delta).rem_euclid(count as i64) as usize;
self.theme_cursor = self.theme_index;
if let Some(name) = self.theme_names.get(self.theme_index) {
self.settings.theme = name.to_lowercase();
}
}
1 => {
// Provider: applies immediately via an engine rebuild.
let choices = self.provider_choices();
let current = choices
.iter()
.position(|kind| *kind == self.settings.provider)
.unwrap_or(0) as i64;
let next = (current + delta).rem_euclid(choices.len() as i64) as usize;
if choices[next] != self.settings.provider {
self.settings.provider = choices[next];
provider_changed = true;
}
}
2 => {
self.settings.refresh_rate =
step_u64(u64::from(self.settings.refresh_rate), 5, 60, 5) as u16;
}
3 => {
let value = (self.settings.animation_speed * 10.0).round() / 10.0;
self.settings.animation_speed = if up {
(value + 0.1).min(5.0)
} else {
(value - 0.1).max(0.1)
};
}
4 => {
self.settings.engine.ping_samples =
step_u64(u64::from(self.settings.engine.ping_samples), 3, 100, 1) as u32;
}
5 => {
self.settings.engine.duration_secs =
step_u64(self.settings.engine.duration_secs, 3, 60, 1);
}
6 => {
self.settings.engine.connections =
step_u64(self.settings.engine.connections as u64, 1, 16, 1) as usize;
}
7 => {
self.settings.engine.timeout_secs =
step_u64(self.settings.engine.timeout_secs, 2, 120, 1);
}
8 => {
self.settings.engine.upload_chunk_kb =
step_u64(self.settings.engine.upload_chunk_kb as u64, 64, 8192, 64) as usize;
}
_ => {
// Auto-repeat interval: applies live.
let presets = Self::REPEAT_PRESETS;
let current_secs = self.repeat_every.map(|d| d.as_secs());
let current = presets
.iter()
.position(|preset| *preset == current_secs)
.unwrap_or(0) as i64;
let next = (current + delta).rem_euclid(presets.len() as i64) as usize;
self.repeat_every = presets[next].map(Duration::from_secs);
self.settings.repeat_interval = presets[next].map(|secs| format!("{secs}s"));
}
}
self.request_redraw();
provider_changed
}
/// Shows a transient status message for a few seconds.
pub fn set_notice(&mut self, message: impl Into<String>) {
self.notice = Some((message.into(), Instant::now()));
self.request_redraw();
}
/// The current notice, if it has not expired yet.
#[must_use]
pub fn notice(&self) -> Option<&str> {
const NOTICE_TTL: Duration = Duration::from_secs(3);
match &self.notice {
Some((message, shown)) if shown.elapsed() < NOTICE_TTL => Some(message),
_ => None,
}
}
/// The currently selected server, if discovery finished.
#[must_use]
pub fn current_server(&self) -> Option<&Server> {
self.servers.get(self.server_index)
}
/// Display name of the selected server.
#[must_use]
pub fn server_name(&self) -> &str {
self.current_server().map_or("—", |server| &server.name)
}
/// Marks the frame as needing a redraw.
pub fn request_redraw(&mut self) {
self.dirty = true;
}
/// Consumes the redraw flag for this frame.
pub fn take_redraw(&mut self) -> bool {
std::mem::take(&mut self.dirty)
}
/// Clears all measurement data ahead of a new test run.
pub fn reset_test(&mut self) {
self.phase = None;
self.ping.reset();
self.download.reset();
self.upload.reset();
self.report = None;
self.error = None;
self.elapsed = Duration::ZERO;
self.started_at = None;
self.request_redraw();
}
/// Advances timers and animations by one tick.
pub fn on_tick(&mut self) {
self.tick = self.tick.wrapping_add(1);
if self.testing {
if let Some(started) = self.started_at {
self.elapsed = started.elapsed();
}
}
if self.screen.is_animated() {
self.request_redraw();
}
// Keep the repeat countdown on the results screen ticking.
if self.repeat_every.is_some() && self.screen == Screen::Results {
self.request_redraw();
}
// Expire transient notices.
if let Some((_, shown)) = &self.notice {
if shown.elapsed() >= Duration::from_secs(3) {
self.notice = None;
self.request_redraw();
}
}
}
/// Time remaining until the next scheduled auto-restart.
#[must_use]
pub fn repeat_remaining(&self) -> Option<Duration> {
let interval = self.repeat_every?;
let finished = self.finished_at?;
Some(interval.saturating_sub(finished.elapsed()))
}
/// Reduces the result of background server discovery.
pub fn apply_servers(&mut self, result: Result<Vec<Server>, String>) {
self.servers_loading = false;
match result {
Ok(servers) => {
self.servers = servers;
// Honor --server by preselecting the first match; fall
// back to the nearest server when nothing matches.
self.server_index = self
.preferred_server
.as_deref()
.and_then(|query| self.servers.iter().position(|s| s.matches(query)))
.unwrap_or(0);
self.server_cursor = self.server_index;
}
Err(message) => {
self.error = Some(message);
self.screen = Screen::Error;
}
}
self.request_redraw();
}
/// Reduces one engine event into the state.
pub fn apply_engine_event(&mut self, event: EngineEvent) {
match event {
EngineEvent::PhaseStarted { phase } => {
self.phase = Some(phase);
if !self.screen.is_overlay() {
self.screen = Screen::Testing;
}
}
EngineEvent::PingSample {
sequence,
latency_ms,
} => {
self.ping.last_ms = Some(latency_ms);
self.ping.samples_done = sequence;
self.ping.history.push(latency_ms);
}
EngineEvent::PingFinished { stats } => {
self.ping.stats = Some(stats);
}
EngineEvent::Progress { progress } => {
let view = match progress.phase {
TestPhase::Download => &mut self.download,
TestPhase::Upload => &mut self.upload,
TestPhase::Ping => return,
};
view.bytes = progress.bytes_transferred;
view.current_bps = progress.current_bps;
view.average_bps = progress.average_bps;
view.peak_bps = progress.peak_bps;
view.eta = progress.eta;
view.ratio = progress.ratio;
view.history.push(progress.current_bps);
}
EngineEvent::TransferFinished { phase, stats } => {
let view = match phase {
TestPhase::Download => &mut self.download,
TestPhase::Upload => &mut self.upload,
TestPhase::Ping => return,
};
view.stats = Some(stats);
view.ratio = 1.0;
view.current_bps = stats.average_bps;
}
EngineEvent::Finished { report } => {
self.report = Some(report);
self.testing = false;
self.phase = None;
self.finished_at = Some(Instant::now());
if !self.screen.is_overlay() {
self.screen = Screen::Results;
}
}
EngineEvent::Failed { message } => {
self.error = Some(message);
self.testing = false;
self.phase = None;
self.screen = Screen::Error;
}
}
self.request_redraw();
}
/// Marks a new test as running.
pub fn begin_test(&mut self) {
self.reset_test();
self.finished_at = None;
self.testing = true;
self.started_at = Some(Instant::now());
self.screen = Screen::Testing;
}
}