feat: update: #4518, Add examples for command: hide, history, from yml, def-env, and table (#4581)

This commit is contained in:
Justin Ma 2022-02-21 21:52:50 +08:00 committed by GitHub
parent 4f367a59de
commit 917886f8ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 136 additions and 46 deletions

View File

@ -1,6 +1,6 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, PipelineData, Signature, SyntaxShape};
use nu_protocol::{Category, Example, PipelineData, Signature, Span, SyntaxShape, Value};
#[derive(Clone)]
pub struct DefEnv;
@ -35,4 +35,15 @@ impl Command for DefEnv {
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new(call.head))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Set environment variable by call a custom command",
example: r#"def-env foo [] { let-env BAR = "BAZ" }; foo; $env.BAR"#,
result: Some(Value::String {
val: "BAZ".to_string(),
span: Span::test_data(),
}),
}]
}
}

View File

@ -1,6 +1,8 @@
use nu_protocol::ast::{Call, Expr, Expression, ImportPatternMember};
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, PipelineData, ShellError, Signature, SyntaxShape};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
};
#[derive(Clone)]
pub struct Hide;
@ -116,4 +118,24 @@ impl Command for Hide {
Ok(PipelineData::new(call.head))
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Hide the alias just defined",
example: r#"alias lll = ls -l; hide lll"#,
result: None,
},
Example {
description: "Hide a custom command",
example: r#"def say-hi [] { echo 'Hi!' }; hide say-hi"#,
result: None,
},
Example {
description: "Hide an environment variable",
example: r#"let-env HZ_ENV_ABC = 1; hide HZ_ENV_ABC; 'HZ_ENV_ABC' in (env).name"#,
result: Some(Value::boolean(false, Span::test_data())),
},
]
}
}

View File

@ -1,7 +1,7 @@
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, Value,
};
const NEWLINE_ESCAPE_CODE: &str = "<\\n>";
@ -68,4 +68,24 @@ impl Command for History {
Err(ShellError::FileNotFound(head))
}
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
example: "history | length",
description: "Get current history length",
result: None,
},
Example {
example: "history | last 5",
description: "Show last 5 commands you have ran",
result: None,
},
Example {
example: "history | wrap cmd | where cmd =~ cargo",
description: "Search all the commands from history that contains 'cargo'",
result: None,
},
]
}
}

View File

@ -37,8 +37,7 @@ impl Command for Shuffle {
fn examples(&self) -> Vec<Example> {
vec![Example {
description:
"Shuffle rows randomly(execute it a few more times and see the difference)",
description: "Shuffle rows randomly (execute it several times and see the difference)",
example: r#"echo [[version patch]; [1.0.0 $false] [3.0.1 $true] [2.0.0 $false]] | shuffle"#,
result: None,
}]

View File

@ -25,42 +25,7 @@ impl Command for FromYaml {
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
example: "'a: 1' | from yaml",
description: "Converts yaml formatted string to table",
result: Some(Value::Record {
cols: vec!["a".to_string()],
vals: vec![Value::Int {
val: 1,
span: Span::test_data(),
}],
span: Span::test_data(),
}),
},
Example {
example: "'[ a: 1, b: [1, 2] ]' | from yaml",
description: "Converts yaml formatted string to table",
result: Some(Value::List {
vals: vec![
Value::Record {
cols: vec!["a".to_string()],
vals: vec![Value::test_int(1)],
span: Span::test_data(),
},
Value::Record {
cols: vec!["b".to_string()],
vals: vec![Value::List {
vals: vec![Value::test_int(1), Value::test_int(2)],
span: Span::test_data(),
}],
span: Span::test_data(),
},
],
span: Span::test_data(),
}),
},
]
get_examples()
}
fn run(
@ -103,6 +68,10 @@ impl Command for FromYml {
let config = stack.get_config().unwrap_or_default();
from_yaml(input, head, &config)
}
fn examples(&self) -> Vec<Example> {
get_examples()
}
}
fn convert_yaml_value_to_nu_value(v: &serde_yaml::Value, span: Span) -> Result<Value, ShellError> {
@ -205,6 +174,45 @@ pub fn from_yaml_string_to_value(s: String, span: Span) -> Result<Value, ShellEr
}
}
pub fn get_examples() -> Vec<Example> {
vec![
Example {
example: "'a: 1' | from yaml",
description: "Converts yaml formatted string to table",
result: Some(Value::Record {
cols: vec!["a".to_string()],
vals: vec![Value::Int {
val: 1,
span: Span::test_data(),
}],
span: Span::test_data(),
}),
},
Example {
example: "'[ a: 1, b: [1, 2] ]' | from yaml",
description: "Converts yaml formatted string to table",
result: Some(Value::List {
vals: vec![
Value::Record {
cols: vec!["a".to_string()],
vals: vec![Value::test_int(1)],
span: Span::test_data(),
},
Value::Record {
cols: vec!["b".to_string()],
vals: vec![Value::List {
vals: vec![Value::test_int(1), Value::test_int(2)],
span: Span::test_data(),
}],
span: Span::test_data(),
},
],
span: Span::test_data(),
}),
},
]
}
fn from_yaml(input: PipelineData, head: Span, config: &Config) -> Result<PipelineData, ShellError> {
let concat_string = input.collect_string("", config)?;

View File

@ -5,8 +5,8 @@ use nu_engine::{env_to_string, CallExt};
use nu_protocol::ast::{Call, PathMember};
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Config, DataSource, IntoPipelineData, ListStream, PipelineData, PipelineMetadata,
RawStream, ShellError, Signature, Span, SyntaxShape, Value,
Category, Config, DataSource, Example, IntoPipelineData, ListStream, PipelineData,
PipelineMetadata, RawStream, ShellError, Signature, Span, SyntaxShape, Value,
};
use nu_table::{StyledString, TextStyle, Theme};
use std::sync::atomic::{AtomicBool, Ordering};
@ -232,6 +232,36 @@ impl Command for Table {
x => Ok(x),
}
}
fn examples(&self) -> Vec<Example> {
let span = Span::test_data();
vec![
Example {
description: "List the files in current directory with index number start from 1.",
example: r#"ls | table -n 1"#,
result: None,
},
Example {
description: "Render data in table view",
example: r#"echo [[a b]; [1 2] [3 4]] | table"#,
result: Some(Value::List {
vals: vec![
Value::Record {
cols: vec!["a".to_string(), "b".to_string()],
vals: vec![Value::test_int(1), Value::test_int(2)],
span,
},
Value::Record {
cols: vec!["a".to_string(), "b".to_string()],
vals: vec![Value::test_int(3), Value::test_int(4)],
span,
},
],
span,
}),
},
]
}
}
fn convert_to_table(

View File

@ -76,16 +76,16 @@ fn test_no_color_flag() {
}
#[test]
fn test_html_color_history_flag_dark_false() {
fn test_html_color_cd_flag_dark_false() {
let actual = nu!(
cwd: ".", pipeline(
r#"
history --help | to html --html-color
cd --help | to html --html-color
"#
)
);
assert_eq!(
actual.out,
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>"
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>"
);
}