From 42f1874a3aae2e3bc3c669cb0aac0132a586ea24 Mon Sep 17 00:00:00 2001 From: Justin Ma Date: Tue, 1 Mar 2022 21:05:29 +0800 Subject: [PATCH] Update some examples and docs (#4682) * Update some examples and docs * Update now.rs * Update date_now.md Co-authored-by: JT <547158+jntrnr@users.noreply.github.com> --- .../nu-command/src/conversions/into/bool.rs | 2 +- crates/nu-command/src/core_commands/debug.rs | 27 ++++++++++++++----- crates/nu-command/src/date/now.rs | 5 ++++ crates/nu-command/src/filters/each.rs | 11 ++++++++ crates/nu-command/src/filters/par_each.rs | 25 ++++++++++++----- docs/commands/date_now.md | 5 ++++ docs/commands/debug.md | 7 ++++- docs/commands/each.md | 5 ++++ docs/commands/into_bool.md | 2 +- docs/commands/par-each.md | 5 ++++ src/tests/test_engine.rs | 2 +- 11 files changed, 80 insertions(+), 16 deletions(-) diff --git a/crates/nu-command/src/conversions/into/bool.rs b/crates/nu-command/src/conversions/into/bool.rs index 44e3b43253..88e14fee81 100644 --- a/crates/nu-command/src/conversions/into/bool.rs +++ b/crates/nu-command/src/conversions/into/bool.rs @@ -80,7 +80,7 @@ impl Command for SubCommand { result: Some(Value::boolean(true, span)), }, Example { - description: "convert decimal to boolean", + description: "convert integer to boolean", example: "1 | into bool", result: Some(Value::boolean(true, span)), }, diff --git a/crates/nu-command/src/core_commands/debug.rs b/crates/nu-command/src/core_commands/debug.rs index 1a0d618172..e0cc0c952e 100644 --- a/crates/nu-command/src/core_commands/debug.rs +++ b/crates/nu-command/src/core_commands/debug.rs @@ -1,6 +1,6 @@ use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; -use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, Value}; +use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, Span, Value}; #[derive(Clone)] pub struct Debug; @@ -52,11 +52,26 @@ impl Command for Debug { } fn examples(&self) -> Vec { - vec![Example { - description: "Describe the type of a string", - example: "'hello' | debug", - result: Some(Value::test_string("hello")), - }] + vec![ + Example { + description: "Print the value of a string", + example: "'hello' | debug", + result: Some(Value::test_string("hello")), + }, + Example { + description: "Print the value of a table", + example: + "echo [[version patch]; [0.1.0 $false] [0.1.1 $true] [0.2.0 $false]] | debug", + result: Some(Value::List { + vals: vec![ + Value::test_string("{version: 0.1.0, patch: false}"), + Value::test_string("{version: 0.1.1, patch: true}"), + Value::test_string("{version: 0.2.0, patch: false}"), + ], + span: Span::test_data(), + }), + }, + ] } } diff --git a/crates/nu-command/src/date/now.rs b/crates/nu-command/src/date/now.rs index b1c602de76..d53cc4e6df 100644 --- a/crates/nu-command/src/date/now.rs +++ b/crates/nu-command/src/date/now.rs @@ -51,6 +51,11 @@ impl Command for SubCommand { example: r#"(date now) - 2019-05-01T04:12:05.20+08:00"#, result: None, }, + Example { + description: "Get current time in full RFC3339 format with timezone", + example: r#"date now | debug"#, + result: None, + }, ] } } diff --git a/crates/nu-command/src/filters/each.rs b/crates/nu-command/src/filters/each.rs index 495e6c56cc..6ac8d1acc5 100644 --- a/crates/nu-command/src/filters/each.rs +++ b/crates/nu-command/src/filters/each.rs @@ -79,6 +79,17 @@ impl Command for Each { span: Span::test_data(), }), }, + Example { + example: r#"[1 2 3] | each -n { |it| if $it.item == 2 { echo $"found 2 at ($it.index)!"} }"#, + description: "Iterate over each element, print the matching value and it's index", + result: Some(Value::List { + vals: vec![Value::String { + val: "found 2 at 1!".to_string(), + span: Span::test_data(), + }], + span: Span::test_data(), + }), + }, Example { example: r#"[1 2 3] | each --keep-empty { |it| if $it == 2 { echo "found 2!"} }"#, description: "Iterate over each element, keeping all results", diff --git a/crates/nu-command/src/filters/par_each.rs b/crates/nu-command/src/filters/par_each.rs index 6f1542a978..32a2042553 100644 --- a/crates/nu-command/src/filters/par_each.rs +++ b/crates/nu-command/src/filters/par_each.rs @@ -3,7 +3,7 @@ use nu_protocol::ast::Call; use nu_protocol::engine::{CaptureBlock, Command, EngineState, Stack}; use nu_protocol::{ Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, Signature, - SyntaxShape, Value, + Span, SyntaxShape, Value, }; use rayon::prelude::*; @@ -31,11 +31,24 @@ impl Command for ParEach { } fn examples(&self) -> Vec { - vec![Example { - example: "[1 2 3] | par-each { |it| 2 * $it }", - description: "Multiplies elements in list", - result: None, - }] + vec![ + Example { + example: "[1 2 3] | par-each { |it| 2 * $it }", + description: "Multiplies elements in list", + result: None, + }, + Example { + example: r#"[1 2 3] | par-each -n { |it| if $it.item == 2 { echo $"found 2 at ($it.index)!"} }"#, + description: "Iterate over each element, print the matching value and it's index", + result: Some(Value::List { + vals: vec![Value::String { + val: "found 2 at 1!".to_string(), + span: Span::test_data(), + }], + span: Span::test_data(), + }), + }, + ] } fn run( diff --git a/docs/commands/date_now.md b/docs/commands/date_now.md index 17146ff344..e29fd6ea6e 100644 --- a/docs/commands/date_now.md +++ b/docs/commands/date_now.md @@ -26,3 +26,8 @@ Get the time duration since a more accurate time ```shell > (date now) - 2019-05-01T04:12:05.20+08:00 ``` + +Get current time in full RFC3339 format with timezone +```shell +> date now | debug +``` diff --git a/docs/commands/debug.md b/docs/commands/debug.md index e240d02115..5f4c2585ed 100644 --- a/docs/commands/debug.md +++ b/docs/commands/debug.md @@ -16,7 +16,12 @@ Debug print the value(s) piped in. ## Examples -Describe the type of a string +Print the value of a string ```shell > 'hello' | debug ``` + +Print the value of a table +```shell +> echo [[version patch]; [0.1.0 $false] [0.1.1 $true] [0.2.0 $false]] | debug +``` diff --git a/docs/commands/each.md b/docs/commands/each.md index d54ce095c9..19356c7e39 100644 --- a/docs/commands/each.md +++ b/docs/commands/each.md @@ -28,6 +28,11 @@ Iterate over each element, keeping only values that succeed > [1 2 3] | each { |it| if $it == 2 { echo "found 2!"} } ``` +Iterate over each element, print the matching value and it's index +```shell +> [1 2 3] | each -n { |it| if $it.item == 2 { echo $"found 2 at ($it.index)!"} } +``` + Iterate over each element, keeping all results ```shell > [1 2 3] | each --keep-empty { |it| if $it == 2 { echo "found 2!"} } diff --git a/docs/commands/into_bool.md b/docs/commands/into_bool.md index f57dfe642b..9767c30a1a 100644 --- a/docs/commands/into_bool.md +++ b/docs/commands/into_bool.md @@ -26,7 +26,7 @@ Convert bool to boolean > $true | into bool ``` -convert decimal to boolean +convert integer to boolean ```shell > 1 | into bool ``` diff --git a/docs/commands/par-each.md b/docs/commands/par-each.md index 8cc3dcfa9a..1d62f12cc0 100644 --- a/docs/commands/par-each.md +++ b/docs/commands/par-each.md @@ -21,3 +21,8 @@ Multiplies elements in list ```shell > [1 2 3] | par-each { |it| 2 * $it } ``` + +Iterate over each element, print the matching value and it's index +```shell +> [1 2 3] | par-each -n { |it| if $it.item == 2 { echo $"found 2 at ($it.index)!"} } +``` diff --git a/src/tests/test_engine.rs b/src/tests/test_engine.rs index 8a3c71ff8a..cfaff509a4 100644 --- a/src/tests/test_engine.rs +++ b/src/tests/test_engine.rs @@ -72,7 +72,7 @@ fn in_variable_6() -> TestResult { #[test] fn help_works_with_missing_requirements() -> TestResult { - run_test(r#"each --help | lines | length"#, "30") + run_test(r#"each --help | lines | length"#, "33") } #[test]