socktop 1.60.1

Remote system monitor over WebSocket, TUI like top
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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
//! Top processes table with per-cell coloring, zebra striping, sorting, and a scrollbar.

use crossterm::event::{MouseButton, MouseEvent, MouseEventKind};
use ratatui::style::Modifier;
use ratatui::{
    layout::{Constraint, Direction, Layout, Rect},
    style::{Color, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Paragraph, Scrollbar, ScrollbarOrientation, ScrollbarState, Table},
};
use std::cmp::Ordering;

use crate::types::Metrics;
use crate::ui::cpu::{per_core_clamp, per_core_handle_scrollbar_mouse};
use crate::ui::fit;
use crate::ui::theme::{
    PROCESS_SELECTION_BG, PROCESS_SELECTION_FG, PROCESS_TOOLTIP_BG, PROCESS_TOOLTIP_FG, SB_ARROW,
    SB_THUMB, SB_TRACK,
};

/// Simple fuzzy matching: returns true if all characters in needle appear in
/// haystack in order, ASCII-case-insensitive. Lowercase normalization is done
/// on the fly so we don't allocate two `String`s per haystack like the old
/// version did (this runs once per process per frame).
fn fuzzy_match(haystack: &str, needle: &str) -> bool {
    if needle.is_empty() {
        return true;
    }
    let mut haystack_chars = haystack.chars().map(|c| c.to_ascii_lowercase());
    for needle_char in needle.chars().map(|c| c.to_ascii_lowercase()) {
        if !haystack_chars.any(|c| c == needle_char) {
            return false;
        }
    }
    true
}

/// Fill `out` with filtered + sorted process indices. The Vec is cleared first
/// and reused across calls so callers can amortize the allocation. This is
/// the underlying helper for the App-side cached slice.
pub fn fill_filtered_sorted_indices(
    metrics: &Metrics,
    search_query: &str,
    sort_by: ProcSortBy,
    out: &mut Vec<usize>,
) {
    out.clear();
    out.reserve(metrics.top_processes.len());
    if search_query.is_empty() {
        out.extend(0..metrics.top_processes.len());
    } else {
        out.extend(
            (0..metrics.top_processes.len())
                .filter(|&i| fuzzy_match(&metrics.top_processes[i].name, search_query)),
        );
    }
    match sort_by {
        ProcSortBy::CpuDesc => out.sort_by(|&a, &b| {
            let aa = metrics.top_processes[a].cpu_usage;
            let bb = metrics.top_processes[b].cpu_usage;
            bb.partial_cmp(&aa).unwrap_or(Ordering::Equal)
        }),
        ProcSortBy::MemDesc => out.sort_by(|&a, &b| {
            let aa = metrics.top_processes[a].mem_bytes;
            let bb = metrics.top_processes[b].mem_bytes;
            bb.cmp(&aa)
        }),
    }
}

/// Parameters for drawing the top processes table
pub struct ProcessDisplayParams<'a> {
    pub metrics: Option<&'a Metrics>,
    pub scroll_offset: usize,
    pub sort_by: ProcSortBy,
    pub selected_process_pid: Option<u32>,
    pub selected_process_index: Option<usize>,
    pub search_query: &'a str,
    pub search_active: bool,
    /// Precomputed filtered + sorted indices into `metrics.top_processes`.
    /// Maintained on the App side so the draw path never recomputes the list.
    pub filtered_indices: &'a [usize],
    /// Pre-formatted strings for each row of `metrics.top_processes`.
    /// Indexed the same as `metrics.top_processes`. Empty when no procs poll
    /// has run yet (the draw path falls back to fast inline formatting).
    pub cached_rows: &'a [CachedRow],
    /// Peak cpu_usage from the most recent cache build; used to bold the
    /// busiest process. -1.0 if no cache.
    pub peak_cpu: f32,
    /// Agent is on this machine, so the `t` kill hint applies. Without it the
    /// hint would advertise a key that deliberately does nothing.
    pub is_local: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ProcSortBy {
    #[default]
    CpuDesc,
    MemDesc,
}

/// Pre-formatted strings for one row of the process table. Built once per
/// `Processes` poll (cadence ~2s) and reused by every draw frame in between
/// so the diff renderer can suppress repaints when nothing changed.
#[derive(Debug, Clone)]
pub struct CachedRow {
    pub pid_str: String,
    pub cpu_str: String,
    pub mem_str: String,
    pub mem_pct_str: String,
    pub mem_pct: f64,
    pub cpu_val: f32,
}

/// Build a fresh row cache parallel to `metrics.top_processes`. Reuses `out`'s
/// allocation when possible. Also returns the peak cpu_usage observed, which
/// the draw path uses to bold the busiest process.
pub fn rebuild_row_cache(metrics: &Metrics, out: &mut Vec<CachedRow>) -> f32 {
    out.clear();
    out.reserve(metrics.top_processes.len());
    let total = metrics.mem_total.max(1);
    let mut peak = 0.0_f32;
    for p in &metrics.top_processes {
        let mem_pct = (p.mem_bytes as f64 / total as f64) * 100.0;
        let cpu_val = p.cpu_usage;
        if cpu_val > peak {
            peak = cpu_val;
        }
        out.push(CachedRow {
            pid_str: p.pid.to_string(),
            cpu_str: format!("{:>5.1}", cpu_val.clamp(0.0, 100.0)),
            mem_str: crate::ui::util::human(p.mem_bytes),
            mem_pct_str: format!("{mem_pct:.2}%"),
            mem_pct,
            cpu_val,
        });
    }
    peak
}

const PID_W: u16 = 8;
const CPU_W: u16 = 8;
const MEM_W: u16 = 12;
const MEM_PCT_W: u16 = 8;
/// Columns the Name field needs to identify anything. Every other column is only added
/// once Name already has this much, so Name can no longer be squeezed to nothing.
const NAME_MIN_W: u16 = 8;
/// `Table::column_spacing`.
const COL_SPACING: u16 = 1;

/// Which process columns fit in the pane, and where they sit.
///
/// The table used to hand the layout solver a fixed, over-constrained set, so on a narrow
/// pane the solver crushed the percentage-sized Name column to nothing while the fixed
/// PID and Mem % columns kept their full width — losing the one field that identifies the
/// process while keeping the ones that do not.
///
/// Columns are now added in priority order as the pane widens, so they are shed in
/// reverse as it narrows: Name is unconditional, then CPU %, then Mem, then PID, and
/// Mem % last (it is derivable from Mem, so it is the least costly to lose).
///
/// Both the draw path and the header-click hit-testing build this from the same width, so
/// a sort click always lands on the column the user can actually see.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ProcColumns {
    pub pid: bool,
    pub cpu: bool,
    pub mem: bool,
    pub mem_pct: bool,
}

impl ProcColumns {
    pub fn for_width(width: u16) -> Self {
        // Each tier is the previous one plus a column and the gap before it.
        let with_cpu = NAME_MIN_W + COL_SPACING + CPU_W;
        let with_mem = with_cpu + COL_SPACING + MEM_W;
        let with_pid = with_mem + COL_SPACING + PID_W;
        let with_mem_pct = with_pid + COL_SPACING + MEM_PCT_W;
        Self {
            cpu: width >= with_cpu,
            mem: width >= with_mem,
            pid: width >= with_pid,
            mem_pct: width >= with_mem_pct,
        }
    }

    /// Column constraints in render order. Name takes whatever the others leave.
    pub fn constraints(&self) -> Vec<Constraint> {
        let mut c = Vec::with_capacity(5);
        if self.pid {
            c.push(Constraint::Length(PID_W));
        }
        c.push(Constraint::Fill(1)); // Name
        if self.cpu {
            c.push(Constraint::Length(CPU_W));
        }
        if self.mem {
            c.push(Constraint::Length(MEM_W));
        }
        if self.mem_pct {
            c.push(Constraint::Length(MEM_PCT_W));
        }
        c
    }

    /// Position of the CPU % column, which is clickable to sort. `None` when too narrow
    /// to render it.
    pub fn cpu_index(&self) -> Option<usize> {
        self.cpu.then(|| 1 + usize::from(self.pid))
    }

    /// Position of the Mem column, which is clickable to sort.
    pub fn mem_index(&self) -> Option<usize> {
        self.mem
            .then(|| 1 + usize::from(self.pid) + usize::from(self.cpu))
    }
}

pub fn draw_top_processes(f: &mut ratatui::Frame<'_>, area: Rect, params: ProcessDisplayParams) {
    // Draw outer block and title
    let Some(mm) = params.metrics else { return };
    let total = mm.process_count.unwrap_or(mm.top_processes.len());
    let block = Block::default()
        .borders(Borders::ALL)
        .title(format!("Top Processes ({total} total)"));
    f.render_widget(block, area);

    // Inner area (reserve space for search box if active)
    let inner = Rect {
        x: area.x + 1,
        y: area.y + 1,
        width: area.width.saturating_sub(2),
        height: area.height.saturating_sub(2),
    };

    // Draw search box if active
    let content_start_y = if params.search_active || !params.search_query.is_empty() {
        let search_area = Rect {
            x: inner.x,
            y: inner.y,
            width: inner.width,
            height: 3, // Height for border + content
        };

        let search_text = if params.search_active {
            format!("Search: {}_", params.search_query)
        } else {
            format!(
                "Filter: {} (press / to edit, c to clear)",
                params.search_query
            )
        };

        let search_block = Block::default()
            .borders(Borders::ALL)
            .border_style(Style::default().fg(Color::Yellow));
        let search_paragraph = Paragraph::new(search_text)
            .block(search_block)
            .style(Style::default().fg(Color::Yellow));
        f.render_widget(search_paragraph, search_area);

        inner.y + 3
    } else {
        inner.y
    };

    // Content area (reserve 2 columns for scrollbar)
    let inner = Rect {
        x: inner.x,
        y: content_start_y,
        width: inner.width,
        height: inner.height.saturating_sub(content_start_y - (area.y + 1)),
    };
    if inner.height < 1 || inner.width < 3 {
        return;
    }
    let content = Rect {
        x: inner.x,
        y: inner.y,
        width: inner.width.saturating_sub(2),
        height: inner.height,
    };

    let idxs = params.filtered_indices;

    // Scrolling
    let total_rows = idxs.len();
    let header_rows = 1usize;
    let viewport_rows = content.height.saturating_sub(header_rows as u16) as usize;
    let max_off = total_rows.saturating_sub(viewport_rows);
    let offset = params.scroll_offset.min(max_off);
    let show_n = total_rows.saturating_sub(offset).min(viewport_rows);

    // Use the App-side cache when available so we avoid allocating ~5 strings
    // per row every frame. Falls back to inline formatting (slow path) when
    // the cache hasn't been built yet — e.g. the very first frame before the
    // initial procs poll completes.
    let cache_ok = params.cached_rows.len() == mm.top_processes.len();
    let total_mem_bytes = mm.mem_total.max(1);
    let peak_cpu = if cache_ok {
        params.peak_cpu
    } else {
        mm.top_processes
            .iter()
            .map(|p| p.cpu_usage)
            .fold(0.0_f32, f32::max)
    };

    let columns = ProcColumns::for_width(content.width);

    let rows_iter = idxs.iter().skip(offset).take(show_n).map(|&ix| {
        let p = &mm.top_processes[ix];

        let (
            cpu_val,
            mem_pct,
            pid_span,
            name_span,
            cpu_span_text,
            mem_span_text,
            mem_pct_span_text,
        ) = if cache_ok {
            let row = &params.cached_rows[ix];
            (
                row.cpu_val,
                row.mem_pct,
                Span::raw(row.pid_str.as_str()),
                Span::raw(p.name.as_str()),
                row.cpu_str.as_str(),
                row.mem_str.as_str(),
                row.mem_pct_str.as_str(),
            )
        } else {
            let mem_pct = (p.mem_bytes as f64 / total_mem_bytes as f64) * 100.0;
            // SLOW path: only the very first frame before the cache exists.
            // We leak the formatted strings via Box::leak'd statics? No —
            // simpler: emit empty placeholders. Cache will exist within
            // ~500ms and the diff renderer fills it in.
            (
                p.cpu_usage,
                mem_pct,
                Span::raw(""),
                Span::raw(""),
                "",
                "",
                "",
            )
        };

        let cpu_fg = match cpu_val {
            x if x < 25.0 => Color::Green,
            x if x < 60.0 => Color::Yellow,
            _ => Color::Red,
        };
        let mem_fg = match mem_pct {
            x if x < 5.0 => Color::Blue,
            x if x < 20.0 => Color::Magenta,
            _ => Color::Red,
        };

        let mut emphasis = if (cpu_val - peak_cpu).abs() < f32::EPSILON {
            Style::default().add_modifier(Modifier::BOLD)
        } else {
            Style::default()
        };

        let is_selected = if let Some(selected_pid) = params.selected_process_pid {
            selected_pid == p.pid
        } else if let Some(selected_idx) = params.selected_process_index {
            selected_idx == ix
        } else {
            false
        };

        if is_selected {
            emphasis = emphasis
                .bg(PROCESS_SELECTION_BG)
                .fg(PROCESS_SELECTION_FG)
                .add_modifier(Modifier::BOLD);
        }

        let mut cells = Vec::with_capacity(5);
        if columns.pid {
            cells.push(
                ratatui::widgets::Cell::from(pid_span).style(Style::default().fg(Color::DarkGray)),
            );
        }
        cells.push(ratatui::widgets::Cell::from(name_span));
        if columns.cpu {
            cells.push(
                ratatui::widgets::Cell::from(Span::raw(cpu_span_text))
                    .style(Style::default().fg(cpu_fg)),
            );
        }
        if columns.mem {
            cells.push(ratatui::widgets::Cell::from(Span::raw(mem_span_text)));
        }
        if columns.mem_pct {
            cells.push(
                ratatui::widgets::Cell::from(Span::raw(mem_pct_span_text))
                    .style(Style::default().fg(mem_fg)),
            );
        }
        ratatui::widgets::Row::new(cells).style(emphasis)
    });

    // Header with sort indicator
    let cpu_hdr = match params.sort_by {
        ProcSortBy::CpuDesc => "CPU % •",
        _ => "CPU %",
    };
    let mem_hdr = match params.sort_by {
        ProcSortBy::MemDesc => "Mem •",
        _ => "Mem",
    };
    let mut header_cells = Vec::with_capacity(5);
    if columns.pid {
        header_cells.push("PID");
    }
    header_cells.push("Name");
    if columns.cpu {
        header_cells.push(cpu_hdr);
    }
    if columns.mem {
        header_cells.push(mem_hdr);
    }
    if columns.mem_pct {
        header_cells.push("Mem %");
    }
    let header = ratatui::widgets::Row::new(header_cells).style(
        Style::default()
            .fg(Color::Cyan)
            .add_modifier(Modifier::BOLD),
    );

    // Render table inside content area (no borders here; outer block already drawn)
    let table = Table::new(rows_iter, columns.constraints())
        .header(header)
        .column_spacing(COL_SPACING);
    f.render_widget(table, content);

    // Draw tooltip if a process is selected
    if let Some(selected_pid) = params.selected_process_pid {
        // Find the selected process to get its name
        let process_info = if let Some(metrics) = params.metrics {
            metrics
                .top_processes
                .iter()
                .find(|p| p.pid == selected_pid)
                .map(|p| format!("PID {}{}", p.pid, p.name))
                .unwrap_or_else(|| format!("PID {selected_pid}"))
        } else {
            format!("PID {selected_pid}")
        };

        // Key hints, built as spans so the keys read as keys. `t` only appears
        // for a local agent, since that is the only case where it does anything.
        let key = Style::default()
            .fg(PROCESS_TOOLTIP_FG)
            .add_modifier(Modifier::BOLD);
        let label = Style::default().fg(PROCESS_TOOLTIP_FG);
        let mut hints: Vec<Span> = vec![
            Span::styled("", key),
            Span::styled(" details", label),
            Span::styled("  ·  ", label),
        ];
        if params.is_local {
            hints.push(Span::styled("t", key));
            hints.push(Span::styled(" kill", label));
            hints.push(Span::styled("  ·  ", label));
        }
        hints.push(Span::styled("x", key));
        hints.push(Span::styled(" unselect", label));

        let hints_w: u16 = hints.iter().map(|s| fit::cols(&s.content)).sum();
        // One row, borders on both sides, a space of padding each side.
        let tooltip_height = 3;
        let max_w = area.width.saturating_sub(2);
        if max_w > hints_w + 6 && area.height > tooltip_height + 1 {
            // The process name is the elastic part: truncate it so the hint
            // always fits. The old version sized the box from the full string
            // and skipped rendering entirely when a long process name made it
            // wider than the pane — so the hint silently vanished exactly when
            // a long-named process was selected.
            let room_for_info = max_w - hints_w - 6;
            let info = fit::truncate_cols(&process_info, room_for_info);
            let mut spans: Vec<Span> = vec![
                Span::styled(" ", label),
                Span::styled(
                    info.clone(),
                    Style::default()
                        .fg(PROCESS_TOOLTIP_FG)
                        .add_modifier(Modifier::BOLD),
                ),
                Span::styled("", label),
            ];
            spans.extend(hints);
            spans.push(Span::styled(" ", label));

            let width = spans
                .iter()
                .map(|s| fit::cols(&s.content))
                .sum::<u16>()
                .saturating_add(2)
                .min(area.width);
            let tooltip_area = Rect {
                x: area.x + area.width.saturating_sub(width + 1),
                y: area.y + area.height.saturating_sub(tooltip_height + 1),
                width,
                height: tooltip_height,
            };

            let tooltip_block = Block::default().borders(Borders::ALL).style(
                Style::default()
                    .bg(PROCESS_TOOLTIP_BG)
                    .fg(PROCESS_TOOLTIP_FG),
            );

            f.render_widget(
                Paragraph::new(Line::from(spans)).block(tooltip_block),
                tooltip_area,
            );
        }
    }

    // Scrollbar (ratatui built-in). Skip drawing when content fits in viewport.
    let scroll_area = Rect {
        x: inner.x + inner.width.saturating_sub(1),
        y: inner.y,
        width: 1,
        height: inner.height,
    };
    let max_off_for_bar = total_rows.saturating_sub(viewport_rows);
    if scroll_area.height >= 3 && max_off_for_bar > 0 {
        let scrollbar = Scrollbar::new(ScrollbarOrientation::VerticalRight)
            .begin_symbol(Some(""))
            .end_symbol(Some(""))
            .thumb_symbol("")
            .track_symbol(Some(""))
            .thumb_style(Style::default().fg(SB_THUMB))
            .track_style(Style::default().fg(SB_TRACK))
            .begin_style(Style::default().fg(SB_ARROW))
            .end_style(Style::default().fg(SB_ARROW));
        let mut state = ScrollbarState::new(max_off_for_bar).position(offset);
        f.render_stateful_widget(scrollbar, scroll_area, &mut state);
    }
}

/// Parameters for process key event handling
pub struct ProcessKeyParams<'a> {
    pub selected_process_pid: &'a mut Option<u32>,
    pub selected_process_index: &'a mut Option<usize>,
    pub key: crossterm::event::KeyEvent,
    pub metrics: Option<&'a Metrics>,
    pub filtered_indices: &'a [usize],
}

pub fn processes_handle_key_with_selection(params: ProcessKeyParams) -> bool {
    use crossterm::event::KeyCode;

    let move_selection = |delta: isize,
                          sel_idx: &mut Option<usize>,
                          sel_pid: &mut Option<u32>,
                          metrics: Option<&Metrics>,
                          idxs: &[usize]| {
        let Some(m) = metrics else { return };
        if idxs.is_empty() {
            *sel_idx = None;
            *sel_pid = None;
            return;
        }
        if sel_idx.is_none() || sel_pid.is_none() {
            let first_idx = idxs[0];
            *sel_idx = Some(first_idx);
            *sel_pid = Some(m.top_processes[first_idx].pid);
            return;
        }
        let current_idx = sel_idx.unwrap();
        match idxs.iter().position(|&idx| idx == current_idx) {
            Some(pos) => {
                let new_pos = (pos as isize + delta).clamp(0, idxs.len() as isize - 1) as usize;
                if new_pos != pos {
                    let new_idx = idxs[new_pos];
                    *sel_idx = Some(new_idx);
                    *sel_pid = Some(m.top_processes[new_idx].pid);
                }
            }
            None => {
                // Current selection no longer in filtered list
                let first_idx = idxs[0];
                *sel_idx = Some(first_idx);
                *sel_pid = Some(m.top_processes[first_idx].pid);
            }
        }
    };

    match params.key.code {
        KeyCode::Up => {
            move_selection(
                -1,
                params.selected_process_index,
                params.selected_process_pid,
                params.metrics,
                params.filtered_indices,
            );
            true
        }
        KeyCode::Down => {
            move_selection(
                1,
                params.selected_process_index,
                params.selected_process_pid,
                params.metrics,
                params.filtered_indices,
            );
            true
        }
        KeyCode::Char('x') | KeyCode::Char('X')
            if params.selected_process_pid.is_some() || params.selected_process_index.is_some() =>
        {
            *params.selected_process_pid = None;
            *params.selected_process_index = None;
            true
        }
        KeyCode::Char('x') | KeyCode::Char('X') => false,
        KeyCode::Enter => {
            // Signal that Enter was pressed with a selection
            params.selected_process_pid.is_some() // Return true if we have a selection to handle
        }
        _ => {
            // No other keys handled - let scrollbar handle all navigation
            false
        }
    }
}

/// Parameters for process mouse event handling
pub struct ProcessMouseParams<'a> {
    pub scroll_offset: &'a mut usize,
    pub selected_process_pid: &'a mut Option<u32>,
    pub selected_process_index: &'a mut Option<usize>,
    pub drag: &'a mut Option<crate::ui::cpu::PerCoreScrollDrag>,
    pub mouse: MouseEvent,
    pub area: Rect,
    pub total_rows: usize,
    pub metrics: Option<&'a Metrics>,
    /// True when the on-screen search box is currently being drawn (active
    /// edit mode OR a non-empty filter is showing). The caller computes this
    /// from the same condition as the draw path.
    pub search_box_visible: bool,
    pub filtered_indices: &'a [usize],
}

/// Enhanced mouse handler that also manages process selection
/// Returns Some(new_sort) if the header was clicked, or handles row selection
pub fn processes_handle_mouse_with_selection(params: ProcessMouseParams) -> Option<ProcSortBy> {
    // Inner and content areas (match draw_top_processes)
    let inner = Rect {
        x: params.area.x + 1,
        y: params.area.y + 1,
        width: params.area.width.saturating_sub(2),
        height: params.area.height.saturating_sub(2),
    };
    if inner.height == 0 || inner.width <= 2 {
        return None;
    }

    // Calculate content area - must match draw_top_processes exactly!
    // If a search box is being drawn (active edit mode OR a filter showing),
    // content starts 3 rows below.
    let content_start_y = if params.search_box_visible {
        inner.y + 3
    } else {
        inner.y
    };

    let content = Rect {
        x: inner.x,
        y: content_start_y,
        width: inner.width.saturating_sub(2),
        height: inner
            .height
            .saturating_sub(if params.search_box_visible { 3 } else { 0 }),
    };

    // Scrollbar interactions (click arrows/page/drag)
    per_core_handle_scrollbar_mouse(
        params.scroll_offset,
        params.drag,
        params.mouse,
        params.area,
        params.total_rows,
    );

    // Wheel scrolling when inside the content
    crate::ui::cpu::per_core_handle_mouse(
        params.scroll_offset,
        params.mouse,
        content,
        content.height as usize,
    );

    // Header click to change sort
    let header_area = Rect {
        x: content.x,
        y: content.y,
        width: content.width,
        height: 1,
    };
    let inside_header = params.mouse.row == header_area.y
        && params.mouse.column >= header_area.x
        && params.mouse.column < header_area.x + header_area.width;

    if inside_header && matches!(params.mouse.kind, MouseEventKind::Down(MouseButton::Left)) {
        // Split the header the same way the draw path did, so a click lands on the
        // column actually on screen even when PID has been dropped.
        let columns = ProcColumns::for_width(header_area.width);
        let cols = Layout::default()
            .direction(Direction::Horizontal)
            .constraints(columns.constraints())
            .spacing(COL_SPACING) // must match Table::column_spacing in the draw path
            .split(header_area);
        if let Some(cpu) = columns.cpu_index().map(|i| cols[i])
            && params.mouse.column >= cpu.x
            && params.mouse.column < cpu.x + cpu.width
        {
            return Some(ProcSortBy::CpuDesc);
        }
        if let Some(mem) = columns.mem_index().map(|i| cols[i])
            && params.mouse.column >= mem.x
            && params.mouse.column < mem.x + mem.width
        {
            return Some(ProcSortBy::MemDesc);
        }
    }

    // Row click for process selection
    let data_start_row = content.y + 1; // Skip header
    let data_area_height = content.height.saturating_sub(1); // Exclude header

    if matches!(params.mouse.kind, MouseEventKind::Down(MouseButton::Left))
        && params.mouse.row >= data_start_row
        && params.mouse.row < data_start_row + data_area_height
        && params.mouse.column >= content.x
        && params.mouse.column < content.x + content.width
    {
        let clicked_row = (params.mouse.row - data_start_row) as usize;

        if let Some(m) = params.metrics {
            let idxs = params.filtered_indices;
            let visible_process_position = *params.scroll_offset + clicked_row;
            if visible_process_position < idxs.len() {
                let actual_process_index = idxs[visible_process_position];
                let clicked_process = &m.top_processes[actual_process_index];
                *params.selected_process_pid = Some(clicked_process.pid);
                *params.selected_process_index = Some(actual_process_index);
            }
        }
    }

    // Clamp to valid range
    per_core_clamp(
        params.scroll_offset,
        params.total_rows,
        (content.height.saturating_sub(1)) as usize,
    );
    None
}

#[cfg(test)]
mod column_tests {
    use super::*;
    use ratatui::layout::{Direction, Layout, Rect};

    fn name_width(w: u16) -> u16 {
        let c = ProcColumns::for_width(w);
        let rects = Layout::default()
            .direction(Direction::Horizontal)
            .constraints(c.constraints())
            .spacing(COL_SPACING)
            .split(Rect::new(0, 0, w, 1));
        rects[usize::from(c.pid)].width
    }

    /// The complaint this fixes: on a narrow pane the Name column was the first thing to
    /// disappear, leaving a table of numbers with nothing to identify the process. Name
    /// must now be the last column standing, at every width that can render anything.
    #[test]
    fn name_is_never_the_column_that_gets_dropped() {
        for width in NAME_MIN_W..=200u16 {
            assert!(
                name_width(width) >= 1,
                "width {width}: Name was squeezed to nothing"
            );
        }
    }

    /// Columns are shed in reverse priority order, so a narrower pane can never show a
    /// column that a wider one hid.
    #[test]
    fn columns_are_shed_in_priority_order() {
        for width in 0..=200u16 {
            let c = ProcColumns::for_width(width);
            assert!(!c.mem_pct || c.pid, "width {width}: Mem % outlived PID");
            assert!(!c.pid || c.mem, "width {width}: PID outlived Mem");
            assert!(!c.mem || c.cpu, "width {width}: Mem outlived CPU %");
        }
    }

    /// Columns come back as the pane widens and never flap.
    #[test]
    fn columns_are_monotonic_in_width() {
        let mut prev = ProcColumns::for_width(0);
        for width in 1..=200u16 {
            let c = ProcColumns::for_width(width);
            for (was, now, name) in [
                (prev.cpu, c.cpu, "CPU %"),
                (prev.mem, c.mem, "Mem"),
                (prev.pid, c.pid, "PID"),
                (prev.mem_pct, c.mem_pct, "Mem %"),
            ] {
                assert!(!was || now, "width {width}: {name} vanished as it widened");
            }
            prev = c;
        }
    }

    /// The tiers, from a comfortable pane down to a very narrow one.
    #[test]
    fn narrow_panes_shed_columns_in_order() {
        let full = ProcColumns::for_width(48);
        assert_eq!(full.constraints().len(), 5);
        assert!(full.pid && full.cpu && full.mem && full.mem_pct);

        // Mem % goes first.
        let c = ProcColumns::for_width(45);
        assert!(c.pid && c.mem && !c.mem_pct);

        // Then PID.
        let c = ProcColumns::for_width(35);
        assert!(!c.pid && c.cpu && c.mem);

        // Then Mem, leaving the name and its CPU load.
        let c = ProcColumns::for_width(20);
        assert!(!c.mem && c.cpu);
        assert_eq!(c.constraints().len(), 2);

        // At the floor, just the name.
        let c = ProcColumns::for_width(10);
        assert!(!c.cpu && !c.mem);
        assert_eq!(c.constraints().len(), 1);
    }

    /// Regression guard for the old behaviour: a 130-column terminal gives the process
    /// pane ~48 columns, and every column still fits there.
    #[test]
    fn a_wide_terminal_keeps_the_full_table() {
        assert_eq!(ProcColumns::for_width(48).constraints().len(), 5);
    }

    /// Sort clicks are resolved by index, so those indices must track the columns that
    /// are actually rendered — otherwise clicking "CPU %" would sort by Mem.
    #[test]
    fn sort_indices_follow_the_rendered_columns() {
        let wide = ProcColumns::for_width(48);
        assert_eq!(wide.cpu_index(), Some(2)); // PID, Name, CPU %
        assert_eq!(wide.mem_index(), Some(3));

        let narrow = ProcColumns::for_width(35);
        assert_eq!(narrow.cpu_index(), Some(1)); // Name, CPU %
        assert_eq!(narrow.mem_index(), Some(2));

        // A column that is not rendered has no index to click.
        let tiny = ProcColumns::for_width(10);
        assert_eq!(tiny.cpu_index(), None);
        assert_eq!(tiny.mem_index(), None);

        // Whatever the width, any index returned is inside the rendered set.
        for width in 0..=200u16 {
            let c = ProcColumns::for_width(width);
            let n = c.constraints().len();
            for i in [c.cpu_index(), c.mem_index()].into_iter().flatten() {
                assert!(i < n, "width {width}: index {i} outside {n} columns");
            }
        }
    }

    /// Name takes the slack, so it grows with the pane instead of being pinned to a
    /// percentage that the fixed columns can crush.
    #[test]
    fn name_absorbs_the_leftover_width() {
        assert!(
            name_width(80) > name_width(60),
            "Name did not grow with the pane"
        );
    }
}

#[cfg(test)]
mod click_tests {
    use super::*;
    use crossterm::event::{KeyModifiers, MouseButton, MouseEvent, MouseEventKind};
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    use ratatui::layout::Rect;
    use socktop_connector::{Metrics, ProcessInfo};

    fn metrics() -> Metrics {
        Metrics {
            sampled_at_ms: None,
            cpu_total: 0.0,
            cpu_per_core: vec![],
            mem_total: 32_000_000_000,
            mem_used: 0,
            swap_total: 0,
            swap_used: 0,
            hostname: "t".into(),
            cpu_temp_c: None,
            disks: vec![],
            networks: vec![],
            top_processes: vec![ProcessInfo {
                pid: 4242,
                name: "some-process".into(),
                cpu_usage: 1.5,
                mem_bytes: 1_000_000,
            }],
            gpus: None,
            process_count: Some(1),
        }
    }

    /// Renders the pane and returns its header row as text.
    fn header_row(width: u16) -> String {
        let m = metrics();
        let mut cache = Vec::new();
        let peak = rebuild_row_cache(&m, &mut cache);
        let idxs = [0usize];
        let mut terminal = Terminal::new(TestBackend::new(width, 8)).unwrap();
        terminal
            .draw(|f| {
                draw_top_processes(
                    f,
                    Rect::new(0, 0, width, 8),
                    ProcessDisplayParams {
                        metrics: Some(&m),
                        scroll_offset: 0,
                        sort_by: ProcSortBy::CpuDesc,
                        selected_process_pid: None,
                        selected_process_index: None,
                        search_query: "",
                        search_active: false,
                        filtered_indices: &idxs,
                        cached_rows: &cache,
                        peak_cpu: peak,
                        is_local: false,
                    },
                )
            })
            .unwrap();
        let buf = terminal.backend().buffer();
        (0..width)
            .map(|x| buf[(x, 1)].symbol().to_string())
            .collect()
    }

    fn click(width: u16, column: u16) -> Option<ProcSortBy> {
        let m = metrics();
        let mut scroll = 0usize;
        let mut drag = None;
        let mut sel_pid = None;
        let mut sel_idx = None;
        let idxs = [0usize];
        processes_handle_mouse_with_selection(ProcessMouseParams {
            scroll_offset: &mut scroll,
            selected_process_pid: &mut sel_pid,
            selected_process_index: &mut sel_idx,
            drag: &mut drag,
            mouse: MouseEvent {
                kind: MouseEventKind::Down(MouseButton::Left),
                column,
                row: 1,
                modifiers: KeyModifiers::NONE,
            },
            area: Rect::new(0, 0, width, 8),
            total_rows: 1,
            metrics: Some(&m),
            search_box_visible: false,
            filtered_indices: &idxs,
        })
    }

    /// The hit-test rects are computed by a separate `Layout` call from the one `Table`
    /// renders with. This walks the rendered header text and clicks each label where it
    /// actually appears, which catches any drift between the two — including column
    /// spacing, which the two APIs configure differently.
    #[test]
    fn clicking_a_rendered_sort_header_sorts_by_that_column() {
        for width in [40u16, 50, 60, 80, 120] {
            let row = header_row(width);
            let cpu_at = row.find("CPU").map(|i| row[..i].chars().count() as u16);
            let mem_at = row.find("Mem").map(|i| row[..i].chars().count() as u16);

            if let Some(x) = cpu_at {
                assert_eq!(
                    click(width, x),
                    Some(ProcSortBy::CpuDesc),
                    "width {width}: clicking the rendered 'CPU %' header at column {x} \
                     did not sort by CPU (header row: {row:?})"
                );
            }
            if let Some(x) = mem_at {
                assert_eq!(
                    click(width, x),
                    Some(ProcSortBy::MemDesc),
                    "width {width}: clicking the rendered 'Mem' header at column {x} \
                     did not sort by Mem (header row: {row:?})"
                );
            }
        }
    }

    /// Name is what identifies the row, so it must be rendered at every width the pane
    /// can draw anything at.
    #[test]
    fn the_name_column_is_rendered_even_when_narrow() {
        for width in [30u16, 40, 60, 120] {
            let row = header_row(width);
            assert!(
                row.contains("Name"),
                "width {width}: no Name column in header {row:?}"
            );
        }
    }
}

#[cfg(test)]
mod tooltip_tests {
    use super::*;
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    use ratatui::layout::Rect;
    use socktop_connector::{Metrics, ProcessInfo};

    fn metrics(name: &str) -> Metrics {
        Metrics {
            sampled_at_ms: None,
            cpu_total: 0.0,
            cpu_per_core: vec![],
            mem_total: 32_000_000_000,
            mem_used: 0,
            swap_total: 0,
            swap_used: 0,
            hostname: "t".into(),
            cpu_temp_c: None,
            disks: vec![],
            networks: vec![],
            top_processes: vec![ProcessInfo {
                pid: 4242,
                name: name.into(),
                cpu_usage: 1.5,
                mem_bytes: 1_000_000,
            }],
            gpus: None,
            process_count: Some(1),
        }
    }

    /// Render the pane with a selection and return the whole buffer as text.
    fn rendered(name: &str, width: u16, is_local: bool) -> String {
        let m = metrics(name);
        let mut cache = Vec::new();
        let peak = rebuild_row_cache(&m, &mut cache);
        let idxs = [0usize];
        let mut terminal = Terminal::new(TestBackend::new(width, 12)).unwrap();
        terminal
            .draw(|f| {
                draw_top_processes(
                    f,
                    Rect::new(0, 0, width, 12),
                    ProcessDisplayParams {
                        metrics: Some(&m),
                        scroll_offset: 0,
                        sort_by: ProcSortBy::CpuDesc,
                        selected_process_pid: Some(4242),
                        selected_process_index: Some(0),
                        search_query: "",
                        search_active: false,
                        filtered_indices: &idxs,
                        cached_rows: &cache,
                        peak_cpu: peak,
                        is_local,
                    },
                )
            })
            .unwrap();
        let buf = terminal.backend().buffer();
        let mut out = String::new();
        for y in 0..12 {
            for x in 0..width {
                out.push_str(buf[(x, y)].symbol());
            }
            out.push('\n');
        }
        out
    }

    #[test]
    fn hint_offers_kill_for_a_local_agent() {
        let out = rendered("some-process", 80, true);
        assert!(out.contains("details"), "no hint rendered at all:\n{out}");
        assert!(
            out.contains("kill"),
            "local agent should offer kill:\n{out}"
        );
        assert!(out.contains("unselect"));
    }

    /// The key does nothing for a remote agent, so advertising it would be a lie.
    #[test]
    fn hint_omits_kill_for_a_remote_agent() {
        let out = rendered("some-process", 80, false);
        assert!(out.contains("details"), "no hint rendered at all:\n{out}");
        assert!(
            !out.contains("kill"),
            "remote agent must not offer kill:\n{out}"
        );
    }

    /// Regression: the hint used to be sized from the full label including the
    /// process name, and was skipped entirely when that made it wider than the
    /// pane — so it vanished exactly when a long-named process was selected.
    #[test]
    fn hint_survives_a_very_long_process_name() {
        let long = "/usr/lib/firefox-esr/firefox-esr-with-a-really-long-suffix";
        let out = rendered(long, 80, true);
        assert!(
            out.contains("details") && out.contains("kill"),
            "hint disappeared for a long process name:\n{out}"
        );
    }
}