feat: add examples for length,lines,reject,benchmark and drop column (#4547)

This commit is contained in:
Justin Ma 2022-02-19 09:03:24 +08:00 committed by GitHub
parent 0f4f660759
commit 3f14b75153
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 82 additions and 8 deletions

View File

@ -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<Example> {
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(

View File

@ -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<Example> {
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

View File

@ -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<Example> {
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(),
}),
}]
}
}

View File

@ -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<Example> {
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(

View File

@ -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<Example> {
vec![Example {
description: "Benchmarks a command within a block",
example: "benchmark { sleep 500ms }",
result: None,
}]
}
}