Skip to content

Commit

Permalink
Switch from lazy_static to once_cell
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Dec 13, 2024
1 parent 6f286aa commit 56c7cbd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 5 additions & 5 deletions src/ansi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Regex> = 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<Match<'a>> for regex::Match<'_> {
fn eq(&self, other: &Match<'a>) -> bool {
Expand Down
13 changes: 7 additions & 6 deletions src/unix_term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<bool> = 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 {
Expand Down
10 changes: 5 additions & 5 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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<AtomicBool> =
Lazy::new(|| AtomicBool::new(default_colors_enabled(&Term::stdout())));
static STDERR_COLORS: Lazy<AtomicBool> =
Lazy::new(|| AtomicBool::new(default_colors_enabled(&Term::stderr())));

/// Returns `true` if colors should be enabled for stdout.
///
Expand Down

0 comments on commit 56c7cbd

Please sign in to comment.