Skip to main content

collapse_cr

Function collapse_cr 

Source
fn collapse_cr(pending: &mut Vec<u8>)
Expand description

Collapse lone carriage-returns in the pending byte buffer, simulating terminal overwrite: each \r NOT followed by \n discards the current visual line back to column 0.

Why here and not in strip_ansi: real spinner animations emit each frame as a separate write() syscall, so the preceding frame content and the overwriting \r arrive in different PTY reads. Running collapse on the pending accumulator (which spans reads for the current visual line) catches those cross-read overwrites.

Leading \r is preserved — including through mid-buffer overwrites. When a lone \r appears at the very start of a visual line, it is emitted as-is rather than discarded. When a subsequent lone \r overwrites a line that itself started with \r (e.g. "\rframe1\rframe2"), the leading \r is preserved in the result ("\rframe2", not "frame2"). This lets the caller’s pending_cr_override slot chain-collapse throttled spinner frames on successive reads: the collapsed result still starts with \r, so the slot engages again on the next quiet-window expiry.

Trailing \r is left in place. A \r at the very end of pending might be the first byte of a CRLF pair split across reads. Collapsing it immediately would discard the line’s content before the \n arrives, causing silent data loss. The caller suppresses the eager partial-line flush when pending ends with \r so the next read can disambiguate.