Skip to content

Commit

Permalink
ui: add per-type FormatterFactory constructors, inline selection
Browse files Browse the repository at this point in the history
This should be better than passing (bool, bool, bool) arguments.
  • Loading branch information
yuja committed May 12, 2024
1 parent 46246a7 commit e3b3b55
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
27 changes: 13 additions & 14 deletions cli/src/formatter.rs
Expand Up @@ -136,20 +136,19 @@ enum FormatterFactoryKind {
}

impl FormatterFactory {
pub fn prepare(
config: &config::Config,
debug: bool,
color: bool,
sanitized: bool,
) -> Result<Self, config::ConfigError> {
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<Self, config::ConfigError> {
let rules = Arc::new(rules_from_config(config)?);
let kind = FormatterFactoryKind::Color { rules, debug };
Ok(FormatterFactory { kind })
}

Expand Down
13 changes: 9 additions & 4 deletions cli/src/ui.rs
Expand Up @@ -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 {
Expand Down

0 comments on commit e3b3b55

Please sign in to comment.