nushell/src/commands/ps.rs

50 lines
1.2 KiB
Rust
Raw Normal View History

2019-05-10 18:59:12 +02:00
use crate::errors::ShellError;
use crate::object::process::Process;
use crate::object::{ShellObject, Value};
2019-05-11 10:08:21 +02:00
use crate::Command;
2019-05-10 18:59:12 +02:00
use derive_new::new;
2019-05-11 10:08:21 +02:00
use std::cell::RefCell;
use std::rc::Rc;
2019-05-10 18:59:12 +02:00
use sysinfo::SystemExt;
2019-05-11 10:08:21 +02:00
#[derive(new)]
pub struct PsBlueprint {
system: Rc<RefCell<sysinfo::System>>,
}
impl crate::CommandBlueprint for PsBlueprint {
fn create(
&self,
2019-05-12 00:59:57 +02:00
args: crate::Args,
2019-05-11 10:08:21 +02:00
host: &dyn crate::Host,
env: &mut crate::Environment,
2019-05-12 00:59:57 +02:00
) -> Result<Box<dyn Command>, ShellError> {
Ok(Box::new(Ps::new(self.system.clone())))
2019-05-11 10:08:21 +02:00
}
}
2019-05-10 18:59:12 +02:00
#[derive(new)]
pub struct Ps {
2019-05-11 10:08:21 +02:00
system: Rc<RefCell<sysinfo::System>>,
2019-05-10 18:59:12 +02:00
}
impl crate::Command for Ps {
2019-05-11 10:08:21 +02:00
fn run(&mut self) -> Result<crate::CommandSuccess, ShellError> {
let mut system = self.system.borrow_mut();
system.refresh_all();
2019-05-10 18:59:12 +02:00
2019-05-11 10:08:21 +02:00
let list = system.get_process_list();
2019-05-10 18:59:12 +02:00
let list = list
.into_iter()
.map(|(_, process)| Value::Object(Box::new(Process::new(process.clone()))))
.take(5)
.collect();
2019-05-11 10:08:21 +02:00
Ok(crate::CommandSuccess {
value: Value::List(list),
action: vec![],
})
2019-05-10 18:59:12 +02:00
}
}