add quotes and escapes for necessary filenames

This commit is contained in:
merelymyself 2022-09-20 19:00:47 +08:00 committed by Abin Simon
parent ebe7417a88
commit d2e97e01bc
2 changed files with 11 additions and 2 deletions

View file

@ -321,8 +321,7 @@ 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),
meta.indicator.render(flags),
]);
if !(flags.no_symlink.0 || flags.dereference.0 || flags.layout == Layout::Grid) {

View file

@ -79,6 +79,16 @@ impl Name {
}
fn escape(&self, string: &str) -> 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);
}
let string = name;
if string
.chars()
.all(|c| c >= 0x20 as char && c != 0x7f as char)