This commit extracts Tag, Span, Text, as well as source-related debug facilities into a new crate called nu_source. This change is much bigger than one might have expected because the previous code relied heavily on implementing inherent methods on `Tagged<T>` and `Spanned<T>`, which is no longer possible. As a result, this change creates more concrete types instead of using `Tagged<T>`. One notable example: Tagged<Value> became Value, and Value became UntaggedValue. This change clarifies the intent of the code in many places, but it does make it a big change.
30 lines
855 B
Rust
30 lines
855 B
Rust
use crate::data::{TaggedDictBuilder, Value};
|
|
use crate::prelude::*;
|
|
use itertools::join;
|
|
use sysinfo::ProcessExt;
|
|
|
|
pub(crate) fn process_dict(proc: &sysinfo::Process, tag: impl Into<Tag>) -> Value {
|
|
let mut dict = TaggedDictBuilder::new(tag);
|
|
|
|
let cmd = proc.cmd();
|
|
|
|
let cmd_value = if cmd.len() == 0 {
|
|
UntaggedValue::nothing()
|
|
} else {
|
|
UntaggedValue::string(join(cmd, ""))
|
|
};
|
|
|
|
dict.insert("pid", UntaggedValue::int(proc.pid() as i64));
|
|
dict.insert("status", UntaggedValue::string(proc.status().to_string()));
|
|
dict.insert("cpu", UntaggedValue::number(proc.cpu_usage()));
|
|
|
|
match cmd_value {
|
|
UntaggedValue::Primitive(Primitive::Nothing) => {
|
|
dict.insert("name", UntaggedValue::string(proc.name()));
|
|
}
|
|
_ => dict.insert("name", cmd_value),
|
|
}
|
|
|
|
dict.into_value()
|
|
}
|