From f3c0754bf5c6ac48552bc5bc0f1a35d8e55d3794 Mon Sep 17 00:00:00 2001 From: Kevin Vigor Date: Sat, 5 Aug 2023 07:38:17 -0600 Subject: [PATCH] Add `style::force_color_output()` API to override NO_COLOR. (#802) --- src/style.rs | 12 ++++++++++++ src/style/types/colored.rs | 1 - 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/style.rs b/src/style.rs index 9555cab4..72c22811 100644 --- a/src/style.rs +++ b/src/style.rs @@ -166,6 +166,18 @@ pub fn available_color_count() -> u16 { .unwrap_or(8) } +/// Forces colored output on or off globally, overriding NO_COLOR. +/// +/// # Notes +/// +/// crossterm supports NO_COLOR (https://no-color.org/) to disabled colored output. +/// +/// This API allows applications to override that behavior and force colorized output +/// even if NO_COLOR is set. +pub fn force_color_output(enabled: bool) { + Colored::set_ansi_color_disabled(!enabled) +} + /// A command that sets the the foreground color. /// /// See [`Color`](enum.Color.html) for more info. diff --git a/src/style/types/colored.rs b/src/style/types/colored.rs index 2830d09d..85f921b3 100644 --- a/src/style/types/colored.rs +++ b/src/style/types/colored.rs @@ -86,7 +86,6 @@ impl Colored { ANSI_COLOR_DISABLED.load(Ordering::SeqCst) } - #[cfg(test)] pub fn set_ansi_color_disabled(val: bool) { // Force the one-time initializer to run. _ = Self::ansi_color_disabled_memoized();