deno
deno copied to clipboard
Support forcing colors
If deno is not running in a tty there is currently no way to override it so that colors are shown
https://github.com/denoland/deno/blob/98bbf87742969802b392130c536454aa61a20395/runtime/js/99_main.js#L250
Some options are:
FORCE_COLORenv var - this would ignore the other values and never setnoColor--colorflag - similar tolsand other CLI utilsauto-> current behaviornever-> same behavior asNO_COLORalways-> force color on
Also open to making the actual change if this is something wanted, just don't know which behavior is preferred.
If we were to support it, it should be via env var. Is there a prior art for FORCE_COLOR env variable?
Prior art for FORCE_COLOR
- Node supports
FORCE_COLORalbeit it in a slightly different way than a simple boolean: https://nodejs.org/api/tty.html#writestreamgetcolordepthenv
More approaches:
-
An environment variable that controls
always,never,autowould be similar to cargo, i.e.DENO_TERM_COLOR: https://doc.rust-lang.org/cargo/reference/config.html#termcolor -
There is also the
CLICOLORandCLICOLOR_FORCEenv vars, but I figured this would not work as well withNO_COLOR: https://bixense.com/clicolors/
I do not have any strong preference on the approach here
@dsherret thoughts on this one?
We should probably do exactly what Node does, so go with FORCE_COLOR. It defaults to 2 when non-empty and not 0-3:
https://github.com/nodejs/node/blob/9eb363a3e00dbba572756c7ed314273f17ea8e2e/lib/internal/tty.js#L123
Environment variable sounds much better than a flag.
Seems like the lib termcolor only supports forcing color, no color or auto, but has no concept of color depth support: https://docs.rs/termcolor/latest/termcolor/enum.ColorChoice.html
Would replicating the logic of what is "enabled" for the flags be enough or should the it support the concept of color depth as well?
I think for compatibility with Node we should get it to work the same way, so maybe that means using a different crate or implementing this ourselves. As a stepping stone, we could maybe implement this broken for now with the same API as Node then fix it up later.
this would be beneficial to allow forcing color to be enabled in ci like github actions which arent tty but still can show colors
related: https://github.com/pypa/pip/issues/10909 https://github.com/Textualize/rich/issues/2425 https://github.com/wntrblm/nox/issues/502 https://github.com/python-poetry/cleo/issues/341 https://github.com/astral-sh/ruff/issues/5499 https://github.com/dotnet/command-line-api/issues/1710 https://github.com/pre-commit/pre-commit/issues/2918 https://github.com/pytest-dev/pytest/issues/7443 https://github.com/python/mypy/issues/13806
Also the extremely useful case of piping to less.