Skip to content

Commit 04dd0b5

Browse files
committed
Processes tab: Copy process tree; show the cgroup tree right after switching
"Copy process tree" puts what's listed, in the current grouping, on the clipboard as aligned plain text: each app or cgroup with every process, its memory, CPU, running time, pid and (shortened) command. Switching to "cgroup" is a click, so the pointer is in the window and rows were held: the tree was never built and the view stayed empty until the pointer left. It's now built whenever it's empty, and cleared when the grouping changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018m3cDLt91kYSuwngoPZTxn
1 parent fce4bc3 commit 04dd0b5

2 files changed

Lines changed: 173 additions & 1 deletion

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ On Ubuntu, Linux Mint and other Ubuntu-based systems, the settings window has tw
146146
- **Left running in the background**, listed first: programs with no window whose terminal, Claude session or IDE has already closed (old dev servers, Gradle daemons, scripts stuck in a loop). **Close all of these** clears them in one go.
147147
- **Show** brings an app's window to the front, so you can check before closing. **Close** asks a whole app to quit; **End** stops one process (for a Chrome tab: that tab shows "Aw, Snap!" and can be reloaded; which tab is which is only visible in Chrome's own Task Manager, <kbd>Shift</kbd>+<kbd>Esc</kbd>). If something doesn't quit within 5 seconds, **Force quit** appears.
148148
- **Group by cgroup** shows the same processes as a tree of the kernel's control groups instead: which systemd service, scope or slice each one runs in (apps started from the menu usually get their own `app-….scope`, snaps and Flatpaks theirs). Groups using 100 MB or more open expanded. Memory is what the kernel counts for the group, which includes file cache.
149+
- **Copy process tree** puts what's listed, grouped as you see it, on the clipboard as plain text: every process with its memory, CPU, running time, process id and command. Handy for pasting into a chat or a bug report; commands can contain paths or tokens, so look before you share.
149150
- Memory and CPU keep updating, but while the mouse pointer is in the window the rows stay where they are, so a click always hits what you aimed at. A program that quits meanwhile shows "ended"; new ones appear when the pointer leaves.
150151

151152
**Uninstall an app you no longer need**, without remembering how you installed it: click **Uninstall…** on its row. appkey works out how it was installed and removes it the matching way:

‎src/ui/processes.rs‎

Lines changed: 172 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,9 @@ impl ProcessesTab {
262262
/// Rebuilds the cgroup tree, unless rows are held (then only the kernel's numbers
263263
/// change; [`Node::recount`] updates the sums).
264264
fn refresh_tree(&mut self, list: &[Proc]) {
265-
if !(self.holding && self.refreshes > 2) {
265+
// Built right away when the view was just switched on (the pointer is on the
266+
// switch, so rows would otherwise be held empty).
267+
if self.tree.is_empty() || !(self.holding && self.refreshes > 2) {
266268
let items: Vec<(u32, &str, u64)> = list
267269
.iter()
268270
.filter(|p| p.uid == self.uid && !p.cgroup.is_empty())
@@ -858,6 +860,100 @@ impl ProcessesTab {
858860
s
859861
}
860862

863+
/// What the tab shows, in the current grouping, as plain text to paste somewhere:
864+
/// every process of each listed app (or cgroup), with its numbers, pid and command.
865+
fn tree_text(&self) -> String {
866+
let mut out = String::new();
867+
let grouping = if self.group_by == GroupBy::Cgroup { "cgroup" } else { "app" };
868+
let _ = write!(out, "Processes by {grouping}");
869+
if let Some(s) = &self.snapshot {
870+
let _ = write!(
871+
out,
872+
", {} of {} free",
873+
memory::format_kib(s.mem.available_kib),
874+
memory::format_kib(s.mem.total_kib)
875+
);
876+
}
877+
out.push('\n');
878+
let mut rows = Vec::new();
879+
if self.group_by == GroupBy::Cgroup {
880+
out.push('\n');
881+
for n in &self.tree {
882+
self.cgroup_text(&mut rows, n, "", "");
883+
}
884+
} else {
885+
for (title, groups) in
886+
[("Left running in the background", &self.background), ("Apps", &self.apps)]
887+
{
888+
if groups.is_empty() {
889+
continue;
890+
}
891+
rows.push(Row::heading(title));
892+
for g in groups {
893+
let cpu = cpu_text(self.group_cpu(g));
894+
let mut name = format!("{} ({})", g.label, g.members.len());
895+
if !g.detail.is_empty() {
896+
let _ = write!(name, " {}", truncate(&g.detail, 60));
897+
}
898+
rows.push(Row::new(name, memory::format_kib(g.rss_kib), cpu, String::new()));
899+
for (i, &pid) in g.members.iter().enumerate() {
900+
let last = i + 1 == g.members.len();
901+
rows.extend(self.proc_text(pid, if last { "└─ " } else { "├─ " }));
902+
}
903+
}
904+
}
905+
rows.push(Row::heading(&format!(
906+
"(apps under {} and idle are not listed)",
907+
memory::format_kib(MIN_KIB)
908+
)));
909+
}
910+
out.push_str(&align_rows(&rows));
911+
out
912+
}
913+
914+
fn cgroup_text(&self, rows: &mut Vec<Row>, n: &Node, prefix: &str, branch: &str) {
915+
let kernel = self.cgroup_memory.get(&n.path).copied().unwrap_or_default();
916+
let used = kernel.current.map_or(n.rss_kib, |b| b / 1024);
917+
let mut name = format!("{prefix}{branch}{} ({})", n.name, n.count);
918+
if let Some(limit) = limit_text(kernel) {
919+
let _ = write!(name, " [{limit}]");
920+
}
921+
rows.push(Row::new(
922+
name,
923+
memory::format_kib(used),
924+
cpu_text(self.node_cpu(n)),
925+
String::new(),
926+
));
927+
// What's below this node lines up under its name.
928+
let below = match branch {
929+
"├─ " => format!("{prefix}│ "),
930+
"└─ " => format!("{prefix} "),
931+
_ => prefix.to_owned(),
932+
};
933+
let count = n.children.len() + n.procs.len();
934+
for (i, c) in n.children.iter().enumerate() {
935+
self.cgroup_text(rows, c, &below, if i + 1 == count { "└─ " } else { "├─ " });
936+
}
937+
for (i, &pid) in n.procs.iter().enumerate() {
938+
let last = n.children.len() + i + 1 == count;
939+
rows.extend(
940+
self.proc_text(pid, &format!("{below}{}", if last { "└─ " } else { "├─ " })),
941+
);
942+
}
943+
}
944+
945+
fn proc_text(&self, pid: u32, prefix: &str) -> Option<Row> {
946+
let p = self.procs.get(&pid)?;
947+
let mut name = format!("{prefix}{}", procs::describe(p));
948+
if self.ended.contains(&pid) {
949+
name.push_str(" (ended)");
950+
}
951+
let cpu = cpu_text(self.cpu.get(&pid).copied().unwrap_or(0.0));
952+
let mut row = Row::new(name, memory::format_kib(p.rss_kib), cpu, self.age_text(pid));
953+
row.extra = format!("pid {pid} {}", truncate(&p.cmdline.join(" "), 120));
954+
Some(row)
955+
}
956+
861957
/// Close selected, sorting and grouping, and Dismiss / Snooze while warning.
862958
pub fn footer(&mut self, ui: &mut egui::Ui) -> Option<Answer> {
863959
let mut action = None;
@@ -889,6 +985,7 @@ impl ProcessesTab {
889985
Apps started with the memory limit on share one slice.",
890986
);
891987
if self.group_by != before {
988+
self.tree.clear();
892989
self.refresh();
893990
}
894991
ui.separator();
@@ -903,6 +1000,20 @@ impl ProcessesTab {
9031000
self.sort_groups(&mut a);
9041001
(self.background, self.apps) = (b, a);
9051002
}
1003+
ui.separator();
1004+
if ui
1005+
.button("Copy process tree")
1006+
.on_hover_text(
1007+
"Copies what's listed, as grouped now, as text: names, memory, CPU, pids \
1008+
and commands. Commands can contain paths or tokens; check before sharing.",
1009+
)
1010+
.clicked()
1011+
{
1012+
let text = self.tree_text();
1013+
let lines = text.lines().count();
1014+
ui.ctx().copy_text(text);
1015+
self.status = Some(format!("Copied the process tree ({lines} lines)."));
1016+
}
9061017
if self.mode.is_some() {
9071018
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
9081019
if ui.button("Dismiss").clicked() {
@@ -952,6 +1063,52 @@ fn limit_text(m: CgroupMemory) -> Option<String> {
9521063
(!parts.is_empty()).then(|| parts.join(", "))
9531064
}
9541065

1066+
/// One line of the copied process tree.
1067+
struct Row {
1068+
name: String,
1069+
memory: String,
1070+
cpu: String,
1071+
age: String,
1072+
extra: String,
1073+
heading: bool,
1074+
}
1075+
1076+
impl Row {
1077+
fn new(name: String, memory: String, cpu: String, age: String) -> Self {
1078+
Self { name, memory, cpu, age, extra: String::new(), heading: false }
1079+
}
1080+
1081+
fn heading(text: &str) -> Self {
1082+
Self {
1083+
heading: true,
1084+
..Self::new(text.to_owned(), String::new(), String::new(), String::new())
1085+
}
1086+
}
1087+
}
1088+
1089+
/// The rows as text, with the number columns lined up after the longest name.
1090+
fn align_rows(rows: &[Row]) -> String {
1091+
let width = |f: fn(&Row) -> &str| {
1092+
rows.iter().filter(|r| !r.heading).map(|r| f(r).chars().count()).max().unwrap_or(0)
1093+
};
1094+
let (name_w, mem_w, cpu_w, age_w) =
1095+
(width(|r| &r.name), width(|r| &r.memory), width(|r| &r.cpu), width(|r| &r.age));
1096+
let mut out = String::new();
1097+
for r in rows {
1098+
if r.heading {
1099+
let _ = writeln!(out, "\n{}", r.name);
1100+
continue;
1101+
}
1102+
let pad = name_w - r.name.chars().count();
1103+
let _ = writeln!(
1104+
out,
1105+
"{}{:pad$} {:>mem_w$} {:>cpu_w$} {:>age_w$} {}",
1106+
r.name, "", r.memory, r.cpu, r.age, r.extra
1107+
);
1108+
}
1109+
out.lines().map(str::trim_end).collect::<Vec<_>>().join("\n") + "\n"
1110+
}
1111+
9551112
fn cpu_text(pct: f32) -> String {
9561113
if pct < 0.5 { "–".into() } else { format!("{pct:.0}% CPU") }
9571114
}
@@ -1195,6 +1352,20 @@ mod tests {
11951352
}
11961353
}
11971354

1355+
#[test]
1356+
fn copied_rows_line_up() {
1357+
let mut rows = vec![
1358+
Row::heading("Apps"),
1359+
Row::new("Chrome (2)".into(), "1.2 GB".into(), "5% CPU".into(), String::new()),
1360+
Row::new("└─ tab".into(), "300 MB".into(), "–".into(), "2 min".into()),
1361+
];
1362+
rows[2].extra = "pid 7 chrome --type=renderer".into();
1363+
assert_eq!(
1364+
align_rows(&rows),
1365+
"\nApps\nChrome (2) 1.2 GB 5% CPU\n└─ tab 300 MB – 2 min pid 7 chrome --type=renderer\n"
1366+
);
1367+
}
1368+
11981369
#[test]
11991370
fn held_rows_update_numbers_but_not_layout() {
12001371
let listed = [group(1, &[1, 2, 3], 100), group(5, &[5], 50)];

0 commit comments

Comments
 (0)