From 39cf43ef067063afb3f07524a74817b0a8132898 Mon Sep 17 00:00:00 2001 From: Olilin1 <64328283+Olilin1@users.noreply.github.com> Date: Fri, 1 Mar 2024 14:11:13 +0100 Subject: [PATCH] Fix: string_to_table in ssv.rs now filters comments. (issue #11997) (#12035) - Fixes #11997 # Description Fixes the issue that comments are not ignored in SSV formatted data. ![Fix image](https://github.com/nushell/nushell/assets/64328283/1c1bd7dd-ced8-4276-8c21-b50e1c0dba53) # User-Facing Changes If you have a comment in the beginning of SSV formatted data it is now not included in the SSV table. # Tests + Formatting The PR adds one test in the ssv.rs file. All previous test-cases are still passing. Clippy and Fmt have been ran. --- crates/nu-command/src/formats/from/ssv.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/crates/nu-command/src/formats/from/ssv.rs b/crates/nu-command/src/formats/from/ssv.rs index 2fdb40ee80..7df3b809da 100644 --- a/crates/nu-command/src/formats/from/ssv.rs +++ b/crates/nu-command/src/formats/from/ssv.rs @@ -239,7 +239,9 @@ fn string_to_table( aligned_columns: bool, split_at: usize, ) -> Vec> { - let mut lines = s.lines().filter(|l| !l.trim().is_empty()); + let mut lines = s + .lines() + .filter(|l| !l.trim().is_empty() && !l.trim().starts_with('#')); let separator = " ".repeat(std::cmp::max(split_at, 1)); let (ls, header_options) = if noheaders { @@ -314,6 +316,24 @@ mod tests { (String::from(x), String::from(y)) } + #[test] + fn it_filters_comment_lines() { + let input = r#" + a b + 1 2 + 3 4 + #comment line + "#; + let result = string_to_table(input, false, true, 1); + assert_eq!( + result, + vec![ + vec![owned("a", "1"), owned("b", "2")], + vec![owned("a", "3"), owned("b", "4")] + ] + ); + } + #[test] fn it_trims_empty_and_whitespace_only_lines() { let input = r#"