At https://codeberg.org/ziglang/zig/src/branch/master/lib/std/Io/Terminal.zig#L110, there is try t.writer.writeAll(color_string); in the .escape_codes branch.
In https://codeberg.org/ziglang/zig/src/branch/master/lib/std/Io/Terminal.zig#L136 there is try t.writer.flush(); in the .windows_api branch.
This seems unusual, is there a a reason?
Why not flushing in the .escape_codes branch?
Thanks
Usually after writing a colour code, you want to print some text in that colour too before flushing, so having an immediate flush doesn’t make any sense.
The same doesn’t seem to be true for the windows api - presumably the current colour is applied to the entire flushed buffer at once, necessitating a flush when you want to change colours.
4 Likes
In short: flushing is slow, so this code only flushes in the part it HAS to.
But if the code runs on an Unix system, try t.writer.writeAll(color_string); is not followed by a flush.
Thanks.
Yes exactly, because a flush isn’t needed to make the colour change correctly.
The flush is only needed on windows.
The flush isn’t something the set color function guarantees, just something it is forced to do on windows to work right.
If your code needs a flush, you’re supposed to just do that yourself