From 50cbf91bc56d37bdb145646f68f9478c2728007f Mon Sep 17 00:00:00 2001 From: Chris Gillespie <6572184+gillespiecd@users.noreply.github.com> Date: Wed, 16 Sep 2020 12:59:32 -0700 Subject: [PATCH] Remove trim in favor of str trim (#2560) --- crates/nu-cli/src/cli.rs | 1 - crates/nu-cli/src/commands.rs | 2 - crates/nu-cli/src/commands/trim.rs | 95 ------------------- crates/nu-cli/tests/commands/last.rs | 2 +- crates/nu-cli/tests/commands/lines.rs | 2 +- crates/nu-cli/tests/commands/mod.rs | 1 - crates/nu-cli/tests/commands/move_/column.rs | 6 +- crates/nu-cli/tests/commands/reverse.rs | 2 +- crates/nu-cli/tests/commands/sort_by.rs | 4 +- crates/nu-cli/tests/commands/split_column.rs | 2 +- crates/nu-cli/tests/commands/split_row.rs | 2 +- crates/nu-cli/tests/commands/trim.rs | 85 ----------------- crates/nu-cli/tests/commands/where_.rs | 2 +- crates/nu-cli/tests/format_conversions/csv.rs | 4 +- docs/commands/trim.md | 65 ------------- docs/sample_config/config.toml | 2 +- 16 files changed, 14 insertions(+), 263 deletions(-) delete mode 100644 crates/nu-cli/src/commands/trim.rs delete mode 100644 crates/nu-cli/tests/commands/trim.rs delete mode 100644 docs/commands/trim.md diff --git a/crates/nu-cli/src/cli.rs b/crates/nu-cli/src/cli.rs index a11dd415ed..51f43ede4d 100644 --- a/crates/nu-cli/src/cli.rs +++ b/crates/nu-cli/src/cli.rs @@ -138,7 +138,6 @@ pub fn create_default_context( whole_stream_command(SplitRow), whole_stream_command(SplitChars), whole_stream_command(Lines), - whole_stream_command(Trim), whole_stream_command(Echo), whole_stream_command(Parse), whole_stream_command(Str), diff --git a/crates/nu-cli/src/commands.rs b/crates/nu-cli/src/commands.rs index 4fcbebb53b..4f1ce3825c 100644 --- a/crates/nu-cli/src/commands.rs +++ b/crates/nu-cli/src/commands.rs @@ -116,7 +116,6 @@ pub(crate) mod to_tsv; pub(crate) mod to_url; pub(crate) mod to_xml; pub(crate) mod to_yaml; -pub(crate) mod trim; pub(crate) mod uniq; pub(crate) mod update; pub(crate) mod url_; @@ -261,7 +260,6 @@ pub(crate) use to_url::ToURL; pub(crate) use to_xml::ToXML; pub(crate) use to_yaml::ToYAML; pub(crate) use touch::Touch; -pub(crate) use trim::Trim; pub(crate) use uniq::Uniq; pub(crate) use url_::{UrlCommand, UrlHost, UrlPath, UrlQuery, UrlScheme}; pub(crate) use version::Version; diff --git a/crates/nu-cli/src/commands/trim.rs b/crates/nu-cli/src/commands/trim.rs deleted file mode 100644 index a324eb9093..0000000000 --- a/crates/nu-cli/src/commands/trim.rs +++ /dev/null @@ -1,95 +0,0 @@ -use crate::commands::WholeStreamCommand; - -use crate::prelude::*; -use nu_errors::ShellError; -use nu_protocol::{Dictionary, Primitive, ReturnSuccess, Signature, UntaggedValue, Value}; - -pub struct Trim; - -#[async_trait] -impl WholeStreamCommand for Trim { - fn name(&self) -> &str { - "trim" - } - - fn signature(&self) -> Signature { - Signature::build("trim") - } - - fn usage(&self) -> &str { - "Trim leading and following whitespace from text data." - } - - async fn run( - &self, - args: CommandArgs, - registry: &CommandRegistry, - ) -> Result { - trim(args, registry) - } - - fn examples(&self) -> Vec { - vec![Example { - description: "Trims surrounding whitespace and outputs \"Hello world\"", - example: "echo \" Hello world\" | trim", - result: Some(vec![Value::from("Hello world")]), - }] - } -} - -fn trim_primitive(p: &mut Primitive) { - match p { - Primitive::String(s) | Primitive::Line(s) => *p = Primitive::String(s.trim().to_string()), - Primitive::Nothing - | Primitive::Int(_) - | Primitive::Decimal(_) - | Primitive::Filesize(_) - | Primitive::ColumnPath(_) - | Primitive::Pattern(_) - | Primitive::Boolean(_) - | Primitive::Date(_) - | Primitive::Duration(_) - | Primitive::Range(_) - | Primitive::Path(_) - | Primitive::Binary(_) - | Primitive::BeginningOfStream - | Primitive::EndOfStream => (), - } -} - -fn trim_row(d: &mut Dictionary) { - for (_, mut value) in d.entries.iter_mut() { - trim_value(&mut value); - } -} - -fn trim_value(v: &mut Value) { - match &mut v.value { - UntaggedValue::Primitive(p) => trim_primitive(p), - UntaggedValue::Row(row) => trim_row(row), - _ => (), - }; -} - -fn trim(args: CommandArgs, _registry: &CommandRegistry) -> Result { - Ok(args - .input - .map(|v| { - let mut trimmed = v; - trim_value(&mut trimmed); - ReturnSuccess::value(trimmed) - }) - .to_output_stream()) -} - -#[cfg(test)] -mod tests { - use super::Trim; - - #[test] - fn examples_work_as_expected() { - use crate::examples::test as test_examples; - - test_examples(Trim {}) - } -} diff --git a/crates/nu-cli/tests/commands/last.rs b/crates/nu-cli/tests/commands/last.rs index a8ee9c25d5..18ab4b0c50 100644 --- a/crates/nu-cli/tests/commands/last.rs +++ b/crates/nu-cli/tests/commands/last.rs @@ -6,7 +6,7 @@ use nu_test_support::{nu, pipeline}; fn gets_the_last_row() { let actual = nu!( cwd: "tests/fixtures/formats", - "ls | sort-by name | last 1 | get name | trim | echo $it" + "ls | sort-by name | last 1 | get name | str trim | echo $it" ); assert_eq!(actual.out, "utf16.ini"); diff --git a/crates/nu-cli/tests/commands/lines.rs b/crates/nu-cli/tests/commands/lines.rs index db165bc56d..6f394fad87 100644 --- a/crates/nu-cli/tests/commands/lines.rs +++ b/crates/nu-cli/tests/commands/lines.rs @@ -12,7 +12,7 @@ fn lines() { | first 1 | split column "=" | get Column1 - | trim + | str trim | echo $it "# )); diff --git a/crates/nu-cli/tests/commands/mod.rs b/crates/nu-cli/tests/commands/mod.rs index f29e7c8906..65ab993c86 100644 --- a/crates/nu-cli/tests/commands/mod.rs +++ b/crates/nu-cli/tests/commands/mod.rs @@ -50,7 +50,6 @@ mod split_column; mod split_row; mod str_; mod touch; -mod trim; mod uniq; mod update; mod where_; diff --git a/crates/nu-cli/tests/commands/move_/column.rs b/crates/nu-cli/tests/commands/move_/column.rs index 62c749ee84..88ee8e6c12 100644 --- a/crates/nu-cli/tests/commands/move_/column.rs +++ b/crates/nu-cli/tests/commands/move_/column.rs @@ -25,7 +25,7 @@ fn moves_a_column_before() { | move column column99 --before column1 | rename chars | get chars - | trim + | str trim | str collect | echo $it "# @@ -60,7 +60,7 @@ fn moves_columns_before() { | move column column99 column3 --before column2 | rename _ chars_1 chars_2 | get chars_2 chars_1 - | trim + | str trim | str collect | echo $it "# @@ -96,7 +96,7 @@ fn moves_a_column_after() { | move column letters and_more --before column2 | rename _ chars_1 chars_2 | get chars_1 chars_2 - | trim + | str trim | str collect | echo $it "# diff --git a/crates/nu-cli/tests/commands/reverse.rs b/crates/nu-cli/tests/commands/reverse.rs index 6e6a993ef8..5b0231a8dc 100644 --- a/crates/nu-cli/tests/commands/reverse.rs +++ b/crates/nu-cli/tests/commands/reverse.rs @@ -4,7 +4,7 @@ use nu_test_support::nu; fn can_get_reverse_first() { let actual = nu!( cwd: "tests/fixtures/formats", - "ls | sort-by name | reverse | first 1 | get name | trim | echo $it" + "ls | sort-by name | reverse | first 1 | get name | str trim | echo $it" ); assert_eq!(actual.out, "utf16.ini"); diff --git a/crates/nu-cli/tests/commands/sort_by.rs b/crates/nu-cli/tests/commands/sort_by.rs index a155c38187..6b0afde9ff 100644 --- a/crates/nu-cli/tests/commands/sort_by.rs +++ b/crates/nu-cli/tests/commands/sort_by.rs @@ -14,7 +14,7 @@ fn by_column() { | skip 1 | first 1 | get Column1 - | trim + | str trim | echo $it "# )); @@ -36,7 +36,7 @@ fn by_invalid_column() { | skip 1 | first 1 | get Column1 - | trim + | str trim | echo $it "# )); diff --git a/crates/nu-cli/tests/commands/split_column.rs b/crates/nu-cli/tests/commands/split_column.rs index fe2358dc7b..8c6d9250e5 100644 --- a/crates/nu-cli/tests/commands/split_column.rs +++ b/crates/nu-cli/tests/commands/split_column.rs @@ -17,7 +17,7 @@ fn to_column() { r#" open sample.txt | lines - | trim + | str trim | split column "," | get Column2 | echo $it diff --git a/crates/nu-cli/tests/commands/split_row.rs b/crates/nu-cli/tests/commands/split_row.rs index 19fb22f862..ce006e8e6b 100644 --- a/crates/nu-cli/tests/commands/split_row.rs +++ b/crates/nu-cli/tests/commands/split_row.rs @@ -17,7 +17,7 @@ fn to_row() { r#" open sample.txt | lines - | trim + | str trim | split row "," | count | echo $it diff --git a/crates/nu-cli/tests/commands/trim.rs b/crates/nu-cli/tests/commands/trim.rs deleted file mode 100644 index dd1e1c8129..0000000000 --- a/crates/nu-cli/tests/commands/trim.rs +++ /dev/null @@ -1,85 +0,0 @@ -use nu_test_support::fs::Stub::FileWithContent; -use nu_test_support::playground::Playground; -use nu_test_support::{nu, pipeline}; - -#[test] -fn string() { - Playground::setup("trim_test_1", |dirs, _sandbox| { - let test_strings = ["\n", " \n ", "\thi\n\n", "\u{2003}a"]; - assert!(test_strings[3].chars().count() == 2); - - for test_string in &test_strings { - let commandline = format!( - r#" - echo {} - | trim - "#, - test_string - ); - let actual = nu!( - cwd: dirs.test(), pipeline(&commandline - )); - assert_eq!(actual.out, test_string.trim()) - } - }) -} - -#[test] -fn row() { - Playground::setup("trim_test_2", |dirs, sandbox| { - sandbox.with_files(vec![ - FileWithContent("lines.csv", "lines\n l0\n\tl1\n l2\t \n\n"), - FileWithContent("lines_trimmed.csv", "lines\nl0\nl1\nl2\n"), - ]); - - let actual = nu!( - cwd: dirs.test(), pipeline( - r#" - open lines.csv - | trim - "# - )); - - let expected = nu!( - cwd: dirs.test(), pipeline( - r#" - open lines_trimmed.csv - "# - )); - - assert_eq!(actual.out, expected.out) - }) -} - -#[test] -fn nested() { - Playground::setup("trim_test_3", |dirs, sandbox| { - sandbox.with_files(vec![ - FileWithContent( - "nested.json", - r#"{ "l0" : {"l1": {"l2" : {"a" : " s0", "b" : "\t\ts1\n"} } } }"#, - ), - FileWithContent( - "nested_trimmed.json", - r#"{ "l0" : {"l1": {"l2" : {"a" : "s0", "b" : "s1"} } } }"#, - ), - ]); - - let actual = nu!( - cwd: dirs.test(), pipeline( - r#" - open nested.json - | trim - "# - )); - - let expected = nu!( - cwd: dirs.test(), pipeline( - r#" - open nested_trimmed.json - "# - )); - - assert_eq!(actual.out, expected.out) - }) -} diff --git a/crates/nu-cli/tests/commands/where_.rs b/crates/nu-cli/tests/commands/where_.rs index 9f6b1b88e6..fbb61ac3ec 100644 --- a/crates/nu-cli/tests/commands/where_.rs +++ b/crates/nu-cli/tests/commands/where_.rs @@ -7,7 +7,7 @@ use nu_test_support::pipeline; fn filters_by_unit_size_comparison() { let actual = nu!( cwd: "tests/fixtures/formats", - "ls | where size > 1kb | sort-by size | get name | first 1 | trim | echo $it" + "ls | where size > 1kb | sort-by size | get name | first 1 | str trim | echo $it" ); assert_eq!(actual.out, "cargo_sample.toml"); diff --git a/crates/nu-cli/tests/format_conversions/csv.rs b/crates/nu-cli/tests/format_conversions/csv.rs index 47811e12ea..8f7256231c 100644 --- a/crates/nu-cli/tests/format_conversions/csv.rs +++ b/crates/nu-cli/tests/format_conversions/csv.rs @@ -29,7 +29,7 @@ fn table_to_csv_text() { r#" open csv_text_sample.txt | lines - | trim + | str trim | split column "," a b c d origin | last 1 | to csv @@ -62,7 +62,7 @@ fn table_to_csv_text_skipping_headers_after_conversion() { r#" open csv_text_sample.txt | lines - | trim + | str trim | split column "," a b c d origin | last 1 | to csv --headerless diff --git a/docs/commands/trim.md b/docs/commands/trim.md deleted file mode 100644 index 584698279a..0000000000 --- a/docs/commands/trim.md +++ /dev/null @@ -1,65 +0,0 @@ -# trim - -Trim leading and following whitespace from text data - -## Example - -```shell -> echo " Hello world" - Hello world -``` - -```shell -> echo " Hello world" | trim -Hello world -``` - -Trim can also be passed a list or table of text, for which it will trim each item individually. -It will fail if any element in the list or table is not of type String. - -```shell -> open greetings.json | to json -[" hi ", " hello ", " wassup "] -``` - -```shell -> open greetings.json | trim | to json -["hi", "hello", "wassup"] -``` - -```shell -> cargo search shells --limit 10 | lines | parse "{crate_name} = {version} #{description}" -━━━┯━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - # │ crate_name │ version │ description -───┼────────────────────┼─────────────────────────────┼────────────────────────────────────────────────────────────────────────────────── - 0 │ shells │ "0.2.0" │ Sugar-coating for invoking shell commands directly from Rust. - 1 │ ion-shell │ "0.0.0" │ The Ion Shell - 2 │ shell-words │ "0.1.0" │ Process command line according to parsing rules of UNIX shell - 3 │ nu │ "0.5.0" │ A shell for the GitHub era - 4 │ dotenv-shell │ "1.0.1" │ Launch a new shell (or another program) with your loaded dotenv - 5 │ shell_completion │ "0.0.1" │ Write shell completion scripts in pure Rust - 6 │ shell-hist │ "0.2.0" │ A CLI tool for inspecting shell history - 7 │ tokei │ "10.0.1" │ A utility that allows you to count code, quickly. - 8 │ rash-shell │ "0.1.0" │ A bourne-compatible shell inspired by dash - 9 │ rust_keylock_shell │ "0.10.0" │ Shell access to the rust-keylock. rust-keylock is a password manager with goals - │ │ │ to be Secure, … -━━━┷━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ -``` - -```shell -> cargo search shells --limit 10 | lines | parse "{crate_name} = {version} #{description}" | trim -━━━┯━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - # │ crate_name │ version │ description -───┼────────────────────┼──────────┼────────────────────────────────────────────────────────────────────────────────── - 0 │ shells │ "0.2.0" │ Sugar-coating for invoking shell commands directly from Rust. - 1 │ ion-shell │ "0.0.0" │ The Ion Shell - 2 │ shell-words │ "0.1.0" │ Process command line according to parsing rules of UNIX shell - 3 │ nu │ "0.5.0" │ A shell for the GitHub era - 4 │ dotenv-shell │ "1.0.1" │ Launch a new shell (or another program) with your loaded dotenv - 5 │ shell_completion │ "0.0.1" │ Write shell completion scripts in pure Rust - 6 │ shell-hist │ "0.2.0" │ A CLI tool for inspecting shell history - 7 │ tokei │ "10.0.1" │ A utility that allows you to count code, quickly. - 8 │ rash-shell │ "0.1.0" │ A bourne-compatible shell inspired by dash - 9 │ rust_keylock_shell │ "0.10.0" │ Shell access to the rust-keylock. rust-keylock is a password manager with goals - │ │ │ to be Secure, … -━━━┷━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ diff --git a/docs/sample_config/config.toml b/docs/sample_config/config.toml index bb212cd938..7dc852e208 100644 --- a/docs/sample_config/config.toml +++ b/docs/sample_config/config.toml @@ -12,7 +12,7 @@ pivot_mode = "auto" ctrlc_exit = false complete_from_path = true rm_always_trash = true -prompt = "echo [ $(ansi gb) $(pwd) $(ansi reset) \"(\" $(ansi cb) $(do -i { git rev-parse --abbrev-ref HEAD | trim }) $(ansi reset) \")\" $(char newline) $(ansi yb) $(date --format \"%m/%d/%Y %I:%M:%S%.3f %p\" --raw) $(ansi reset) \"> \" ] | str collect" +prompt = "echo [ $(ansi gb) $(pwd) $(ansi reset) \"(\" $(ansi cb) $(do -i { git rev-parse --abbrev-ref HEAD | str trim }) $(ansi reset) \")\" $(char newline) $(ansi yb) $(date --format \"%m/%d/%Y %I:%M:%S%.3f %p\" --raw) $(ansi reset) \"> \" ] | str collect" # for each of the options in the color_config section, you are able to set # the color alone or with one of the following attributes.