From 31da0a1356133bec7c4b9859ad772f22d4136ec6 Mon Sep 17 00:00:00 2001 From: Alexey <247128645+axkurcom@users.noreply.github.com> Date: Tue, 26 May 2026 12:20:28 +0300 Subject: [PATCH] Fixes for Disable Colors --- src/maestro/helpers.rs | 42 +++++++++++++++++++++++++++++++++++++++--- src/maestro/mod.rs | 2 ++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/maestro/helpers.rs b/src/maestro/helpers.rs index 1c9b337..257f6ba 100644 --- a/src/maestro/helpers.rs +++ b/src/maestro/helpers.rs @@ -1,6 +1,7 @@ #![allow(clippy::items_after_test_module)] use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicBool, Ordering}; use std::time::Duration; use tokio::sync::watch; @@ -18,8 +19,30 @@ use crate::transport::middle_proxy::{ const MAESTRO_COLOR: &str = "\x1b[92m"; const COLOR_RESET: &str = "\x1b[0m"; +static MAESTRO_COLORS_ENABLED: AtomicBool = AtomicBool::new(true); + +/// Enables or disables ANSI color in direct MAESTRO status lines. +pub(crate) fn set_maestro_colors_enabled(enabled: bool) { + MAESTRO_COLORS_ENABLED.store(enabled, Ordering::Relaxed); +} + +fn format_maestro_line(message: impl AsRef, colors_enabled: bool) -> String { + if colors_enabled { + format!("{MAESTRO_COLOR}MAESTRO{COLOR_RESET}: {}", message.as_ref()) + } else { + format!("MAESTRO: {}", message.as_ref()) + } +} + +/// Prints a direct MAESTRO status line outside the tracing subscriber. pub(crate) fn print_maestro_line(message: impl AsRef) { - eprintln!("{MAESTRO_COLOR}MAESTRO{COLOR_RESET}: {}", message.as_ref()); + eprintln!( + "{}", + format_maestro_line( + message, + MAESTRO_COLORS_ENABLED.load(Ordering::Relaxed) + ) + ); } pub(crate) fn resolve_runtime_config_path( @@ -274,11 +297,24 @@ mod tests { use std::path::{Path, PathBuf}; use super::{ - expected_handshake_close_description, is_expected_handshake_eof, peer_close_description, - resolve_runtime_base_dir, resolve_runtime_config_path, + expected_handshake_close_description, format_maestro_line, is_expected_handshake_eof, + peer_close_description, resolve_runtime_base_dir, resolve_runtime_config_path, }; use crate::error::{ProxyError, StreamError}; + #[test] + fn maestro_line_formatter_respects_disabled_colors() { + let plain = format_maestro_line("boot", false); + assert_eq!(plain, "MAESTRO: boot"); + assert!(!plain.contains('\x1b')); + } + + #[test] + fn maestro_line_formatter_keeps_color_when_enabled() { + let colored = format_maestro_line("boot", true); + assert!(colored.contains("\x1b[92mMAESTRO\x1b[0m")); + } + #[test] fn resolve_runtime_config_path_anchors_relative_to_startup_cwd() { let nonce = std::time::SystemTime::now() diff --git a/src/maestro/mod.rs b/src/maestro/mod.rs index 834704f..e711ab4 100644 --- a/src/maestro/mod.rs +++ b/src/maestro/mod.rs @@ -49,6 +49,7 @@ use crate::transport::UpstreamManager; use crate::transport::middle_proxy::MePool; use helpers::{ parse_cli, print_maestro_line, resolve_runtime_base_dir, resolve_runtime_config_path, + set_maestro_colors_enabled, }; #[cfg(unix)] @@ -314,6 +315,7 @@ async fn run_telemt_core( eprintln!("[telemt] Invalid network.dns_overrides: {}", e); std::process::exit(1); } + set_maestro_colors_enabled(!config.general.disable_colors); startup_tracker .complete_component(COMPONENT_CONFIG_LOAD, Some("config is ready".to_string())) .await;