xfr 0.7.1

Modern network bandwidth testing with TUI
Documentation
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
//! Settings modal for TUI

use std::time::Duration;

use crate::protocol::{Direction, Protocol};

use super::theme::Theme;

/// Settings category tabs
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SettingsCategory {
    #[default]
    Display,
    Test,
}

impl SettingsCategory {
    pub fn next(&self) -> Self {
        match self {
            Self::Display => Self::Test,
            Self::Test => Self::Display,
        }
    }
}

/// Timestamp format for display
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TimestampFormat {
    #[default]
    Relative,
    Iso8601,
    Unix,
}

impl TimestampFormat {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Relative => "relative",
            Self::Iso8601 => "iso8601",
            Self::Unix => "unix",
        }
    }

    pub fn next(&self) -> Self {
        match self {
            Self::Relative => Self::Iso8601,
            Self::Iso8601 => Self::Unix,
            Self::Unix => Self::Relative,
        }
    }

    pub fn prev(&self) -> Self {
        match self {
            Self::Relative => Self::Unix,
            Self::Iso8601 => Self::Relative,
            Self::Unix => Self::Iso8601,
        }
    }
}

/// Display units preference
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Units {
    #[default]
    Auto,
    Bits,
    Bytes,
}

impl Units {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Auto => "auto",
            Self::Bits => "bits",
            Self::Bytes => "bytes",
        }
    }

    pub fn next(&self) -> Self {
        match self {
            Self::Auto => Self::Bits,
            Self::Bits => Self::Bytes,
            Self::Bytes => Self::Auto,
        }
    }

    pub fn prev(&self) -> Self {
        match self {
            Self::Auto => Self::Bytes,
            Self::Bits => Self::Auto,
            Self::Bytes => Self::Bits,
        }
    }
}

/// State for the settings modal
#[derive(Debug, Clone)]
pub struct SettingsState {
    /// Whether the modal is visible
    pub visible: bool,
    /// Current category tab
    pub category: SettingsCategory,
    /// Selected item index within category
    pub selected_index: usize,
    /// Whether a button is focused (vs a setting)
    pub button_focused: bool,
    /// Which button is selected (0 = Apply & Restart, 1 = Close)
    pub button_index: usize,

    // Display settings (live-editable, auto-persist)
    pub theme_index: usize,
    pub timestamp_format: TimestampFormat,
    pub units: Units,

    // Test settings (require restart)
    pub streams: u8,
    pub protocol: Protocol,
    pub duration_secs: u64,
    pub direction: Direction,

    // Original values to detect changes
    original_streams: u8,
    original_protocol: Protocol,
    original_duration_secs: u64,
    original_direction: Direction,
}

impl Default for SettingsState {
    fn default() -> Self {
        Self {
            visible: false,
            category: SettingsCategory::Display,
            selected_index: 0,
            button_focused: false,
            button_index: 0,

            theme_index: 0,
            timestamp_format: TimestampFormat::Relative,
            units: Units::Auto,

            streams: 1,
            protocol: Protocol::Tcp,
            duration_secs: 10,
            direction: Direction::Upload,

            original_streams: 1,
            original_protocol: Protocol::Tcp,
            original_duration_secs: 10,
            original_direction: Direction::Upload,
        }
    }
}

impl SettingsState {
    /// Create settings state from current app state
    pub fn new(
        theme_index: usize,
        streams: u8,
        protocol: Protocol,
        duration: Duration,
        direction: Direction,
    ) -> Self {
        let duration_secs = duration.as_secs();
        Self {
            visible: false,
            category: SettingsCategory::Display,
            selected_index: 0,
            button_focused: false,
            button_index: 0,

            theme_index,
            timestamp_format: TimestampFormat::Relative,
            units: Units::Auto,

            streams,
            protocol,
            duration_secs,
            direction,

            original_streams: streams,
            original_protocol: protocol,
            original_duration_secs: duration_secs,
            original_direction: direction,
        }
    }

    /// Toggle visibility
    pub fn toggle(&mut self) {
        self.visible = !self.visible;
        if self.visible {
            // Reset to first item when opening
            self.selected_index = 0;
            self.button_focused = false;
        }
    }

    /// Close the modal
    pub fn close(&mut self) {
        self.visible = false;
    }

    /// Check if test parameters have changed
    pub fn test_params_dirty(&self) -> bool {
        self.streams != self.original_streams
            || self.protocol != self.original_protocol
            || self.duration_secs != self.original_duration_secs
            || self.direction != self.original_direction
    }

    /// Number of items in current category
    fn items_in_category(&self) -> usize {
        match self.category {
            SettingsCategory::Display => 3, // Theme, Timestamp, Units
            SettingsCategory::Test => 4,    // Streams, Protocol, Duration, Direction
        }
    }

    /// Move selection up (or to buttons if at top)
    pub fn move_up(&mut self) {
        if self.button_focused {
            // Move from buttons to last item
            self.button_focused = false;
            self.selected_index = self.items_in_category().saturating_sub(1);
        } else if self.selected_index > 0 {
            self.selected_index -= 1;
        }
    }

    /// Move selection down (or to buttons if at bottom)
    pub fn move_down(&mut self) {
        if self.button_focused {
            // Stay on buttons
            return;
        }

        if self.selected_index < self.items_in_category().saturating_sub(1) {
            self.selected_index += 1;
        } else {
            // Move to buttons
            self.button_focused = true;
            // Select first visible button (Apply if dirty, else Close)
            self.button_index = if self.test_params_dirty() { 0 } else { 1 };
        }
    }

    /// Switch category tab
    pub fn switch_tab(&mut self) {
        self.category = self.category.next();
        self.selected_index = 0;
        self.button_focused = false;
    }

    /// Cycle selected value to next
    pub fn value_next(&mut self) {
        if self.button_focused {
            // Only cycle if both buttons are visible
            if self.test_params_dirty() {
                self.button_index = (self.button_index + 1) % 2;
            }
            return;
        }

        match self.category {
            SettingsCategory::Display => match self.selected_index {
                0 => {
                    // Theme
                    let theme_list = Theme::list();
                    self.theme_index = (self.theme_index + 1) % theme_list.len();
                }
                1 => self.timestamp_format = self.timestamp_format.next(),
                2 => self.units = self.units.next(),
                _ => {}
            },
            SettingsCategory::Test => match self.selected_index {
                0 => self.streams = self.streams.saturating_add(1).min(128),
                1 => self.protocol = self.protocol.next(),
                2 => self.duration_secs = self.duration_secs.saturating_add(5).min(3600),
                3 => self.direction = self.direction.next(),
                _ => {}
            },
        }
    }

    /// Cycle selected value to previous
    pub fn value_prev(&mut self) {
        if self.button_focused {
            // Only cycle if both buttons are visible
            if self.test_params_dirty() {
                self.button_index = if self.button_index == 0 { 1 } else { 0 };
            }
            return;
        }

        match self.category {
            SettingsCategory::Display => match self.selected_index {
                0 => {
                    // Theme
                    let theme_list = Theme::list();
                    self.theme_index = if self.theme_index == 0 {
                        theme_list.len() - 1
                    } else {
                        self.theme_index - 1
                    };
                }
                1 => self.timestamp_format = self.timestamp_format.prev(),
                2 => self.units = self.units.prev(),
                _ => {}
            },
            SettingsCategory::Test => match self.selected_index {
                0 => self.streams = self.streams.saturating_sub(1).max(1),
                1 => self.protocol = self.protocol.prev(),
                2 => self.duration_secs = self.duration_secs.saturating_sub(5).max(1),
                3 => self.direction = self.direction.prev(),
                _ => {}
            },
        }
    }

    /// Get current theme name
    pub fn theme_name(&self) -> &str {
        let list = Theme::list();
        list.get(self.theme_index).copied().unwrap_or("default")
    }

    /// Handle Enter key - returns true if should restart test
    pub fn on_enter(&mut self) -> SettingsAction {
        if !self.button_focused {
            // Move to buttons when pressing enter on a setting
            self.button_focused = true;
            // Select first visible button (Apply if dirty, else Close)
            self.button_index = if self.test_params_dirty() { 0 } else { 1 };
            return SettingsAction::None;
        }

        match self.button_index {
            0 => {
                // Apply & Restart
                if self.test_params_dirty() {
                    self.close();
                    SettingsAction::Restart
                } else {
                    self.close();
                    SettingsAction::Close
                }
            }
            1 => {
                // Close
                self.close();
                SettingsAction::Close
            }
            _ => SettingsAction::None,
        }
    }
}

/// Action to take after settings interaction
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SettingsAction {
    None,
    Close,
    Restart,
}

/// Result of TUI loop - either exit or restart with new params
#[derive(Debug, Clone)]
pub enum TuiLoopResult {
    Exit {
        result: Option<crate::protocol::TestResult>,
        prefs: crate::prefs::Prefs,
        print_json: bool,
    },
    Restart {
        streams: u8,
        protocol: Protocol,
        duration: Duration,
        direction: Direction,
        prefs: crate::prefs::Prefs,
    },
}

// Add helper methods to Protocol and Direction for cycling
impl Protocol {
    fn next(&self) -> Self {
        match self {
            Self::Tcp => Self::Udp,
            Self::Udp => Self::Quic,
            Self::Quic => Self::Tcp,
        }
    }

    fn prev(&self) -> Self {
        match self {
            Self::Tcp => Self::Quic,
            Self::Udp => Self::Tcp,
            Self::Quic => Self::Udp,
        }
    }
}

impl Direction {
    fn next(&self) -> Self {
        match self {
            Self::Upload => Self::Download,
            Self::Download => Self::Bidir,
            Self::Bidir => Self::Upload,
        }
    }

    fn prev(&self) -> Self {
        match self {
            Self::Upload => Self::Bidir,
            Self::Download => Self::Upload,
            Self::Bidir => Self::Download,
        }
    }
}