Skip to content

Commit 0c71dec

Browse files
committed
top: tui impl ^g (show cgroup)
1 parent 9264e6a commit 0c71dec

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/uu/top/src/picker.rs

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

435+
pub(crate) fn get_cgroup(pid: u32) -> String {
436+
let path = PathBuf::from_str(&format!("/proc/{pid}/cgroup")).unwrap();
437+
if let Ok(file) = File::open(path) {
438+
read_to_string(file).unwrap()
439+
} else {
440+
String::new()
441+
}
442+
}
443+
435444
pub(crate) fn get_command(pid: u32, full_command_line: bool) -> String {
436445
let f = |cmd: &[OsString]| -> String {
437446
let binding = cmd

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

Lines changed: 29 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_command;
7+
use crate::picker::{get_cgroup, 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,34 @@ 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+
Event::Key(KeyEvent {
102+
code: KeyCode::Char('g'),
103+
modifiers: KeyModifiers::CONTROL,
104+
..
105+
}) => {
106+
let mut data = data.write().unwrap();
107+
if data.2.is_some() {
108+
data.2 = None;
109+
} else {
110+
let tui_stat = tui_stat.read().unwrap();
111+
let mut nth = tui_stat.list_offset;
112+
if data.1.collected.is_empty() {
113+
return false;
114+
}
115+
if data.1.collected.len() <= nth {
116+
nth = data.1.collected.len() - 1;
117+
}
118+
let pid = data.1.collected[nth].0;
119+
let title = format!(
120+
"control groups for pid {}, {}",
121+
pid,
122+
get_command(pid, false)
123+
);
124+
let content = get_cgroup(pid);
125+
data.2 = Some(InfoBar { title, content });
126+
}
127+
should_update.store(true, Ordering::Relaxed);
128+
}
101129
char!('I') => {
102130
{
103131
let mut stat = tui_stat.write().unwrap();
@@ -111,7 +139,6 @@ pub fn handle_input(
111139
data.write().unwrap().1 = ProcList::new(settings, &tui_stat.read().unwrap());
112140
should_update.store(true, Ordering::Relaxed);
113141
}
114-
115142
Event::Key(KeyEvent {
116143
code: KeyCode::Char('k'),
117144
modifiers: KeyModifiers::CONTROL,

0 commit comments

Comments
 (0)