Remove trim in favor of str trim (#2560)

This commit is contained in:
Chris Gillespie 2020-09-16 12:59:32 -07:00 committed by GitHub
parent d05f9b3b1e
commit 50cbf91bc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 14 additions and 263 deletions

View File

@ -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),

View File

@ -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;

View File

@ -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<OutputStream, ShellError> {
trim(args, registry)
}
fn examples(&self) -> Vec<Example> {
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<OutputStream, ShellError> {
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 {})
}
}

View File

@ -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");

View File

@ -12,7 +12,7 @@ fn lines() {
| first 1
| split column "="
| get Column1
| trim
| str trim
| echo $it
"#
));

View File

@ -50,7 +50,6 @@ mod split_column;
mod split_row;
mod str_;
mod touch;
mod trim;
mod uniq;
mod update;
mod where_;

View File

@ -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
"#

View File

@ -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");

View File

@ -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
"#
));

View File

@ -17,7 +17,7 @@ fn to_column() {
r#"
open sample.txt
| lines
| trim
| str trim
| split column ","
| get Column2
| echo $it

View File

@ -17,7 +17,7 @@ fn to_row() {
r#"
open sample.txt
| lines
| trim
| str trim
| split row ","
| count
| echo $it

View File

@ -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)
})
}

View File

@ -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");

View File

@ -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

View File

@ -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, …
━━━┷━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

View File

@ -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.