Only highlight escaped characters in file names

Rather than the *entire* file name.

The current method is extremely inefficient, but having control characters in file names is also extremely uncommon; it’s something that should be fixed, only eventually.
This commit is contained in:
Benjamin Sago 2017-05-01 15:06:37 +01:00
parent a53c268c54
commit eb7e53ef6c
5 changed files with 46 additions and 6 deletions

View File

@ -114,13 +114,21 @@ fn coloured_file_name<'a>(file: &File, colours: &Colours) -> Vec<ANSIString<'a>>
bits.push(colour.paint(file.name.clone()));
}
else {
// The `escape_default` method on `char` is *almost* what we want here, but
// it still escapes non-ASCII UTF-8 characters, which are still printable.'
let escaped_name = file.name.chars()
.flat_map(char::escape_default)
.collect::<String>();
for c in file.name.chars() {
// The `escape_default` method on `char` is *almost* what we want here, but
// it still escapes non-ASCII UTF-8 characters, which are still printable.
bits.push(colours.broken_arrow.paint(escaped_name));
if c >= 0x20 as char {
// TODO: This allocates way too much,
// hence the `all` check above.
let mut s = String::new();
s.push(c);
bits.push(colour.paint(s));
} else {
let s = c.escape_default().collect::<String>();
bits.push(colours.broken_arrow.paint(s));
}
}
}
bits

6
xtests/file_names Normal file
View File

@ -0,0 +1,6 @@
ansi: [\u{1b}[34mblue\u{1b}[0m] form-feed: [\u{c}] return: [\r]
ascii: hello invalid-utf8-1: [<5B>] tab: [\t]
backspace: [\u{8}] invalid-utf8-2: [<5B>(] utf-8: pâté
bell: [\u{7}] invalid-utf8-3: [<5B>(] vertical-tab: [\u{b}]
emoji: [🆒] invalid-utf8-4: [<5B>(<28>(]
escape: [\u{1b}] new-line: [\n]

16
xtests/file_names_1 Normal file
View File

@ -0,0 +1,16 @@
ansi: [\u{1b}[34mblue\u{1b}[0m]
ascii: hello
backspace: [\u{8}]
bell: [\u{7}]
emoji: [🆒]
escape: [\u{1b}]
form-feed: [\u{c}]
invalid-utf8-1: [<5B>]
invalid-utf8-2: [<5B>(]
invalid-utf8-3: [<5B>(]
invalid-utf8-4: [<5B>(<28>(]
new-line: [\n]
return: [\r]
tab: [\t]
utf-8: pâté
vertical-tab: [\u{b}]

6
xtests/file_names_x Normal file
View File

@ -0,0 +1,6 @@
ansi: [\u{1b}[34mblue\u{1b}[0m] ascii: hello backspace: [\u{8}]
bell: [\u{7}] emoji: [🆒] escape: [\u{1b}]
form-feed: [\u{c}] invalid-utf8-1: [<5B>] invalid-utf8-2: [<5B>(]
invalid-utf8-3: [<5B>(] invalid-utf8-4: [<5B>(<28>(] new-line: [\n]
return: [\r] tab: [\t] utf-8: pâté
vertical-tab: [\u{b}]

View File

@ -54,6 +54,10 @@ $exa $testcases/passwd -lgh | diff -q - $results/passwd || exit 1
sudo -u cassowary $exa $testcases/permissions -lghR 2>&1 | diff -q - $results/permissions_sudo || exit 1
$exa $testcases/permissions -lghR 2>&1 | diff -q - $results/permissions || exit 1
# File names
COLUMNS=80 $exa $testcases/file-names 2>&1 | diff -q - $results/file_names || exit 1
COLUMNS=80 $exa $testcases/file-names -x 2>&1 | diff -q - $results/file_names_x || exit 1
$exa $testcases/file-names -1 2>&1 | diff -q - $results/file_names_1 || exit 1
# File types
$exa $testcases/file-names-exts -1 2>&1 | diff -q - $results/file-names-exts || exit 1