Skip to content

Commit

Permalink
replace lazy_static with OnceLock
Browse files Browse the repository at this point in the history
  • Loading branch information
xTachyon committed Jan 17, 2024
1 parent 749ff4e commit a380e33
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ rust-version = "1.70"
no-color = []

[dependencies]
lazy_static = "1"

[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.48"
Expand Down
10 changes: 6 additions & 4 deletions src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::default::Default;
use std::env;
use std::io::{self, IsTerminal};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::OnceLock;

/// Sets a flag to the console to use a virtual terminal environment.
///
Expand Down Expand Up @@ -69,18 +70,19 @@ pub struct ShouldColorize {
/// Use this to force colored to ignore the environment and always/never colorize
/// See example/control.rs
pub fn set_override(override_colorize: bool) {
SHOULD_COLORIZE.set_override(override_colorize)
should_colorize_global().set_override(override_colorize)
}

/// Remove the manual override and let the environment decide if it's ok to colorize
/// See example/control.rs
pub fn unset_override() {
SHOULD_COLORIZE.unset_override()
should_colorize_global().unset_override()
}

lazy_static! {
/// The persistent [`ShouldColorize`].
pub static ref SHOULD_COLORIZE: ShouldColorize = ShouldColorize::from_env();
pub fn should_colorize_global() -> &'static ShouldColorize {
static SHOULD_COLORIZE: OnceLock<ShouldColorize> = OnceLock::new();
SHOULD_COLORIZE.get_or_init(ShouldColorize::from_env)
}

impl Default for ShouldColorize {
Expand Down
5 changes: 1 addition & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
//! modify them.
#![warn(missing_docs)]

#[macro_use]
extern crate lazy_static;

#[cfg(test)]
extern crate rspec;

Expand Down Expand Up @@ -491,7 +488,7 @@ impl ColoredString {

#[cfg(not(feature = "no-color"))]
fn has_colors(&self) -> bool {
control::SHOULD_COLORIZE.should_colorize()
control::should_colorize_global().should_colorize()
}

#[cfg(feature = "no-color")]
Expand Down

0 comments on commit a380e33

Please sign in to comment.