Skip to content

Commit bb658fb

Browse files
committed
flushes stderr and stdout accordingly to old behavior
1 parent 5aadbcd commit bb658fb

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
lines changed

crates/chat-cli-ui/src/conduit.rs

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ impl std::io::Write for ControlEnd<DestinationStderr> {
269269
.insert_content(buf)
270270
.map_err(|_e| std::io::Error::other("Error inserting content"))?;
271271

272+
// By default stderr is unbuffered (the content is flushed immediately)
273+
self.flush()?;
274+
272275
Ok(buf.len())
273276
}
274277

@@ -283,21 +286,57 @@ impl std::io::Write for ControlEnd<DestinationStderr> {
283286

284287
impl std::io::Write for ControlEnd<DestinationStdout> {
285288
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
286-
if self.current_event.is_none() {
287-
self.current_event
288-
.replace(Event::LegacyPassThrough(LegacyPassThroughOutput::Stdout(
289-
Default::default(),
290-
)));
289+
// By default stdout is line buffered, so we'll delimit the incoming buffer with new line
290+
// and flush accordingly.
291+
let mut start = 0_usize;
292+
let mut end = 0_usize;
293+
while end < buf.len() {
294+
let Some(byte) = buf.get(end) else {
295+
break;
296+
};
297+
298+
if byte == &10 || byte == &13 {
299+
if self.current_event.is_none() {
300+
self.current_event
301+
.replace(Event::LegacyPassThrough(LegacyPassThroughOutput::Stderr(
302+
Default::default(),
303+
)));
304+
}
305+
306+
let current_event = self
307+
.current_event
308+
.as_mut()
309+
.ok_or(std::io::Error::other("No event set"))?;
310+
311+
current_event
312+
.insert_content(&buf[start..=end])
313+
.map_err(std::io::Error::other)?;
314+
315+
self.flush()?;
316+
317+
start = end + 1;
318+
}
319+
320+
end += 1;
291321
}
292322

293-
let current_event = self
294-
.current_event
295-
.as_mut()
296-
.ok_or(std::io::Error::other("No event set"))?;
323+
if start < end {
324+
if self.current_event.is_none() {
325+
self.current_event
326+
.replace(Event::LegacyPassThrough(LegacyPassThroughOutput::Stderr(
327+
Default::default(),
328+
)));
329+
}
297330

298-
current_event
299-
.insert_content(buf)
300-
.map_err(|_e| std::io::Error::other("Error inserting content"))?;
331+
let current_event = self
332+
.current_event
333+
.as_mut()
334+
.ok_or(std::io::Error::other("No event set"))?;
335+
336+
current_event
337+
.insert_content(&buf[start..end])
338+
.map_err(std::io::Error::other)?;
339+
}
301340

302341
Ok(buf.len())
303342
}

crates/chat-cli/src/cli/chat/custom_spinner.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crossterm::{
22
cursor,
33
execute,
4-
style,
54
terminal,
65
};
76
use indicatif::{
@@ -21,7 +20,7 @@ pub struct Spinners {
2120
impl Spinners {
2221
pub fn new(message: String) -> Self {
2322
// Hide the cursor when starting the spinner
24-
let _ = execute!(std::io::stderr(), style::Print("\n"), cursor::Hide);
23+
let _ = execute!(std::io::stderr(), cursor::Hide);
2524

2625
let pb = ProgressBar::new_spinner();
2726
pb.set_style(

0 commit comments

Comments
 (0)