From eb7e53ef6c321e8f3123d87d4f8fb8e3b533f1ee Mon Sep 17 00:00:00 2001 From: Benjamin Sago Date: Mon, 1 May 2017 15:06:37 +0100 Subject: [PATCH] Only highlight escaped characters in file names MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/output/mod.rs | 20 ++++++++++++++------ xtests/file_names | 6 ++++++ xtests/file_names_1 | 16 ++++++++++++++++ xtests/file_names_x | 6 ++++++ xtests/run.sh | 4 ++++ 5 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 xtests/file_names create mode 100644 xtests/file_names_1 create mode 100644 xtests/file_names_x diff --git a/src/output/mod.rs b/src/output/mod.rs index a923c2a..849acf6 100644 --- a/src/output/mod.rs +++ b/src/output/mod.rs @@ -114,13 +114,21 @@ fn coloured_file_name<'a>(file: &File, colours: &Colours) -> Vec> 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::(); + 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::(); + bits.push(colours.broken_arrow.paint(s)); + } + } } bits diff --git a/xtests/file_names b/xtests/file_names new file mode 100644 index 0000000..5120bb4 --- /dev/null +++ b/xtests/file_names @@ -0,0 +1,6 @@ +ansi: [\u{1b}[34mblue\u{1b}[0m] form-feed: [\u{c}] return: [\r] +ascii: hello invalid-utf8-1: [�] tab: [\t] +backspace: [\u{8}] invalid-utf8-2: [�(] utf-8: pâté +bell: [\u{7}] invalid-utf8-3: [�(] vertical-tab: [\u{b}] +emoji: [🆒] invalid-utf8-4: [�(�(] +escape: [\u{1b}] new-line: [\n] diff --git a/xtests/file_names_1 b/xtests/file_names_1 new file mode 100644 index 0000000..b0c2508 --- /dev/null +++ b/xtests/file_names_1 @@ -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: [�] +invalid-utf8-2: [�(] +invalid-utf8-3: [�(] +invalid-utf8-4: [�(�(] +new-line: [\n] +return: [\r] +tab: [\t] +utf-8: pâté +vertical-tab: [\u{b}] diff --git a/xtests/file_names_x b/xtests/file_names_x new file mode 100644 index 0000000..f5f22bc --- /dev/null +++ b/xtests/file_names_x @@ -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: [�] invalid-utf8-2: [�(] +invalid-utf8-3: [�(] invalid-utf8-4: [�(�(] new-line: [\n] +return: [\r] tab: [\t] utf-8: pâté +vertical-tab: [\u{b}] diff --git a/xtests/run.sh b/xtests/run.sh index 33af7b3..b234f16 100755 --- a/xtests/run.sh +++ b/xtests/run.sh @@ -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