all-smi 0.18.0

Command-line utility for monitoring GPU hardware. It provides a real-time view of GPU utilization, memory usage, temperature, power consumption, and other metrics.
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
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
// Copyright 2025 Lablup Inc. and Jeongkyu Shin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Frame content assembly operating on a `RenderSnapshot`.
//!
//! All methods here are pure functions: they read the snapshot and produce
//! a `String` (or write into a `BufferWriter`) without touching shared state.
//! This means the `AppState` mutex is not held during any of this work.

use std::borrow::Cow;
use std::io::Write;

use chrono::Local;
use crossterm::{
    queue,
    style::{Color, Print},
};

use crate::app_state::AppState;
use crate::cli::ViewArgs;
use crate::device::ProcessInfo;
use crate::ui::buffer::BufferWriter;
use crate::ui::dashboard::{draw_dashboard_items, draw_system_view};
use crate::ui::layout::LayoutCalculator;
use crate::ui::renderer::{
    print_chassis_info, print_cpu_info, print_function_keys, print_gpu_info,
    print_loading_indicator, print_memory_info, print_process_info, print_storage_info,
};
use crate::ui::tabs::draw_tabs;
use crate::ui::text::print_colored_text;
use crate::view::render_snapshot::RenderSnapshot;
use crate::view::view_cache::ViewCache;

/// Stateless frame renderer that operates on a `RenderSnapshot`.
///
/// This struct holds no mutable state of its own. Each method takes an
/// immutable snapshot and returns the assembled frame content as a `String`.
pub struct FrameRenderer;

impl FrameRenderer {
    /// Render help popup content from the snapshot.
    pub fn render_help(snapshot: &RenderSnapshot, args: &ViewArgs, cols: u16, rows: u16) -> String {
        let is_remote = args.hosts.is_some() || args.hostfile.is_some();
        let view_state = snapshot.as_app_state();
        crate::ui::help::generate_help_popup_content(cols, rows, &view_state, is_remote)
    }

    /// Render loading screen content from the snapshot.
    pub fn render_loading(
        snapshot: &RenderSnapshot,
        is_remote: bool,
        cols: u16,
        rows: u16,
    ) -> String {
        let mut buffer = BufferWriter::new();
        let view_state = snapshot.as_app_state();
        print_function_keys(&mut buffer, cols, rows, &view_state, is_remote);
        print_loading_indicator(
            &mut buffer,
            cols,
            rows,
            snapshot.frame_counter,
            &snapshot.startup_status_lines,
        );
        buffer.get_buffer().to_string()
    }

    /// Render main content (the primary monitoring view) from the snapshot.
    ///
    /// When a `ViewCache` is provided, pre-computed sorted/filtered indices
    /// are used instead of re-sorting and re-filtering on every frame.
    /// Render the main TUI view.
    ///
    /// Returns `(content, visible_process_rows)` where `visible_process_rows`
    /// is the actual number of process rows that fit on screen. The caller
    /// should store this value so the event handler can scroll correctly.
    pub fn render_main(
        snapshot: &RenderSnapshot,
        args: &ViewArgs,
        cols: u16,
        rows: u16,
        cache: Option<&ViewCache>,
    ) -> (String, usize) {
        let width = cols as usize;
        let mut buffer = BufferWriter::new();

        // Reconstruct AppState view once for all downstream UI functions that
        // still accept `&AppState`. This single allocation is shared across
        // the header, dashboard, tabs, GPU layout, and function-key bar.
        let view_state = snapshot.as_app_state();

        // Write time/date header to buffer first
        let current_time = Local::now().format("%Y-%m-%d %H:%M:%S").to_string();
        let version = env!("CARGO_PKG_VERSION");
        let header_text = format!("all-smi - {current_time}");
        let version_text = format!("v{version}");

        // Get runtime environment info
        let runtime_shield =
            if let Some((name, color)) = snapshot.runtime_environment.display_info() {
                let shield_content = format!(" {name} ");
                let shield_len = shield_content.len();
                Some((shield_content, color, shield_len))
            } else {
                None
            };

        // Calculate spacing to right-align version, accounting for runtime shield
        let total_width = cols as usize;
        let runtime_shield_len = runtime_shield
            .as_ref()
            .map(|(_, _, len)| len + 1)
            .unwrap_or(0);
        let content_length = header_text.len() + runtime_shield_len + version_text.len();
        let spacing = if total_width > content_length {
            " ".repeat(total_width - content_length)
        } else {
            " ".to_string()
        };

        // Print header with runtime environment shield
        print_colored_text(&mut buffer, &header_text, Color::White, None, None);

        if let Some((shield_content, shield_color, _)) = runtime_shield {
            print_colored_text(&mut buffer, " ", Color::White, None, None);
            print_colored_text(
                &mut buffer,
                &shield_content,
                Color::Black,
                Some(shield_color),
                None,
            );
        }

        print_colored_text(
            &mut buffer,
            &format!("{spacing}{version_text}\r\n"),
            Color::White,
            None,
            None,
        );

        // Write remaining header content to buffer
        print_colored_text(&mut buffer, "Cluster Overview\r\n", Color::Cyan, None, None);
        draw_system_view(&mut buffer, &view_state, cols);

        draw_dashboard_items(&mut buffer, &view_state, cols);
        draw_tabs(&mut buffer, &view_state, cols);

        let is_remote = args.hosts.is_some() || args.hostfile.is_some();

        // Render chassis information (node-level metrics)
        Self::render_chassis_section(&mut buffer, snapshot, width, cache);

        // Render GPU information (reuse the single view_state for layout calculation)
        Self::render_gpu_section(&mut buffer, snapshot, &view_state, args, cols, rows, cache);

        // Render other device information based on mode
        let visible_process_rows = if is_remote {
            Self::render_remote_devices(&mut buffer, snapshot, width, cache);
            0
        } else {
            Self::render_local_devices(&mut buffer, snapshot, cols, rows, cache)
        };

        // Add function keys to main content view
        print_function_keys(&mut buffer, cols, rows, &view_state, is_remote);

        (buffer.get_buffer().to_string(), visible_process_rows)
    }

    fn render_gpu_section(
        buffer: &mut BufferWriter,
        snapshot: &RenderSnapshot,
        view_state: &AppState,
        args: &ViewArgs,
        cols: u16,
        rows: u16,
        cache: Option<&ViewCache>,
    ) {
        // Use cached sorted indices when available, otherwise fall back to
        // the previous per-frame filter + sort path.
        let cached_indices;
        let fallback_indices;
        let display_indices: &[usize] = if let Some(indices) = cache.and_then(|c| c.gpu_indices()) {
            cached_indices = indices;
            cached_indices
        } else {
            // Fallback: filter + sort inline (only reached when cache is None).
            // Use .get() to guard against out-of-bounds current_tab.
            let mut indices: Vec<usize> =
                if let Some(tab_name) = snapshot.tabs.get(snapshot.current_tab) {
                    if tab_name == "All" {
                        (0..snapshot.gpu_info.len()).collect()
                    } else {
                        snapshot
                            .gpu_info
                            .iter()
                            .enumerate()
                            .filter(|(_, info)| info.host_id == *tab_name)
                            .map(|(i, _)| i)
                            .collect()
                    }
                } else {
                    // Out-of-bounds tab index: show all (defensive)
                    (0..snapshot.gpu_info.len()).collect()
                };
            indices.sort_by(|&a, &b| {
                snapshot
                    .sort_criteria
                    .sort_gpus(&snapshot.gpu_info[a], &snapshot.gpu_info[b])
            });
            fallback_indices = indices;
            &fallback_indices
        };

        // Calculate content area and GPU display parameters using the shared
        // view_state from render_main, avoiding a second as_app_state() call.
        let content_area = LayoutCalculator::calculate_content_area(view_state, cols, rows);
        let gpu_display_params =
            LayoutCalculator::calculate_gpu_display_params(view_state, args, &content_area);
        let max_gpu_items = gpu_display_params.max_items;

        // Display GPUs with scrolling
        let start_gpu_index = snapshot.gpu_scroll_offset;
        let end_gpu_index = (start_gpu_index + max_gpu_items).min(display_indices.len());

        for (i, &gpu_idx) in display_indices
            .iter()
            .enumerate()
            .skip(start_gpu_index)
            .take(end_gpu_index.saturating_sub(start_gpu_index))
        {
            let gpu_info = &snapshot.gpu_info[gpu_idx];
            let device_name_scroll_offset = snapshot
                .device_name_scroll_offsets
                .get(&gpu_info.uuid)
                .copied()
                .unwrap_or(0);
            let hostname_scroll_offset = snapshot
                .host_id_scroll_offsets
                .get(&gpu_info.host_id)
                .copied()
                .unwrap_or(0);

            print_gpu_info(
                buffer,
                i,
                gpu_info,
                cols as usize,
                device_name_scroll_offset,
                hostname_scroll_offset,
            );
        }
    }

    fn render_chassis_section(
        buffer: &mut BufferWriter,
        snapshot: &RenderSnapshot,
        width: usize,
        cache: Option<&ViewCache>,
    ) {
        if snapshot.chassis_info.is_empty() {
            return;
        }

        // Use cached chassis indices when available
        if let Some(hd) = cache.and_then(|c| c.host_device_indices()) {
            if hd.chassis_indices.is_empty() {
                return;
            }
            for (i, &idx) in hd.chassis_indices.iter().enumerate() {
                let chassis = &snapshot.chassis_info[idx];
                let hostname_scroll_offset = snapshot
                    .host_id_scroll_offsets
                    .get(&chassis.host_id)
                    .copied()
                    .unwrap_or(0);
                print_chassis_info(buffer, i, chassis, width, hostname_scroll_offset);
            }
            return;
        }

        // Fallback: filter inline (only reached when cache is None)
        let chassis_to_display: Vec<_> = if snapshot.is_local_mode {
            snapshot.chassis_info.iter().collect()
        } else if snapshot.current_tab == 0 {
            return;
        } else if snapshot.current_tab < snapshot.tabs.len() {
            let current_host = &snapshot.tabs[snapshot.current_tab];
            snapshot
                .chassis_info
                .iter()
                .filter(|c| c.host_id == *current_host || c.hostname == *current_host)
                .collect()
        } else {
            snapshot.chassis_info.iter().collect()
        };

        for (i, chassis) in chassis_to_display.iter().enumerate() {
            let hostname_scroll_offset = snapshot
                .host_id_scroll_offsets
                .get(&chassis.host_id)
                .copied()
                .unwrap_or(0);
            print_chassis_info(buffer, i, chassis, width, hostname_scroll_offset);
        }
    }

    fn render_remote_devices(
        buffer: &mut BufferWriter,
        snapshot: &RenderSnapshot,
        width: usize,
        cache: Option<&ViewCache>,
    ) {
        if snapshot.current_tab == 0 || snapshot.current_tab >= snapshot.tabs.len() {
            return;
        }

        let current_hostname = &snapshot.tabs[snapshot.current_tab];

        // Check connection status for the current node
        let is_connected = if let Some(host_id) = snapshot.hostname_to_host_id.get(current_hostname)
        {
            snapshot
                .connection_status
                .get(host_id)
                .map(|status| status.is_connected)
                .unwrap_or(false)
        } else {
            snapshot
                .connection_status
                .get(current_hostname)
                .map(|status| status.is_connected)
                .unwrap_or(true)
        };

        if !is_connected {
            Self::render_disconnection_notification(buffer, current_hostname, width);
            return;
        }

        // Resolve host-device indices: use cache when available, otherwise
        // build a temporary index list from an inline filter.
        let fallback_cpu;
        let fallback_mem;
        let fallback_stor;
        let (cpu_idx, mem_idx, stor_idx) = if let Some(hd) =
            cache.and_then(|c| c.host_device_indices())
        {
            (
                hd.cpu_indices.as_slice(),
                hd.memory_indices.as_slice(),
                hd.storage_indices.as_slice(),
            )
        } else {
            fallback_cpu =
                Self::filter_indices(&snapshot.cpu_info, |c| c.host_id == *current_hostname);
            fallback_mem =
                Self::filter_indices(&snapshot.memory_info, |m| m.host_id == *current_hostname);
            fallback_stor =
                Self::filter_indices(&snapshot.storage_info, |s| s.host_id == *current_hostname);
            (
                fallback_cpu.as_slice(),
                fallback_mem.as_slice(),
                fallback_stor.as_slice(),
            )
        };

        // CPU
        for (i, &idx) in cpu_idx.iter().enumerate() {
            let cpu_info = &snapshot.cpu_info[idx];
            let cpu_name_scroll_offset = snapshot
                .cpu_name_scroll_offsets
                .get(&format!("{}-{}", cpu_info.hostname, cpu_info.cpu_model))
                .copied()
                .unwrap_or(0);
            let hostname_scroll_offset = snapshot
                .host_id_scroll_offsets
                .get(&cpu_info.host_id)
                .copied()
                .unwrap_or(0);
            print_cpu_info(
                buffer,
                i,
                cpu_info,
                width,
                snapshot.show_per_core_cpu,
                cpu_name_scroll_offset,
                hostname_scroll_offset,
            );
        }

        // Memory
        for (i, &idx) in mem_idx.iter().enumerate() {
            let memory_info = &snapshot.memory_info[idx];
            let hostname_scroll_offset = snapshot
                .host_id_scroll_offsets
                .get(&memory_info.host_id)
                .copied()
                .unwrap_or(0);
            print_memory_info(buffer, i, memory_info, width, hostname_scroll_offset);
        }

        // Storage with scroll offset
        for (i, &idx) in stor_idx
            .iter()
            .skip(snapshot.storage_scroll_offset)
            .take(10)
            .enumerate()
        {
            let storage_info = &snapshot.storage_info[idx];
            let hostname_scroll_offset = snapshot
                .host_id_scroll_offsets
                .get(&storage_info.host_id)
                .copied()
                .unwrap_or(0);
            print_storage_info(buffer, i, storage_info, width, hostname_scroll_offset);
        }
    }

    /// Collect indices of elements matching a predicate.
    fn filter_indices<T>(items: &[T], predicate: impl Fn(&T) -> bool) -> Vec<usize> {
        items
            .iter()
            .enumerate()
            .filter(|(_, item)| predicate(item))
            .map(|(i, _)| i)
            .collect()
    }

    fn render_disconnection_notification(buffer: &mut BufferWriter, hostname: &str, width: usize) {
        writeln!(buffer).unwrap();
        writeln!(buffer).unwrap();

        let box_width = width.saturating_sub(4).min(60);
        // Ensure minimum box width for the border characters
        if box_width < 6 {
            return;
        }
        let margin = width.saturating_sub(box_width) / 2;
        let margin_str = " ".repeat(margin);

        // Top border
        write!(buffer, "{margin_str}").unwrap();
        print_colored_text(buffer, "\u{250c}", Color::Red, None, None);
        print_colored_text(
            buffer,
            &"\u{2500}".repeat(box_width.saturating_sub(2)),
            Color::Red,
            None,
            None,
        );
        print_colored_text(buffer, "\u{2510}", Color::Red, None, None);
        writeln!(buffer).unwrap();

        // Content rows: title, blank, hostname, status, blank
        let rows: &[(&str, Color)] = &[
            ("CONNECTION LOST", Color::Red),
            ("", Color::White),
            (&format!("Node: {hostname}"), Color::Yellow),
            ("Unable to retrieve node information", Color::DarkGrey),
            ("", Color::White),
        ];
        // Inner width available for text content (between "| " and " |")
        let inner_width = box_width.saturating_sub(4);
        for (text, color) in rows {
            write!(buffer, "{margin_str}").unwrap();
            if text.is_empty() {
                // Empty row
                print_colored_text(buffer, "\u{2502}", Color::Red, None, None);
                print_colored_text(
                    buffer,
                    &" ".repeat(box_width.saturating_sub(2)),
                    Color::White,
                    None,
                    None,
                );
                print_colored_text(buffer, "\u{2502}", Color::Red, None, None);
            } else {
                // Truncate text if it exceeds available inner width
                let display_text: Cow<'_, str> = if text.len() > inner_width {
                    Cow::Owned(text.chars().take(inner_width).collect())
                } else {
                    Cow::Borrowed(text)
                };
                let pad_left = inner_width.saturating_sub(display_text.len()) / 2;
                let pad_right = inner_width.saturating_sub(pad_left + display_text.len());
                print_colored_text(buffer, "\u{2502} ", Color::Red, None, None);
                print_colored_text(buffer, &" ".repeat(pad_left), Color::White, None, None);
                print_colored_text(buffer, &display_text, *color, None, None);
                print_colored_text(buffer, &" ".repeat(pad_right), Color::White, None, None);
                print_colored_text(buffer, " \u{2502}", Color::Red, None, None);
            }
            writeln!(buffer).unwrap();
        }

        // Bottom border
        write!(buffer, "{margin_str}").unwrap();
        print_colored_text(buffer, "\u{2514}", Color::Red, None, None);
        print_colored_text(
            buffer,
            &"\u{2500}".repeat(box_width.saturating_sub(2)),
            Color::Red,
            None,
            None,
        );
        print_colored_text(buffer, "\u{2518}", Color::Red, None, None);
        writeln!(buffer).unwrap();
    }

    /// Returns the number of visible process rows for event handler scroll calculation.
    fn render_local_devices(
        buffer: &mut BufferWriter,
        snapshot: &RenderSnapshot,
        cols: u16,
        rows: u16,
        cache: Option<&ViewCache>,
    ) -> usize {
        let width = cols as usize;

        // CPU information for local mode
        for (i, cpu_info) in snapshot.cpu_info.iter().enumerate() {
            let cpu_name_scroll_offset = snapshot
                .cpu_name_scroll_offsets
                .get(&format!("{}-{}", cpu_info.hostname, cpu_info.cpu_model))
                .copied()
                .unwrap_or(0);
            let hostname_scroll_offset = snapshot
                .host_id_scroll_offsets
                .get(&cpu_info.host_id)
                .copied()
                .unwrap_or(0);
            print_cpu_info(
                buffer,
                i,
                cpu_info,
                width,
                snapshot.show_per_core_cpu,
                cpu_name_scroll_offset,
                hostname_scroll_offset,
            );
        }

        // Memory information for local mode
        for (i, memory_info) in snapshot.memory_info.iter().enumerate() {
            let hostname_scroll_offset = snapshot
                .host_id_scroll_offsets
                .get(&memory_info.host_id)
                .copied()
                .unwrap_or(0);
            print_memory_info(buffer, i, memory_info, width, hostname_scroll_offset);
        }

        // Storage information for local mode
        for (i, storage_info) in snapshot.storage_info.iter().enumerate() {
            let hostname_scroll_offset = snapshot
                .host_id_scroll_offsets
                .get(&storage_info.host_id)
                .copied()
                .unwrap_or(0);
            print_storage_info(buffer, i, storage_info, width, hostname_scroll_offset);
        }

        // Process information for local mode (if available)
        if !snapshot.process_info.is_empty() {
            let lines_used = buffer.line_count();

            // Add a blank line before process list
            queue!(buffer, Print("\r\n")).unwrap();

            // Reserve 1 line for function keys at the bottom
            let function_key_rows = 1;

            let available_rows = rows.saturating_sub(lines_used as u16 + 1 + function_key_rows);

            // Calculate actual visible process rows (must match process_renderer logic)
            // RESERVED_HEADER_ROWS = 4 ("Processes:" title, column header, separator, blank)
            // footer_rows = 2 ("Showing..." + "Active..." stats)
            let visible = (available_rows as usize).saturating_sub(4 + 2);

            // Get current user for process coloring
            let current_user = whoami::username().unwrap_or_default();

            // Use cached GPU-filtered process list when available, avoiding
            // a per-frame clone of the entire process vector.
            let processes_to_display: Cow<'_, [ProcessInfo]> =
                if let Some(pl) = cache.and_then(|c| c.process_display_list()) {
                    match &pl.filtered {
                        Some(filtered) => Cow::Borrowed(filtered.as_slice()),
                        None => Cow::Borrowed(&snapshot.process_info),
                    }
                } else if snapshot.gpu_filter_enabled {
                    // Fallback: filter inline (only when cache is None)
                    Cow::Owned(
                        snapshot
                            .process_info
                            .iter()
                            .filter(|p| p.used_memory > 0)
                            .cloned()
                            .collect(),
                    )
                } else {
                    Cow::Borrowed(&snapshot.process_info)
                };

            print_process_info(
                buffer,
                &processes_to_display,
                snapshot.selected_process_index,
                snapshot.start_index,
                available_rows,
                cols,
                snapshot.process_horizontal_scroll_offset,
                &current_user,
                &snapshot.sort_criteria,
                &snapshot.sort_direction,
            );

            return visible;
        }
        0
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::app_state::AppState;
    use crate::view::render_snapshot::RenderSnapshot;

    fn make_local_args() -> ViewArgs {
        ViewArgs {
            hosts: None,
            hostfile: None,
            interval: None,
        }
    }

    fn make_snapshot() -> RenderSnapshot {
        let state = AppState::new();
        RenderSnapshot::capture(&state)
    }

    // -----------------------------------------------------------------------
    // FrameRenderer: construction
    // -----------------------------------------------------------------------

    #[test]
    fn test_frame_renderer_is_zero_sized() {
        // FrameRenderer is a unit struct; assert it holds no state.
        assert_eq!(std::mem::size_of::<FrameRenderer>(), 0);
    }

    // -----------------------------------------------------------------------
    // render_loading: smoke tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_render_loading_does_not_panic() {
        let snapshot = make_snapshot();
        let output = FrameRenderer::render_loading(&snapshot, false, 80, 24);
        // Loading screen must produce some output even when state is empty.
        assert!(!output.is_empty());
    }

    #[test]
    fn test_render_loading_remote_does_not_panic() {
        let snapshot = make_snapshot();
        let output = FrameRenderer::render_loading(&snapshot, true, 80, 24);
        assert!(!output.is_empty());
    }

    #[test]
    fn test_render_loading_with_startup_status_lines() {
        let mut state = AppState::new();
        state
            .startup_status_lines
            .push("Connecting to GPUs...".to_string());
        let snapshot = RenderSnapshot::capture(&state);
        // Should not panic and produce output with the status line.
        let output = FrameRenderer::render_loading(&snapshot, false, 80, 24);
        assert!(!output.is_empty());
    }

    // -----------------------------------------------------------------------
    // render_main: smoke tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_render_main_does_not_panic_empty_state() {
        let snapshot = make_snapshot();
        let args = make_local_args();
        let (output, _) = FrameRenderer::render_main(&snapshot, &args, 80, 24, None);
        // Header must be present.
        assert!(output.contains("all-smi"));
    }

    #[test]
    fn test_render_main_contains_header_timestamp() {
        let snapshot = make_snapshot();
        let args = make_local_args();
        let (output, _) = FrameRenderer::render_main(&snapshot, &args, 120, 40, None);
        // The header includes the current year which is deterministic for the test run.
        assert!(output.contains("all-smi - 20"));
    }

    #[test]
    fn test_render_main_contains_version() {
        let snapshot = make_snapshot();
        let args = make_local_args();
        let (output, _) = FrameRenderer::render_main(&snapshot, &args, 80, 24, None);
        let version = env!("CARGO_PKG_VERSION");
        assert!(output.contains(version));
    }

    // -----------------------------------------------------------------------
    // render_disconnection_notification: box geometry
    // -----------------------------------------------------------------------

    #[test]
    fn test_disconnection_notification_width_too_narrow_produces_no_box() {
        // width=9 → box_width = min(9-4, 60) = 5 which is < 6, so nothing is rendered
        // (only the two leading blank lines appear).
        let mut buffer = BufferWriter::new();
        FrameRenderer::render_disconnection_notification(&mut buffer, "node1", 9);
        let output = buffer.get_buffer().to_string();
        // The box should NOT be rendered; the output must not contain the box corner.
        assert!(!output.contains('\u{250c}'));
    }

    #[test]
    fn test_disconnection_notification_normal_width_contains_hostname() {
        let mut buffer = BufferWriter::new();
        FrameRenderer::render_disconnection_notification(&mut buffer, "my-node", 80);
        let output = buffer.get_buffer().to_string();
        assert!(output.contains("my-node"));
        assert!(output.contains("CONNECTION LOST"));
    }

    #[test]
    fn test_disconnection_notification_box_max_width_capped_at_60() {
        // With a very wide terminal (200 cols) the box should be capped at 60 chars.
        let mut buffer = BufferWriter::new();
        FrameRenderer::render_disconnection_notification(&mut buffer, "node1", 200);
        let output = buffer.get_buffer().to_string();
        // The box top border is: "─" repeated (box_width-2) times, capped at 58 for width=200.
        // Count the number of consecutive box-drawing horizontal lines.
        let horizontal_line_count = output.matches('\u{2500}').count();
        // max box_width = 60, so max horizontal lines per border = 58
        // Two borders (top + bottom) → at most 116.
        assert!(horizontal_line_count <= 116);
        // But there must be at least some lines (it renders).
        assert!(horizontal_line_count > 0);
    }

    #[test]
    fn test_disconnection_notification_long_hostname_is_truncated() {
        // A hostname that exceeds inner_width (box_width - 4) should be truncated.
        let long_hostname = "a".repeat(200);
        let mut buffer = BufferWriter::new();
        FrameRenderer::render_disconnection_notification(&mut buffer, &long_hostname, 80);
        let output = buffer.get_buffer().to_string();
        // "Node: " prefix plus some of the hostname must appear, but not all 200 'a's.
        assert!(output.contains("Node: "));
        assert!(!output.contains(&long_hostname));
    }

    // -----------------------------------------------------------------------
    // render_help: smoke test
    // -----------------------------------------------------------------------

    #[test]
    fn test_render_help_does_not_panic() {
        let snapshot = make_snapshot();
        let args = make_local_args();
        let output = FrameRenderer::render_help(&snapshot, &args, 80, 24);
        // Help popup must produce output.
        assert!(!output.is_empty());
    }
}