on v0.16.0 help sign in
bat/src/preprocessor.rs
147 lines 4.8 kB history raw
1use console::AnsiCodeIterator;
3/// Expand tabs like an ANSI-enabled expand(1).
4pub fn expand_tabs(line: &str, width: usize, cursor: &mut usize) -> String {
5 let mut buffer = String::with_capacity(line.len() * 2);
7 for chunk in AnsiCodeIterator::new(line) {
8 match chunk {
9 (text, true) => buffer.push_str(text),
10 (mut text, false) => {
11 while let Some(index) = text.find('\t') {
12 // Add previous text.
13 if index > 0 {
14 *cursor += index;
15 buffer.push_str(&text[0..index]);
16 }
18 // Add tab.
19 let spaces = width - (*cursor % width);
20 *cursor += spaces;
21 buffer.push_str(&*" ".repeat(spaces));
23 // Next.
24 text = &text[index + 1..text.len()];
25 }
27 *cursor += text.len();
28 buffer.push_str(text);
29 }
30 }
31 }
33 buffer
36fn try_parse_utf8_char(input: &[u8]) -> Option<(char, usize)> {
37 let str_from_utf8 = |seq| std::str::from_utf8(seq).ok();
39 let decoded = input
40 .get(0..1)
41 .and_then(str_from_utf8)
42 .map(|c| (c, 1))
43 .or_else(|| input.get(0..2).and_then(str_from_utf8).map(|c| (c, 2)))
44 .or_else(|| input.get(0..3).and_then(str_from_utf8).map(|c| (c, 3)))
45 .or_else(|| input.get(0..4).and_then(str_from_utf8).map(|c| (c, 4)));
47 decoded.map(|(seq, n)| (seq.chars().next().unwrap(), n))
50pub fn replace_nonprintable(input: &[u8], tab_width: usize) -> String {
51 let mut output = String::new();
53 let tab_width = if tab_width == 0 { 4 } else { tab_width };
55 let mut idx = 0;
56 let len = input.len();
57 while idx < len {
58 if let Some((chr, skip_ahead)) = try_parse_utf8_char(&input[idx..]) {
59 idx += skip_ahead;
61 match chr {
62 // space
63 ' ' => output.push('·'),
64 // tab
65 '\t' => {
66 if tab_width == 1 {
67 output.push('↹');
68 } else {
69 output.push('├');
70 output.push_str(&"─".repeat(tab_width - 2));
71 output.push('┤');
72 }
73 }
74 // line feed
75 '\x0A' => output.push('␊'),
76 // carriage return
77 '\x0D' => output.push('␍'),
78 // null
79 '\x00' => output.push('␀'),
80 // bell
81 '\x07' => output.push('␇'),
82 // backspace
83 '\x08' => output.push('␈'),
84 // escape
85 '\x1B' => output.push('␛'),
86 // printable ASCII
87 c if c.is_ascii_alphanumeric()
88 || c.is_ascii_punctuation()
89 || c.is_ascii_graphic() =>
90 {
91 output.push(c)
92 }
93 // everything else
94 c => output.push_str(&c.escape_unicode().collect::<String>()),
95 }
96 } else {
97 output.push_str(&format!("\\x{:02X}", input[idx]));
98 idx += 1;
99 }
100 }
102 output
105#[test]
106fn test_try_parse_utf8_char() {
107 assert_eq!(try_parse_utf8_char(&[0x20]), Some((' ', 1)));
108 assert_eq!(try_parse_utf8_char(&[0x20, 0x20]), Some((' ', 1)));
109 assert_eq!(try_parse_utf8_char(&[0x20, 0xef]), Some((' ', 1)));
111 assert_eq!(try_parse_utf8_char(&[0x00]), Some(('\x00', 1)));
112 assert_eq!(try_parse_utf8_char(&[0x1b]), Some(('\x1b', 1)));
114 assert_eq!(try_parse_utf8_char(&[0xc3, 0xa4]), Some(('ä', 2)));
115 assert_eq!(try_parse_utf8_char(&[0xc3, 0xa4, 0xef]), Some(('ä', 2)));
116 assert_eq!(try_parse_utf8_char(&[0xc3, 0xa4, 0x20]), Some(('ä', 2)));
118 assert_eq!(try_parse_utf8_char(&[0xe2, 0x82, 0xac]), Some(('€', 3)));
119 assert_eq!(
120 try_parse_utf8_char(&[0xe2, 0x82, 0xac, 0xef]),
121 Some(('€', 3))
122 );
123 assert_eq!(
124 try_parse_utf8_char(&[0xe2, 0x82, 0xac, 0x20]),
125 Some(('€', 3))
126 );
128 assert_eq!(try_parse_utf8_char(&[0xe2, 0x88, 0xb0]), Some(('∰', 3)));
130 assert_eq!(
131 try_parse_utf8_char(&[0xf0, 0x9f, 0x8c, 0x82]),
132 Some(('🌂', 4))
133 );
134 assert_eq!(
135 try_parse_utf8_char(&[0xf0, 0x9f, 0x8c, 0x82, 0xef]),
136 Some(('🌂', 4))
137 );
138 assert_eq!(
139 try_parse_utf8_char(&[0xf0, 0x9f, 0x8c, 0x82, 0x20]),
140 Some(('🌂', 4))
141 );
143 assert_eq!(try_parse_utf8_char(&[]), None);
144 assert_eq!(try_parse_utf8_char(&[0xef]), None);
145 assert_eq!(try_parse_utf8_char(&[0xef, 0x20]), None);
146 assert_eq!(try_parse_utf8_char(&[0xf0, 0xf0]), None);