Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not quote filename when piping to another program #748

Merged
merged 2 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 11 additions & 2 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -441,6 +446,7 @@ mod tests {
&Icons::new(icon::Theme::NoIcon, " ".to_string()),
&DisplayOption::FileName,
HyperlinkOption::Never,
true,
)
.to_string();

Expand Down Expand Up @@ -475,6 +481,7 @@ mod tests {
&Icons::new(icon::Theme::Fancy, " ".to_string()),
&DisplayOption::FileName,
HyperlinkOption::Never,
true,
)
.to_string();

Expand Down Expand Up @@ -508,6 +515,7 @@ mod tests {
&Icons::new(icon::Theme::NoIcon, " ".to_string()),
&DisplayOption::FileName,
HyperlinkOption::Never,
true,
)
.to_string();

Expand Down Expand Up @@ -549,6 +557,7 @@ mod tests {
&Icons::new(icon::Theme::NoIcon, " ".to_string()),
&DisplayOption::FileName,
HyperlinkOption::Never,
true,
)
.to_string();

Expand Down
2 changes: 2 additions & 0 deletions src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub struct Flags {
pub symlink_arrow: SymlinkArrow,
pub hyperlink: HyperlinkOption,
pub header: Header,
pub should_quote: bool,
}

impl Flags {
Expand Down Expand Up @@ -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,
})
}
}
Expand Down
61 changes: 38 additions & 23 deletions src/meta/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
),
};

Expand Down Expand Up @@ -254,7 +257,8 @@ mod test {
&colors,
&icons,
&DisplayOption::FileName,
HyperlinkOption::Never
HyperlinkOption::Never,
false,
)
);
}
Expand All @@ -277,7 +281,8 @@ mod test {
&colors,
&icons,
&DisplayOption::FileName,
HyperlinkOption::Never
HyperlinkOption::Never,
false
)
);
}
Expand Down Expand Up @@ -310,7 +315,8 @@ mod test {
&colors,
&icons,
&DisplayOption::FileName,
HyperlinkOption::Never
HyperlinkOption::Never,
false
)
);
}
Expand Down Expand Up @@ -343,7 +349,8 @@ mod test {
&colors,
&icons,
&DisplayOption::FileName,
HyperlinkOption::Never
HyperlinkOption::Never,
false
)
);
}
Expand Down Expand Up @@ -374,7 +381,8 @@ mod test {
&colors,
&icons,
&DisplayOption::FileName,
HyperlinkOption::Never
HyperlinkOption::Never,
false
)
);
}
Expand All @@ -398,7 +406,8 @@ mod test {
&colors,
&icons,
&DisplayOption::FileName,
HyperlinkOption::Never
HyperlinkOption::Never,
false
)
.to_string()
);
Expand Down Expand Up @@ -430,7 +439,8 @@ mod test {
&colors,
&icons,
&DisplayOption::FileName,
HyperlinkOption::Always
HyperlinkOption::Always,
false
)
.to_string()
);
Expand Down Expand Up @@ -650,7 +660,8 @@ mod test {
&colors,
&icons,
&DisplayOption::FileName,
HyperlinkOption::Never
HyperlinkOption::Never,
true,
)
);

Expand All @@ -668,7 +679,8 @@ mod test {
&colors,
&icons,
&DisplayOption::FileName,
HyperlinkOption::Never
HyperlinkOption::Never,
true,
)
);

Expand All @@ -686,7 +698,8 @@ mod test {
&colors,
&icons,
&DisplayOption::FileName,
HyperlinkOption::Never
HyperlinkOption::Never,
true,
)
);

Expand All @@ -706,7 +719,8 @@ mod test {
&colors,
&icons,
&DisplayOption::FileName,
HyperlinkOption::Never
HyperlinkOption::Never,
true,
)
);

Expand All @@ -726,7 +740,8 @@ mod test {
&colors,
&icons,
&DisplayOption::FileName,
HyperlinkOption::Never
HyperlinkOption::Never,
true,
)
);
}
Expand Down