Skip to content

Commit 3aeeb2c

Browse files
committed
top: tui impl ^u (show supp group)
1 parent b133d9c commit 3aeeb2c

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

src/uu/top/src/picker.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,33 @@ fn mem(pid: u32, _stat: Stat) -> Box<dyn Column> {
432432
)
433433
}
434434

435+
#[cfg(target_os = "linux")]
436+
pub(crate) fn get_supplementary_groups(pid: u32) -> String {
437+
use sysinfo::{Gid, Groups};
438+
let groups = Groups::new_with_refreshed_list();
439+
let path = PathBuf::from_str(&format!("/proc/{pid}/status")).unwrap();
440+
if let Ok(file) = File::open(path) {
441+
let content = read_to_string(file).unwrap();
442+
for line in content.lines() {
443+
if line.starts_with("Groups:") {
444+
let groups = line
445+
.split_whitespace()
446+
.skip(1)
447+
.filter_map(|s| Gid::from_str(s).ok())
448+
.filter_map(|gid| groups.iter().find(|g| g.id() == &gid))
449+
.map(|group| group.name())
450+
.collect::<Vec<_>>()
451+
.join(",");
452+
return groups;
453+
}
454+
}
455+
String::new()
456+
} else {
457+
String::new()
458+
}
459+
}
460+
461+
#[cfg(target_os = "linux")]
435462
pub(crate) fn get_cgroup(pid: u32) -> String {
436463
let path = PathBuf::from_str(&format!("/proc/{pid}/cgroup")).unwrap();
437464
if let Ok(file) = File::open(path) {

src/uu/top/src/tui/input.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// file that was distributed with this source code.
55

66
use crate::header::Header;
7-
use crate::picker::{get_cgroup, get_command};
7+
use crate::picker::get_command;
88
use crate::platform::get_numa_nodes;
99
use crate::tui::stat::{CpuValueMode, TuiStat};
1010
use crate::Filter::{EUser, User};
@@ -98,6 +98,7 @@ pub fn handle_input(
9898
data.write().unwrap().1 = ProcList::new(settings, &tui_stat.read().unwrap());
9999
should_update.store(true, Ordering::Relaxed);
100100
}
101+
#[cfg(target_os = "linux")]
101102
Event::Key(KeyEvent {
102103
code: KeyCode::Char('g'),
103104
modifiers: KeyModifiers::CONTROL,
@@ -121,7 +122,7 @@ pub fn handle_input(
121122
pid,
122123
get_command(pid, false)
123124
);
124-
let content = get_cgroup(pid);
125+
let content = crate::picker::get_cgroup(pid);
125126
data.2 = Some(InfoBar { title, content });
126127
}
127128
should_update.store(true, Ordering::Relaxed);
@@ -199,6 +200,35 @@ pub fn handle_input(
199200
stat.cpu_graph_mode = stat.cpu_graph_mode.next();
200201
should_update.store(true, Ordering::Relaxed);
201202
}
203+
#[cfg(target_os = "linux")]
204+
Event::Key(KeyEvent {
205+
code: KeyCode::Char('u'),
206+
modifiers: KeyModifiers::CONTROL,
207+
..
208+
}) => {
209+
let mut data = data.write().unwrap();
210+
if data.2.is_some() {
211+
data.2 = None;
212+
} else {
213+
let tui_stat = tui_stat.read().unwrap();
214+
let mut nth = tui_stat.list_offset;
215+
if data.1.collected.is_empty() {
216+
return false;
217+
}
218+
if data.1.collected.len() <= nth {
219+
nth = data.1.collected.len() - 1;
220+
}
221+
let pid = data.1.collected[nth].0;
222+
let title = format!(
223+
"supplementary groups for pid {}, {}",
224+
pid,
225+
get_command(pid, false)
226+
);
227+
let content = crate::picker::get_supplementary_groups(pid);
228+
data.2 = Some(InfoBar { title, content });
229+
}
230+
should_update.store(true, Ordering::Relaxed);
231+
}
202232
char!('U') => {
203233
let mut stat = tui_stat.write().unwrap();
204234
stat.input_label = "Which user (blank for all) ".into();

0 commit comments

Comments
 (0)