deno-cliffy icon indicating copy to clipboard operation
deno-cliffy copied to clipboard

Disable colors if the standard output is not a TTY

Open dahlia opened this issue 10 months ago • 0 comments

For the rationale, please read this article: Color and TTYs.

The reality is that assuming that a terminal supports ANSI color codes is a bad idea for a bunch of reasons.

The most important reason is that the file descriptor you're printing to might not be a TTY. This commonly happens when stdout is redirected to another program or a file. If you emit ANSI escape codes then the output is no longer "plain text" and it makes a bunch of common operations more difficult. It can break programs like grep if the search term spans escape codes. It also causes weird things to happen when you use programs like less (which will escape the escape codes by default).

In Deno, we can detect if it's a TTY or not using Deno.stdout.isTerminal() method.

dahlia avatar Apr 11 '24 07:04 dahlia