Skip to content

Commit b04103f

Browse files
MichaReisersharkdp
andauthored
[red-knot] Add --color CLI option (#16758)
## Summary This PR adds a new `--color` CLI option that controls whether the output should be colorized or not. This is implements part of https://github.com/astral-sh/ruff/issues/16727 except that it doesn't implement the persistent configuration support as initially proposed in the CLI document. I realized, that having this as a persistent configuration is somewhat awkward because we may end up writing tracing logs **before** we loaded and resolved the settings. Arguably, it's probably fine to color the output up to that point, but it feels like a somewhat broken experience. That's why I decided not to add the persistent configuration option for now. ## Test Plan I tested this change manually by running Red Knot with `--color=always`, `--color=never`, and `--color=auto` (or no argument) and verified that: * The diagnostics are or aren't colored * The tracing output is or isn't colored. --------- Co-authored-by: David Peter <[email protected]>
1 parent c100d51 commit b04103f

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

crates/red_knot/src/args.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ pub(crate) struct CheckCommand {
7979
#[arg(long)]
8080
pub(crate) output_format: Option<OutputFormat>,
8181

82+
/// Control when colored output is used.
83+
#[arg(long, value_name = "WHEN")]
84+
pub(crate) color: Option<TerminalColor>,
85+
8286
/// Use exit code 1 if there are any warning-level diagnostics.
8387
#[arg(long, conflicts_with = "exit_zero", default_missing_value = "true", num_args=0..1)]
8488
pub(crate) error_on_warning: Option<bool>,
@@ -247,3 +251,17 @@ impl From<OutputFormat> for ruff_db::diagnostic::DiagnosticFormat {
247251
}
248252
}
249253
}
254+
255+
/// Control when colored output is used.
256+
#[derive(Copy, Clone, Hash, Debug, PartialEq, Eq, PartialOrd, Ord, Default, clap::ValueEnum)]
257+
pub(crate) enum TerminalColor {
258+
/// Display colors if the output goes to an interactive terminal.
259+
#[default]
260+
Auto,
261+
262+
/// Always display colors.
263+
Always,
264+
265+
/// Never display colors.
266+
Never,
267+
}

crates/red_knot/src/main.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::process::{ExitCode, Termination};
44
use anyhow::Result;
55
use std::sync::Mutex;
66

7-
use crate::args::{Args, CheckCommand, Command};
7+
use crate::args::{Args, CheckCommand, Command, TerminalColor};
88
use crate::logging::setup_tracing;
99
use anyhow::{anyhow, Context};
1010
use clap::Parser;
@@ -76,6 +76,8 @@ pub(crate) fn version() -> Result<()> {
7676
}
7777

7878
fn run_check(args: CheckCommand) -> anyhow::Result<ExitStatus> {
79+
set_colored_override(args.color);
80+
7981
let verbosity = args.verbosity.level();
8082
countme::enable(verbosity.is_trace());
8183
let _guard = setup_tracing(verbosity)?;
@@ -257,16 +259,16 @@ impl MainLoop {
257259
result,
258260
revision: check_revision,
259261
} => {
262+
let terminal_settings = db.project().settings(db).terminal();
260263
let display_config = DisplayDiagnosticConfig::default()
261-
.format(db.project().settings(db).terminal().output_format)
264+
.format(terminal_settings.output_format)
262265
.color(colored::control::SHOULD_COLORIZE.should_colorize());
263266

264-
let min_error_severity =
265-
if db.project().settings(db).terminal().error_on_warning {
266-
Severity::Warning
267-
} else {
268-
Severity::Error
269-
};
267+
let min_error_severity = if terminal_settings.error_on_warning {
268+
Severity::Warning
269+
} else {
270+
Severity::Error
271+
};
270272

271273
if check_revision == revision {
272274
if db.project().files(db).is_empty() {
@@ -363,3 +365,21 @@ enum MainLoopMessage {
363365
ApplyChanges(Vec<watch::ChangeEvent>),
364366
Exit,
365367
}
368+
369+
fn set_colored_override(color: Option<TerminalColor>) {
370+
let Some(color) = color else {
371+
return;
372+
};
373+
374+
match color {
375+
TerminalColor::Auto => {
376+
colored::control::unset_override();
377+
}
378+
TerminalColor::Always => {
379+
colored::control::set_override(true);
380+
}
381+
TerminalColor::Never => {
382+
colored::control::set_override(false);
383+
}
384+
}
385+
}

0 commit comments

Comments
 (0)