From f2c26ed7a72828f94f174ab8c84676856fbe2097 Mon Sep 17 00:00:00 2001 From: jaseg Date: Thu, 4 Jul 2024 11:44:47 +0200 Subject: Make lolcat interleave nicely with non-colored stderr This works by making lolcat always reset the color at the end of a line on its input, and only setting a new color immediately before printing the first character on the next line. This way, assuming that whatever process is piped into lolcat follow the standard convention where stdout is buffered line-wise, and only writes full lines to stderr, a process that outputs to both stdout and stderr but that has only stdout piped through lolcat (as most people would do) would have it's stderr output interleave nicely uncolored with the lolcat-colored stdout output. This won't always work, but it gets us most of the way there with little effort and negligible (2 extra escape sequences per line of output) performance overhead. Thanks to github user @kolayne for reporting this as an issue! --- lolcat.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lolcat.c b/lolcat.c index a323720..ff39189 100755 --- a/lolcat.c +++ b/lolcat.c @@ -390,8 +390,11 @@ int main(int argc, char** argv) if (c == '\n') { l++; i = 0; + cc = -1; if (invert) { wprintf(L"\033[49m"); + } else { + wprintf(L"\033[0m"); } } else { -- cgit