Don't print ANSI escape codes if terminal is not a TTY
I suggest we style printed text the same way the Console API does rather than using ANSI escape codes across the codebase. In the terminal, styling should look the same. However, the advantage is that escape codes are omitted in places where raw characters aren't handled in special ways. E.g., text files. The differences can be seen here.
console.log("%cHello world", "color: red");
produces output.txt:
Hello world
and
import { red } from "jsr:@std/fmt/colors";
console.log(red("Hello world"));
produces output.txt:
[31mHello world[39m
This behavior is especially cumbersome in scenarios where analyzing output is important. For example, deno test output is printed in the terminal in CI. When advanced troubleshooting is required, a user will download the logs but must deal with the clutter generated from ANSI escape codes.
Disclaimer: I'm unfamiliar with the Console API's inner workings, including how it applies styling to printed output.
Related #23316
Do you mean specifically that internal color printing should not happen if stdout is not a TTY? I think I agree with that.
Does this issue happen anymore? I can't reproduce. I think it was fixed by https://github.com/denoland/deno/pull/23813
I'll check and confirm.
I think I did something wrong when I was looking at this last. I see the jsr code outputs with ansi codes. I think that should be expected? I'm not sure we should be stripping ansi codes that are explicitly passed to console.log. It would have some performance overhead too.
I ran into this by piping from deno info ..., not sure this is the right place to report btw.