From 17cd3b59dddefafc625bb12dd4f20c3946b7d3dc Mon Sep 17 00:00:00 2001 From: Narawit Rakket Date: Sun, 2 Oct 2022 18:42:06 +0700 Subject: [PATCH 1/2] fix: do not quote filename when piping to another program --- src/core.rs | 2 ++ src/display.rs | 13 +++++++++-- src/flags.rs | 2 ++ src/meta/name.rs | 61 ++++++++++++++++++++++++++++++------------------ 4 files changed, 53 insertions(+), 25 deletions(-) diff --git a/src/core.rs b/src/core.rs index 407d42c9d..ee6329e70 100644 --- a/src/core.rs +++ b/src/core.rs @@ -71,6 +71,8 @@ impl Core { // Most of the programs does not handle correctly the ansi colors // or require a raw output (like the `wc` command). inner_flags.layout = Layout::OneLine; + + flags.should_quote = false; }; let sorters = sort::assemble_sorters(&flags); diff --git a/src/display.rs b/src/display.rs index 21139a36f..f3bf09fea 100644 --- a/src/display.rs +++ b/src/display.rs @@ -321,8 +321,13 @@ fn get_output( Block::Date => block_vec.push(meta.date.render(colors, flags)), Block::Name => { block_vec.extend([ - meta.name - .render(colors, icons, display_option, flags.hyperlink), + meta.name.render( + colors, + icons, + display_option, + flags.hyperlink, + flags.should_quote, + ), meta.indicator.render(flags), ]); if !(flags.no_symlink.0 || flags.dereference.0 || flags.layout == Layout::Grid) { @@ -441,6 +446,7 @@ mod tests { &Icons::new(icon::Theme::NoIcon, " ".to_string()), &DisplayOption::FileName, HyperlinkOption::Never, + true, ) .to_string(); @@ -475,6 +481,7 @@ mod tests { &Icons::new(icon::Theme::Fancy, " ".to_string()), &DisplayOption::FileName, HyperlinkOption::Never, + true, ) .to_string(); @@ -508,6 +515,7 @@ mod tests { &Icons::new(icon::Theme::NoIcon, " ".to_string()), &DisplayOption::FileName, HyperlinkOption::Never, + true, ) .to_string(); @@ -549,6 +557,7 @@ mod tests { &Icons::new(icon::Theme::NoIcon, " ".to_string()), &DisplayOption::FileName, HyperlinkOption::Never, + true, ) .to_string(); diff --git a/src/flags.rs b/src/flags.rs index 17788c275..296bdd3f9 100644 --- a/src/flags.rs +++ b/src/flags.rs @@ -72,6 +72,7 @@ pub struct Flags { pub symlink_arrow: SymlinkArrow, pub hyperlink: HyperlinkOption, pub header: Header, + pub should_quote: bool, } impl Flags { @@ -101,6 +102,7 @@ impl Flags { symlink_arrow: SymlinkArrow::configure_from(matches, config), hyperlink: HyperlinkOption::configure_from(matches, config), header: Header::configure_from(matches, config), + should_quote: true, }) } } diff --git a/src/meta/name.rs b/src/meta/name.rs index a9e092ca9..2ff18eab0 100644 --- a/src/meta/name.rs +++ b/src/meta/name.rs @@ -78,15 +78,17 @@ impl Name { .collect() } - fn escape(&self, string: &str) -> String { + fn escape(&self, string: &str, should_quote: bool) -> String { let mut name = string.to_string(); - if name.contains('\\') || name.contains('"') { - name = name.replace('\'', "\'\\\'\'"); - name = format!("\'{}\'", &name); - } else if name.contains('\'') { - name = format!("\"{}\"", &name); - } else if name.contains(' ') || name.contains('$') { - name = format!("\'{}\'", &name); + if should_quote { + if name.contains('\\') || name.contains('"') { + name = name.replace('\'', "\'\\\'\'"); + name = format!("\'{}\'", &name); + } else if name.contains('\'') { + name = format!("\"{}\"", &name); + } else if name.contains(' ') || name.contains('$') { + name = format!("\'{}\'", &name); + } } let string = name; if string @@ -144,27 +146,28 @@ impl Name { icons: &Icons, display_option: &DisplayOption, hyperlink: HyperlinkOption, + quote: bool, ) -> ColoredString { let content = match display_option { DisplayOption::FileName => { format!( "{}{}", icons.get(self), - self.hyperlink(self.escape(self.file_name()), hyperlink) + self.hyperlink(self.escape(self.file_name(), quote), hyperlink) ) } DisplayOption::Relative { base_path } => format!( "{}{}", icons.get(self), self.hyperlink( - self.escape(&self.relative_path(base_path).to_string_lossy()), + self.escape(&self.relative_path(base_path).to_string_lossy(), quote), hyperlink ) ), DisplayOption::None => format!( "{}{}", icons.get(self), - self.hyperlink(self.escape(&self.path.to_string_lossy()), hyperlink) + self.hyperlink(self.escape(&self.path.to_string_lossy(), quote), hyperlink) ), }; @@ -254,7 +257,8 @@ mod test { &colors, &icons, &DisplayOption::FileName, - HyperlinkOption::Never + HyperlinkOption::Never, + false, ) ); } @@ -277,7 +281,8 @@ mod test { &colors, &icons, &DisplayOption::FileName, - HyperlinkOption::Never + HyperlinkOption::Never, + false ) ); } @@ -310,7 +315,8 @@ mod test { &colors, &icons, &DisplayOption::FileName, - HyperlinkOption::Never + HyperlinkOption::Never, + false ) ); } @@ -343,7 +349,8 @@ mod test { &colors, &icons, &DisplayOption::FileName, - HyperlinkOption::Never + HyperlinkOption::Never, + false ) ); } @@ -374,7 +381,8 @@ mod test { &colors, &icons, &DisplayOption::FileName, - HyperlinkOption::Never + HyperlinkOption::Never, + false ) ); } @@ -398,7 +406,8 @@ mod test { &colors, &icons, &DisplayOption::FileName, - HyperlinkOption::Never + HyperlinkOption::Never, + false ) .to_string() ); @@ -430,7 +439,8 @@ mod test { &colors, &icons, &DisplayOption::FileName, - HyperlinkOption::Always + HyperlinkOption::Always, + false ) .to_string() ); @@ -650,7 +660,8 @@ mod test { &colors, &icons, &DisplayOption::FileName, - HyperlinkOption::Never + HyperlinkOption::Never, + true, ) ); @@ -668,7 +679,8 @@ mod test { &colors, &icons, &DisplayOption::FileName, - HyperlinkOption::Never + HyperlinkOption::Never, + true, ) ); @@ -686,7 +698,8 @@ mod test { &colors, &icons, &DisplayOption::FileName, - HyperlinkOption::Never + HyperlinkOption::Never, + true, ) ); @@ -706,7 +719,8 @@ mod test { &colors, &icons, &DisplayOption::FileName, - HyperlinkOption::Never + HyperlinkOption::Never, + true, ) ); @@ -726,7 +740,8 @@ mod test { &colors, &icons, &DisplayOption::FileName, - HyperlinkOption::Never + HyperlinkOption::Never, + true, ) ); } From f1572674e3ef5f990977b20bb00af83023527a82 Mon Sep 17 00:00:00 2001 From: Narawit Rakket Date: Thu, 6 Oct 2022 00:00:33 +0700 Subject: [PATCH 2/2] chore: update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b60c097f..2f9a02c96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - In keeping with the coreutils change, add quotes and escapes for necessary filenames from [merelymyself](https://github.com/merelymyself) +### Fixed +- Do not quote filename when piping into another program from [TeamTamoad](https://github.com/TeamTamoad) + ## [0.23.1] - 2022-09-13 ### Fixed