diff --git a/crates/nu-command/src/filesystem/cd.rs b/crates/nu-command/src/filesystem/cd.rs index 2ad032db81..6e20544f32 100644 --- a/crates/nu-command/src/filesystem/cd.rs +++ b/crates/nu-command/src/filesystem/cd.rs @@ -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 { + vec![Example { + description: "Change to your home directory", + example: r#"cd ~"#, + result: None, + }] + } } diff --git a/crates/nu-command/src/filters/transpose.rs b/crates/nu-command/src/filters/transpose.rs index ac7922ef44..522ac6120e 100644 --- a/crates/nu-command/src/filters/transpose.rs +++ b/crates/nu-command/src/filters/transpose.rs @@ -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 { transpose(engine_state, stack, call, input) } + + fn examples(&self) -> Vec { + 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( diff --git a/crates/nu-command/src/strings/detect_columns.rs b/crates/nu-command/src/strings/detect_columns.rs index 4e3f2f7a81..b5111083c6 100644 --- a/crates/nu-command/src/strings/detect_columns.rs +++ b/crates/nu-command/src/strings/detect_columns.rs @@ -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>; @@ -44,6 +44,37 @@ impl Command for DetectColumns { ) -> Result { detect_columns(engine_state, stack, call, input) } + + fn examples(&self) -> Vec { + 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( diff --git a/crates/nu-command/src/strings/split/column.rs b/crates/nu-command/src/strings/split/column.rs index 6c2f00df0c..f2dbe81b52 100644 --- a/crates/nu-command/src/strings/split/column.rs +++ b/crates/nu-command/src/strings/split/column.rs @@ -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 { split_column(engine_state, stack, call, input) } + + fn examples(&self) -> Vec { + 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( diff --git a/crates/nu-command/src/strings/split/row.rs b/crates/nu-command/src/strings/split/row.rs index d81679a259..93515ff7b1 100644 --- a/crates/nu-command/src/strings/split/row.rs +++ b/crates/nu-command/src/strings/split/row.rs @@ -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 { split_row(engine_state, stack, call, input) } + + fn examples(&self) -> Vec { + 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( diff --git a/crates/nu-command/tests/format_conversions/html.rs b/crates/nu-command/tests/format_conversions/html.rs index e7857e9c6f..121091ad06 100644 --- a/crates/nu-command/tests/format_conversions/html.rs +++ b/crates/nu-command/tests/format_conversions/html.rs @@ -56,7 +56,7 @@ fn test_cd_html_color_flag_dark_false() { ); assert_eq!( actual.out, - r"Change directory.

Usage:
> cd (path)

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

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

" + 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 ~

" ); } @@ -71,21 +71,21 @@ fn test_no_color_flag() { ); assert_eq!( actual.out, - r"Change directory.

Usage:
> cd (path)

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

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

" + 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 ~

" ); } #[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"Change directory.

Usage:
> cd (path)

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

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

" + r"Get the command history

Usage:
> history {flags}

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

" ); }