Skip to content

Commit

Permalink
Fix colors
Browse files Browse the repository at this point in the history
  • Loading branch information
pvshvp-oss committed May 12, 2024
1 parent 2473d77 commit 1a2356e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 40 deletions.
6 changes: 4 additions & 2 deletions paxy/src/app/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ impl figment::Provider for ConfigTemplate {
figment::Metadata::named("Config Object")
}

fn data(&self) -> Result<figment::value::Map<figment::Profile, figment::value::Dict>, figment::Error> {
fn data(
&self,
) -> Result<figment::value::Map<figment::Profile, figment::value::Dict>, figment::Error> {
figment::providers::Serialized::defaults(ConfigTemplate::default()).data()
}

Expand All @@ -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()),
}
}
}
Expand Down
31 changes: 28 additions & 3 deletions paxy/src/app/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ pub fn init_log(config: &config::ConfigTemplate) -> Result<Handle, Error> {
.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) =
Expand Down Expand Up @@ -137,15 +146,31 @@ pub fn init_log(config: &config::ConfigTemplate) -> Result<Handle, Error> {
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![
_file_writer_guard,
_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 {
Expand Down Expand Up @@ -266,6 +291,6 @@ use tracing_subscriber::{
Layer,
};

use crate::app::{self, config};
use crate::app::{self, ui, config};

// endregion: IMPORTS
37 changes: 2 additions & 35 deletions paxy/src/app/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 1a2356e

Please sign in to comment.