Skip to content

Commit 42d52eb

Browse files
authored
Support FORCE_COLOR env var (#10839)
Fixes #5499 ## Summary Add support for `FORCE_COLOR` env var, as specified at https://force-color.org/ ## Test Plan I wrote an integration test for this, and then realized that can't work, since we use a dev-dependency on `colored` with the `no-color` feature to avoid ANSI color codes in test snapshots. So this is just tested manually. `cargo run --features test-rules -- check --no-cache --isolated - --select RUF901 --diff < /dev/null` shows a colored diff. `cargo run --features test-rules -- check --no-cache --isolated - --select RUF901 --diff < /dev/null | less` does not have color, since we pipe it to `less`. `FORCE_COLOR=1 cargo run --features test-rules -- check --no-cache --isolated - --select RUF901 --diff < /dev/null | less` does have color (after this diff), even though we pipe it to `less`.
1 parent 3fd2297 commit 42d52eb

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

crates/ruff/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ pub fn run(
149149
#[cfg(windows)]
150150
assert!(colored::control::set_virtual_terminal(true).is_ok());
151151

152+
// support FORCE_COLOR env var
153+
if let Some(force_color) = std::env::var_os("FORCE_COLOR") {
154+
if force_color.len() > 0 {
155+
colored::control::set_override(true);
156+
}
157+
}
158+
152159
set_up_logging(global_options.log_level())?;
153160

154161
if let Some(deprecated_alias_warning) = deprecated_alias_warning {

docs/faq.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,11 +628,12 @@ Even still, given the dynamic nature of Python, it's difficult to have _complete
628628
making changes to code, even for seemingly trivial fixes. If a "safe" fix breaks your code, please
629629
[file an Issue](https://github.com/astral-sh/ruff/issues/new).
630630

631-
## How can I disable Ruff's color output?
631+
## How can I disable/force Ruff's color output?
632632

633633
Ruff's color output is powered by the [`colored`](https://crates.io/crates/colored) crate, which
634634
attempts to automatically detect whether the output stream supports color. However, you can force
635-
colors off by setting the `NO_COLOR` environment variable to any value (e.g., `NO_COLOR=1`).
635+
colors off by setting the `NO_COLOR` environment variable to any value (e.g., `NO_COLOR=1`), or
636+
force colors on by setting `FORCE_COLOR` to any non-empty value (e.g. `FORCE_COLOR=1`).
636637

637638
[`colored`](https://crates.io/crates/colored) also supports the `CLICOLOR` and `CLICOLOR_FORCE`
638639
environment variables (see the [spec](https://bixense.com/clicolors/)).

0 commit comments

Comments
 (0)