Add unicode/caret replacements for all control characters (#2712)

This commit is contained in:
einfachIrgendwer0815 2023-10-20 19:41:48 +02:00 committed by GitHub
parent f2f6902279
commit 3d87b25b19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 51 deletions

View File

@ -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()

BIN
tests/examples/control_characters.txt vendored Normal file

Binary file not shown.

View File

@ -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]

View File

@ -1,36 +1,36 @@
␀␊
\u{1}␊
\u{2}␊
\u{3}␊
\u{4}␊
\u{5}␊
\u{6}␊
␁␊
␂␊
␃␊
␄␊
␅␊
␆␊
␇␊
␈␊
├──┤␊
␊
␊
\u{b}␊
\u{c}␊
␋␊
␌␊
␊
\u{e}␊
\u{f}␊
\u{10}␊
\u{11}␊
\u{12}␊
\u{13}␊
\u{14}␊
\u{15}␊
\u{16}␊
\u{17}␊
\u{18}␊
\u{19}␊
\u{1a}␊
␎␊
␏␊
␐␊
␑␊
␒␊
␓␊
␔␊
␕␊
␖␊
␗␊
␘␊
␙␊
␚␊
␛␊
\u{1c}␊
\u{1d}␊
\u{1e}␊
\u{1f}␊
␜␊
␝␊
␞␊
␟␊
·␊
!␊
"␊
@ -126,7 +126,7 @@
|␊
}␊
~␊
\u{7f}␊
␡␊
\u{80}␊
\u{81}␊
\u{82}␊