rust-cli / anstyle

ANSI text styling
https://docs.rs/anstyle
Other
106 stars 16 forks source link

Please support disabling any force-enable environment variables #163

Closed joshtriplett closed 7 months ago

joshtriplett commented 7 months ago

I'd like to use anstream, and I'd like to make use of the automatic detection of terminal color support. However, I need to disable the use of force-enable environment variables like CLICOLOR_FORCE, since that would cause breakage when my program's output is piped to another program and parsed. (Any program whose output might be parsed potentially has this problem.) Could you please provide a documented mechanism to use Auto but disable this force-enable mechanism, so that the use of color only depends on whether the output device is a terminal that supports color (or on NO_COLOR, which can't cause breakage)?

Ideally, I'd like to have an option I can set to get this behavior for Auto.

Alternatively, if that's not possible, I'd love to have a function I can call to do this subset of detection and then set the global colorchoice to either Always or Never accordingly, which would skip subsequent Auto handling.

epage commented 7 months ago

Background: My previous effort in improving terminal styling was concolor. In working on anstream, I found impedance mismatches between the answers concolor provided and the questions anstream asked and has to abandon it and put all low level logic in anstyle_query and all highlevel logic directly in anstream.

So I have a certain level of caution when it comes to presenting a subset of terminal detection.

In addition, I'm concerned that there won't be one "right" answer for a subset of detection (e.g. whether to include CI detection)(. We can more easily make breaking changes than anstyle so we can more easily entertain the idea.

As a workaround, anstream does expose AutoStream::new and you can look at our choice logic and do something similar yourself, if you want https://github.com/rust-cli/anstyle/blob/main/crates/anstream/src/auto.rs#L152 What would be helpful also is understanding the problems or limitations with using the workaround.

joshtriplett commented 7 months ago

@epage OK, that split seems fair enough: with all the low-level logic put in anstyle_query, it seems easy enough to manually call the desired functions.

Would you consider documenting that approach in anstream, as the suggested alternative for folks who don't want anstream's detection? This would go hand-in-hand with the documentation in https://github.com/rust-cli/anstyle/pull/155 , with an added sentence like "If you want to do the detection differently (e.g. checking different environment variables), you can use the functions in anstyle_query directly, and then call AutoStream::always or AutoStream::never accordingly."