From 7c4b1325cdab2a6bb8c4d1fc3e8ca0a64a6479bb Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 7 Feb 2025 00:05:21 +0100 Subject: [PATCH] Fix Porgressbar rendering Checkbox (Fixes #1626) (#1627) * fixes #1610 --- lychee-bin/src/commands/check.rs | 13 ++++++++++++- lychee-bin/src/formatters/mod.rs | 7 ++++++- lychee-bin/src/options.rs | 4 +++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lychee-bin/src/commands/check.rs b/lychee-bin/src/commands/check.rs index b5cae77..e83f812 100644 --- a/lychee-bin/src/commands/check.rs +++ b/lychee-bin/src/commands/check.rs @@ -17,6 +17,7 @@ use lychee_lib::{ResponseBody, Status}; use crate::archive::{Archive, Suggestion}; use crate::formatters::get_response_formatter; use crate::formatters::response::ResponseFormatter; +use crate::options::OutputMode; use crate::parse::parse_duration_secs; use crate::verbosity::Verbosity; use crate::{cache::Cache, stats::ResponseStats, ExitCode}; @@ -66,7 +67,17 @@ where accept, )); - let formatter = get_response_formatter(¶ms.cfg.mode); + // Set the default formatter for progress bar output + let formatter_default = OutputMode::default(); + + // Make it easier to add new formatters in the future (without breaking the progress bar) + let allowed_output_modes = [OutputMode::Emoji, OutputMode::Plain, OutputMode::Color]; + + let formatter = get_response_formatter(if allowed_output_modes.contains(¶ms.cfg.mode) { + ¶ms.cfg.mode + } else { + &formatter_default + }); let show_results_task = tokio::spawn(progress_bar_task( recv_resp, diff --git a/lychee-bin/src/formatters/mod.rs b/lychee-bin/src/formatters/mod.rs index 9b11d94..ff8712d 100644 --- a/lychee-bin/src/formatters/mod.rs +++ b/lychee-bin/src/formatters/mod.rs @@ -30,8 +30,13 @@ pub(crate) fn get_stats_formatter( /// Create a response formatter based on the given format option pub(crate) fn get_response_formatter(mode: &OutputMode) -> Box { + // Checks if color is supported in current environment or NO_COLOR is set (https://no-color.org) if !supports_color() { - return Box::new(response::PlainFormatter); + // To fix `TaskFormatter` not working if color is not supported + return match mode { + OutputMode::Task => Box::new(response::TaskFormatter), + _ => Box::new(response::PlainFormatter), + }; } match mode { OutputMode::Plain => Box::new(response::PlainFormatter), diff --git a/lychee-bin/src/options.rs b/lychee-bin/src/options.rs index 3ce21b3..be47b5e 100644 --- a/lychee-bin/src/options.rs +++ b/lychee-bin/src/options.rs @@ -78,7 +78,9 @@ impl FromStr for StatsFormat { /// /// This decides over whether to use color, /// emojis, or plain text for the output. -#[derive(Debug, Deserialize, Default, Clone, Display, EnumIter, EnumString, VariantNames)] +#[derive( + Debug, Deserialize, Default, Clone, Display, EnumIter, EnumString, VariantNames, PartialEq, +)] #[non_exhaustive] pub(crate) enum OutputMode { /// Plain text output.