From 1a2356eaa489bef7a71572cec444154d3955ea9c Mon Sep 17 00:00:00 2001 From: pvshvp-oss Date: Sat, 11 May 2024 22:54:15 -0500 Subject: [PATCH] Fix colors --- paxy/src/app/config.rs | 6 ++++-- paxy/src/app/logging.rs | 31 ++++++++++++++++++++++++++++--- paxy/src/app/ui.rs | 37 ++----------------------------------- 3 files changed, 34 insertions(+), 40 deletions(-) diff --git a/paxy/src/app/config.rs b/paxy/src/app/config.rs index b6dab34..7f45a7c 100644 --- a/paxy/src/app/config.rs +++ b/paxy/src/app/config.rs @@ -142,7 +142,9 @@ impl figment::Provider for ConfigTemplate { figment::Metadata::named("Config Object") } - fn data(&self) -> Result, figment::Error> { + fn data( + &self, + ) -> Result, figment::Error> { figment::providers::Serialized::defaults(ConfigTemplate::default()).data() } @@ -159,7 +161,7 @@ pub struct Config { impl Default for Config { fn default() -> Self { Self { - figment: Figment::from(ConfigTemplate::default()) + figment: Figment::from(ConfigTemplate::default()), } } } diff --git a/paxy/src/app/logging.rs b/paxy/src/app/logging.rs index 3d792dc..6dd81bf 100644 --- a/paxy/src/app/logging.rs +++ b/paxy/src/app/logging.rs @@ -8,6 +8,15 @@ pub fn init_log(config: &config::ConfigTemplate) -> Result { .max_verbosity, ); + // Turn off colors in the console streams if requested + if config + .console_output_format + .no_color + { + anstream::ColorChoice::Never.write_global(); + owo_colors::set_override(false); + } + // Obtain writers to various logging destinations and worker guards (for // keeping the streams alive) let (non_blocking_file_writer, _file_writer_guard) = @@ -137,7 +146,7 @@ pub fn init_log(config: &config::ConfigTemplate) -> Result { tracing::subscriber::set_global_default(subscriber) .context(SetGlobalDefaultSubscriberSnafu {})?; - Ok(Handle { + let mut logging_handle = Handle { _switch_stdout_inner: Some(Box::new(switch_stdout)), _switch_stderr_inner: Some(Box::new(switch_stderr)), worker_guards: vec![ @@ -145,7 +154,23 @@ pub fn init_log(config: &config::ConfigTemplate) -> Result { _stdout_writer_guard, _stderr_writer_guard, ], - }) + }; + + // Change the output mode if requested + match config + .console_output_format + .mode + { + ui::ConsoleOutputMode::Plain => logging_handle + .switch_to_plain()?, + ui::ConsoleOutputMode::Json => logging_handle + .switch_to_json()?, + ui::ConsoleOutputMode::Test => logging_handle + .switch_to_test()?, + _ => {} + }; + + Ok(logging_handle) } fn tracing_level_filter_from_log_level_filter(level_filter: log::LevelFilter) -> LevelFilter { @@ -266,6 +291,6 @@ use tracing_subscriber::{ Layer, }; -use crate::app::{self, config}; +use crate::app::{self, ui, config}; // endregion: IMPORTS diff --git a/paxy/src/app/ui.rs b/paxy/src/app/ui.rs index aa092f5..6c43fe7 100644 --- a/paxy/src/app/ui.rs +++ b/paxy/src/app/ui.rs @@ -11,14 +11,11 @@ where .context(app::ConfigSnafu {}) .context(crate::AppSnafu)?; - // Begin logging - let mut logging_handle = logging::init_log(&config) + // Begin logging and outputting + let logging_handle = logging::init_log(&config) .context(app::LoggingSnafu {}) .context(crate::AppSnafu {})?; - // Adjust output formatting if requested - adjust_output_formatting(&config.console_output_format, &mut logging_handle)?; - emit_welcome_messages(); emit_diagnostic_messages(&config, &console_input); @@ -120,36 +117,6 @@ fn emit_test_messages() { tracing::info!(target:"PLAIN", "{} Testing: Plain Target", console::Emoji("🧪", "")); } -fn adjust_output_formatting( - internally_consistent_console_output_format: &ConsoleOutputFormat, - logging_handle: &mut logging::Handle, -) -> Result<(), crate::Error> { - // Turn off colors if requested - if internally_consistent_console_output_format.no_color { - anstream::ColorChoice::Never.write_global(); - owo_colors::set_override(false); - } - - // Change output mode if requested - match internally_consistent_console_output_format.mode { - ConsoleOutputMode::Plain => logging_handle - .switch_to_plain() - .context(app::LoggingSnafu {}) - .context(crate::AppSnafu {})?, - ConsoleOutputMode::Json => logging_handle - .switch_to_json() - .context(app::LoggingSnafu {}) - .context(crate::AppSnafu {})?, - ConsoleOutputMode::Test => logging_handle - .switch_to_test() - .context(app::LoggingSnafu {}) - .context(crate::AppSnafu {})?, - _ => {} - }; - - Ok(()) -} - #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ConsoleOutputFormat { pub mode: ConsoleOutputMode,