mirror of
https://github.com/nushell/nushell
synced 2024-12-30 15:03:25 +00:00
starting to build this
This commit is contained in:
parent
1c52919103
commit
b1d7e3aa49
3 changed files with 65 additions and 0 deletions
|
@ -8,6 +8,7 @@ mod filesystem;
|
||||||
mod filters;
|
mod filters;
|
||||||
mod formats;
|
mod formats;
|
||||||
mod strings;
|
mod strings;
|
||||||
|
mod math;
|
||||||
mod system;
|
mod system;
|
||||||
mod viewers;
|
mod viewers;
|
||||||
|
|
||||||
|
@ -21,5 +22,6 @@ pub use filesystem::*;
|
||||||
pub use filters::*;
|
pub use filters::*;
|
||||||
pub use formats::*;
|
pub use formats::*;
|
||||||
pub use strings::*;
|
pub use strings::*;
|
||||||
|
pub use math::*;
|
||||||
pub use system::*;
|
pub use system::*;
|
||||||
pub use viewers::*;
|
pub use viewers::*;
|
||||||
|
|
60
crates/nu-command/src/math/abs.rs
Normal file
60
crates/nu-command/src/math/abs.rs
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
use nu_protocol::ast::Call;
|
||||||
|
use nu_protocol::engine::{Command, EvaluationContext};
|
||||||
|
use nu_protocol::{Example, ShellError, Signature, Span, Type, Value};
|
||||||
|
|
||||||
|
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, context: &EvaluationContext, call: &Call, input: Value) -> Result<Value, ShellError> {
|
||||||
|
let mapped = input.map(move |val| match val.value {
|
||||||
|
Value::Int { val, span } => Value::int(val.abs(), span),
|
||||||
|
Value::Float { val, span } => Value::Float{val: val.abs(), span: span},
|
||||||
|
Value::Duration { val, span } => Value::Duration{val: val.abs(), span: span},
|
||||||
|
other => abs_default(other)?,
|
||||||
|
});
|
||||||
|
Ok(mapped)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
vec![Example {
|
||||||
|
description: "Get absolute of each value in a list of numbers",
|
||||||
|
example: "echo [-50 25] | math abs",
|
||||||
|
result: Some(vec![
|
||||||
|
Value::test_int(50),
|
||||||
|
Value::test_int(25),
|
||||||
|
]),
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn abs_default(_: Value) -> Result<Value, ShellError> {
|
||||||
|
Value::Error(ShellError::unexpected(
|
||||||
|
"Only numerical values are supported",
|
||||||
|
))
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::ShellError;
|
||||||
|
use super::SubCommand;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn examples_work_as_expected() -> Result<(), ShellError> {
|
||||||
|
use crate::examples::test as test_examples;
|
||||||
|
|
||||||
|
test_examples(SubCommand {})
|
||||||
|
}
|
||||||
|
}
|
3
crates/nu-command/src/math/mod.rs
Normal file
3
crates/nu-command/src/math/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
pub mod abs;
|
||||||
|
|
||||||
|
pub use abs::SubCommand as MathAbs;
|
Loading…
Reference in a new issue