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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 189 additions and 11 deletions

View File

@ -1,7 +1,7 @@
use nu_engine::{current_dir, CallExt};
use nu_protocol::ast::{Call, Expr, Expression};
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, PipelineData, ShellError, Signature, SyntaxShape, Value};
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Value};
#[derive(Clone)]
pub struct Cd;
@ -138,4 +138,12 @@ impl Command for Cd {
stack.add_env_var("PWD".into(), path_value);
Ok(PipelineData::new(call.head))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Change to your home directory",
example: r#"cd ~"#,
result: None,
}]
}
}

View File

@ -3,7 +3,8 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Value,
Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Span, Spanned,
SyntaxShape, Value,
};
#[derive(Clone)]
@ -52,6 +53,70 @@ impl Command for Transpose {
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
transpose(engine_state, stack, call, input)
}
fn examples(&self) -> Vec<Example> {
let span = Span::test_data();
vec![
Example {
description: "Transposes the table contents with default column names",
example: "echo [[c1 c2]; [1 2]] | transpose",
result: Some(Value::List {
vals: vec![
Value::Record {
cols: vec!["Column0".to_string(), "Column1".to_string()],
vals: vec![Value::test_string("c1"), Value::test_int(1)],
span,
},
Value::Record {
cols: vec!["Column0".to_string(), "Column1".to_string()],
vals: vec![Value::test_string("c2"), Value::test_int(2)],
span,
},
],
span,
}),
},
Example {
description: "Transposes the table contents with specified column names",
example: "echo [[c1 c2]; [1 2]] | transpose key val",
result: Some(Value::List {
vals: vec![
Value::Record {
cols: vec!["key".to_string(), "val".to_string()],
vals: vec![Value::test_string("c1"), Value::test_int(1)],
span,
},
Value::Record {
cols: vec!["key".to_string(), "val".to_string()],
vals: vec![Value::test_string("c2"), Value::test_int(2)],
span,
},
],
span,
}),
},
Example {
description:
"Transposes the table without column names and specify a new column name",
example: "echo [[c1 c2]; [1 2]] | transpose -i val",
result: Some(Value::List {
vals: vec![
Value::Record {
cols: vec!["val".to_string()],
vals: vec![Value::test_int(1)],
span,
},
Value::Record {
cols: vec!["val".to_string()],
vals: vec![Value::test_int(2)],
span,
},
],
span,
}),
},
]
}
}
pub fn transpose(

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(

View File

@ -56,7 +56,7 @@ fn test_cd_html_color_flag_dark_false() {
);
assert_eq!(
actual.out,
r"<html><style>body { background-color:white;color:black; }</style><body>Change directory.<br><br>Usage:<br> &gt; cd (path) <br><br>Flags:<br> -h, --help<br> Display this help message<br><br>Parameters:<br> (optional) path: the path to change to<br><br></body></html>"
r"<html><style>body { background-color:white;color:black; }</style><body>Change directory.<br><br>Usage:<br> &gt; cd (path) <br><br>Flags:<br> -h, --help<br> Display this help message<br><br>Parameters:<br> (optional) path: the path to change to<br><br>Examples:<br> Change to your home directory<br> &gt; <span style='color:#037979;font-weight:bold;'>cd<span style='color:black;font-weight:normal;'> </span></span><span style='color:#037979;'>~<span style='color:black;font-weight:normal;'><br><br></body></html></span></span>"
);
}
@ -71,21 +71,21 @@ fn test_no_color_flag() {
);
assert_eq!(
actual.out,
r"<html><style>body { background-color:white;color:black; }</style><body>Change directory.<br><br>Usage:<br> &gt; cd (path) <br><br>Flags:<br> -h, --help<br> Display this help message<br><br>Parameters:<br> (optional) path: the path to change to<br><br></body></html>"
r"<html><style>body { background-color:white;color:black; }</style><body>Change directory.<br><br>Usage:<br> &gt; cd (path) <br><br>Flags:<br> -h, --help<br> Display this help message<br><br>Parameters:<br> (optional) path: the path to change to<br><br>Examples:<br> Change to your home directory<br> &gt; cd ~<br><br></body></html>"
);
}
#[test]
fn test_html_color_cd_flag_dark_false() {
fn test_html_color_history_flag_dark_false() {
let actual = nu!(
cwd: ".", pipeline(
r#"
cd --help | to html --html-color
history --help | to html --html-color
"#
)
);
assert_eq!(
actual.out,
r"<html><style>body { background-color:white;color:black; }</style><body>Change directory.<br><br>Usage:<br> &gt; cd (path) <br><br>Flags:<br> -h, --help<br> Display this help message<br><br>Parameters:<br> (optional) path: the path to change to<br><br></body></html>"
r"<html><style>body { background-color:white;color:black; }</style><body>Get the command history<br><br>Usage:<br> &gt; history {flags} <br><br>Flags:<br> -h, --help<br> Display this help message<br> -c, --clear<br> Clears out the history entries<br><br></body></html>"
);
}