From 56c7cbdb8653020fb30e82a28edca541461b67da Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 13 Dec 2024 11:33:42 +0100 Subject: [PATCH] Switch from lazy_static to once_cell --- Cargo.toml | 2 +- src/ansi.rs | 10 +++++----- src/unix_term.rs | 13 +++++++------ src/utils.rs | 10 +++++----- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a32a5eab..f6d008ef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,8 +19,8 @@ ansi-parsing = [] [dependencies] libc = "0.2.99" +once_cell = "1.8" unicode-width = { version = "0.2", optional = true } -lazy_static = "1.4.0" [target.'cfg(windows)'.dependencies] encode_unicode = "1" diff --git a/src/ansi.rs b/src/ansi.rs index bc54b6d0..f615da8e 100644 --- a/src/ansi.rs +++ b/src/ansi.rs @@ -271,18 +271,18 @@ impl FusedIterator for AnsiCodeIterator<'_> {} 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( + 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(); - } + .unwrap() + }); impl<'a> PartialEq> for regex::Match<'_> { 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 be998a38..c320f76a 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. ///