deno icon indicating copy to clipboard operation
deno copied to clipboard

Support forcing colors

Open grant0417 opened this issue 2 years ago • 9 comments

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_COLOR env var - this would ignore the other values and never set noColor
  • --color flag - similar to ls and other CLI utils
    • auto -> current behavior
    • never -> same behavior as NO_COLOR
    • always -> force color on

grant0417 avatar Dec 30 '22 19:12 grant0417

Also open to making the actual change if this is something wanted, just don't know which behavior is preferred.

grant0417 avatar Dec 30 '22 19:12 grant0417

If we were to support it, it should be via env var. Is there a prior art for FORCE_COLOR env variable?

bartlomieju avatar Dec 31 '22 13:12 bartlomieju

Prior art for FORCE_COLOR

  • Node supports FORCE_COLOR albeit 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, auto would be similar to cargo, i.e. DENO_TERM_COLOR: https://doc.rust-lang.org/cargo/reference/config.html#termcolor

  • There is also the CLICOLOR and CLICOLOR_FORCE env vars, but I figured this would not work as well with NO_COLOR: https://bixense.com/clicolors/

I do not have any strong preference on the approach here

grant0417 avatar Jan 02 '23 20:01 grant0417

@dsherret thoughts on this one?

bartlomieju avatar Jan 02 '23 21:01 bartlomieju

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.

dsherret avatar Jan 03 '23 00:01 dsherret

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?

grant0417 avatar Jan 03 '23 19:01 grant0417

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.

dsherret avatar Jan 03 '23 22:01 dsherret

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

jcbhmr avatar Oct 24 '23 20:10 jcbhmr

Also the extremely useful case of piping to less.

mdekstrand avatar Dec 19 '23 19:12 mdekstrand