tr300-tui 1.4.3

Real-time interactive system diagnostic 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use ratatui::layout::Rect;
use ratatui::style::Style;
use ratatui::text::{Line, Span};
use ratatui::widgets::Paragraph;
use ratatui::Frame;

use crate::app::App;
use crate::collectors::drivers::{DeviceStatus, DriverScanStatus, ServiceInfo};
use crate::types::{DiagnosticMode, HealthStatus};
use crate::ui::common::*;

pub fn render(frame: &mut Frame, app: &App, area: Rect, mode: DiagnosticMode) {
    match mode {
        DiagnosticMode::User => render_user(frame, app, area),
        DiagnosticMode::Technician => render_tech(frame, app, area),
    }
}

fn render_user(frame: &mut Frame, app: &App, area: Rect) {
    let drivers = &app.snapshot.drivers;

    let outer = content_block("Device Health");
    let inner = outer.inner(area);
    frame.render_widget(outer, area);

    let mut lines = vec![Line::from("")];

    // Scan status
    match &drivers.scan_status {
        DriverScanStatus::Scanning => {
            lines.push(Line::from(Span::styled(
                "  \u{27F3} Scanning devices...",
                Style::default().fg(COLOR_ACCENT),
            )));
            lines.push(Line::from(""));
        }
        DriverScanStatus::ScanFailed(ref msg) => {
            lines.push(Line::from(Span::styled(
                format!("  \u{26A0} {}", msg),
                Style::default().fg(COLOR_WARN),
            )));
            lines.push(Line::from(""));
            lines.push(Line::from(Span::styled(
                "  Device information requires administrator privileges or may be temporarily unavailable.",
                Style::default().fg(COLOR_MUTED),
            )));
            lines.push(Line::from(Span::styled(
                "  Try running SD-300 as Administrator.",
                Style::default().fg(COLOR_MUTED),
            )));
            let panel = Paragraph::new(lines);
            frame.render_widget(panel, inner);
            return;
        }
        DriverScanStatus::NotScanned => {
            lines.push(Line::from(Span::styled(
                "  Waiting for scan...",
                Style::default().fg(COLOR_MUTED),
            )));
            lines.push(Line::from(""));
        }
        DriverScanStatus::Success => {}
    }

    // Categories
    render_user_category(&mut lines, "NETWORK", &drivers.network);
    render_user_category(&mut lines, "BLUETOOTH", &drivers.bluetooth);
    render_user_category(&mut lines, "AUDIO", &drivers.audio);
    render_user_category(&mut lines, "KEYBOARD & MOUSE", &drivers.input);
    render_user_category(&mut lines, "DISPLAY", &drivers.display);
    render_user_category(&mut lines, "STORAGE", &drivers.storage);
    render_user_category(&mut lines, "USB", &drivers.usb);

    let panel = Paragraph::new(lines);
    frame.render_widget(panel, inner);
}

fn render_user_category(
    lines: &mut Vec<Line<'_>>,
    title: &str,
    devices: &[crate::collectors::drivers::DeviceInfo],
) {
    if devices.is_empty() {
        return;
    }
    lines.push(section_header(title));
    for dev in devices {
        let status = match &dev.status {
            DeviceStatus::Ok => HealthStatus::Good,
            DeviceStatus::Disabled => HealthStatus::Warning,
            DeviceStatus::Error(_) => HealthStatus::Critical,
            DeviceStatus::NotFound => HealthStatus::Unknown,
            DeviceStatus::Unknown => HealthStatus::Unknown,
        };
        lines.push(status_line(
            &status,
            &dev.name,
            dev.status.user_description(),
        ));
    }
    lines.push(Line::from(""));
}

fn render_tech(frame: &mut Frame, app: &App, area: Rect) {
    let drivers = &app.snapshot.drivers;
    let os_name = &app.snapshot.system.os_name;

    let outer = content_block(&format!(
        "Drivers & Devices \u{2014} {}    (press r to refresh)",
        os_name
    ));
    let inner = outer.inner(area);
    frame.render_widget(outer, area);

    let mut lines = Vec::new();

    // Scan status
    match &drivers.scan_status {
        DriverScanStatus::Scanning => {
            lines.push(Line::from(Span::styled(
                "  \u{27F3} Scanning devices...",
                Style::default().fg(COLOR_ACCENT),
            )));
            lines.push(Line::from(""));
        }
        DriverScanStatus::ScanFailed(msg) => {
            lines.push(Line::from(Span::styled(
                format!("  \u{26A0} {}", msg),
                Style::default().fg(COLOR_WARN),
            )));
            lines.push(Line::from(""));
            lines.push(Line::from(Span::styled(
                "  Device information requires administrator privileges or may be temporarily unavailable.",
                Style::default().fg(COLOR_MUTED),
            )));
            lines.push(Line::from(Span::styled(
                "  Try running SD-300 as Administrator.",
                Style::default().fg(COLOR_MUTED),
            )));
            let panel = Paragraph::new(lines);
            frame.render_widget(panel, inner);
            return;
        }
        DriverScanStatus::NotScanned => {
            lines.push(Line::from(Span::styled(
                "  Scan status: Not scanned yet",
                Style::default().fg(COLOR_MUTED),
            )));
            lines.push(Line::from(""));
        }
        DriverScanStatus::Success => {}
    }

    // Network Adapters table
    render_tech_category(&mut lines, "NETWORK ADAPTERS", &drivers.network, true);

    let net_services: Vec<&ServiceInfo> = drivers
        .services
        .iter()
        .filter(|s| {
            [
                "Dhcp",
                "Dnscache",
                "WlanSvc",
                "NlaSvc",
                "NetworkManager",
                "wpa_supplicant",
            ]
            .contains(&s.name.as_str())
        })
        .collect();
    if !net_services.is_empty() {
        lines.push(render_service_line("Services", &net_services));
    }
    lines.push(Line::from(""));

    // Display
    render_tech_category(&mut lines, "DISPLAY", &drivers.display, true);
    let display_services: Vec<&ServiceInfo> = drivers
        .services
        .iter()
        .filter(|s| ["DisplayEnhancementService"].contains(&s.name.as_str()))
        .collect();
    if !display_services.is_empty() {
        lines.push(render_service_line("Services", &display_services));
    }
    lines.push(Line::from(""));

    // Storage
    render_tech_category(&mut lines, "STORAGE", &drivers.storage, true);
    let storage_services: Vec<&ServiceInfo> = drivers
        .services
        .iter()
        .filter(|s| ["StorSvc", "VSS"].contains(&s.name.as_str()))
        .collect();
    if !storage_services.is_empty() {
        lines.push(render_service_line("Services", &storage_services));
    }
    lines.push(Line::from(""));

    // Bluetooth
    render_tech_category(&mut lines, "BLUETOOTH", &drivers.bluetooth, false);
    let bt_services: Vec<&ServiceInfo> = drivers
        .services
        .iter()
        .filter(|s| {
            ["bthserv", "BthAvctpSvc", "bluetooth", "com.apple.blued"].contains(&s.name.as_str())
        })
        .collect();
    if !bt_services.is_empty() {
        lines.push(render_service_line("Services", &bt_services));
    }
    lines.push(Line::from(""));

    // Audio
    render_tech_category(&mut lines, "AUDIO", &drivers.audio, true);
    let audio_services: Vec<&ServiceInfo> = drivers
        .services
        .iter()
        .filter(|s| {
            [
                "Audiosrv",
                "AudioEndpointBuilder",
                "pipewire",
                "pulseaudio",
                "com.apple.audio.coreaudiod",
            ]
            .contains(&s.name.as_str())
        })
        .collect();
    if !audio_services.is_empty() {
        lines.push(render_service_line("Services", &audio_services));
    }
    lines.push(Line::from(""));

    // Input
    render_tech_category(&mut lines, "INPUT DEVICES", &drivers.input, false);
    let input_services: Vec<&ServiceInfo> = drivers
        .services
        .iter()
        .filter(|s| ["hidserv"].contains(&s.name.as_str()))
        .collect();
    if !input_services.is_empty() {
        lines.push(render_service_line("Services", &input_services));
    }
    lines.push(Line::from(""));

    // USB
    render_tech_category(&mut lines, "USB", &drivers.usb, false);
    let usb_services: Vec<&ServiceInfo> = drivers
        .services
        .iter()
        .filter(|s| ["USBHUB3"].contains(&s.name.as_str()))
        .collect();
    if !usb_services.is_empty() {
        lines.push(render_service_line("Services", &usb_services));
    }
    lines.push(Line::from(""));

    // System
    if !drivers.system.is_empty() {
        render_tech_category(&mut lines, "SYSTEM", &drivers.system, false);
        lines.push(Line::from(""));
    }

    // Other (limit to 20 with "+ N more")
    if !drivers.other.is_empty() {
        lines.push(section_header("OTHER DEVICES"));
        lines.push(Line::from(Span::styled(
            format!("  {:<30} {:<16} {:>8}", "DEVICE", "DRIVER VER", "STATUS"),
            Style::default().fg(COLOR_DIM),
        )));

        let show_count = drivers.other.len().min(20);
        for dev in drivers.other.iter().take(show_count) {
            let status_color = match &dev.status {
                DeviceStatus::Ok => COLOR_GOOD,
                DeviceStatus::Error(_) => COLOR_CRIT,
                _ => COLOR_DIM,
            };
            lines.push(Line::from(vec![
                Span::styled(
                    format!(
                        "  {:<30} {:<16} ",
                        truncate_str(&dev.name, 30),
                        truncate_str(&dev.driver_version, 16)
                    ),
                    Style::default().fg(COLOR_TEXT),
                ),
                Span::styled(
                    format!("{} {}", dev.status.icon(), dev.status),
                    Style::default().fg(status_color),
                ),
            ]));
        }
        if drivers.other.len() > 20 {
            lines.push(Line::from(Span::styled(
                format!("  + {} more devices", drivers.other.len() - 20),
                Style::default().fg(COLOR_DIM),
            )));
        }
        lines.push(Line::from(""));
    }

    // Clamp scroll and add indicator
    let total_lines = lines.len();
    let visible_height = inner.height as usize;
    let max_scroll = total_lines.saturating_sub(visible_height);
    let scroll = app.driver_scroll.min(max_scroll);

    if total_lines > visible_height {
        lines.push(Line::from(""));
        lines.push(Line::from(Span::styled(
            format!(
                "  Scroll: j/k  ({}/{})",
                scroll + visible_height.min(total_lines),
                total_lines
            ),
            Style::default().fg(COLOR_DIM),
        )));
    }

    let panel = Paragraph::new(lines).scroll((scroll as u16, 0));
    frame.render_widget(panel, inner);
}

fn render_tech_category(
    lines: &mut Vec<Line<'_>>,
    title: &str,
    devices: &[crate::collectors::drivers::DeviceInfo],
    show_date: bool,
) {
    if devices.is_empty() {
        return;
    }
    lines.push(section_header(title));
    if show_date {
        lines.push(Line::from(Span::styled(
            format!(
                "  {:<30} {:<16} {:>8} {}",
                "DEVICE", "DRIVER VER", "STATUS", "DATE"
            ),
            Style::default().fg(COLOR_DIM),
        )));
    } else {
        lines.push(Line::from(Span::styled(
            format!("  {:<30} {:<16} {:>8}", "DEVICE", "DRIVER VER", "STATUS"),
            Style::default().fg(COLOR_DIM),
        )));
    }

    for dev in devices {
        let status_color = match &dev.status {
            DeviceStatus::Ok => COLOR_GOOD,
            DeviceStatus::Disabled => COLOR_WARN,
            DeviceStatus::Error(_) => COLOR_CRIT,
            _ => COLOR_DIM,
        };

        if show_date {
            lines.push(Line::from(vec![
                Span::styled(
                    format!(
                        "  {:<30} {:<16} ",
                        truncate_str(&dev.name, 30),
                        truncate_str(&dev.driver_version, 16)
                    ),
                    Style::default().fg(COLOR_TEXT),
                ),
                Span::styled(
                    format!("{} {:<6}", dev.status.icon(), dev.status),
                    Style::default().fg(status_color),
                ),
                Span::styled(
                    format!(" {}", dev.driver_date),
                    Style::default().fg(COLOR_DIM),
                ),
            ]));
        } else {
            lines.push(Line::from(vec![
                Span::styled(
                    format!(
                        "  {:<30} {:<16} ",
                        truncate_str(&dev.name, 30),
                        truncate_str(&dev.driver_version, 16)
                    ),
                    Style::default().fg(COLOR_TEXT),
                ),
                Span::styled(
                    format!("{} {}", dev.status.icon(), dev.status),
                    Style::default().fg(status_color),
                ),
            ]));
        }
    }
}

fn render_service_line<'a>(label: &str, services: &[&ServiceInfo]) -> Line<'a> {
    let mut spans = vec![Span::styled(
        format!("  {}: ", label),
        Style::default().fg(COLOR_DIM),
    )];

    for (i, svc) in services.iter().enumerate() {
        let icon = if svc.is_running {
            "\u{2713}"
        } else {
            "\u{2717}"
        };
        let color = if svc.is_running {
            COLOR_GOOD
        } else {
            COLOR_CRIT
        };
        let name = if svc.display_name.is_empty() {
            &svc.name
        } else {
            &svc.display_name
        };

        spans.push(Span::styled(
            format!("{} {}", name, icon),
            Style::default().fg(color),
        ));

        if i < services.len() - 1 {
            spans.push(Span::styled("  ", Style::default()));
        }
    }

    Line::from(spans)
}