diff options
author | Patrick Hilhorst <git@hilhorst.be> | 2022-01-20 14:35:25 +0100 |
---|---|---|
committer | David Peter <sharkdp@users.noreply.github.com> | 2022-03-06 20:03:00 +0100 |
commit | da5921b4a9f6f86df47053bf2f89833697ca02bf (patch) | |
tree | becc4b059d52fe41fc5d3fae818dafc5f80b103f | |
parent | cde239e809936525f0845c2aceb57e813d395760 (diff) |
Correctly render tab stops
-rw-r--r-- | src/preprocessor.rs | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/src/preprocessor.rs b/src/preprocessor.rs index 7001ec88..64600419 100644 --- a/src/preprocessor.rs +++ b/src/preprocessor.rs @@ -53,26 +53,32 @@ pub fn replace_nonprintable(input: &[u8], tab_width: usize) -> String { let tab_width = if tab_width == 0 { 4 } else { tab_width }; let mut idx = 0; + let mut line_idx = 0; let len = input.len(); while idx < len { if let Some((chr, skip_ahead)) = try_parse_utf8_char(&input[idx..]) { idx += skip_ahead; + line_idx += 1; match chr { // space ' ' => output.push('·'), // tab '\t' => { - if tab_width == 1 { + let tab_stop = tab_width - ((line_idx - 1) % tab_width); + if tab_stop == 1 { output.push('↹'); } else { output.push('├'); - output.push_str(&"─".repeat(tab_width - 2)); + output.push_str(&"─".repeat(tab_stop - 2)); output.push('┤'); } } // line feed - '\x0A' => output.push_str("␊\x0A"), + '\x0A' => { + output.push_str("␊\x0A"); + line_idx = 0; + } // carriage return '\x0D' => output.push('␍'), // null |