nushell/crates/nu-command/src/system/ps.rs

126 lines
3.8 KiB
Rust
Raw Normal View History

use std::time::Duration;
2021-10-01 21:53:13 +00:00
use nu_protocol::{
ast::Call,
2021-10-25 16:58:58 +00:00
engine::{Command, EngineState, Stack},
Category, Example, IntoInterruptiblePipelineData, PipelineData, Record, ShellError, Signature,
Type, Value,
2021-10-01 21:53:13 +00:00
};
2021-10-25 04:01:02 +00:00
#[derive(Clone)]
2021-10-01 21:53:13 +00:00
pub struct Ps;
impl Command for Ps {
fn name(&self) -> &str {
"ps"
}
fn signature(&self) -> Signature {
Signature::build("ps")
.input_output_types(vec![(Type::Nothing, Type::Table(vec![]))])
2021-10-01 21:53:13 +00:00
.switch(
"long",
"list all available columns for each entry",
Some('l'),
)
.filter()
.category(Category::System)
2021-10-01 21:53:13 +00:00
}
fn usage(&self) -> &str {
"View information about system processes."
}
fn search_terms(&self) -> Vec<&str> {
vec!["procedures", "operations", "tasks", "ops"]
}
2021-10-01 21:53:13 +00:00
fn run(
&self,
2021-10-28 04:13:10 +00:00
engine_state: &EngineState,
2021-10-25 06:31:39 +00:00
_stack: &mut Stack,
2021-10-01 21:53:13 +00:00
call: &Call,
2021-10-25 04:01:02 +00:00
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
2021-10-28 04:13:10 +00:00
run_ps(engine_state, call)
2021-10-01 21:53:13 +00:00
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "List the system processes",
example: "ps",
result: None,
},
Example {
description: "List the top 5 system processes with the highest memory usage",
example: "ps | sort-by mem | last 5",
result: None,
},
Example {
description: "List the top 3 system processes with the highest CPU usage",
example: "ps | sort-by cpu | last 3",
result: None,
},
Example {
description: "List the system processes with 'nu' in their names",
example: "ps | where name =~ 'nu'",
result: None,
},
Example {
description: "Get the parent process id of the current nu process",
example: "ps | where pid == $nu.pid | get ppid",
result: None,
},
]
2021-10-01 21:53:13 +00:00
}
}
2021-10-28 04:13:10 +00:00
fn run_ps(engine_state: &EngineState, call: &Call) -> Result<PipelineData, ShellError> {
let mut output = vec![];
2021-10-01 21:53:13 +00:00
let span = call.head;
let long = call.has_flag("long");
for proc in nu_system::collect_proc(Duration::from_millis(100), false) {
let mut record = Record::new();
record.push("pid", Value::int(proc.pid() as i64, span));
record.push("ppid", Value::int(proc.ppid() as i64, span));
record.push("name", Value::string(proc.name(), span));
#[cfg(not(windows))]
{
// Hide status on Windows until we can find a good way to support it
record.push("status", Value::string(proc.status(), span));
}
record.push("cpu", Value::float(proc.cpu_usage(), span));
record.push("mem", Value::filesize(proc.mem_size() as i64, span));
record.push("virtual", Value::filesize(proc.virtual_size() as i64, span));
if long {
record.push("command", Value::string(proc.command(), span));
#[cfg(windows)]
{
record.push("cwd", Value::string(proc.cwd(), span));
record.push(
"environment",
Value::list(
proc.environ()
.iter()
.map(|x| Value::string(x.to_string(), span))
.collect(),
span,
),
);
}
2021-10-01 21:53:13 +00:00
}
output.push(Value::record(record, span));
2021-10-01 21:53:13 +00:00
}
2021-10-28 04:13:10 +00:00
Ok(output
.into_iter()
.into_pipeline_data(engine_state.ctrlc.clone()))
2021-10-01 21:53:13 +00:00
}