Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace lazy_static with OnceLock #164

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- Implemented bitwise operators `BitAnd`, `BitOr`, `BitXor`, and `Not` which all combine `Styles`\'s and output `Style`\'s. These can also take a `Style` as an operand.
- Added additional testing for all of the above changes.
- Added methods `with_style` and `with_color_and_style` to `Colorize`.
- Replaced lazy_static with `std::sync::OnceLock`. Soft breaking change by removing the global `SHOULD_COLORIZE`.

# 2.1.0
* Impl From<String> for ColoredString by @mahor1221 in https://github.com/colored-rs/colored/pull/126
Expand Down
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
Loading