collapseRedraws function
line with everything an earlier redraw already painted over discarded.
The same rule as lastFrame, but it keeps the carriage return and the surrounding whitespace, so the result can be fed back in as the still unterminated tail of a line that is being drawn right now.
A caller buffering a partial line needs this: a spinner running for the length of a build writes a frame every 80ms and never a newline, so a tail kept verbatim grows for as long as the step takes, to hold frames that were painted over seconds ago.
Implementation
String collapseRedraws(String line) {
final cut = _withoutLineEnding(line).lastIndexOf('\r');
// `cut == 0` is already collapsed; re-slicing it would be a no-op.
return cut <= 0 ? line : line.substring(cut);
}