diff options
author | einfachIrgendwer0815 <85333734+einfachIrgendwer0815@users.noreply.github.com> | 2023-10-20 19:41:48 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-20 19:41:48 +0200 |
commit | 3d87b25b190e0990e0e75a2ab8f994d6c277d263 (patch) | |
tree | 6b9f88ec613749c08bb667fa78989437ebce5430 | |
parent | f2f6902279be0bd8daa463486ddb9d10dc2dbcc6 (diff) |
Add unicode/caret replacements for all control characters (#2712)
-rw-r--r-- | src/preprocessor.rs | 46 | ||||
-rw-r--r-- | tests/examples/control_characters.txt | bin | 0 -> 33 bytes | |||
-rw-r--r-- | tests/integration_tests.rs | 19 | ||||
-rw-r--r-- | tests/syntax-tests/highlighted/Plaintext/plaintext.txt | 52 |
4 files changed, 66 insertions, 51 deletions
diff --git a/src/preprocessor.rs b/src/preprocessor.rs index ab19322b..3328f3b6 100644 --- a/src/preprocessor.rs +++ b/src/preprocessor.rs @@ -91,31 +91,27 @@ pub fn replace_nonprintable( }); line_idx = 0; } - // carriage return - '\x0D' => output.push_str(match nonprintable_notation { - NonprintableNotation::Caret => "^M", - NonprintableNotation::Unicode => "␍", - }), - // null - '\x00' => output.push_str(match nonprintable_notation { - NonprintableNotation::Caret => "^@", - NonprintableNotation::Unicode => "␀", - }), - // bell - '\x07' => output.push_str(match nonprintable_notation { - NonprintableNotation::Caret => "^G", - NonprintableNotation::Unicode => "␇", - }), - // backspace - '\x08' => output.push_str(match nonprintable_notation { - NonprintableNotation::Caret => "^H", - NonprintableNotation::Unicode => "␈", - }), - // escape - '\x1B' => output.push_str(match nonprintable_notation { - NonprintableNotation::Caret => "^[", - NonprintableNotation::Unicode => "␛", - }), + // ASCII control characters + '\x00'..='\x1F' => { + let c = u32::from(chr); + + match nonprintable_notation { + NonprintableNotation::Caret => { + let caret_character = char::from_u32(0x40 + c).unwrap(); + write!(output, "^{caret_character}").ok(); + } + + NonprintableNotation::Unicode => { + let replacement_symbol = char::from_u32(0x2400 + c).unwrap(); + output.push(replacement_symbol) + } + } + } + // delete + '\x7F' => match nonprintable_notation { + NonprintableNotation::Caret => output.push_str("^?"), + NonprintableNotation::Unicode => output.push('\u{2421}'), + }, // printable ASCII c if c.is_ascii_alphanumeric() || c.is_ascii_punctuation() diff --git a/tests/examples/control_characters.txt b/tests/examples/control_characters.txt Binary files differnew file mode 100644 index 00000000..378445f7 --- /dev/null +++ b/tests/examples/control_characters.txt diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 8d2d3624..be70fdca 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -1728,6 +1728,25 @@ fn show_all_with_caret_notation() { .assert() .stdout("hello·world^J\n├──┤^M^@^G^H^[") .stderr(""); + + bat() + .arg("--show-all") + .arg("--nonprintable-notation=caret") + .arg("control_characters.txt") + .assert() + .stdout("^@^A^B^C^D^E^F^G^H├─┤^J\n^K^L^M^N^O^P^Q^R^S^T^U^V^W^X^Y^Z^[^\\^]^^^_^?") + .stderr(""); +} + +#[test] +fn show_all_with_unicode() { + bat() + .arg("--show-all") + .arg("--nonprintable-notation=unicode") + .arg("control_characters.txt") + .assert() + .stdout("␀␁␂␃␄␅␆␇␈├─┤␊\n␋␌␍␎␏␐␑␒␓␔␕␖␗␘␙␚␛␜␝␞␟␡") + .stderr(""); } #[test] diff --git a/tests/syntax-tests/highlighted/Plaintext/plaintext.txt b/tests/syntax-tests/highlighted/Plaintext/plaintext.txt index 1f27a79f..6eb229d2 100644 --- a/tests/syntax-tests/highlighted/Plaintext/plaintext.txt +++ b/tests/syntax-tests/highlighted/Plaintext/plaintext.txt @@ -1,36 +1,36 @@ [38;2;166;226;46m␀[0m[38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{1}[0m[38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{2}[0m[38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{3}[0m[38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{4}[0m[38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{5}[0m[38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{6}[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␁[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␂[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␃[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␄[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␅[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␆[0m[38;2;249;38;114m␊[0m [38;2;166;226;46m␇[0m[38;2;249;38;114m␊[0m [38;2;166;226;46m␈[0m[38;2;249;38;114m␊[0m [38;2;190;132;255m├──┤[0m[38;2;249;38;114m␊[0m [38;2;249;38;114m␊[0m [38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{b}[0m[38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{c}[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␋[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␌[0m[38;2;249;38;114m␊[0m [38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{e}[0m[38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{f}[0m[38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{10}[0m[38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{11}[0m[38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{12}[0m[38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{13}[0m[38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{14}[0m[38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{15}[0m[38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{16}[0m[38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{17}[0m[38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{18}[0m[38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{19}[0m[38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{1a}[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␎[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␏[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␐[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␑[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␒[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␓[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␔[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␕[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␖[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␗[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␘[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␙[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␚[0m[38;2;249;38;114m␊[0m [38;2;166;226;46m␛[0m[38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{1c}[0m[38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{1d}[0m[38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{1e}[0m[38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{1f}[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␜[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␝[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␞[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␟[0m[38;2;249;38;114m␊[0m [38;2;102;217;239m·[0m[38;2;249;38;114m␊[0m [38;2;248;248;242m![0m[38;2;249;38;114m␊[0m [38;2;248;248;242m"[0m[38;2;249;38;114m␊[0m @@ -126,7 +126,7 @@ [38;2;248;248;242m|[0m[38;2;249;38;114m␊[0m [38;2;248;248;242m}[0m[38;2;249;38;114m␊[0m [38;2;248;248;242m~[0m[38;2;249;38;114m␊[0m -[38;2;117;113;94m\u{7f}[0m[38;2;249;38;114m␊[0m +[38;2;248;248;242m␡[0m[38;2;249;38;114m␊[0m [38;2;117;113;94m\u{80}[0m[38;2;249;38;114m␊[0m [38;2;117;113;94m\u{81}[0m[38;2;249;38;114m␊[0m [38;2;117;113;94m\u{82}[0m[38;2;249;38;114m␊[0m |