From 3f14b751533946ef67fbb798939dd41f50a943f9 Mon Sep 17 00:00:00 2001 From: Justin Ma Date: Sat, 19 Feb 2022 09:03:24 +0800 Subject: [PATCH] feat: add examples for length,lines,reject,benchmark and drop column (#4547) --- crates/nu-command/src/filters/drop/column.rs | 19 ++++++++++++-- crates/nu-command/src/filters/length.rs | 19 ++++++++++++-- crates/nu-command/src/filters/lines.rs | 14 ++++++++++- crates/nu-command/src/filters/reject.rs | 26 ++++++++++++++++++-- crates/nu-command/src/system/benchmark.rs | 12 ++++++++- 5 files changed, 82 insertions(+), 8 deletions(-) diff --git a/crates/nu-command/src/filters/drop/column.rs b/crates/nu-command/src/filters/drop/column.rs index 3b4564a2a4..c8a77e9ddf 100644 --- a/crates/nu-command/src/filters/drop/column.rs +++ b/crates/nu-command/src/filters/drop/column.rs @@ -2,8 +2,8 @@ use nu_engine::CallExt; use nu_protocol::ast::{Call, CellPath}; use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::{ - Category, FromValue, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, ShellError, - Signature, Span, SyntaxShape, Value, + Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, + ShellError, Signature, Span, SyntaxShape, Value, }; #[derive(Clone)] @@ -47,6 +47,21 @@ impl Command for DropColumn { dropcol(engine_state, span, input, columns_to_drop) } + + fn examples(&self) -> Vec { + vec![Example { + description: "Remove the last column of a table", + example: "echo [[lib, extension]; [nu-lib, rs] [nu-core, rb]] | drop column", + result: Some(Value::List { + vals: vec![Value::Record { + cols: vec!["lib".into()], + vals: vec![Value::test_string("nu-lib"), Value::test_string("nu-core")], + span: Span::test_data(), + }], + span: Span::test_data(), + }), + }] + } } fn dropcol( diff --git a/crates/nu-command/src/filters/length.rs b/crates/nu-command/src/filters/length.rs index ef7c5275c2..05c321166d 100644 --- a/crates/nu-command/src/filters/length.rs +++ b/crates/nu-command/src/filters/length.rs @@ -2,8 +2,8 @@ use nu_engine::column::get_columns; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::{ - Category, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, ShellError, Signature, - Span, Value, + Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, ShellError, + Signature, Span, Value, }; #[derive(Clone)] @@ -38,6 +38,21 @@ impl Command for Length { length_row(call, input) } } + + fn examples(&self) -> Vec { + vec![ + Example { + description: "Count the number of entries in a list", + example: "echo [1 2 3 4 5] | length", + result: Some(Value::test_int(5)), + }, + Example { + description: "Count the number of columns in the calendar table", + example: "cal | length -c", + result: Some(Value::test_int(7)), + }, + ] + } } // this simulates calling input | columns | length diff --git a/crates/nu-command/src/filters/lines.rs b/crates/nu-command/src/filters/lines.rs index c5c13647e2..24c8acc86d 100644 --- a/crates/nu-command/src/filters/lines.rs +++ b/crates/nu-command/src/filters/lines.rs @@ -1,7 +1,8 @@ use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::{ - Category, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Value, + Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Span, + Value, }; #[derive(Clone)] @@ -144,4 +145,15 @@ impl Command for Lines { } } } + + fn examples(&self) -> Vec { + vec![Example { + description: "Split multi-line string into lines", + example: "echo $'two(char nl)lines' | lines", + result: Some(Value::List { + vals: vec![Value::test_string("two"), Value::test_string("lines")], + span: Span::test_data(), + }), + }] + } } diff --git a/crates/nu-command/src/filters/reject.rs b/crates/nu-command/src/filters/reject.rs index 0e1f3fa6fd..5038f2ec9a 100644 --- a/crates/nu-command/src/filters/reject.rs +++ b/crates/nu-command/src/filters/reject.rs @@ -2,8 +2,8 @@ use nu_engine::CallExt; use nu_protocol::ast::{Call, CellPath}; use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::{ - Category, FromValue, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, ShellError, - Signature, Span, SyntaxShape, Value, + Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, + ShellError, Signature, Span, SyntaxShape, Value, }; #[derive(Clone)] @@ -39,6 +39,28 @@ impl Command for Reject { let span = call.head; reject(engine_state, span, input, columns) } + + fn examples(&self) -> Vec { + vec![ + Example { + description: "Lists the files in a directory without showing the modified column", + example: "ls | reject modified", + result: None, + }, + Example { + description: "Reject the specified field in a record", + example: "echo {a: 1, b: 2} | reject a", + result: Some(Value::Record { + cols: vec!["b".into()], + vals: vec![Value::Int { + val: 2, + span: Span::test_data(), + }], + span: Span::test_data(), + }), + }, + ] + } } fn reject( diff --git a/crates/nu-command/src/system/benchmark.rs b/crates/nu-command/src/system/benchmark.rs index bb42f2522e..ce1d537112 100644 --- a/crates/nu-command/src/system/benchmark.rs +++ b/crates/nu-command/src/system/benchmark.rs @@ -3,7 +3,9 @@ use std::time::Instant; use nu_engine::{eval_block, CallExt}; use nu_protocol::ast::Call; use nu_protocol::engine::{CaptureBlock, Command, EngineState, Stack}; -use nu_protocol::{Category, IntoPipelineData, PipelineData, Signature, SyntaxShape, Value}; +use nu_protocol::{ + Category, Example, IntoPipelineData, PipelineData, Signature, SyntaxShape, Value, +}; #[derive(Clone)] pub struct Benchmark; @@ -56,4 +58,12 @@ impl Command for Benchmark { Ok(output.into_pipeline_data()) } + + fn examples(&self) -> Vec { + vec![Example { + description: "Benchmarks a command within a block", + example: "benchmark { sleep 500ms }", + result: None, + }] + } }