Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions crates/red_knot/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ pub(crate) struct CheckCommand {
#[arg(long)]
pub(crate) output_format: Option<OutputFormat>,

/// Control when colored output is used.
#[arg(long)]
pub(crate) color: Option<TerminalColor>,

/// Use exit code 1 if there are any warning-level diagnostics.
#[arg(long, conflicts_with = "exit_zero", default_missing_value = "true", num_args=0..1)]
pub(crate) error_on_warning: Option<bool>,
Expand Down Expand Up @@ -247,3 +251,17 @@ impl From<OutputFormat> for ruff_db::diagnostic::DiagnosticFormat {
}
}
}

/// Control when colored output is used.
#[derive(Copy, Clone, Hash, Debug, PartialEq, Eq, PartialOrd, Ord, Default, clap::ValueEnum)]
pub(crate) enum TerminalColor {
/// Automatically detect if color support is available on the terminal.
#[default]
Auto,

/// Always display colors
Always,

/// Never display colors.
Never,
}
36 changes: 28 additions & 8 deletions crates/red_knot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::process::{ExitCode, Termination};
use anyhow::Result;
use std::sync::Mutex;

use crate::args::{Args, CheckCommand, Command};
use crate::args::{Args, CheckCommand, Command, TerminalColor};
use crate::logging::setup_tracing;
use anyhow::{anyhow, Context};
use clap::Parser;
Expand Down Expand Up @@ -76,6 +76,8 @@ pub(crate) fn version() -> Result<()> {
}

fn run_check(args: CheckCommand) -> anyhow::Result<ExitStatus> {
set_colored_override(args.color);

let verbosity = args.verbosity.level();
countme::enable(verbosity.is_trace());
let _guard = setup_tracing(verbosity)?;
Expand Down Expand Up @@ -255,16 +257,16 @@ impl MainLoop {
result,
revision: check_revision,
} => {
let terminal_settings = db.project().settings(db).terminal();
let display_config = DisplayDiagnosticConfig::default()
.format(db.project().settings(db).terminal().output_format)
.format(terminal_settings.output_format)
.color(colored::control::SHOULD_COLORIZE.should_colorize());

let min_error_severity =
if db.project().settings(db).terminal().error_on_warning {
Severity::Warning
} else {
Severity::Error
};
let min_error_severity = if terminal_settings.error_on_warning {
Severity::Warning
} else {
Severity::Error
};

if check_revision == revision {
if db.project().files(db).is_empty() {
Expand Down Expand Up @@ -361,3 +363,21 @@ enum MainLoopMessage {
ApplyChanges(Vec<watch::ChangeEvent>),
Exit,
}

fn set_colored_override(color: Option<TerminalColor>) {
let Some(color) = color else {
return;
};

match color {
TerminalColor::Auto => {
colored::control::unset_override();
}
TerminalColor::Always => {
colored::control::set_override(true);
}
TerminalColor::Never => {
colored::control::set_override(false);
}
}
}
Loading