1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//! What a program printed to a terminal, as plain text: the run output a
//! person or an agent reads outside the terminal (History's command views,
//! the parsers that find errors and links in a run) without the escape
//! sequences that drew it.
/// `text` without its escape sequences (CSI, OSC and the other strings,
/// charset designations, other `ESC x`), carriage returns resolved to the
/// last overwrite of each line.
#[must_use]
pub fn plain(text: &str) -> String {
let mut out = String::with_capacity(text.len());
let mut chars = text.chars().peekable();
while let Some(c) = chars.next() {
match c {
'\u{1b}' => match chars.next() {
Some('[') => {
for next in chars.by_ref() {
if ('\u{40}'..='\u{7e}').contains(&next) {
break;
}
}
}
// OSC, and DCS, SOS, PM and APC: a string up to BEL or ST.
Some(']' | 'P' | 'X' | '^' | '_') => {
while let Some(next) = chars.next() {
if next == '\u{7}' {
break;
}
if next == '\u{1b}' && chars.peek() == Some(&'\\') {
chars.next();
break;
}
}
}
// Intermediates, then one final byte: the charset
// designations `ESC ( B` (which `tput sgr0` prints),
// `ESC ) 0`, `ESC $ ( B` and the like.
Some(' '..='/') => {
while chars.next_if(|next| (' '..='/').contains(next)).is_some() {}
chars.next_if(|next| ('0'..='~').contains(next));
}
// Any other `ESC x`, such as `ESC 7` or `ESC =`.
_ => {}
},
'\r' => {
if chars.peek() == Some(&'\n') {
continue;
}
let start = out.rfind('\n').map_or(0, |at| at.saturating_add(1));
out.truncate(start);
}
c if c.is_control() && c != '\n' && c != '\t' => {}
c => out.push(c),
}
}
out
}
/// The last lines of `output`, a run's raw output, as plain text: at most
/// `lines` lines and `bytes` bytes of them, and whether any were left out.
#[must_use]
pub fn tail(output: &[u8], lines: usize, bytes: usize) -> (Vec<String>, bool) {
let text = plain(&String::from_utf8_lossy(output));
let mut kept: Vec<String> = Vec::new();
let mut used = 0_usize;
let mut all = text.trim_end_matches('\n').split('\n').rev().peekable();
while let Some(line) = all.next() {
let line = line.trim_end();
if kept.len() == lines || used.saturating_add(line.len()) > bytes {
return (finish(kept), true);
}
used = used.saturating_add(line.len());
kept.push(line.to_owned());
if all.peek().is_none() {
break;
}
}
(finish(kept), false)
}
fn finish(mut reversed: Vec<String>) -> Vec<String> {
reversed.reverse();
if reversed.len() == 1 && reversed.first().is_some_and(String::is_empty) {
reversed.clear();
}
reversed
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_tail_is_the_last_lines_without_escapes() {
let output = b"\x1b[1mCompiling\x1b[0m signup\r\nrunning 3 tests\ntest ok\nall passed\n";
assert_eq!(
tail(output, 2, 1024),
(vec!["test ok".to_owned(), "all passed".to_owned()], true)
);
assert_eq!(
tail(output, 10, 1024).0,
[
"Compiling signup",
"running 3 tests",
"test ok",
"all passed"
]
);
assert!(!tail(output, 10, 1024).1);
assert_eq!(tail(b"", 10, 1024), (Vec::new(), false));
assert_eq!(
tail(b"0123456789\nab\n", 10, 4),
(vec!["ab".to_owned()], true)
);
}
}