Correctly render tab stops

pull/2124/head
Patrick Hilhorst 2 years ago committed by David Peter
parent cde239e809
commit da5921b4a9

@ -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 tab_width = if tab_width == 0 { 4 } else { tab_width };
let mut idx = 0; let mut idx = 0;
let mut line_idx = 0;
let len = input.len(); let len = input.len();
while idx < len { while idx < len {
if let Some((chr, skip_ahead)) = try_parse_utf8_char(&input[idx..]) { if let Some((chr, skip_ahead)) = try_parse_utf8_char(&input[idx..]) {
idx += skip_ahead; idx += skip_ahead;
line_idx += 1;
match chr { match chr {
// space // space
' ' => output.push('·'), ' ' => output.push('·'),
// tab // tab
'\t' => { '\t' => {
if tab_width == 1 { let tab_stop = tab_width - ((line_idx - 1) % tab_width);
if tab_stop == 1 {
output.push('↹'); output.push('↹');
} else { } else {
output.push('├'); output.push('├');
output.push_str(&"─".repeat(tab_width - 2)); output.push_str(&"─".repeat(tab_stop - 2));
output.push('┤'); output.push('┤');
} }
} }
// line feed // line feed
'\x0A' => output.push_str("␊\x0A"), '\x0A' => {
output.push_str("␊\x0A");
line_idx = 0;
}
// carriage return // carriage return
'\x0D' => output.push('␍'), '\x0D' => output.push('␍'),
// null // null

Loading…
Cancel
Save