From d4cfd3b1a59cee3e6f1d52ae0929c62283a04941 Mon Sep 17 00:00:00 2001 From: pvshvp-oss Date: Mon, 6 May 2024 19:19:59 -0500 Subject: [PATCH] WIP --- paxy-cli/src/lib.rs | 2 +- paxy-gui/src/lib.rs | 2 +- paxy/src/app/config.rs | 89 +++++++++++++++++++++++++++++------------- paxy/src/app/ui.rs | 33 ++++++---------- 4 files changed, 75 insertions(+), 51 deletions(-) diff --git a/paxy-cli/src/lib.rs b/paxy-cli/src/lib.rs index 3402db6..7784a8d 100644 --- a/paxy-cli/src/lib.rs +++ b/paxy-cli/src/lib.rs @@ -97,7 +97,7 @@ mod cli_template { .is_no_color() } - fn verbosity_filter(&self) -> &log::LevelFilter { + fn verbosity_filter(&self) -> log::LevelFilter { self.global_args .verbosity_filter() } diff --git a/paxy-gui/src/lib.rs b/paxy-gui/src/lib.rs index b110471..4f97089 100644 --- a/paxy-gui/src/lib.rs +++ b/paxy-gui/src/lib.rs @@ -80,7 +80,7 @@ mod gui_cli_template { .is_no_color() } - fn verbosity_filter(&self) -> &log::LevelFilter { + fn verbosity_filter(&self) -> log::LevelFilter { self.global_args .verbosity_filter() } diff --git a/paxy/src/app/config.rs b/paxy/src/app/config.rs index 3820579..052a72c 100644 --- a/paxy/src/app/config.rs +++ b/paxy/src/app/config.rs @@ -89,6 +89,23 @@ fn admerge_from_stub(candidate_config_filepath_stub: &PathBuf, mut figment: Figm figment } +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ConfigTemplate { + pub config_filepaths: Vec, + pub log_filepath_stub: PathBuf, + pub console_output_format: ConsoleOutputFormat, +} + +impl Default for ConfigTemplate { + fn default() -> Self { + Self { + config_filepaths: Vec::new(), + log_filepath_stub: PathBuf::default(), + console_output_format: ConsoleOutputFormat::default(), + } + } +} + pub struct Config { pub figment: Figment, } @@ -147,18 +164,51 @@ impl Config { self } - pub fn with_overriding_env_var>(env_var_name: S) -> &mut Self { - let env_var_name = env_var_name.as_ref(); - self.figment = self - .figment - .admerge(Env::raw().only(&[env_var_name])); + pub fn with_overriding_args(&mut self, cli_arguments: A) -> &mut Self { + if let Some(path) = cli_arguments.config_filepath() { + self.figment = self + .figment + .admerge(("config_filepaths", path)); + } - self - } + let console_output_mode = cli_arguments.console_output_mode(); + if console_output_mode != ConsoleOutputMode::Regular { + self.figment = self + .figment + .admerge(("console_output_format.mode", console_output_mode)); + } - pub fn with_overriding_args(&mut self, arguments: A) -> &mut Self { - if let Some(path) = arguments.config_filepath() { - self.figment = self.figment.admerge(("config_filepaths", path)); + let current_max_verbosity = self + .figment + .extract_inner::("console_output_format.max_verbosity"); + let requested_max_verbosity = cli_arguments.max_output_verbosity(); + if let Ok(current_max_verbosity) = current_max_verbosity { + if cli_requested_max_verbosity > current_max_verbosity { + self.figment = self + .figment + .admerge(( + "console_output_format.max_verbosity", + requested_max_verbosity, + )) + } + } + + let current_no_color = self + .figment + .extract_inner::("console_output_format.no_color"); + let requested_no_color = + cli_arguments.is_no_color() || cli_arguments.is_plain() || cli_arguments.is_json(); + let env_no_color = env::var("NO_COLOR").is_ok() + || env::var(format!( + "{}_NO_COLOR", + String::from(*app::APP_NAME).to_uppercase() + )) + .is_ok() + || env::var("TERM").is_ok_and(|env_term_value| env_term_value.to_lowercase == "dumb"); + if !current_no_color && (requested_no_color || env_no_color) { + self.figment = self + .figment + .admerge(("console_output_format.no_color", true)); } self @@ -171,23 +221,6 @@ impl Config { } } -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct ConfigTemplate { - pub config_filepaths: Vec, - pub log_filepath_stub: PathBuf, - pub console_output_format: ConsoleOutputFormat, -} - -impl Default for ConfigTemplate { - fn default() -> Self { - Self { - config_filepaths: Vec::new(), - log_filepath_stub: PathBuf::default(), - console_output_format: ConsoleOutputFormat::default(), - } - } -} - // region: IMPORTS use std::path::PathBuf; @@ -204,7 +237,7 @@ use log::LevelFilter; use serde::{Deserialize, Serialize}; use snafu::{OptionExt, ResultExt, Snafu}; -use super::ui::GlobalArguments; +use super::ui::{ConsoleOutputMode, GlobalArguments}; use crate::app; use crate::app::ui; diff --git a/paxy/src/app/ui.rs b/paxy/src/app/ui.rs index 00d1fad..0aa279f 100644 --- a/paxy/src/app/ui.rs +++ b/paxy/src/app/ui.rs @@ -40,7 +40,7 @@ fn resolve_max_output_verbosity( let verbosity_flag_filter = cli_output_format.requested_verbosity; if matches!( - cli_output_format.output_mode, + cli_output_format.mode, CliOutputMode::Plain | CliOutputMode::Json ) { return Some(LevelFilter::Info); @@ -62,7 +62,7 @@ fn adjust_output_formatting( ) { // Turn off colors if requested if matches!( - cli_output_format.output_mode, + cli_output_format.mode, CliOutputMode::Plain | CliOutputMode::Json ) || cli_output_format.no_color || is_env_variable_set("NO_COLOR") @@ -76,7 +76,7 @@ fn adjust_output_formatting( } // Change output mode if requested - match cli_output_format.output_mode { + match cli_output_format.mode { CliOutputMode::Plain => logging_handle .switch_to_plain() .context(app::LoggingSnafu {}) @@ -195,7 +195,7 @@ fn emit_test_messages() { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ConsoleOutputFormat { - pub output_mode: ConsoleOutputMode, + pub mode: ConsoleOutputMode, pub max_verbosity: log::LevelFilter, @@ -205,7 +205,7 @@ pub struct ConsoleOutputFormat { impl Default for ConsoleOutputFormat { fn default() -> Self { Self { - output_mode: ConsoleOutputMode::default(), + mode: ConsoleOutputMode::default(), max_verbosity: log::LevelFilter::Info, no_color: false, } @@ -277,7 +277,7 @@ pub trait GlobalArguments { fn is_test(&self) -> bool; - fn verbosity_filter(&self) -> &log::LevelFilter; + fn verbosity_filter(&self) -> log::LevelFilter; fn console_output_mode(&self) -> ConsoleOutputMode { if self.is_json() { @@ -292,21 +292,12 @@ pub trait GlobalArguments { } fn max_output_verbosity(&self) -> log::LevelFilter { - let verbosity_flag_filter = cli_output_format.requested_verbosity; - if matches!( - cli_output_format.output_mode, - CliOutputMode::Plain | CliOutputMode::Json - ) { - return Some(LevelFilter::Info); - } else if verbosity_flag_filter < clap_verbosity_flag::LevelFilter::Debug - && cli_global_arguments.is_debug() - { - return Some(LevelFilter::Debug); + if self.is_plain() || self.is_json() { + log::LevelFilter::Info + } else if self.is_debug() && log::LevelFilter::Debug > self.verbosity_filter() { + log::LevelFilter::Debug } else { - return verbosity_flag_filter - .as_str() - .parse() - .ok(); + self.verbosity_filter() } } } @@ -424,7 +415,7 @@ pub mod cli_template { self.no_color_flag } - fn verbosity_filter(&self) -> &log::LevelFilter { + fn verbosity_filter(&self) -> log::LevelFilter { self.verbosity .log_level_filter() .and_then(|log_level_filter| {