on master help sign in
Correctly render tab stops
commitda5921b4a9f6f86df47053bf2f89833697ca02bf authorPatrick Hilhorst <git@hilhorst.be> dateThu 20 Jan 2022, 14:35 (5 years ago) parentcde239e
1 file changed+9−3
src/preprocessor.rs+9−3
src/preprocessor.rs+9−3
@@ -53,26 +53,32 @@ pub fn replace_nonprintable(input: &[u8], tab_width: usize) -> String {
5353 let tab_width = if tab_width == 0 { 4 } else { tab_width };
5454
5555 let mut idx = 0;
56+ let mut line_idx = 0;
5657 let len = input.len();
5758 while idx < len {
5859 if let Some((chr, skip_ahead)) = try_parse_utf8_char(&input[idx..]) {
5960 idx += skip_ahead;
61+ line_idx += 1;
6062
6163 match chr {
6264 // space
6365 ' ' => output.push('·'),
6466 // tab
6567 '\t' => {
66− if tab_width == 1 {
68+ let tab_stop = tab_width - ((line_idx - 1) % tab_width);
69+ if tab_stop == 1 {
6770 output.push('↹');
6871 } else {
6972 output.push('├');
70− output.push_str(&"─".repeat(tab_width - 2));
73+ output.push_str(&"─".repeat(tab_stop - 2));
7174 output.push('┤');
7275 }
7376 }
7477 // line feed
75− '\x0A' => output.push_str("␊\x0A"),
78+ '\x0A' => {
79+ output.push_str("␊\x0A");
80+ line_idx = 0;
81+ }
7682 // carriage return
7783 '\x0D' => output.push('␍'),
7884 // null