From 5a1e29a1383e743699b8b85fc0f205758db7daec Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sat, 3 Aug 2024 13:09:05 -0400 Subject: [PATCH] Switch from lazy_static to once_cell This will ultimately facilitate migrating to the std solution, once the MSRV is high enough. --- Cargo.toml | 2 +- src/ansi.rs | 14 +++++++------- src/unix_term.rs | 13 +++++++------ src/utils.rs | 10 +++++----- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d850f3f7..73ee503a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ ansi-parsing = [] [dependencies] libc = "0.2.99" unicode-width = { version = "0.1", optional = true } -lazy_static = "1.4.0" +once_cell = "1" [target.'cfg(windows)'.dependencies] encode_unicode = "0.3" diff --git a/src/ansi.rs b/src/ansi.rs index 3a3c96c3..a9ca7ee4 100644 --- a/src/ansi.rs +++ b/src/ansi.rs @@ -271,18 +271,18 @@ impl<'a> FusedIterator for AnsiCodeIterator<'a> {} mod tests { use super::*; - use lazy_static::lazy_static; + use once_cell::sync::Lazy; use proptest::prelude::*; use regex::Regex; // The manual dfa `State` is a handwritten translation from the previously used regex. That // regex is kept here and used to ensure that the new matches are the same as the old - lazy_static! { - static ref STRIP_ANSI_RE: Regex = Regex::new( - r"[\x1b\x9b]([()][012AB]|[\[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-PRZcf-nqry=><])", - ) - .unwrap(); - } + static STRIP_ANSI_RE: Lazy = Lazy::new(|| { + Regex::new( + r"[\x1b\x9b]([()][012AB]|[\[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-PRZcf-nqry=><])", + ) + .unwrap() + }); impl<'a, 'b> PartialEq> for regex::Match<'b> { fn eq(&self, other: &Match<'a>) -> bool { diff --git a/src/unix_term.rs b/src/unix_term.rs index 271709f2..e775d96a 100644 --- a/src/unix_term.rs +++ b/src/unix_term.rs @@ -7,6 +7,9 @@ use std::mem; use std::os::unix::io::AsRawFd; use std::str; +#[cfg(not(target_os = "macos"))] +use once_cell::sync::Lazy; + use crate::kb::Key; use crate::term::Term; @@ -343,12 +346,10 @@ pub fn key_from_utf8(buf: &[u8]) -> Key { } #[cfg(not(target_os = "macos"))] -lazy_static::lazy_static! { - static ref IS_LANG_UTF8: bool = match std::env::var("LANG") { - Ok(lang) => lang.to_uppercase().ends_with("UTF-8"), - _ => false, - }; -} +static IS_LANG_UTF8: Lazy = Lazy::new(|| match std::env::var("LANG") { + Ok(lang) => lang.to_uppercase().ends_with("UTF-8"), + _ => false, +}); #[cfg(target_os = "macos")] pub fn wants_emoji() -> bool { diff --git a/src/utils.rs b/src/utils.rs index cfecc78f..27d9e964 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -4,7 +4,7 @@ use std::env; use std::fmt; use std::sync::atomic::{AtomicBool, Ordering}; -use lazy_static::lazy_static; +use once_cell::sync::Lazy; use crate::term::{wants_emoji, Term}; @@ -22,10 +22,10 @@ fn default_colors_enabled(out: &Term) -> bool { || &env::var("CLICOLOR_FORCE").unwrap_or_else(|_| "0".into()) != "0" } -lazy_static! { - static ref STDOUT_COLORS: AtomicBool = AtomicBool::new(default_colors_enabled(&Term::stdout())); - static ref STDERR_COLORS: AtomicBool = AtomicBool::new(default_colors_enabled(&Term::stderr())); -} +static STDOUT_COLORS: Lazy = + Lazy::new(|| AtomicBool::new(default_colors_enabled(&Term::stdout()))); +static STDERR_COLORS: Lazy = + Lazy::new(|| AtomicBool::new(default_colors_enabled(&Term::stderr()))); /// Returns `true` if colors should be enabled for stdout. ///