summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjaseg <git@jaseg.de>2024-07-04 11:44:47 +0200
committerjaseg <git@jaseg.de>2024-07-04 11:45:58 +0200
commitf2c26ed7a72828f94f174ab8c84676856fbe2097 (patch)
tree27780539d70c0b843ef1434d0d608fb391f11393
parentdc3ddf395017bf903f5bfb8f0c5c6d08d84fcdc0 (diff)
downloadlolcat-f2c26ed7a72828f94f174ab8c84676856fbe2097.tar.gz
lolcat-f2c26ed7a72828f94f174ab8c84676856fbe2097.tar.bz2
lolcat-f2c26ed7a72828f94f174ab8c84676856fbe2097.zip
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!
-rwxr-xr-xlolcat.c3
1 files changed, 3 insertions, 0 deletions
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 {