2019-09-08 09:06:15 +02:00
|
|
|
use futures::executor::block_on;
|
|
|
|
use futures::stream::{StreamExt, TryStreamExt};
|
|
|
|
|
|
|
|
use heim::process::{self as process, Process, ProcessResult};
|
|
|
|
use heim::units::{ratio, Ratio};
|
|
|
|
use std::usize;
|
|
|
|
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
use nu::{serve_plugin, value, Plugin, TaggedDictBuilder};
|
|
|
|
use nu_errors::ShellError;
|
|
|
|
use nu_protocol::{CallInfo, ReturnSuccess, ReturnValue, Signature, Value};
|
2019-11-21 15:33:14 +01:00
|
|
|
use nu_source::Tag;
|
|
|
|
|
2019-09-08 09:06:15 +02:00
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
struct Ps;
|
|
|
|
impl Ps {
|
|
|
|
fn new() -> Ps {
|
|
|
|
Ps
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn usage(process: Process) -> ProcessResult<(process::Process, Ratio)> {
|
|
|
|
let usage_1 = process.cpu_usage().await?;
|
2019-11-10 18:48:49 +01:00
|
|
|
futures_timer::Delay::new(Duration::from_millis(100)).await;
|
2019-09-08 09:06:15 +02:00
|
|
|
let usage_2 = process.cpu_usage().await?;
|
|
|
|
|
|
|
|
Ok((process, usage_2 - usage_1))
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
async fn ps(tag: Tag) -> Vec<Value> {
|
2019-09-08 09:06:15 +02:00
|
|
|
let processes = process::processes()
|
|
|
|
.map_ok(|process| {
|
|
|
|
// Note that there is no `.await` here,
|
|
|
|
// as we want to pass the returned future
|
|
|
|
// into the `.try_buffer_unordered`.
|
|
|
|
usage(process)
|
|
|
|
})
|
|
|
|
.try_buffer_unordered(usize::MAX);
|
|
|
|
pin_utils::pin_mut!(processes);
|
|
|
|
|
|
|
|
let mut output = vec![];
|
|
|
|
while let Some(res) = processes.next().await {
|
|
|
|
if let Ok((process, usage)) = res {
|
2019-10-13 06:12:43 +02:00
|
|
|
let mut dict = TaggedDictBuilder::new(&tag);
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
dict.insert_untagged("pid", value::int(process.pid()));
|
2019-09-08 09:06:15 +02:00
|
|
|
if let Ok(name) = process.name().await {
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
dict.insert_untagged("name", value::string(name));
|
2019-09-08 09:06:15 +02:00
|
|
|
}
|
|
|
|
if let Ok(status) = process.status().await {
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
dict.insert_untagged("status", value::string(format!("{:?}", status)));
|
2019-09-08 09:06:15 +02:00
|
|
|
}
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
dict.insert_untagged("cpu", value::number(usage.get::<ratio::percent>()));
|
2019-11-21 15:33:14 +01:00
|
|
|
output.push(dict.into_value());
|
2019-09-08 09:06:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Plugin for Ps {
|
|
|
|
fn config(&mut self) -> Result<Signature, ShellError> {
|
|
|
|
Ok(Signature::build("ps")
|
|
|
|
.desc("View information about system processes.")
|
|
|
|
.filter())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn begin_filter(&mut self, callinfo: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
|
2019-09-14 18:30:24 +02:00
|
|
|
Ok(block_on(ps(callinfo.name_tag))
|
2019-09-08 09:06:15 +02:00
|
|
|
.into_iter()
|
|
|
|
.map(ReturnSuccess::value)
|
|
|
|
.collect())
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
fn filter(&mut self, _: Value) -> Result<Vec<ReturnValue>, ShellError> {
|
2019-09-08 09:06:15 +02:00
|
|
|
Ok(vec![])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
serve_plugin(&mut Ps::new());
|
|
|
|
}
|