Add `command_prelude` module (#12291)
# Description
When implementing a `Command`, one must also import all the types
present in the function signatures for `Command`. This makes it so that
we often import the same set of types in each command implementation
file. E.g., something like this:
```rust
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
ShellError, Signature, Span, Type, Value,
};
```
This PR adds the `nu_engine::command_prelude` module which contains the
necessary and commonly used types to implement a `Command`:
```rust
// command_prelude.rs
pub use crate::CallExt;
pub use nu_protocol::{
ast::{Call, CellPath},
engine::{Command, EngineState, Stack},
record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, IntoSpanned,
PipelineData, Record, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
};
```
This should reduce the boilerplate needed to implement a command and
also gives us a place to track the breadth of the `Command` API. I tried
to be conservative with what went into the prelude modules, since it
might be hard/annoying to remove items from the prelude in the future.
Let me know if something should be included or excluded.
2024-03-26 21:17:30 +00:00
|
|
|
use crate::math::{
|
|
|
|
reducers::{reducer_for, Reduce},
|
|
|
|
utils::run_with_function,
|
Allow tables and records as input to math commands (#11496)
# Description
The math functions `avg`, `max`, `median`, `min`, `product`, `stddev`,
`sum` and `variance` all takes a list as input and return a number.
<https://github.com/nushell/nushell/blob/main/crates/nu-command/src/math/utils.rs>
contains code that makes these functions work for tables (by running the
function on each column), but this functionality has not been accessible
because the input types are too strict. This PR remedies this.
The functions should also work on records, since a record is basically a
one-row table.
Most of these functions also make sense for durations and file sizes,
except `product` of course. There's an implementation issue with
`stddev` and `variance` for durations and file sizes, but they could in
principle support it.
# User-Facing Changes
This PR only adds supported types, and doesn't remove any, so there
should be no breaking changes.
2024-01-17 12:39:50 +00:00
|
|
|
};
|
Add `command_prelude` module (#12291)
# Description
When implementing a `Command`, one must also import all the types
present in the function signatures for `Command`. This makes it so that
we often import the same set of types in each command implementation
file. E.g., something like this:
```rust
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
ShellError, Signature, Span, Type, Value,
};
```
This PR adds the `nu_engine::command_prelude` module which contains the
necessary and commonly used types to implement a `Command`:
```rust
// command_prelude.rs
pub use crate::CallExt;
pub use nu_protocol::{
ast::{Call, CellPath},
engine::{Command, EngineState, Stack},
record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, IntoSpanned,
PipelineData, Record, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
};
```
This should reduce the boilerplate needed to implement a command and
also gives us a place to track the breadth of the `Command` API. I tried
to be conservative with what went into the prelude modules, since it
might be hard/annoying to remove items from the prelude in the future.
Let me know if something should be included or excluded.
2024-03-26 21:17:30 +00:00
|
|
|
use nu_engine::command_prelude::*;
|
2021-11-01 21:29:34 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct SubCommand;
|
|
|
|
|
|
|
|
impl Command for SubCommand {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"math sum"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2022-11-09 21:55:05 +00:00
|
|
|
Signature::build("math sum")
|
2023-07-14 03:20:35 +00:00
|
|
|
.input_output_types(vec![
|
|
|
|
(Type::List(Box::new(Type::Number)), Type::Number),
|
2023-07-28 21:34:47 +00:00
|
|
|
(Type::List(Box::new(Type::Duration)), Type::Duration),
|
|
|
|
(Type::List(Box::new(Type::Filesize)), Type::Filesize),
|
2023-07-14 03:20:35 +00:00
|
|
|
(Type::Range, Type::Number),
|
Allow tables and records as input to math commands (#11496)
# Description
The math functions `avg`, `max`, `median`, `min`, `product`, `stddev`,
`sum` and `variance` all takes a list as input and return a number.
<https://github.com/nushell/nushell/blob/main/crates/nu-command/src/math/utils.rs>
contains code that makes these functions work for tables (by running the
function on each column), but this functionality has not been accessible
because the input types are too strict. This PR remedies this.
The functions should also work on records, since a record is basically a
one-row table.
Most of these functions also make sense for durations and file sizes,
except `product` of course. There's an implementation issue with
`stddev` and `variance` for durations and file sizes, but they could in
principle support it.
# User-Facing Changes
This PR only adds supported types, and doesn't remove any, so there
should be no breaking changes.
2024-01-17 12:39:50 +00:00
|
|
|
(Type::Table(vec![]), Type::Record(vec![])),
|
|
|
|
(Type::Record(vec![]), Type::Record(vec![])),
|
2023-07-14 03:20:35 +00:00
|
|
|
])
|
|
|
|
.allow_variants_without_examples(true)
|
2022-11-09 21:55:05 +00:00
|
|
|
.category(Category::Math)
|
2021-11-01 21:29:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2023-03-01 05:33:02 +00:00
|
|
|
"Returns the sum of a list of numbers or of each column in a table."
|
2021-11-01 21:29:34 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 21:33:32 +00:00
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
2022-11-09 21:55:05 +00:00
|
|
|
vec!["plus", "add", "total", "+"]
|
2022-04-18 21:33:32 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 21:29:34 +00:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
_engine_state: &EngineState,
|
|
|
|
_stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
2023-02-05 21:17:46 +00:00
|
|
|
) -> Result<PipelineData, ShellError> {
|
2021-11-01 21:29:34 +00:00
|
|
|
run_with_function(call, input, summation)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
description: "Sum a list of numbers",
|
|
|
|
example: "[1 2 3] | math sum",
|
|
|
|
result: Some(Value::test_int(6)),
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Get the disk usage for the current directory",
|
|
|
|
example: "ls | get size | math sum",
|
|
|
|
result: None,
|
|
|
|
},
|
Allow tables and records as input to math commands (#11496)
# Description
The math functions `avg`, `max`, `median`, `min`, `product`, `stddev`,
`sum` and `variance` all takes a list as input and return a number.
<https://github.com/nushell/nushell/blob/main/crates/nu-command/src/math/utils.rs>
contains code that makes these functions work for tables (by running the
function on each column), but this functionality has not been accessible
because the input types are too strict. This PR remedies this.
The functions should also work on records, since a record is basically a
one-row table.
Most of these functions also make sense for durations and file sizes,
except `product` of course. There's an implementation issue with
`stddev` and `variance` for durations and file sizes, but they could in
principle support it.
# User-Facing Changes
This PR only adds supported types, and doesn't remove any, so there
should be no breaking changes.
2024-01-17 12:39:50 +00:00
|
|
|
Example {
|
|
|
|
description: "Compute the sum of each column in a table",
|
|
|
|
example: "[[a b]; [1 2] [3 4]] | math sum",
|
|
|
|
result: Some(Value::test_record(record! {
|
|
|
|
"a" => Value::test_int(4),
|
|
|
|
"b" => Value::test_int(6),
|
|
|
|
})),
|
|
|
|
},
|
2021-11-01 21:29:34 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-31 19:47:46 +00:00
|
|
|
pub fn summation(values: &[Value], span: Span, head: Span) -> Result<Value, ShellError> {
|
2021-11-01 21:29:34 +00:00
|
|
|
let sum_func = reducer_for(Reduce::Summation);
|
2023-07-31 19:47:46 +00:00
|
|
|
sum_func(Value::nothing(head), values.to_vec(), span, head)
|
2021-11-01 21:29:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_examples() {
|
|
|
|
use crate::test_examples;
|
|
|
|
|
|
|
|
test_examples(SubCommand {})
|
|
|
|
}
|
|
|
|
}
|