This commit is contained in:
JT
2021-10-25 17:01:02 +13:00
parent ab9d6b206d
commit b6d269e90a
63 changed files with 1075 additions and 1013 deletions

View File

@ -3,8 +3,9 @@ use std::time::Instant;
use nu_engine::eval_block;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Signature, SyntaxShape, Value};
use nu_protocol::{PipelineData, Signature, SyntaxShape, Value};
#[derive(Clone)]
pub struct Benchmark;
impl Command for Benchmark {
@ -28,21 +29,18 @@ impl Command for Benchmark {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let block = call.positional[0]
.as_block()
.expect("internal error: expected block");
let engine_state = context.engine_state.borrow();
let block = engine_state.get_block(block);
let block = context.engine_state.get_block(block);
let state = context.enter_scope();
let start_time = Instant::now();
eval_block(&state, block, Value::nothing())?;
eval_block(&state, block, PipelineData::new())?;
let end_time = Instant::now();
println!("{} ms", (end_time - start_time).as_millis());
Ok(Value::Nothing {
span: call.positional[0].span,
})
Ok(PipelineData::new())
}
}

View File

@ -1,10 +1,11 @@
use nu_protocol::{
ast::Call,
engine::{Command, EvaluationContext},
Example, ShellError, Signature, Value,
Example, IntoPipelineData, PipelineData, ShellError, Signature, Value,
};
use sysinfo::{ProcessExt, System, SystemExt};
#[derive(Clone)]
pub struct Ps;
impl Command for Ps {
@ -31,8 +32,8 @@ impl Command for Ps {
&self,
_context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
run_ps(call)
}
@ -45,7 +46,7 @@ impl Command for Ps {
}
}
fn run_ps(call: &Call) -> Result<Value, ShellError> {
fn run_ps(call: &Call) -> Result<PipelineData, ShellError> {
let span = call.head;
let long = call.has_flag("long");
let mut sys = System::new_all();
@ -124,5 +125,5 @@ fn run_ps(call: &Call) -> Result<Value, ShellError> {
}
}
Ok(Value::List { vals: output, span })
Ok(output.into_iter().into_pipeline_data())
}

View File

@ -11,12 +11,13 @@ use nu_protocol::{
engine::{Command, EvaluationContext},
ShellError, Signature, SyntaxShape, Value,
};
use nu_protocol::{Span, ValueStream};
use nu_protocol::{IntoPipelineData, PipelineData, Span, ValueStream};
use nu_engine::eval_expression;
const OUTPUT_BUFFER_SIZE: usize = 8192;
#[derive(Clone)]
pub struct External;
impl Command for External {
@ -38,8 +39,8 @@ impl Command for External {
&self,
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<Value, ShellError> {
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let command = ExternalCommand::try_new(call, context)?;
command.run_with_input(input)
}
@ -82,7 +83,7 @@ impl<'call, 'contex> ExternalCommand<'call, 'contex> {
.collect()
}
pub fn run_with_input(&self, input: Value) -> Result<Value, ShellError> {
pub fn run_with_input(&self, input: PipelineData) -> Result<PipelineData, ShellError> {
let mut process = self.create_command();
// TODO. We don't have a way to know the current directory
@ -101,11 +102,11 @@ impl<'call, 'contex> ExternalCommand<'call, 'contex> {
// If there is an input from the pipeline. The stdin from the process
// is piped so it can be used to send the input information
if let Value::String { .. } = input {
if let PipelineData::Value(Value::String { .. }) = input {
process.stdin(Stdio::piped());
}
if let Value::Stream { .. } = input {
if let PipelineData::Stream { .. } = input {
process.stdin(Stdio::piped());
}
@ -116,33 +117,18 @@ impl<'call, 'contex> ExternalCommand<'call, 'contex> {
)),
Ok(mut child) => {
// if there is a string or a stream, that is sent to the pipe std
match input {
Value::String { val, span: _ } => {
if let Some(mut stdin_write) = child.stdin.take() {
self.write_to_stdin(&mut stdin_write, val.as_bytes())?
}
}
Value::Binary { val, span: _ } => {
if let Some(mut stdin_write) = child.stdin.take() {
self.write_to_stdin(&mut stdin_write, &val)?
}
}
Value::Stream { stream, span: _ } => {
if let Some(mut stdin_write) = child.stdin.take() {
for value in stream {
match value {
Value::String { val, span: _ } => {
self.write_to_stdin(&mut stdin_write, val.as_bytes())?
}
Value::Binary { val, span: _ } => {
self.write_to_stdin(&mut stdin_write, &val)?
}
_ => continue,
}
if let Some(mut stdin_write) = child.stdin.take() {
for value in input {
match value {
Value::String { val, span: _ } => {
self.write_to_stdin(&mut stdin_write, val.as_bytes())?
}
Value::Binary { val, span: _ } => {
self.write_to_stdin(&mut stdin_write, &val)?
}
_ => continue,
}
}
_ => (),
}
// If this external is not the last expression, then its output is piped to a channel
@ -185,12 +171,9 @@ impl<'call, 'contex> ExternalCommand<'call, 'contex> {
});
// The ValueStream is consumed by the next expression in the pipeline
Value::Stream {
stream: ValueStream(Rc::new(RefCell::new(ChannelReceiver::new(rx)))),
span: Span::unknown(),
}
ChannelReceiver::new(rx).into_pipeline_data()
} else {
Value::nothing()
PipelineData::new()
};
match child.wait() {

View File

@ -1,10 +1,11 @@
use nu_protocol::{
ast::Call,
engine::{Command, EvaluationContext},
Example, ShellError, Signature, Span, Value,
Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Value,
};
use sysinfo::{ComponentExt, DiskExt, NetworkExt, ProcessorExt, System, SystemExt, UserExt};
#[derive(Clone)]
pub struct Sys;
impl Command for Sys {
@ -26,8 +27,8 @@ impl Command for Sys {
&self,
_context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
run_sys(call)
}
@ -40,7 +41,7 @@ impl Command for Sys {
}
}
fn run_sys(call: &Call) -> Result<Value, ShellError> {
fn run_sys(call: &Call) -> Result<PipelineData, ShellError> {
let span = call.head;
let mut sys = System::new();
@ -76,7 +77,8 @@ fn run_sys(call: &Call) -> Result<Value, ShellError> {
cols: headers,
vals: values,
span,
})
}
.into_pipeline_data())
}
pub fn trim_cstyle_null(s: String) -> String {