Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: refactor formatter selection #3668

Merged
merged 4 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 17 additions & 14 deletions cli/src/formatter.rs
Original file line number Diff line number Diff line change
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 All @@ -165,6 +164,10 @@ impl FormatterFactory {
}
}
}

pub fn is_color(&self) -> bool {
matches!(self.kind, FormatterFactoryKind::Color { .. })
}
}

pub struct PlainTextFormatter<W> {
Expand Down
47 changes: 22 additions & 25 deletions cli/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ impl Write for UiStderr<'_> {
}

pub struct Ui {
color: bool,
quiet: bool,
pager_cmd: CommandNameAndArgs,
paginate: PaginationChoice,
Expand Down Expand Up @@ -218,16 +217,25 @@ fn color_setting(config: &config::Config) -> ColorChoice {
.unwrap_or_default()
}

fn debug_color(choice: ColorChoice) -> bool {
matches!(choice, ColorChoice::Debug)
}

fn use_color(choice: ColorChoice) -> bool {
match choice {
ColorChoice::Always => true,
ColorChoice::Never => false,
ColorChoice::Debug => true,
ColorChoice::Auto => io::stdout().is_terminal(),
fn prepare_formatter_factory(
config: &config::Config,
stdout: &Stdout,
) -> Result<FormatterFactory, config::ConfigError> {
let terminal = stdout.is_terminal();
let (color, debug) = match color_setting(config) {
ColorChoice::Always => (true, false),
ColorChoice::Never => (false, false),
ColorChoice::Debug => (true, true),
ColorChoice::Auto => (terminal, false),
};
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())
}
}

Expand Down Expand Up @@ -257,17 +265,10 @@ fn pager_setting(config: &config::Config) -> Result<CommandNameAndArgs, CommandE

impl Ui {
pub fn with_config(config: &config::Config) -> Result<Ui, CommandError> {
let color = color_setting(config);
let debug = debug_color(color);
let color = use_color(color);
let quiet = be_quiet(config);
// 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 = io::stdout().is_terminal();
let formatter_factory = FormatterFactory::prepare(config, debug, color, sanitize)?;
let formatter_factory = prepare_formatter_factory(config, &io::stdout())?;
let progress_indicator = progress_indicator_setting(config);
Ok(Ui {
color,
quiet,
formatter_factory,
pager_cmd: pager_setting(config)?,
Expand All @@ -278,15 +279,11 @@ impl Ui {
}

pub fn reset(&mut self, config: &config::Config) -> Result<(), CommandError> {
let color = color_setting(config);
let debug = debug_color(color);
self.color = use_color(color);
self.quiet = be_quiet(config);
self.paginate = pagination_setting(config)?;
self.pager_cmd = pager_setting(config)?;
self.progress_indicator = progress_indicator_setting(config);
let sanitize = io::stdout().is_terminal();
self.formatter_factory = FormatterFactory::prepare(config, debug, self.color, sanitize)?;
self.formatter_factory = prepare_formatter_factory(config, &io::stdout())?;
Ok(())
}

Expand Down Expand Up @@ -326,7 +323,7 @@ impl Ui {
}

pub fn color(&self) -> bool {
self.color
self.formatter_factory.is_color()
}

pub fn new_formatter<'output, W: Write + 'output>(
Expand Down