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) {
9 (text, true) => buffer.push_str(text),
10 (mut text, false) => {
11 while let Some(index) = text.find('\t') {
15 buffer.push_str(&text[0..index]);
19 let spaces = width - (*cursor % width);
21 buffer.push_str(&*" ".repeat(spaces));
24 text = &text[index + 1..text.len()];
27 *cursor += text.len();
28 buffer.push_str(text);
36fn try_parse_utf8_char(input: &[u8]) -> Option<(char, usize)> {
37 let str_from_utf8 = |seq| std::str::from_utf8(seq).ok();
41 .and_then(str_from_utf8)
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 };
57 let len = input.len();
59 if let Some((chr, skip_ahead)) = try_parse_utf8_char(&input[idx..]) {
65 ' ' => output.push('·'),
68 let tab_stop = tab_width - ((line_idx - 1) % tab_width);
74 output.push_str(&"─".repeat(tab_stop - 2));
80 output.push_str("␊\x0A");
84 '\x0D' => output.push('␍'),
86 '\x00' => output.push('␀'),
88 '\x07' => output.push('␇'),
90 '\x08' => output.push('␈'),
92 '\x1B' => output.push('␛'),
94 c if c.is_ascii_alphanumeric()
95 || c.is_ascii_punctuation()
96 || c.is_ascii_graphic() =>
101 c => output.push_str(&c.escape_unicode().collect::<String>()),
104 output.push_str(&format!("\\x{:02X}", input[idx]));
113fn test_try_parse_utf8_char() {
114 assert_eq!(try_parse_utf8_char(&[0x20]), Some((' ', 1)));
115 assert_eq!(try_parse_utf8_char(&[0x20, 0x20]), Some((' ', 1)));
116 assert_eq!(try_parse_utf8_char(&[0x20, 0xef]), Some((' ', 1)));
118 assert_eq!(try_parse_utf8_char(&[0x00]), Some(('\x00', 1)));
119 assert_eq!(try_parse_utf8_char(&[0x1b]), Some(('\x1b', 1)));
121 assert_eq!(try_parse_utf8_char(&[0xc3, 0xa4]), Some(('ä', 2)));
122 assert_eq!(try_parse_utf8_char(&[0xc3, 0xa4, 0xef]), Some(('ä', 2)));
123 assert_eq!(try_parse_utf8_char(&[0xc3, 0xa4, 0x20]), Some(('ä', 2)));
125 assert_eq!(try_parse_utf8_char(&[0xe2, 0x82, 0xac]), Some(('€', 3)));
127 try_parse_utf8_char(&[0xe2, 0x82, 0xac, 0xef]),
131 try_parse_utf8_char(&[0xe2, 0x82, 0xac, 0x20]),
135 assert_eq!(try_parse_utf8_char(&[0xe2, 0x88, 0xb0]), Some(('∰', 3)));
138 try_parse_utf8_char(&[0xf0, 0x9f, 0x8c, 0x82]),
142 try_parse_utf8_char(&[0xf0, 0x9f, 0x8c, 0x82, 0xef]),
146 try_parse_utf8_char(&[0xf0, 0x9f, 0x8c, 0x82, 0x20]),
150 assert_eq!(try_parse_utf8_char(&[]), None);
151 assert_eq!(try_parse_utf8_char(&[0xef]), None);
152 assert_eq!(try_parse_utf8_char(&[0xef, 0x20]), None);
153 assert_eq!(try_parse_utf8_char(&[0xf0, 0xf0]), None);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list