diff --git a/cli/src/formatter.rs b/cli/src/formatter.rs index bf36ca09de..164e4a201d 100644 --- a/cli/src/formatter.rs +++ b/cli/src/formatter.rs @@ -136,20 +136,19 @@ enum FormatterFactoryKind { } impl FormatterFactory { - pub fn prepare( - config: &config::Config, - debug: bool, - color: bool, - sanitized: bool, - ) -> Result { - let kind = if color { - let rules = Arc::new(rules_from_config(config)?); - FormatterFactoryKind::Color { rules, debug } - } else if sanitized { - FormatterFactoryKind::Sanitized - } else { - FormatterFactoryKind::PlainText - }; + pub fn plain_text() -> Self { + let kind = FormatterFactoryKind::PlainText; + FormatterFactory { kind } + } + + pub fn sanitized() -> Self { + let kind = FormatterFactoryKind::Sanitized; + FormatterFactory { kind } + } + + pub fn color(config: &config::Config, debug: bool) -> Result { + let rules = Arc::new(rules_from_config(config)?); + let kind = FormatterFactoryKind::Color { rules, debug }; Ok(FormatterFactory { kind }) } diff --git a/cli/src/ui.rs b/cli/src/ui.rs index 4d62b1a56e..27db4d8007 100644 --- a/cli/src/ui.rs +++ b/cli/src/ui.rs @@ -228,10 +228,15 @@ fn prepare_formatter_factory( ColorChoice::Debug => (true, true), ColorChoice::Auto => (terminal, false), }; - // Sanitize ANSI escape codes if we're printing to a terminal. Doesn't affect - // ANSI escape codes that originate from the formatter itself. - let sanitize = terminal; - FormatterFactory::prepare(config, debug, color, sanitize) + if color { + FormatterFactory::color(config, debug) + } else if terminal { + // Sanitize ANSI escape codes if we're printing to a terminal. Doesn't + // affect ANSI escape codes that originate from the formatter itself. + Ok(FormatterFactory::sanitized()) + } else { + Ok(FormatterFactory::plain_text()) + } } fn be_quiet(config: &config::Config) -> bool {