Add example for cd,transpose,detect columns,split column and split row (#4549)

This commit is contained in:
Justin Ma
2022-02-19 23:24:48 +08:00
committed by GitHub
parent 3ecf17e7af
commit ac99ac003a
6 changed files with 189 additions and 11 deletions

View File

@ -5,8 +5,8 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Span, Spanned,
SyntaxShape, Value,
Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Span,
Spanned, SyntaxShape, Value,
};
type Input<'t> = Peekable<CharIndices<'t>>;
@ -44,6 +44,37 @@ impl Command for DetectColumns {
) -> Result<PipelineData, ShellError> {
detect_columns(engine_state, stack, call, input)
}
fn examples(&self) -> Vec<Example> {
let span = Span::test_data();
vec![
Example {
description: "Splits string across multiple columns",
example: "echo 'a b c' | detect columns -n",
result: Some(Value::List {
vals: vec![Value::Record {
cols: vec![
"Column0".to_string(),
"Column1".to_string(),
"Column2".to_string(),
],
vals: vec![
Value::test_string("a"),
Value::test_string("b"),
Value::test_string("c"),
],
span,
}],
span,
}),
},
Example {
description: "Splits a multi-line string into columns with headers detected",
example: "echo $'c1 c2 c3(char nl)a b c' | detect columns",
result: None,
},
]
}
}
fn detect_columns(

View File

@ -2,7 +2,7 @@ use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
};
#[derive(Clone)]
@ -42,6 +42,51 @@ impl Command for SubCommand {
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
split_column(engine_state, stack, call, input)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Split a string into columns by the specified separator",
example: "echo 'a--b--c' | split column '--'",
result: Some(Value::List {
vals: vec![Value::Record {
cols: vec![
"Column1".to_string(),
"Column2".to_string(),
"Column3".to_string(),
],
vals: vec![
Value::test_string("a"),
Value::test_string("b"),
Value::test_string("c"),
],
span: Span::test_data(),
}],
span: Span::test_data(),
}),
},
Example {
description: "Split a string into columns of char and remove the empty columns",
example: "echo 'abc' | split column -c ''",
result: Some(Value::List {
vals: vec![Value::Record {
cols: vec![
"Column1".to_string(),
"Column2".to_string(),
"Column3".to_string(),
],
vals: vec![
Value::test_string("a"),
Value::test_string("b"),
Value::test_string("c"),
],
span: Span::test_data(),
}],
span: Span::test_data(),
}),
},
]
}
}
fn split_column(

View File

@ -2,7 +2,7 @@ use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
};
#[derive(Clone)]
@ -36,6 +36,35 @@ impl Command for SubCommand {
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
split_row(engine_state, stack, call, input)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Split a string into rows of char",
example: "echo 'abc' | split row ''",
result: Some(Value::List {
vals: vec![
Value::test_string("a"),
Value::test_string("b"),
Value::test_string("c"),
],
span: Span::test_data(),
}),
},
Example {
description: "Split a string into rows by the specified separator",
example: "echo 'a--b--c' | split row '--'",
result: Some(Value::List {
vals: vec![
Value::test_string("a"),
Value::test_string("b"),
Value::test_string("c"),
],
span: Span::test_data(),
}),
},
]
}
}
fn split_row(