mirror of
https://github.com/nushell/nushell.git
synced 2025-05-07 11:34:26 +02:00
* move commands, futures.rs, script.rs, utils * move over maybe_print_errors * add nu_command crate references to nu_cli * in commands.rs open up to pub mod from pub(crate) * nu-cli, nu-command, and nu tests are now passing * cargo fmt * clean up nu-cli/src/prelude.rs * code cleanup * for some reason lex.rs was not formatted, may be causing my error * remove mod completion from lib.rs which was not being used along with quickcheck macros * add in allow unused imports * comment out one failing external test; comment out one failing internal test * revert commenting out failing tests; something else might be going on; someone with a windows machine should check and see what is going on with these failing windows tests * Update Cargo.toml Extend the optional features to nu-command Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
80 lines
2.0 KiB
Rust
80 lines
2.0 KiB
Rust
use crate::prelude::*;
|
|
use nu_engine::WholeStreamCommand;
|
|
use nu_errors::ShellError;
|
|
use nu_protocol::{Signature, SyntaxShape, UntaggedValue};
|
|
use nu_source::Tagged;
|
|
|
|
pub struct Command;
|
|
|
|
#[derive(Deserialize)]
|
|
pub struct Arguments {
|
|
rows: Option<Tagged<usize>>,
|
|
}
|
|
|
|
#[async_trait]
|
|
impl WholeStreamCommand for Command {
|
|
fn name(&self) -> &str {
|
|
"keep"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("keep").optional(
|
|
"rows",
|
|
SyntaxShape::Int,
|
|
"Starting from the front, the number of rows to keep",
|
|
)
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Keep the number of rows only"
|
|
}
|
|
|
|
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
keep(args).await
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![
|
|
Example {
|
|
description: "Keep the first row",
|
|
example: "echo [1 2 3] | keep",
|
|
result: Some(vec![UntaggedValue::int(1).into()]),
|
|
},
|
|
Example {
|
|
description: "Keep the first four rows",
|
|
example: "echo [1 2 3 4 5] | keep 4",
|
|
result: Some(vec![
|
|
UntaggedValue::int(1).into(),
|
|
UntaggedValue::int(2).into(),
|
|
UntaggedValue::int(3).into(),
|
|
UntaggedValue::int(4).into(),
|
|
]),
|
|
},
|
|
]
|
|
}
|
|
}
|
|
|
|
async fn keep(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
let (Arguments { rows }, input) = args.process().await?;
|
|
let rows_desired = if let Some(quantity) = rows {
|
|
*quantity
|
|
} else {
|
|
1
|
|
};
|
|
|
|
Ok(input.take(rows_desired).to_output_stream())
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::Command;
|
|
use super::ShellError;
|
|
|
|
#[test]
|
|
fn examples_work_as_expected() -> Result<(), ShellError> {
|
|
use crate::examples::test as test_examples;
|
|
|
|
Ok(test_examples(Command {})?)
|
|
}
|
|
}
|