Merge pull request #248 from luccasmmg/engine-q-math

Engine q math(just one command)
This commit is contained in:
JT 2021-10-26 14:32:03 +13:00 committed by GitHub
commit 9b67899f8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 133 additions and 1 deletions

View File

@ -48,6 +48,8 @@ pub fn create_default_context() -> EngineState {
LetEnv, LetEnv,
Lines, Lines,
Ls, Ls,
Math,
MathAbs,
Mkdir, Mkdir,
Module, Module,
Mv, Mv,

View File

@ -5,7 +5,7 @@ use nu_protocol::{
PipelineData, PipelineData,
}; };
use super::{From, Into, Split}; use super::{From, Into, Math, Split};
pub fn test_examples(cmd: impl Command + 'static) { pub fn test_examples(cmd: impl Command + 'static) {
let examples = cmd.examples(); let examples = cmd.examples();
@ -18,6 +18,7 @@ pub fn test_examples(cmd: impl Command + 'static) {
working_set.add_decl(Box::new(From)); working_set.add_decl(Box::new(From));
working_set.add_decl(Box::new(Into)); working_set.add_decl(Box::new(Into));
working_set.add_decl(Box::new(Split)); working_set.add_decl(Box::new(Split));
working_set.add_decl(Box::new(Math));
// Adding the command that is being tested to the working set // Adding the command that is being tested to the working set
working_set.add_decl(Box::new(cmd)); working_set.add_decl(Box::new(cmd));

View File

@ -7,6 +7,7 @@ mod experimental;
mod filesystem; mod filesystem;
mod filters; mod filters;
mod formats; mod formats;
mod math;
mod strings; mod strings;
mod system; mod system;
mod viewers; mod viewers;
@ -20,6 +21,7 @@ pub use experimental::*;
pub use filesystem::*; pub use filesystem::*;
pub use filters::*; pub use filters::*;
pub use formats::*; pub use formats::*;
pub use math::*;
pub use strings::*; pub use strings::*;
pub use system::*; pub use system::*;
pub use viewers::*; pub use viewers::*;

View File

@ -0,0 +1,81 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Example, PipelineData, ShellError, Signature, Span, Value};
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"math abs"
}
fn signature(&self) -> Signature {
Signature::build("math abs")
}
fn usage(&self) -> &str {
"Returns absolute values of a list of numbers"
}
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let head = call.head;
input.map(move |value| abs_helper(value, head))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Get absolute of each value in a list of numbers",
example: "[-50 -100.0 25] | math abs",
result: Some(Value::List {
vals: vec![
Value::test_int(50),
Value::Float {
val: 100.0,
span: Span::unknown(),
},
Value::test_int(25),
],
span: Span::unknown(),
}),
}]
}
}
fn abs_helper(val: Value, head: Span) -> Value {
match val {
Value::Int { val, span } => Value::int(val.abs(), span),
Value::Float { val, span } => Value::Float {
val: val.abs(),
span,
},
Value::Duration { val, span } => Value::Duration {
val: val.abs(),
span,
},
_ => Value::Error {
error: ShellError::UnsupportedInput(
String::from("Only numerical values are supported"),
head,
),
},
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}

View File

@ -0,0 +1,41 @@
use nu_engine::get_full_help;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
IntoPipelineData, PipelineData, Signature, Value,
};
#[derive(Clone)]
pub struct MathCommand;
impl Command for MathCommand {
fn name(&self) -> &str {
"math"
}
fn signature(&self) -> Signature {
Signature::build("math")
}
fn usage(&self) -> &str {
"Use mathematical functions as aggregate functions on a list of numbers or tables."
}
fn run(
&self,
engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(Value::String {
val: get_full_help(
&MathCommand.signature(),
&MathCommand.examples(),
engine_state,
),
span: call.head,
}
.into_pipeline_data())
}
}

View File

@ -0,0 +1,5 @@
mod abs;
pub mod command;
pub use abs::SubCommand as MathAbs;
pub use command::MathCommand as Math;