summaryrefslogtreecommitdiffstats
path: root/src/preprocessor.rs
diff options
context:
space:
mode:
authorsharkdp <davidpeter@web.de>2018-11-01 13:02:29 +0100
committerDavid Peter <sharkdp@users.noreply.github.com>2018-11-01 22:00:47 +0100
commitecd862d9ff729a5de5e7b2771be02155567b72cf (patch)
treebb2a93958db848711906d807e32c89616f11bee7 /src/preprocessor.rs
parentcbed338c3a640c849f932e162ec3685867028f48 (diff)
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.
Diffstat (limited to 'src/preprocessor.rs')
-rw-r--r--src/preprocessor.rs41
1 files changed, 40 insertions, 1 deletions
diff --git a/src/preprocessor.rs b/src/preprocessor.rs
index 0a99e07d..a69f453e 100644
--- a/src/preprocessor.rs
+++ b/src/preprocessor.rs
@@ -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),
+ }
+ }
+}