dkl cg ls: more cols + customizable list
This commit is contained in:
+24
-10
@@ -8,17 +8,36 @@ use crate::{fs, human::Human};
|
||||
|
||||
pub const ROOT: &str = "/sys/fs/cgroup";
|
||||
|
||||
pub async fn ls(parent: Option<impl AsRef<StdPath>>, exclude: &[String]) -> fs::Result<()> {
|
||||
pub async fn ls(
|
||||
parent: Option<impl AsRef<StdPath>>,
|
||||
exclude: &[String],
|
||||
columns: Option<&str>,
|
||||
) -> fs::Result<()> {
|
||||
let mut root = PathBuf::from(ROOT);
|
||||
if let Some(parent) = parent {
|
||||
root = root.join(parent);
|
||||
}
|
||||
|
||||
let mut todo = vec![(Cgroup::root(root).await?, vec![], true)];
|
||||
let cols: [(&str, fn(&Cgroup) -> String); _] = [
|
||||
("wset", |cg| cg.memory.working_set().human()),
|
||||
("anon", |cg| cg.memory.stat.anon.human()),
|
||||
("min", |cg| cg.memory.min.human()),
|
||||
("low", |cg| cg.memory.low.human()),
|
||||
("high", |cg| cg.memory.high.human()),
|
||||
("max", |cg| cg.memory.max.human()),
|
||||
];
|
||||
let cols = if let Some(columns) = columns {
|
||||
(cols.into_iter())
|
||||
.filter(|(n, _)| columns.split(',').any(|col| &col == n))
|
||||
.collect()
|
||||
} else {
|
||||
cols.to_vec()
|
||||
};
|
||||
|
||||
let mut table = tabled::builder::Builder::new();
|
||||
table.push_record(["cgroup", "workg set", "anon", "max"]);
|
||||
table.push_record(["cgroup"].into_iter().chain(cols.iter().map(|(n, _)| *n)));
|
||||
|
||||
let mut todo = vec![(Cgroup::root(root).await?, vec![], true)];
|
||||
while let Some((cg, p_lasts, last)) = todo.pop() {
|
||||
let mut name = String::new();
|
||||
for last in p_lasts.iter().skip(1) {
|
||||
@@ -29,12 +48,7 @@ pub async fn ls(parent: Option<impl AsRef<StdPath>>, exclude: &[String]) -> fs::
|
||||
}
|
||||
name.push_str(&cg.name());
|
||||
|
||||
table.push_record([
|
||||
name,
|
||||
cg.memory.working_set().human(),
|
||||
cg.memory.stat.anon.human(),
|
||||
cg.memory.max.human(),
|
||||
]);
|
||||
table.push_record([name].into_iter().chain(cols.iter().map(|(_, f)| f(&cg))));
|
||||
|
||||
let mut p_lasts = p_lasts.clone();
|
||||
p_lasts.push(last);
|
||||
@@ -50,8 +64,8 @@ pub async fn ls(parent: Option<impl AsRef<StdPath>>, exclude: &[String]) -> fs::
|
||||
}
|
||||
|
||||
use tabled::settings::{
|
||||
Alignment, Modify,
|
||||
object::{Column, Row},
|
||||
Alignment, Modify,
|
||||
};
|
||||
let mut table = table.build();
|
||||
table.with(tabled::settings::Style::psql());
|
||||
|
||||
Reference in New Issue
Block a user