fix(interactive): flush stdout/stderr after streaming command output#1226
Merged
fix(interactive): flush stdout/stderr after streaming command output#1226
Conversation
print!()/eprint!() are line-buffered on TTY, so commands like `clear` that output ANSI escape codes without trailing newlines would stay buffered until the next prompt draw. Flush explicitly after each chunk.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
clear(and other commands producing output without trailing newlines) appearing delayed in interactive modeprint!()/eprint!()in the streaming callback are line-buffered on TTY — ANSI escape codes fromclear(\x1b[2J\x1b[H, no newline) stayed buffered until rustyline's next prompt drawstdout().flush()andstderr().flush()after eachprint!()/eprint!()in the streaming callbackWhat changed
crates/bashkit-cli/src/interactive.rs:use std::io::Writeimportstd::io::stdout().flush()afterprint!()in theexec_streamingcallbackstd::io::stderr().flush()aftereprint!()in theexec_streamingcallbackclear_command_streams_ansi_escape_codesverifyingclearproduces expected ANSI escape sequences via streamingWhy
Users reported that typing
clearin interactive mode didn't clear the screen immediately — instead the screen cleared only when the next command was executed. This is because Rust'sprint!()macro uses line buffering when connected to a TTY, and the clear command's output contains no newline.Test plan
clear_command_streams_ansi_escape_codes— verifiesclearemitsESC[2JandESC[Hviaexec_streamingcargo fmt --checkcleancargo clippy -- -D warningscleanecho 'clear' | cargo runemits correct ANSI codesprint!/eprint!— only other occurrence is inmain.rsone-shot mode whereprocess::exit()follows immediately (OS flushes on exit)