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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use tokio::io::AsyncReadExt as _;
use tokio::process::{ChildStderr, ChildStdout};
use crate::cli::fmt::{ansi, utf8};
/// Drive that takes the out stream and err stream, and produces lines
pub(crate) struct Driver {
out: Option<ChildStdout>,
out_buf: String,
err: Option<ChildStderr>,
err_buf: String,
out_i: usize,
err_i: usize,
buffer: Box<[u8; 1024]>,
line_buf: String,
only_last_line: bool,
}
pub(crate) enum DriverOutput<'a> {
/// A non-empty line ending with a line break
Line(&'a str),
/// A non-empty progress line, typically because a `\r` was detected
Progress(&'a str),
/// No output yet, call me again
Pending,
/// An empty line was detected
Empty,
/// No more to be read from us
Done,
}
impl<'a> DriverOutput<'a> {
fn new(line: &'a str, lf: bool) -> Self {
let line = line.trim();
if line.is_empty() {
Self::Empty
} else if lf {
Self::Line(line)
} else {
Self::Progress(line)
}
}
}
impl Driver {
pub fn new(out: Option<ChildStdout>, err: Option<ChildStderr>, only_last_line: bool) -> Self {
Self {
out,
out_buf: String::new(),
err,
err_buf: String::new(),
out_i: 0,
err_i: 0,
buffer: Box::new([0u8; 1024]),
line_buf: String::new(),
only_last_line,
}
}
/// Return the next line and whether it has a line break
/// If the line is empty after trimming, returns
pub async fn next(&mut self) -> DriverOutput<'_> {
match (self.out.as_mut(), self.err.as_mut()) {
(None, None) => DriverOutput::Done,
(None, Some(s)) => {
match s.read(&mut self.buffer.as_mut()[self.err_i..]).await {
// probably finished reading
Err(_) | Ok(0) => DriverOutput::Done,
Ok(n) => {
let end = self.err_i + n;
let slice = &self.buffer.as_ref()[..end];
let (b, lf) = Self::process(
slice,
&mut self.err_buf,
&mut self.line_buf,
self.only_last_line,
);
let buf_mut = self.buffer.as_mut();
// shift the remaining section of buf
for i in b..end {
buf_mut[i - b] = buf_mut[i];
}
self.err_i = end - b;
DriverOutput::new(&self.line_buf, lf)
}
}
}
(Some(s), None) => {
match s.read(&mut self.buffer.as_mut()[self.out_i..]).await {
// probably finished reading
Err(_) | Ok(0) => DriverOutput::Done,
Ok(n) => {
let end = self.out_i + n;
let slice = &self.buffer.as_ref()[..end];
let (b, lf) = Self::process(
slice,
&mut self.out_buf,
&mut self.line_buf,
self.only_last_line,
);
let buf_mut = self.buffer.as_mut();
// shift the remaining section of buf
for i in b..end {
buf_mut[i - b] = buf_mut[i];
}
self.out_i = end - b;
DriverOutput::new(&self.line_buf, lf)
}
}
}
(Some(so), Some(se)) => {
let mid = self.buffer.len() / 2;
let (buf_o, buf_e) = self.buffer.as_mut().split_at_mut(mid);
// read is cancel safe - if canceled, nothing will be read
tokio::select! {
x = so.read(&mut buf_o[self.out_i..]) => {match x {
Err(_) | Ok(0) => {
let buf_mut = self.buffer.as_mut();
for i in 0..self.err_i {
buf_mut[i] = buf_mut[mid+i];
}
self.out = None;
DriverOutput::Pending
}
Ok(n) => {
let end = self.out_i +n;
let slice = &buf_o[..end];
let (b, lf) = Self::process(slice, &mut self.out_buf, &mut self.line_buf, self.only_last_line);
// shift the remaining section of buf
for i in b..end {
buf_o[i-b] = buf_o[i];
}
self.out_i = end-b;
DriverOutput::new(&self.line_buf, lf)
}
}}
x = se.read(&mut buf_e[self.err_i..]) => {match x{
Err(_) | Ok(0) => {
self.err = None;
DriverOutput::Pending
}
Ok(n) => {
let end = self.err_i +n;
let slice = &buf_e[..end];
let (b, lf) = Self::process(slice, &mut self.err_buf, &mut self.line_buf, self.only_last_line);
// shift the remaining section of buf
for i in b..end {
buf_e[i-b] = buf_e[i];
}
self.err_i = end-b;
DriverOutput::new(&self.line_buf, lf)
}
}}
}
}
}
}
// out will contain the remaining characters that's not a line,
// and line will contain the last non-empty line after stripping
// return how many bytes from buf are used and if the line is ends with `\n`
fn process(
buf: &[u8],
out: &mut String,
line: &mut String,
only_last_line: bool,
) -> (usize, bool) {
let mut i = 0;
let mut invalid_while_escaping = false;
let mut start_escape_pos: Option<usize> = None;
line.clear();
let mut full_line = false;
let mut last: char = '\0';
while i < buf.len() {
match utf8::decode_char(&buf[i..]) {
Err(true) => {
// invalid, skip one
i += 1;
invalid_while_escaping = true;
}
Err(false) => {
// not enough bytes
break;
}
Ok((c, l)) => {
i += l;
let prev = last;
last = c;
if let Some(s) = start_escape_pos {
if ansi::is_esc_end(c) {
start_escape_pos = None;
// allow color codes
if !invalid_while_escaping && c == 'm' {
// safety: !invalid_while_escaping tells us the range is checked
let escape = unsafe { str::from_utf8_unchecked(&buf[s..i]) };
out.push_str(escape);
}
}
continue;
}
if c == '\x1b' {
debug_assert_eq!(l, 1);
invalid_while_escaping = false;
start_escape_pos = Some(i - 1);
continue;
}
if c == '\r' || c == '\n' {
if c == '\n' && prev == '\r' {
full_line = true;
} else {
if only_last_line || !full_line {
line.clear();
} else {
line.push('\n');
}
line.push_str(out);
full_line = c == '\n';
}
out.clear();
continue;
}
if c.is_control() {
continue;
}
out.push(c)
}
}
}
(i, full_line)
}
}