mirror of
https://github.com/nushell/nushell
synced 2024-12-28 14:03:09 +00:00
Merge pull request #271 from jonathandturner/rewrite_ps
Rewrite the ps command
This commit is contained in:
commit
2fe7f76219
8 changed files with 75 additions and 70 deletions
20
Cargo.lock
generated
20
Cargo.lock
generated
|
@ -768,11 +768,6 @@ dependencies = [
|
||||||
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "doc-comment"
|
|
||||||
version = "0.3.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "dtoa"
|
name = "dtoa"
|
||||||
version = "0.4.4"
|
version = "0.4.4"
|
||||||
|
@ -1965,7 +1960,6 @@ dependencies = [
|
||||||
"strip-ansi-escapes 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"strip-ansi-escapes 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"subprocess 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)",
|
"subprocess 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"syntect 3.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"syntect 3.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"sysinfo 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
||||||
"tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
"tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"term 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"term 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
@ -3096,18 +3090,6 @@ dependencies = [
|
||||||
"yaml-rust 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
"yaml-rust 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "sysinfo"
|
|
||||||
version = "0.9.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
dependencies = [
|
|
||||||
"cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
||||||
"doc-comment 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
||||||
"libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
||||||
"rayon 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
||||||
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tempdir"
|
name = "tempdir"
|
||||||
version = "0.3.7"
|
version = "0.3.7"
|
||||||
|
@ -3791,7 +3773,6 @@ dependencies = [
|
||||||
"checksum dirs 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3fd78930633bd1c6e35c4b42b1df7b0cbc6bc191146e512bb3bedf243fcc3901"
|
"checksum dirs 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3fd78930633bd1c6e35c4b42b1df7b0cbc6bc191146e512bb3bedf243fcc3901"
|
||||||
"checksum dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "13aea89a5c93364a98e9b37b2fa237effbb694d5cfe01c5b70941f7eb087d5e3"
|
"checksum dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "13aea89a5c93364a98e9b37b2fa237effbb694d5cfe01c5b70941f7eb087d5e3"
|
||||||
"checksum dirs-sys 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "afa0b23de8fd801745c471deffa6e12d248f962c9fd4b4c33787b055599bde7b"
|
"checksum dirs-sys 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "afa0b23de8fd801745c471deffa6e12d248f962c9fd4b4c33787b055599bde7b"
|
||||||
"checksum doc-comment 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "923dea538cea0aa3025e8685b20d6ee21ef99c4f77e954a30febbaac5ec73a97"
|
|
||||||
"checksum dtoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "ea57b42383d091c85abcc2706240b94ab2a8fa1fc81c10ff23c4de06e2a90b5e"
|
"checksum dtoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "ea57b42383d091c85abcc2706240b94ab2a8fa1fc81c10ff23c4de06e2a90b5e"
|
||||||
"checksum dunce 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d0ad6bf6a88548d1126045c413548df1453d9be094a8ab9fd59bf1fdd338da4f"
|
"checksum dunce 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d0ad6bf6a88548d1126045c413548df1453d9be094a8ab9fd59bf1fdd338da4f"
|
||||||
"checksum either 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5527cfe0d098f36e3f8839852688e63c8fff1c90b2b405aef730615f9a7bcf7b"
|
"checksum either 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5527cfe0d098f36e3f8839852688e63c8fff1c90b2b405aef730615f9a7bcf7b"
|
||||||
|
@ -4035,7 +4016,6 @@ dependencies = [
|
||||||
"checksum syn 0.15.43 (registry+https://github.com/rust-lang/crates.io-index)" = "ee06ea4b620ab59a2267c6b48be16244a3389f8bfa0986bdd15c35b890b00af3"
|
"checksum syn 0.15.43 (registry+https://github.com/rust-lang/crates.io-index)" = "ee06ea4b620ab59a2267c6b48be16244a3389f8bfa0986bdd15c35b890b00af3"
|
||||||
"checksum synstructure 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "02353edf96d6e4dc81aea2d8490a7e9db177bf8acb0e951c24940bf866cb313f"
|
"checksum synstructure 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "02353edf96d6e4dc81aea2d8490a7e9db177bf8acb0e951c24940bf866cb313f"
|
||||||
"checksum syntect 3.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e80b8831c5a543192ffc3727f01cf0e57579c6ac15558e3048bfb5708892167b"
|
"checksum syntect 3.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e80b8831c5a543192ffc3727f01cf0e57579c6ac15558e3048bfb5708892167b"
|
||||||
"checksum sysinfo 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ee7d12b854e48e680bf4b10856a7867e843845158fa8226e6c2f6cc38539cb01"
|
|
||||||
"checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8"
|
"checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8"
|
||||||
"checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9"
|
"checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9"
|
||||||
"checksum term 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "edd106a334b7657c10b7c540a0106114feadeb4dc314513e97df481d5d966f42"
|
"checksum term 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "edd106a334b7657c10b7c540a0106114feadeb4dc314513e97df481d5d966f42"
|
||||||
|
|
|
@ -14,7 +14,6 @@ homepage = "https://github.com/nushell/nushell"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rustyline = "5.0.1"
|
rustyline = "5.0.1"
|
||||||
sysinfo = "0.9"
|
|
||||||
chrono = { version = "0.4.7", features = ["serde"] }
|
chrono = { version = "0.4.7", features = ["serde"] }
|
||||||
chrono-tz = "0.5.1"
|
chrono-tz = "0.5.1"
|
||||||
derive-new = "0.5.7"
|
derive-new = "0.5.7"
|
||||||
|
@ -120,6 +119,10 @@ path = "src/plugins/skip.rs"
|
||||||
name = "nu_plugin_sys"
|
name = "nu_plugin_sys"
|
||||||
path = "src/plugins/sys.rs"
|
path = "src/plugins/sys.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "nu_plugin_ps"
|
||||||
|
path = "src/plugins/ps.rs"
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "nu_plugin_tree"
|
name = "nu_plugin_tree"
|
||||||
path = "src/plugins/tree.rs"
|
path = "src/plugins/tree.rs"
|
||||||
|
|
|
@ -152,7 +152,6 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
|
||||||
command("from-json", Box::new(from_json::from_json)),
|
command("from-json", Box::new(from_json::from_json)),
|
||||||
command("from-toml", Box::new(from_toml::from_toml)),
|
command("from-toml", Box::new(from_toml::from_toml)),
|
||||||
command("from-xml", Box::new(from_xml::from_xml)),
|
command("from-xml", Box::new(from_xml::from_xml)),
|
||||||
command("ps", Box::new(ps::ps)),
|
|
||||||
command("ls", Box::new(ls::ls)),
|
command("ls", Box::new(ls::ls)),
|
||||||
command("cd", Box::new(cd::cd)),
|
command("cd", Box::new(cd::cd)),
|
||||||
command("size", Box::new(size::size)),
|
command("size", Box::new(size::size)),
|
||||||
|
|
|
@ -28,7 +28,6 @@ crate mod open;
|
||||||
crate mod pick;
|
crate mod pick;
|
||||||
crate mod plugin;
|
crate mod plugin;
|
||||||
crate mod prev;
|
crate mod prev;
|
||||||
crate mod ps;
|
|
||||||
crate mod reject;
|
crate mod reject;
|
||||||
crate mod rm;
|
crate mod rm;
|
||||||
crate mod save;
|
crate mod save;
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
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())
|
|
||||||
}
|
|
|
@ -4,7 +4,6 @@ crate mod dict;
|
||||||
crate mod files;
|
crate mod files;
|
||||||
crate mod into;
|
crate mod into;
|
||||||
crate mod meta;
|
crate mod meta;
|
||||||
crate mod process;
|
|
||||||
crate mod types;
|
crate mod types;
|
||||||
|
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
|
|
|
@ -1,29 +0,0 @@
|
||||||
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()
|
|
||||||
}
|
|
71
src/plugins/ps.rs
Normal file
71
src/plugins/ps.rs
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
#![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());
|
||||||
|
}
|
Loading…
Reference in a new issue