1
0
Эх сурвалжийг харах

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.
Benjamin Sago 8 жил өмнө
parent
commit
eb7e53ef6c

+ 15 - 7
src/output/mod.rs

@@ -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>();
-
-        bits.push(colours.broken_arrow.paint(escaped_name));
+        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.
+
+            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 - 0
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]          

+ 16 - 0
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}]

+ 6 - 0
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}]            

+ 4 - 0
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