diff --git a/crates/nu-command/src/core_commands/def_env.rs b/crates/nu-command/src/core_commands/def_env.rs index 6f39e72a6..00a42b3eb 100644 --- a/crates/nu-command/src/core_commands/def_env.rs +++ b/crates/nu-command/src/core_commands/def_env.rs @@ -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 { Ok(PipelineData::new(call.head)) } + + fn examples(&self) -> Vec { + 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(), + }), + }] + } } diff --git a/crates/nu-command/src/core_commands/hide.rs b/crates/nu-command/src/core_commands/hide.rs index 631f381e6..ccc270239 100644 --- a/crates/nu-command/src/core_commands/hide.rs +++ b/crates/nu-command/src/core_commands/hide.rs @@ -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 { + 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())), + }, + ] + } } diff --git a/crates/nu-command/src/core_commands/history.rs b/crates/nu-command/src/core_commands/history.rs index fcff71f51..db7c1d661 100644 --- a/crates/nu-command/src/core_commands/history.rs +++ b/crates/nu-command/src/core_commands/history.rs @@ -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 { + 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, + }, + ] + } } diff --git a/crates/nu-command/src/filters/shuffle.rs b/crates/nu-command/src/filters/shuffle.rs index 89a4209bd..0f121d0cd 100644 --- a/crates/nu-command/src/filters/shuffle.rs +++ b/crates/nu-command/src/filters/shuffle.rs @@ -37,8 +37,7 @@ impl Command for Shuffle { fn examples(&self) -> Vec { 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, }] diff --git a/crates/nu-command/src/formats/from/yaml.rs b/crates/nu-command/src/formats/from/yaml.rs index 64a6d8ded..0fe0e50f8 100644 --- a/crates/nu-command/src/formats/from/yaml.rs +++ b/crates/nu-command/src/formats/from/yaml.rs @@ -25,42 +25,7 @@ impl Command for FromYaml { } fn examples(&self) -> Vec { - 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 { + get_examples() + } } fn convert_yaml_value_to_nu_value(v: &serde_yaml::Value, span: Span) -> Result { @@ -205,6 +174,45 @@ pub fn from_yaml_string_to_value(s: String, span: Span) -> Result Vec { + 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 { let concat_string = input.collect_string("", config)?; diff --git a/crates/nu-command/src/viewers/table.rs b/crates/nu-command/src/viewers/table.rs index 513e49cb7..c21771a89 100644 --- a/crates/nu-command/src/viewers/table.rs +++ b/crates/nu-command/src/viewers/table.rs @@ -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 { + 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( diff --git a/crates/nu-command/tests/format_conversions/html.rs b/crates/nu-command/tests/format_conversions/html.rs index 121091ad0..a5a71427d 100644 --- a/crates/nu-command/tests/format_conversions/html.rs +++ b/crates/nu-command/tests/format_conversions/html.rs @@ -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"Get the command history

Usage:
> history {flags}

Flags:
-h, --help
Display this help message
-c, --clear
Clears out the history entries

" + r"Change directory.

Usage:
> cd (path)

Flags:
-h, --help
Display this help message

Parameters:
(optional) path: the path to change to

Examples:
Change to your home directory
> cd ~

" ); }