use crate::context::Context;
use crate::host::Listed;
pub(crate) enum Open {
No,
Yes(Vec<Listed>),
Unknown(String),
}
pub(crate) fn open(ctx: &Context, holds: impl Fn(&Listed, &[Listed]) -> bool) -> Open {
let all = match ctx.host().listing() {
Ok(all) => all,
Err(why) => return Open::Unknown(why),
};
let running: Vec<Listed> = all.iter().filter(|one| holds(one, &all)).cloned().collect();
if running.is_empty() {
Open::No
} else {
Open::Yes(running)
}
}
pub(crate) fn named(listed: &[Listed]) -> Vec<String> {
listed
.iter()
.map(|one| {
let name = one.path.file_name().unwrap_or(one.path.as_os_str());
format!("{} (pid {})", name.to_string_lossy(), one.pid)
})
.collect()
}