metop 0.3.0

Linux memory analysis TUI: RSS/PSS/USS per process, VMA drill-down
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
use std::cmp::Ordering;
use std::collections::{HashMap, VecDeque};
use std::time::Duration;

use ratatui::DefaultTerminal;
use ratatui::crossterm::event::{
    self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers, MouseButton, MouseEvent,
    MouseEventKind,
};
use ratatui::layout::Rect;
use ratatui::widgets::TableState;

use crate::apps::AppGroup;
use crate::cgroup::CgroupMem;
use crate::i18n::{HelpPage, Lang};
use crate::model::{Focus, ProcRow, Snapshot, SortKey};
use crate::procfs::{Vma, VmaGroup};
use crate::theme::{ColorSupport, Theme};
use crate::ui;

pub const HISTORY: usize = 120;

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum View {
    Procs,
    Cgroups,
    Apps,
}

/// One sample of an app's three metrics, so switching the map metric does not
/// mix series (a PSS history is useless when the view shows RSS).
#[derive(Clone, Copy, Default)]
pub struct AppSample {
    pub pss_kb: u64,
    pub uss_kb: u64,
    pub rss_kb: u64,
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum AppMetric {
    Pss,
    Uss,
    Rss,
}

impl AppMetric {
    pub fn label(self) -> &'static str {
        match self {
            AppMetric::Pss => "PSS",
            AppMetric::Uss => "USS",
            AppMetric::Rss => "RSS",
        }
    }

    pub fn value(self, g: &AppGroup) -> u64 {
        match self {
            AppMetric::Pss => g.pss_kb,
            AppMetric::Uss => g.uss_kb,
            AppMetric::Rss => g.rss_kb,
        }
    }

    fn next(self) -> Self {
        match self {
            AppMetric::Pss => AppMetric::Uss,
            AppMetric::Uss => AppMetric::Rss,
            AppMetric::Rss => AppMetric::Pss,
        }
    }
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum CgroupSort {
    Current,
    Anon,
    File,
    Slab,
}

impl CgroupSort {
    const ALL: [CgroupSort; 4] = [
        CgroupSort::Current,
        CgroupSort::Anon,
        CgroupSort::File,
        CgroupSort::Slab,
    ];

    pub fn label(self) -> &'static str {
        match self {
            CgroupSort::Current => "CUR",
            CgroupSort::Anon => "ANON",
            CgroupSort::File => "FILE",
            CgroupSort::Slab => "SLAB",
        }
    }

    fn next(self) -> Self {
        let i = Self::ALL.iter().position(|k| *k == self).unwrap_or(0);
        Self::ALL[(i + 1) % Self::ALL.len()]
    }

    fn key(self, c: &CgroupMem) -> u64 {
        match self {
            CgroupSort::Current => c.current_kb,
            CgroupSort::Anon => c.anon_kb,
            CgroupSort::File => c.file_kb,
            CgroupSort::Slab => c.slab_kb,
        }
    }
}

/// A row in the detail view: either an aggregated mapping or one VMA inside it.
pub enum DetailRow<'a> {
    Group(&'a VmaGroup),
    Vma(&'a VmaGroup, &'a Vma),
}

/// An open help overlay: which page's documentation is shown and how far it
/// has been scrolled.
pub struct HelpState {
    pub page: HelpPage,
    pub scroll: u16,
}

/// A clickable footer action.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FooterAction {
    View,
    Sort,
    Reverse,
    Filter,
    Enter,
    Back,
    Theme,
    Lang,
    Help,
    Quit,
}

/// What a mouse hit region refers to.
#[derive(Clone, Copy, Debug)]
pub enum MouseTarget {
    Row(usize),
    Tile(usize),
    Button(FooterAction),
    HelpArea,
}

#[derive(Clone, Copy, Debug)]
pub struct Hit {
    pub rect: Rect,
    pub target: MouseTarget,
}

pub struct App {
    pub view: View,
    /// Active built-in theme; `t` cycles it.
    pub theme: Theme,
    /// Terminal colour depth the active palette was rendered for.
    pub support: ColorSupport,
    /// UI language; `l` toggles it.
    pub lang: Lang,
    /// Open help overlay, if any.
    pub help: Option<HelpState>,
    /// Click targets recorded by the last draw.
    pub hits: Vec<Hit>,
    pub sort: SortKey,
    pub cgroup_sort: CgroupSort,
    pub cgroup_sel: usize,
    /// App groups for the treemap, recomputed per snapshot (collapsed to the
    /// top few percent so the map does not dissolve into slivers).
    pub app_groups: Vec<AppGroup>,
    /// Metric history per app name (only the collapsed groups are tracked:
    /// ~20-40 entries, not all 330).
    pub app_history: HashMap<String, VecDeque<AppSample>>,
    pub app_sel: usize,
    pub app_metric: AppMetric,
    pub app_state: TableState,
    /// Drill target coming from the apps view: only these pids are shown in
    /// the process table.
    pub pid_filter: Option<std::collections::HashSet<u32>>,
    pub pid_filter_name: Option<String>,
    /// cgroup path whose processes are being listed (drill-down).
    pub drill: Option<String>,
    pub drill_sel: usize,
    pub desc: bool,
    pub filter: String,
    /// Mapping-name filter for the VMA detail view (independent of `filter`).
    pub detail_filter: String,
    /// Group labels whose VMAs are expanded.
    pub expanded: std::collections::HashSet<String>,
    pub editing: bool,
    /// Whether the in-progress edit targets the detail filter.
    pub editing_detail: bool,
    pub edit_buf: String,
    pub sel: usize,
    pub detail: bool,
    pub detail_sel: usize,
    pub snap: Option<Snapshot>,
    pub table_state: TableState,
    pub cgroup_state: TableState,
    pub drill_state: TableState,
    pub detail_state: TableState,
    pub history: HashMap<u32, VecDeque<u64>>,
    /// PSI `some avg10` over time per cgroup, stored as percent*10 for the
    /// sparkline (u64) with a fixed 0-1000 scale.
    pub cgroup_history: HashMap<String, VecDeque<u64>>,
    /// OOM kills per scan per cgroup (5 s cadence, 120 samples = 10 min).
    pub oom_history: HashMap<String, VecDeque<u64>>,
    last_scan_seq: u64,
    last_sent: Option<(Option<u32>, bool)>,
    started: bool,
    pending_focus: Option<u32>,
    detail_pid: Option<u32>,
}

impl App {
    pub fn new(start_pid: Option<u32>) -> Self {
        let mut app = App {
            view: View::Procs,
            theme: Theme::DEFAULT,
            support: ColorSupport::TrueColor,
            lang: Lang::En,
            help: None,
            hits: Vec::new(),
            sort: SortKey::Rss,
            cgroup_sort: CgroupSort::Current,
            cgroup_sel: 0,
            app_groups: Vec::new(),
            app_history: HashMap::new(),
            app_sel: 0,
            app_metric: AppMetric::Pss,
            app_state: TableState::default(),
            pid_filter: None,
            pid_filter_name: None,
            drill: None,
            drill_sel: 0,
            desc: true,
            filter: String::new(),
            detail_filter: String::new(),
            expanded: std::collections::HashSet::new(),
            editing: false,
            editing_detail: false,
            edit_buf: String::new(),
            sel: 0,
            detail: false,
            detail_sel: 0,
            snap: None,
            table_state: TableState::default(),
            cgroup_state: TableState::default(),
            drill_state: TableState::default(),
            detail_state: TableState::default(),
            history: HashMap::new(),
            cgroup_history: HashMap::new(),
            oom_history: HashMap::new(),
            last_scan_seq: 0,
            last_sent: None,
            started: false,
            pending_focus: None,
            detail_pid: None,
        };
        if let Some(pid) = start_pid {
            app.pending_focus = Some(pid);
        }
        app
    }

    fn ratio(&self, r: &ProcRow) -> f64 {
        let pss = self
            .snap
            .as_ref()
            .and_then(|s| s.pss.get(&r.pid))
            .map(|p| p.rollup.pss_kb)
            .unwrap_or(0);
        if r.rss_kb == 0 {
            0.0
        } else {
            pss as f64 / r.rss_kb as f64
        }
    }

    pub fn visible(&self) -> Vec<&ProcRow> {
        let Some(snap) = &self.snap else {
            return Vec::new();
        };
        let needle = self.filter.to_lowercase();
        let mut rows: Vec<&ProcRow> = snap
            .rows
            .iter()
            .filter(|r| {
                self.pid_filter
                    .as_ref()
                    .is_none_or(|set| set.contains(&r.pid))
            })
            .filter(|r| {
                needle.is_empty()
                    || r.comm.to_lowercase().contains(&needle)
                    || r.pid.to_string().starts_with(&needle)
            })
            .collect();
        let pss = |pid: u32| snap.pss.get(&pid).map(|p| p.rollup.pss_kb).unwrap_or(0);
        let uss = |pid: u32| snap.pss.get(&pid).map(|p| p.rollup.uss_kb).unwrap_or(0);
        let sort = self.sort;
        let desc = self.desc;
        rows.sort_by(|a, b| {
            let ord = match sort {
                SortKey::Rss => a.rss_kb.cmp(&b.rss_kb),
                SortKey::Pss => pss(a.pid).cmp(&pss(b.pid)),
                SortKey::Uss => uss(a.pid).cmp(&uss(b.pid)),
                SortKey::Delta => a.delta_kb.cmp(&b.delta_kb),
                SortKey::Ratio => self
                    .ratio(a)
                    .partial_cmp(&self.ratio(b))
                    .unwrap_or(Ordering::Equal),
                SortKey::Pid => a.pid.cmp(&b.pid),
                SortKey::Comm => a.comm.to_lowercase().cmp(&b.comm.to_lowercase()),
            };
            let ord = if desc { ord.reverse() } else { ord };
            ord.then_with(|| a.pid.cmp(&b.pid))
        });
        rows
    }

    pub fn visible_cgroups(&self) -> Vec<&CgroupMem> {
        let Some(snap) = &self.snap else {
            return Vec::new();
        };
        let needle = self.filter.to_lowercase();
        let mut rows: Vec<&CgroupMem> = snap
            .cgroups
            .iter()
            .filter(|c| needle.is_empty() || c.path.to_lowercase().contains(&needle))
            .collect();
        rows.sort_by_key(|c| self.cgroup_sort.key(c));
        if self.desc {
            rows.reverse();
        }
        rows
    }

    /// Processes whose cgroup is `drill` or a descendant of it. The cgroup
    /// numbers are hierarchical, so the subtree is what you want to see.
    pub fn cgroup_members(&self) -> Vec<&ProcRow> {
        let (Some(path), Some(snap)) = (&self.drill, &self.snap) else {
            return Vec::new();
        };
        let prefix = if path == "/" {
            "/".to_string()
        } else {
            format!("{path}/")
        };
        let mut rows: Vec<&ProcRow> = snap
            .rows
            .iter()
            .filter(|r| {
                snap.pid_cgroup
                    .get(&r.pid)
                    .is_some_and(|c| c == path || c.starts_with(&prefix))
            })
            .collect();
        rows.sort_by_key(|r| std::cmp::Reverse(r.rss_kb));
        rows
    }

    /// Series for the selected metric of `name`, for the header sparkline.
    pub fn app_series(&self, name: &str, metric: AppMetric) -> Vec<u64> {
        self.app_history
            .get(name)
            .map(|h| {
                h.iter()
                    .map(|s| match metric {
                        AppMetric::Pss => s.pss_kb,
                        AppMetric::Uss => s.uss_kb,
                        AppMetric::Rss => s.rss_kb,
                    })
                    .collect()
            })
            .unwrap_or_default()
    }

    pub fn visible_apps(&self) -> Vec<&AppGroup> {
        let needle = self.filter.to_lowercase();
        self.app_groups
            .iter()
            .filter(|g| needle.is_empty() || g.name.to_lowercase().contains(&needle))
            .collect()
    }

    pub fn selected_app(&self) -> Option<&AppGroup> {
        self.visible_apps().get(self.app_sel).copied()
    }

    pub fn selected_pid(&self) -> Option<u32> {
        // While the detail view is open the focused pid comes from the loaded
        // detail, but it must fall back to the table selection: the Enter
        // keypress happens one snapshot before detail exists, and returning
        // None there drops the focus request entirely.
        if self.detail
            && let Some(pid) = self
                .snap
                .as_ref()
                .and_then(|s| s.detail.as_ref())
                .map(|d| d.pid)
        {
            return Some(pid);
        }
        self.visible().get(self.sel).map(|r| r.pid)
    }

    /// The (pid, want_detail) pair the sampler should be working on.
    pub fn focus_target(&self) -> (Option<u32>, bool) {
        if let Some(pid) = self.pending_focus {
            return (Some(pid), self.detail);
        }
        (self.selected_pid(), self.detail)
    }

    pub fn take_focus_update(&mut self) -> Option<Focus> {
        let target = self.focus_target();
        if self.last_sent == Some(target) {
            return None;
        }
        self.last_sent = Some(target);
        Some(Focus::Set {
            pid: target.0,
            want_detail: target.1,
        })
    }

    pub fn on_snapshot(&mut self, snap: Snapshot) {
        let mut seen: Vec<u32> = Vec::with_capacity(snap.rows.len());
        for row in &snap.rows {
            seen.push(row.pid);
            let h = self.history.entry(row.pid).or_default();
            h.push_back(row.rss_kb);
            while h.len() > HISTORY {
                h.pop_front();
            }
        }
        self.history.retain(|pid, _| seen.contains(pid));
        self.app_groups = crate::apps::collapse(
            crate::apps::group(&snap.rows, &snap.pss, &snap.pid_cgroup, &snap.pid_exe),
            0.005,
        );
        let mut app_seen: std::collections::HashSet<&str> =
            std::collections::HashSet::with_capacity(self.app_groups.len());
        for g in &self.app_groups {
            app_seen.insert(g.name.as_str());
            let h = self.app_history.entry(g.name.clone()).or_default();
            h.push_back(AppSample {
                pss_kb: g.pss_kb,
                uss_kb: g.uss_kb,
                rss_kb: g.rss_kb,
            });
            while h.len() > HISTORY {
                h.pop_front();
            }
        }
        self.app_history
            .retain(|name, _| app_seen.contains(name.as_str()));

        // The cgroup list only changes on the 5 s scan; sampling it every tick
        // would record the same value five times and shrink the window.
        if snap.scan_seq != self.last_scan_seq && !snap.cgroups.is_empty() {
            self.last_scan_seq = snap.scan_seq;
            let mut cg_seen: std::collections::HashSet<&str> =
                std::collections::HashSet::with_capacity(snap.cgroups.len());
            for cg in &snap.cgroups {
                cg_seen.insert(cg.path.as_str());
                let h = self.cgroup_history.entry(cg.path.clone()).or_default();
                h.push_back((cg.psi_some10.max(0.0) * 10.0).round() as u64);
                while h.len() > HISTORY {
                    h.pop_front();
                }
                let o = self.oom_history.entry(cg.path.clone()).or_default();
                o.push_back(cg.oom_recent);
                while o.len() > HISTORY {
                    o.pop_front();
                }
            }
            self.cgroup_history
                .retain(|path, _| cg_seen.contains(path.as_str()));
            self.oom_history
                .retain(|path, _| cg_seen.contains(path.as_str()));
        }
        self.snap = Some(snap);
        // A different process means a different mapping tree: reset expansion.
        let pid = self
            .snap
            .as_ref()
            .and_then(|s| s.detail.as_ref())
            .map(|d| d.pid);
        if pid != self.detail_pid {
            self.detail_pid = pid;
            self.expanded.clear();
            self.detail_sel = 0;
        }
        if let Some(pid) = self.pending_focus
            && let Some(i) = self.visible().iter().position(|r| r.pid == pid)
        {
            self.sel = i;
            self.pending_focus = None;
        }
        self.clamp();
    }

    /// Flattened detail rows: every matching group, followed by its VMAs when
    /// expanded. Selection indexes into this vector.
    pub fn detail_rows(&self) -> Vec<DetailRow<'_>> {
        let needle = self.detail_filter.to_lowercase();
        let Some(d) = self.snap.as_ref().and_then(|s| s.detail.as_ref()) else {
            return Vec::new();
        };
        let mut out = Vec::with_capacity(d.report.groups.len());
        for g in &d.report.groups {
            if !needle.is_empty() && !g.label.to_lowercase().contains(&needle) {
                continue;
            }
            out.push(DetailRow::Group(g));
            if self.expanded.contains(&g.label) {
                out.extend(g.vmas.iter().map(|v| DetailRow::Vma(g, v)));
            }
        }
        out
    }

    pub fn detail_len(&self) -> usize {
        self.detail_rows().len()
    }

    fn toggle_expand(&mut self) {
        let label = match self.detail_rows().get(self.detail_sel) {
            Some(DetailRow::Group(g)) => g.label.clone(),
            _ => return,
        };
        if !self.expanded.remove(&label) {
            self.expanded.insert(label);
        }
    }

    fn clamp(&mut self) {
        let n_proc = self.visible().len();
        self.sel = self.sel.min(n_proc.saturating_sub(1));
        let n_cg = self.visible_cgroups().len();
        self.cgroup_sel = self.cgroup_sel.min(n_cg.saturating_sub(1));
        let n_members = self.cgroup_members().len();
        self.drill_sel = self.drill_sel.min(n_members.saturating_sub(1));
        let n_apps = self.visible_apps().len();
        self.app_sel = self.app_sel.min(n_apps.saturating_sub(1));
        self.detail_sel = self.detail_sel.min(self.detail_len().saturating_sub(1));
        self.table_state.select(Some(self.sel));
        self.cgroup_state.select(Some(self.cgroup_sel));
        self.app_state.select(Some(self.app_sel));
        self.drill_state.select(Some(self.drill_sel));
        self.detail_state.select(Some(self.detail_sel));
    }

    fn help_page(&self) -> HelpPage {
        match self.view {
            View::Procs if self.detail => HelpPage::Detail,
            View::Procs => HelpPage::Procs,
            View::Cgroups if self.drill.is_some() => HelpPage::CgroupDrill,
            View::Cgroups => HelpPage::Cgroups,
            View::Apps => HelpPage::Apps,
        }
    }

    fn help_scroll(&mut self, delta: i32) {
        if let Some(help) = &mut self.help {
            help.scroll = if delta >= 0 {
                help.scroll.saturating_add(delta as u16)
            } else {
                help.scroll.saturating_sub((-delta) as u16)
            };
        }
    }

    fn cycle_view(&mut self) {
        self.view = match self.view {
            View::Procs => View::Cgroups,
            View::Cgroups => View::Apps,
            View::Apps => View::Procs,
        };
        self.detail = false;
        self.drill = None;
        self.editing = false;
    }

    fn cycle_sort(&mut self) {
        if self.view == View::Apps {
            self.app_metric = self.app_metric.next();
            self.app_sel = 0;
        } else if self.view == View::Cgroups {
            self.cgroup_sort = self.cgroup_sort.next();
            self.cgroup_sel = 0;
        } else {
            self.sort = self.sort.next();
            self.sel = 0;
        }
    }

    fn start_filter(&mut self) {
        self.editing = true;
        self.editing_detail = self.detail;
        self.edit_buf = if self.detail {
            self.detail_filter.clone()
        } else {
            self.filter.clone()
        };
    }

    fn toggle_reverse(&mut self) {
        self.desc = !self.desc;
    }

    fn toggle_theme(&mut self) {
        self.theme = self.theme.next().rendered(self.support);
    }

    fn toggle_lang(&mut self) {
        self.lang = self.lang.toggle();
    }

    fn open_help(&mut self) {
        self.help = Some(HelpState {
            page: self.help_page(),
            scroll: 0,
        });
    }

    fn enter_action(&mut self) {
        if self.view == View::Apps {
            let target = self
                .selected_app()
                .filter(|g| !g.synthetic)
                .map(|g| (g.name.clone(), g.pids.clone()));
            if let Some((name, pids)) = target {
                self.pid_filter = Some(pids.into_iter().collect());
                self.pid_filter_name = Some(name);
                self.view = View::Procs;
                self.sel = 0;
                self.detail = false;
            }
        } else if self.view == View::Cgroups {
            if self.drill.is_none()
                && let Some(path) = self
                    .visible_cgroups()
                    .get(self.cgroup_sel)
                    .map(|c| c.path.clone())
            {
                self.drill = Some(path);
                self.drill_sel = 0;
            }
        } else if self.detail {
            self.toggle_expand();
        } else if self.selected_pid().is_some() {
            self.detail = true;
            self.detail_sel = 0;
        }
    }

    fn escape_action(&mut self) {
        if self.pid_filter.is_some() {
            self.pid_filter = None;
            self.pid_filter_name = None;
            self.sel = 0;
        } else if self.view == View::Cgroups && self.drill.is_some() {
            self.drill = None;
            self.drill_sel = 0;
        } else if self.detail && !self.detail_filter.is_empty() {
            self.detail_filter.clear();
            self.detail_sel = 0;
        } else if self.detail {
            self.detail = false;
            self.detail_sel = 0;
        } else if !self.filter.is_empty() {
            self.filter.clear();
            self.sel = 0;
        }
    }

    fn click_row(&mut self, index: usize) {
        match self.view {
            View::Procs if self.detail => {
                if self.detail_sel == index {
                    self.enter_action();
                } else {
                    self.detail_sel = index;
                }
            }
            View::Procs => {
                if self.sel == index {
                    self.enter_action();
                } else {
                    self.sel = index;
                }
            }
            View::Cgroups if self.drill.is_some() => {
                self.drill_sel = index;
            }
            View::Cgroups => {
                if self.cgroup_sel == index {
                    self.enter_action();
                } else {
                    self.cgroup_sel = index;
                }
            }
            View::Apps => {
                if self.app_sel == index {
                    self.enter_action();
                } else {
                    self.app_sel = index;
                }
            }
        }
    }

    fn click_tile(&mut self, index: usize) {
        if self.app_sel == index {
            self.enter_action();
        } else {
            self.app_sel = index;
        }
    }

    fn footer_action(&mut self, action: FooterAction) -> bool {
        match action {
            FooterAction::Quit => return true,
            FooterAction::Help => self.open_help(),
            FooterAction::Lang => self.toggle_lang(),
            FooterAction::Theme => self.toggle_theme(),
            FooterAction::View => self.cycle_view(),
            FooterAction::Sort => self.cycle_sort(),
            FooterAction::Reverse => self.toggle_reverse(),
            FooterAction::Filter => self.start_filter(),
            FooterAction::Enter => self.enter_action(),
            FooterAction::Back => self.escape_action(),
        }
        self.clamp();
        false
    }

    pub fn on_mouse(&mut self, mouse: MouseEvent) -> bool {
        if self.editing {
            return false;
        }
        if self.help.is_some() {
            match mouse.kind {
                MouseEventKind::ScrollDown => self.help_scroll(3),
                MouseEventKind::ScrollUp => self.help_scroll(-3),
                MouseEventKind::Down(MouseButton::Left) => {
                    let inside = self.hits.iter().any(|hit| {
                        matches!(hit.target, MouseTarget::HelpArea)
                            && mouse.column >= hit.rect.x
                            && mouse.column < hit.rect.x.saturating_add(hit.rect.width)
                            && mouse.row >= hit.rect.y
                            && mouse.row < hit.rect.y.saturating_add(hit.rect.height)
                    });
                    if !inside {
                        self.help = None;
                    }
                }
                _ => {}
            }
            return false;
        }
        match mouse.kind {
            MouseEventKind::ScrollDown => self.move_sel(3),
            MouseEventKind::ScrollUp => self.move_sel(-3),
            MouseEventKind::Down(MouseButton::Left) => {
                let target = self
                    .hits
                    .iter()
                    .rev()
                    .find(|hit| {
                        let r = hit.rect;
                        mouse.column >= r.x
                            && mouse.column < r.x.saturating_add(r.width)
                            && mouse.row >= r.y
                            && mouse.row < r.y.saturating_add(r.height)
                    })
                    .map(|hit| hit.target);
                match target {
                    Some(MouseTarget::Row(index)) => self.click_row(index),
                    Some(MouseTarget::Tile(index)) => self.click_tile(index),
                    Some(MouseTarget::Button(action)) => return self.footer_action(action),
                    Some(MouseTarget::HelpArea) | None => return false,
                }
            }
            _ => return false,
        }
        self.clamp();
        false
    }

    pub fn handle_event(&mut self, event: Event) -> bool {
        match event {
            Event::Key(key) => self.on_key(key),
            Event::Mouse(mouse) => self.on_mouse(mouse),
            _ => false,
        }
    }

    /// Returns true when the app should exit.
    pub fn on_key(&mut self, key: KeyEvent) -> bool {
        if key.kind != KeyEventKind::Press {
            return false;
        }
        if self.help.is_some() {
            match key.code {
                KeyCode::Char('q') => return true,
                KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => return true,
                KeyCode::Esc | KeyCode::Char('?') => self.help = None,
                KeyCode::Char('j') | KeyCode::Down => self.help_scroll(1),
                KeyCode::Char('k') | KeyCode::Up => self.help_scroll(-1),
                KeyCode::PageDown => self.help_scroll(10),
                KeyCode::PageUp => self.help_scroll(-10),
                KeyCode::Char('g') | KeyCode::Home => {
                    if let Some(help) = &mut self.help {
                        help.scroll = 0;
                    }
                }
                KeyCode::Char('G') | KeyCode::End => {
                    if let Some(help) = &mut self.help {
                        help.scroll = u16::MAX;
                    }
                }
                KeyCode::Char('l') => self.lang = self.lang.toggle(),
                _ => {}
            }
            return false;
        }
        if self.editing {
            return self.on_edit_key(key);
        }
        match key.code {
            KeyCode::Char('q') => return true,
            KeyCode::Char('?') => self.open_help(),
            KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => return true,
            KeyCode::Tab => self.cycle_view(),
            KeyCode::Char('j') | KeyCode::Down => self.move_sel(1),
            KeyCode::Char('k') | KeyCode::Up => self.move_sel(-1),
            KeyCode::PageDown => self.move_sel(10),
            KeyCode::PageUp => self.move_sel(-10),
            KeyCode::Char('g') | KeyCode::Home => {
                self.sel = 0;
                self.detail_sel = 0;
                self.cgroup_sel = 0;
                self.drill_sel = 0;
                self.app_sel = 0;
            }
            KeyCode::Char('G') | KeyCode::End => {
                self.sel = usize::MAX / 2;
                self.cgroup_sel = usize::MAX / 2;
                self.drill_sel = usize::MAX / 2;
                self.app_sel = usize::MAX / 2;
            }
            KeyCode::Char('s') => self.cycle_sort(),
            KeyCode::Char('r') => self.toggle_reverse(),
            KeyCode::Char('t') => self.toggle_theme(),
            KeyCode::Char('l') => self.toggle_lang(),
            KeyCode::Char('f') => self.start_filter(),
            KeyCode::Enter | KeyCode::Char(' ') => self.enter_action(),
            KeyCode::Char('z') if self.detail => self.expanded.clear(),
            KeyCode::Char('Z') if self.detail => {
                if let Some(d) = self.snap.as_ref().and_then(|s| s.detail.as_ref()) {
                    self.expanded
                        .extend(d.report.groups.iter().map(|g| g.label.clone()));
                }
            }
            KeyCode::Esc => self.escape_action(),
            _ => {}
        }
        self.clamp();
        false
    }

    fn on_edit_key(&mut self, key: KeyEvent) -> bool {
        match key.code {
            KeyCode::Enter => {
                if self.editing_detail {
                    self.detail_filter = self.edit_buf.clone();
                    self.detail_sel = 0;
                } else {
                    self.filter = self.edit_buf.clone();
                    self.sel = 0;
                }
                self.editing = false;
                self.editing_detail = false;
            }
            KeyCode::Esc => {
                self.editing = false;
                self.editing_detail = false;
                self.edit_buf.clear();
            }
            KeyCode::Backspace => {
                self.edit_buf.pop();
            }
            KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => return true,
            KeyCode::Char(c) => self.edit_buf.push(c),
            _ => {}
        }
        self.clamp();
        false
    }

    fn move_sel(&mut self, delta: isize) {
        if self.view == View::Apps {
            let n = self.visible_apps().len();
            self.app_sel = step(self.app_sel, delta, n);
            return;
        }
        if self.view == View::Cgroups {
            if self.drill.is_some() {
                let n = self.cgroup_members().len();
                self.drill_sel = step(self.drill_sel, delta, n);
            } else {
                let n = self.visible_cgroups().len();
                self.cgroup_sel = step(self.cgroup_sel, delta, n);
            }
            return;
        }
        if self.detail {
            let n = self.detail_len();
            self.detail_sel = step(self.detail_sel, delta, n);
            return;
        }
        let n = self.visible().len();
        self.sel = step(self.sel, delta, n);
    }

    pub fn run(
        terminal: &mut DefaultTerminal,
        start_pid: Option<u32>,
        theme: Theme,
        support: ColorSupport,
        lang: Lang,
        tx: std::sync::mpsc::Sender<Focus>,
        rx: std::sync::mpsc::Receiver<Snapshot>,
    ) -> std::io::Result<()> {
        let mut app = App::new(start_pid);
        app.support = support;
        app.theme = theme.rendered(support);
        app.lang = lang;
        loop {
            let mut got = false;
            while let Ok(snap) = rx.try_recv() {
                app.on_snapshot(snap);
                got = true;
            }
            if got || app.started {
                terminal.draw(|f| ui::draw(f, &mut app))?;
                app.started = true;
            }
            if let Some(focus) = app.take_focus_update() {
                let _ = tx.send(focus);
            }
            if event::poll(Duration::from_millis(250))? {
                if app.handle_event(event::read()?) {
                    return Ok(());
                }
                while event::poll(Duration::ZERO)? {
                    if app.handle_event(event::read()?) {
                        return Ok(());
                    }
                }
                if let Some(focus) = app.take_focus_update() {
                    let _ = tx.send(focus);
                }
            }
        }
    }

    pub fn sample_age_ms(&self) -> u128 {
        self.snap
            .as_ref()
            .map(|s| s.at.elapsed().as_millis())
            .unwrap_or(0)
    }
}

fn step(cur: usize, delta: isize, n: usize) -> usize {
    if n == 0 {
        return 0;
    }
    let max = n - 1;
    if delta >= 0 {
        (cur + delta as usize).min(max)
    } else {
        cur.saturating_sub((-delta) as usize)
    }
}