1use nu_errors::ShellError;
2use nu_protocol::{TaggedDictBuilder, UntaggedValue, Value};
3use nu_source::Tag;
4use sysinfo::{ProcessExt, System, SystemExt};
5
6#[derive(Default)]
7pub struct Ps;
8
9impl Ps {
10 pub fn new() -> Ps {
11 Ps
12 }
13}
14
15pub async fn ps(tag: Tag, long: bool) -> Result<Vec<Value>, ShellError> {
16 let mut sys = System::new_all();
17 sys.refresh_all();
18
19 let mut output = vec![];
20
21 let result: Vec<_> = sys.get_processes().iter().map(|x| *x.0).collect();
22
23 for pid in result.into_iter() {
24 if let Some(result) = sys.get_process(pid) {
25 let mut dict = TaggedDictBuilder::new(&tag);
26 dict.insert_untagged("pid", UntaggedValue::int(pid as i64));
27 dict.insert_untagged("name", UntaggedValue::string(result.name()));
28 dict.insert_untagged(
29 "status",
30 UntaggedValue::string(format!("{:?}", result.status())),
31 );
32 dict.insert_untagged(
33 "cpu",
34 UntaggedValue::decimal_from_float(result.cpu_usage() as f64, tag.span),
35 );
36 dict.insert_untagged("mem", UntaggedValue::filesize(result.memory() * 1000));
37 dict.insert_untagged(
38 "virtual",
39 UntaggedValue::filesize(result.virtual_memory() * 1000),
40 );
41
42 if long {
43 if let Some(parent) = result.parent() {
44 dict.insert_untagged("parent", UntaggedValue::int(parent as i64));
45 } else {
46 dict.insert_untagged("parent", UntaggedValue::nothing());
47 }
48 dict.insert_untagged("exe", UntaggedValue::filepath(result.exe()));
49 dict.insert_untagged("command", UntaggedValue::string(result.cmd().join(" ")));
50 }
51
52 output.push(dict.into_value());
53 }
54 }
55
56 Ok(output)
57}