mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 00:35:01 +02:00
Add 'number' command for enumeration (#7871)
# Description This adds a `number` command that will enumerate the input, and add an `index` and `item` record for each item. The `index` is the number of the item in the input stream, and `item` is the original value of the item. ``` > ls | number | get 14 ╭───────┬────────────────────────────╮ │ index │ 14 │ │ │ ╭──────────┬─────────────╮ │ │ item │ │ name │ crates │ │ │ │ │ type │ dir │ │ │ │ │ size │ 832 B │ │ │ │ │ modified │ 2 weeks ago │ │ │ │ ╰──────────┴─────────────╯ │ ╰───────┴────────────────────────────╯ ``` # User-Facing Changes This adds a `number` command. # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
@ -95,6 +95,7 @@ pub fn create_default_context() -> EngineState {
|
||||
Each,
|
||||
EachWhile,
|
||||
Empty,
|
||||
Enumerate,
|
||||
Every,
|
||||
Filter,
|
||||
Find,
|
||||
|
95
crates/nu-command/src/filters/enumerate.rs
Normal file
95
crates/nu-command/src/filters/enumerate.rs
Normal file
@ -0,0 +1,95 @@
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Example, IntoInterruptiblePipelineData, PipelineData, Signature, Span, Type, Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Enumerate;
|
||||
|
||||
impl Command for Enumerate {
|
||||
fn name(&self) -> &str {
|
||||
"enumerate"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Enumerate the elements in a stream."
|
||||
}
|
||||
|
||||
fn search_terms(&self) -> Vec<&str> {
|
||||
vec!["itemize"]
|
||||
}
|
||||
|
||||
fn signature(&self) -> nu_protocol::Signature {
|
||||
Signature::build("enumerate")
|
||||
.input_output_types(vec![(Type::Any, Type::Any)])
|
||||
.category(Category::Filters)
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "Add an index to each element of a list",
|
||||
example: r#"[a, b, c] | enumerate "#,
|
||||
result: Some(Value::List {
|
||||
vals: vec![
|
||||
Value::Record {
|
||||
cols: vec!["index".into(), "item".into()],
|
||||
vals: vec![Value::test_int(0), Value::test_string("a")],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Record {
|
||||
cols: vec!["index".into(), "item".into()],
|
||||
vals: vec![Value::test_int(1), Value::test_string("b")],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Record {
|
||||
cols: vec!["index".into(), "item".into()],
|
||||
vals: vec![Value::test_int(2), Value::test_string("c")],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
}]
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let metadata = input.metadata();
|
||||
let ctrlc = engine_state.ctrlc.clone();
|
||||
let span = call.head;
|
||||
|
||||
Ok(input
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.map(move |(idx, x)| Value::Record {
|
||||
cols: vec!["index".into(), "item".into()],
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: idx as i64,
|
||||
span,
|
||||
},
|
||||
x,
|
||||
],
|
||||
span,
|
||||
})
|
||||
.into_pipeline_data_with_metadata(metadata, ctrlc))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_examples() {
|
||||
use crate::test_examples;
|
||||
|
||||
test_examples(Enumerate {})
|
||||
}
|
||||
}
|
@ -9,6 +9,7 @@ mod drop;
|
||||
mod each;
|
||||
mod each_while;
|
||||
mod empty;
|
||||
mod enumerate;
|
||||
mod every;
|
||||
mod filter;
|
||||
mod find;
|
||||
@ -64,6 +65,7 @@ pub use drop::*;
|
||||
pub use each::Each;
|
||||
pub use each_while::EachWhile;
|
||||
pub use empty::Empty;
|
||||
pub use enumerate::Enumerate;
|
||||
pub use every::Every;
|
||||
pub use filter::Filter;
|
||||
pub use find::Find;
|
||||
|
Reference in New Issue
Block a user