Revert "Rewrite the ps command"

This commit is contained in:
Jonathan Turner
2019-08-11 13:41:21 +12:00
committed by GitHub
parent 2fe7f76219
commit e19c618ac5
8 changed files with 70 additions and 75 deletions

View File

@ -152,6 +152,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
command("from-json", Box::new(from_json::from_json)),
command("from-toml", Box::new(from_toml::from_toml)),
command("from-xml", Box::new(from_xml::from_xml)),
command("ps", Box::new(ps::ps)),
command("ls", Box::new(ls::ls)),
command("cd", Box::new(cd::cd)),
command("size", Box::new(size::size)),

View File

@ -28,6 +28,7 @@ crate mod open;
crate mod pick;
crate mod plugin;
crate mod prev;
crate mod ps;
crate mod reject;
crate mod rm;
crate mod save;

17
src/commands/ps.rs Normal file
View File

@ -0,0 +1,17 @@
use crate::errors::ShellError;
use crate::object::process::process_dict;
use crate::prelude::*;
use sysinfo::{RefreshKind, SystemExt};
pub fn ps(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let mut system = sysinfo::System::new_with_specifics(RefreshKind::new().with_processes());
system.refresh_processes();
let list = system.get_process_list();
let list = list
.into_iter()
.map(|(_, process)| process_dict(process, Tag::unknown_origin(args.call_info.name_span)))
.collect::<VecDeque<_>>();
Ok(list.from_input_stream())
}

View File

@ -4,6 +4,7 @@ crate mod dict;
crate mod files;
crate mod into;
crate mod meta;
crate mod process;
crate mod types;
#[allow(unused)]

29
src/object/process.rs Normal file
View File

@ -0,0 +1,29 @@
use crate::object::{TaggedDictBuilder, Value};
use crate::prelude::*;
use itertools::join;
use sysinfo::ProcessExt;
crate fn process_dict(proc: &sysinfo::Process, tag: impl Into<Tag>) -> Tagged<Value> {
let mut dict = TaggedDictBuilder::new(tag);
let cmd = proc.cmd();
let cmd_value = if cmd.len() == 0 {
Value::nothing()
} else {
Value::string(join(cmd, ""))
};
dict.insert("pid", Value::int(proc.pid() as i64));
dict.insert("status", Value::string(proc.status().to_string()));
dict.insert("cpu", Value::float(proc.cpu_usage() as f64));
//dict.insert("name", Value::string(proc.name()));
match cmd_value {
Value::Primitive(Primitive::Nothing) => {
dict.insert("name", Value::string(proc.name()));
}
_ => dict.insert("name", cmd_value),
}
dict.into_tagged_value()
}

View File

@ -1,71 +0,0 @@
#![feature(async_await)]
use futures::executor::block_on;
use futures::stream::StreamExt;
use heim::process;
use indexmap::IndexMap;
use nu::{
serve_plugin, CallInfo, Plugin, ReturnSuccess, ReturnValue, ShellError, Signature, Tag, Tagged,
TaggedDictBuilder, Value,
};
struct Ps;
impl Ps {
fn new() -> Ps {
Ps
}
}
async fn ps(tag: Tag) -> Vec<Tagged<Value>> {
let mut output = vec![];
let mut process = process::processes();
while let Some(process) = process.next().await {
if let Ok(process) = process {
let mut dict = TaggedDictBuilder::new(tag);
dict.insert("pid", Value::int(process.pid() as i64));
if let Ok(parent) = process.parent_pid().await {
dict.insert("parent", Value::int(parent as i64));
}
if let Ok(status) = process.status().await {
dict.insert("status", Value::string(format!("{:?}", status)));
}
if let Ok(name) = process.name().await {
dict.insert("name", Value::string(name));
}
if let Ok(exe) = process.exe().await {
dict.insert("exe", Value::string(exe.to_string_lossy().to_string()));
}
output.push(dict.into_tagged_value());
}
}
output
}
impl Plugin for Ps {
fn config(&mut self) -> Result<Signature, ShellError> {
Ok(Signature {
name: "ps".to_string(),
positional: vec![],
is_filter: true,
named: IndexMap::new(),
rest_positional: true,
})
}
fn begin_filter(&mut self, callinfo: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
Ok(block_on(ps(Tag::unknown_origin(callinfo.name_span)))
.into_iter()
.map(|x| ReturnSuccess::value(x))
.collect())
}
fn filter(&mut self, _: Tagged<Value>) -> Result<Vec<ReturnValue>, ShellError> {
Ok(vec![])
}
}
fn main() {
serve_plugin(&mut Ps::new());
}