mirror of
https://github.com/sharkdp/bat.git
synced 2024-11-25 09:13:39 +01:00
Add unicode/caret replacements for all control characters (#2712)
This commit is contained in:
parent
f2f6902279
commit
3d87b25b19
@ -91,31 +91,27 @@ pub fn replace_nonprintable(
|
|||||||
});
|
});
|
||||||
line_idx = 0;
|
line_idx = 0;
|
||||||
}
|
}
|
||||||
// carriage return
|
// ASCII control characters
|
||||||
'\x0D' => output.push_str(match nonprintable_notation {
|
'\x00'..='\x1F' => {
|
||||||
NonprintableNotation::Caret => "^M",
|
let c = u32::from(chr);
|
||||||
NonprintableNotation::Unicode => "␍",
|
|
||||||
}),
|
match nonprintable_notation {
|
||||||
// null
|
NonprintableNotation::Caret => {
|
||||||
'\x00' => output.push_str(match nonprintable_notation {
|
let caret_character = char::from_u32(0x40 + c).unwrap();
|
||||||
NonprintableNotation::Caret => "^@",
|
write!(output, "^{caret_character}").ok();
|
||||||
NonprintableNotation::Unicode => "␀",
|
}
|
||||||
}),
|
|
||||||
// bell
|
NonprintableNotation::Unicode => {
|
||||||
'\x07' => output.push_str(match nonprintable_notation {
|
let replacement_symbol = char::from_u32(0x2400 + c).unwrap();
|
||||||
NonprintableNotation::Caret => "^G",
|
output.push(replacement_symbol)
|
||||||
NonprintableNotation::Unicode => "␇",
|
}
|
||||||
}),
|
}
|
||||||
// backspace
|
}
|
||||||
'\x08' => output.push_str(match nonprintable_notation {
|
// delete
|
||||||
NonprintableNotation::Caret => "^H",
|
'\x7F' => match nonprintable_notation {
|
||||||
NonprintableNotation::Unicode => "␈",
|
NonprintableNotation::Caret => output.push_str("^?"),
|
||||||
}),
|
NonprintableNotation::Unicode => output.push('\u{2421}'),
|
||||||
// escape
|
},
|
||||||
'\x1B' => output.push_str(match nonprintable_notation {
|
|
||||||
NonprintableNotation::Caret => "^[",
|
|
||||||
NonprintableNotation::Unicode => "␛",
|
|
||||||
}),
|
|
||||||
// printable ASCII
|
// printable ASCII
|
||||||
c if c.is_ascii_alphanumeric()
|
c if c.is_ascii_alphanumeric()
|
||||||
|| c.is_ascii_punctuation()
|
|| c.is_ascii_punctuation()
|
||||||
|
BIN
tests/examples/control_characters.txt
vendored
Normal file
BIN
tests/examples/control_characters.txt
vendored
Normal file
Binary file not shown.
@ -1728,6 +1728,25 @@ fn show_all_with_caret_notation() {
|
|||||||
.assert()
|
.assert()
|
||||||
.stdout("hello·world^J\n├──┤^M^@^G^H^[")
|
.stdout("hello·world^J\n├──┤^M^@^G^H^[")
|
||||||
.stderr("");
|
.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]
|
#[test]
|
||||||
|
@ -1,36 +1,36 @@
|
|||||||
[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;117;113;94m\u{1}[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{2}[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{3}[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{4}[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{5}[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{6}[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;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;190;132;255m├──┤[0m[38;2;249;38;114m␊[0m
|
||||||
[38;2;249;38;114m␊[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;248;248;242m␋[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;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;248;248;242m␎[0m[38;2;249;38;114m␊[0m
|
||||||
[38;2;117;113;94m\u{f}[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{10}[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{11}[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{12}[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{13}[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{14}[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{15}[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{16}[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{17}[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{18}[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{19}[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{1a}[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;117;113;94m\u{1c}[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{1d}[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{1e}[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{1f}[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;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
|
||||||
[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;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{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{81}[0m[38;2;249;38;114m␊[0m
|
||||||
[38;2;117;113;94m\u{82}[0m[38;2;249;38;114m␊[0m
|
[38;2;117;113;94m\u{82}[0m[38;2;249;38;114m␊[0m
|
||||||
|
Loading…
Reference in New Issue
Block a user