mirror of
https://github.com/sharkdp/bat.git
synced 2025-09-02 19:32:25 +01:00
Feature: Highlight non-printable characters
Adds a new `-A`/`--show-all` option (in analogy to GNU Linux `cat`s option) that highlights non-printable characters like space, tab or newline. This works in two steps: - **Preprocessing**: replace space by `•`, replace tab by `├──┤`, replace newline by ``, etc. - **Highlighting**: Use a newly written Sublime syntax to highlight these special symbols. Note: This feature is not technically a drop-in replacement for GNU `cat`s `--show-all` but it has the same purpose.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use console::AnsiCodeIterator;
|
||||
|
||||
/// Expand tabs like an ANSI-enabled expand(1).
|
||||
pub fn expand(line: &str, width: usize, cursor: &mut usize) -> String {
|
||||
pub fn expand_tabs(line: &str, width: usize, cursor: &mut usize) -> String {
|
||||
let mut buffer = String::with_capacity(line.len() * 2);
|
||||
|
||||
for chunk in AnsiCodeIterator::new(line) {
|
||||
@@ -32,3 +32,42 @@ pub fn expand(line: &str, width: usize, cursor: &mut usize) -> String {
|
||||
|
||||
buffer
|
||||
}
|
||||
|
||||
pub fn replace_nonprintable(input: &mut Vec<u8>, output: &mut Vec<u8>, tab_width: usize) {
|
||||
output.clear();
|
||||
|
||||
let tab_width = if tab_width == 0 {
|
||||
4
|
||||
} else if tab_width == 1 {
|
||||
2
|
||||
} else {
|
||||
tab_width
|
||||
};
|
||||
|
||||
for chr in input {
|
||||
match *chr {
|
||||
// space
|
||||
b' ' => output.extend_from_slice("•".as_bytes()),
|
||||
// tab
|
||||
b'\t' => {
|
||||
output.extend_from_slice("├".as_bytes());
|
||||
output.extend_from_slice("─".repeat(tab_width - 2).as_bytes());
|
||||
output.extend_from_slice("┤".as_bytes());
|
||||
}
|
||||
// new line
|
||||
b'\n' => output.extend_from_slice("".as_bytes()),
|
||||
// carriage return
|
||||
b'\r' => output.extend_from_slice("␍".as_bytes()),
|
||||
// null
|
||||
0x00 => output.extend_from_slice("␀".as_bytes()),
|
||||
// bell
|
||||
0x07 => output.extend_from_slice("␇".as_bytes()),
|
||||
// backspace
|
||||
0x08 => output.extend_from_slice("␈".as_bytes()),
|
||||
// escape
|
||||
0x1B => output.extend_from_slice("␛".as_bytes()),
|
||||
// anything else
|
||||
_ => output.push(*chr),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user