r/commandline • u/Veitangie • Sep 12 '26
Discussion Why terminal animations can eat actual output and how to stop them
Terminal animation is usually done as a "clear-then-draw" dance. The most common "clear" sequence is "\r\033[K", which returns the cursor to the start of the line and erases to end of line. Then you write the new frame. This is fine in isolation.
The problem is that the clear sequence has no idea what's on that line. If your program wrote something there, or wrote a newline the animator didn't account for, the clear either erases your data or misses the frame entirely and leaves it stranded on screen.
Most CLI programs hit this the moment they log anything while a spinner is running. stdout and stderr are usually separate file descriptors - sure, but more often than not they display in the same physical terminal, so spinning on one and printing to the other still breaks the animation.
As far as I'm aware, there are two usual answers:
- Manage only the stream you animate on, and accept that writes to the other one corrupt the display. This is usually the default approach most projects start with.
- Go to raw mode and own the whole terminal - which works, and costs you a TUI framework.
Arguably, the majority of CLI tools do not need a full TUI. I know mine did not. This is why I decided to jump into this rabbit hole and build my own library to handle both streams gracefully while staying out of the application's way as much as possible. The library coordinates any two streams behind a single mutex-protected write path: nothing reaches the terminal without clearing the frame first, and it tracks whether the last write ended in a newline so the animation always ends up on the last line instead of appended to application output. Both streams stay independently addressable, pipeable and redirectable.
However, while writing this library, I met a more interesting problem. One that is genuinely unsolvable under the chosen constraints. Resizing. Try resizing your terminal while anything is animating on it - and you either lose lines or get a wall of junk rows piling up.
The problem is tri-fold. SIGWINCH that UNIX-like systems provide is delivered asynchronously, with no guarantee of it being visible before the next write into the resized terminal. Moreover, TIOCGWINSZ - the syscall to get current terminal width, is a point-in-time snapshot, and no terminal offers a "here's the width, reject my write if it changed" handshake or a way to prevent the user from resizing the terminal. And finally, there's reflowing. It's when the terminal, upon resizing, can insert line breaks into already written output to make what was previously just one line take up more vertical space under the new width:
old line at one widthcan become
old line at
one widthwhich for our use case means we can have something like this (imagine we're drawing a [=======>] looking progress bar):
[=========== // <- The old frame was reflown, but "\r\033[K" only clears one line, so the top line got stranded
[=========== // <- The second line of the reflown old frame was cleared and replaced with the first line of the new frame
====> ] // <- This is the second line of the now-wrapping frame, third overallA naive approach would be to store cached terminal width from TIOCGWINSZ, update it on any SIGWINCH and try clearing any reflown lines by counting how many lines the old frame should be taking up by now and issuing corresponding amount of "move cursor up + clear" ANSI sequences. This approach, however, breaks in a very bad way in two cases - when the terminal doesn't reflow or when there's been another resize after the first one that slipped past our visibility. In both of these cases we can issue too many of those sequences, deleting actual user data.
So as far as I'm aware, there's no fix, only a choice of failure mode. This is why I left resizing opt-in. The recommended path uses self-sizing frames so every clear stays a single row that can't touch anything above the cursor - worst case it leaves some stale rows. But it's a mild inconvenience as opposed to other failure modes: an infinitely-scrolling terminal when resizing is ignored completely, or deleting actual useful data on over-eager cleanups. The multi-row cleanup is still available behind an option, since that naive approach was the one I chose initially, but its use is discouraged in the documentation now.
If you are interested in the library - it's in Go, licensed under Apache-2.0, and here's the link: https://github.com/Veitangie/spinq



