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:
JT
2023-01-28 06:45:57 +13:00
committed by GitHub
parent 76292ef10c
commit 36ddbfdc85
6 changed files with 114 additions and 28 deletions

View File

@ -455,16 +455,13 @@ mod string {
assert_eq!(expressions.len(), 1);
if let PipelineElement::Expression(_, expr) = &expressions[0] {
let subexprs: Vec<&Expr>;
match expr {
let subexprs: Vec<&Expr> = match expr {
Expression {
expr: Expr::StringInterpolation(expressions),
..
} => {
subexprs = expressions.iter().map(|e| &e.expr).collect();
}
} => expressions.iter().map(|e| &e.expr).collect(),
_ => panic!("Expected an `Expr::StringInterpolation`"),
}
};
assert_eq!(subexprs.len(), 2);
@ -491,16 +488,13 @@ mod string {
assert_eq!(expressions.len(), 1);
if let PipelineElement::Expression(_, expr) = &expressions[0] {
let subexprs: Vec<&Expr>;
match expr {
let subexprs: Vec<&Expr> = match expr {
Expression {
expr: Expr::StringInterpolation(expressions),
..
} => {
subexprs = expressions.iter().map(|e| &e.expr).collect();
}
} => expressions.iter().map(|e| &e.expr).collect(),
_ => panic!("Expected an `Expr::StringInterpolation`"),
}
};
assert_eq!(subexprs.len(), 1);
@ -531,16 +525,13 @@ mod string {
assert_eq!(expressions.len(), 1);
if let PipelineElement::Expression(_, expr) = &expressions[0] {
let subexprs: Vec<&Expr>;
match expr {
let subexprs: Vec<&Expr> = match expr {
Expression {
expr: Expr::StringInterpolation(expressions),
..
} => {
subexprs = expressions.iter().map(|e| &e.expr).collect();
}
} => expressions.iter().map(|e| &e.expr).collect(),
_ => panic!("Expected an `Expr::StringInterpolation`"),
}
};
assert_eq!(subexprs.len(), 2);
@ -573,16 +564,13 @@ mod string {
assert_eq!(expressions.len(), 1);
if let PipelineElement::Expression(_, expr) = &expressions[0] {
let subexprs: Vec<&Expr>;
match expr {
let subexprs: Vec<&Expr> = match expr {
Expression {
expr: Expr::StringInterpolation(expressions),
..
} => {
subexprs = expressions.iter().map(|e| &e.expr).collect();
}
} => expressions.iter().map(|e| &e.expr).collect(),
_ => panic!("Expected an `Expr::StringInterpolation`"),
}
};
assert_eq!(subexprs.len(), 1);
assert_eq!(subexprs[0], &Expr::String("(1 + 3)(7 - 5)".to_string()));