From 3bdbf32e22431dc102f6121e4d75bf839b43ed83 Mon Sep 17 00:00:00 2001 From: LoricAndre Date: Wed, 22 Jan 2025 23:16:00 +0100 Subject: [PATCH] chore: remove lazy_static --- Cargo.lock | 1 - skim/Cargo.toml | 1 - skim/src/engine/factory.rs | 8 +++----- skim/src/field.rs | 10 ++++++---- skim/src/global.rs | 18 ++++++++---------- skim/src/input.rs | 20 ++++++++++++-------- skim/src/lib.rs | 2 -- skim/src/model/mod.rs | 8 +++----- skim/src/model/status.rs | 6 ------ skim/src/theme.rs | 7 +++---- skim/src/util.rs | 13 +++++-------- 11 files changed, 40 insertions(+), 54 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bd2664aa..038c6c34 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -861,7 +861,6 @@ dependencies = [ "env_logger", "fuzzy-matcher", "indexmap", - "lazy_static", "log", "nix 0.29.0", "rand", diff --git a/skim/Cargo.toml b/skim/Cargo.toml index 9e09fcd3..8864c9a0 100644 --- a/skim/Cargo.toml +++ b/skim/Cargo.toml @@ -23,7 +23,6 @@ path = "src/bin/main.rs" [dependencies] nix = { version = "0.29.0", features = ["fs"] } regex = "1.6.0" -lazy_static = "1.4.0" shlex = { version = "1.1.0", optional = true } unicode-width = "0.2.0" log = "0.4.25" diff --git a/skim/src/engine/factory.rs b/skim/src/engine/factory.rs index 64647eef..a86912ca 100644 --- a/skim/src/engine/factory.rs +++ b/skim/src/engine/factory.rs @@ -6,12 +6,10 @@ use crate::engine::regexp::RegexEngine; use crate::item::RankBuilder; use crate::{CaseMatching, MatchEngine, MatchEngineFactory}; use regex::Regex; -use std::sync::Arc; +use std::sync::{Arc, LazyLock}; -lazy_static! { - static ref RE_AND: Regex = Regex::new(r"([^ |]+( +\| +[^ |]*)+)|( +)").unwrap(); - static ref RE_OR: Regex = Regex::new(r" +\| +").unwrap(); -} +static RE_AND: LazyLock = LazyLock::new(|| Regex::new(r"([^ |]+( +\| +[^ |]*)+)|( +)").unwrap()); +static RE_OR: LazyLock = LazyLock::new(|| Regex::new(r" +\| +").unwrap()); //------------------------------------------------------------------------------ // Exact engine factory pub struct ExactOrFuzzyEngineFactory { diff --git a/skim/src/field.rs b/skim/src/field.rs index 9378ecc8..317db65a 100644 --- a/skim/src/field.rs +++ b/skim/src/field.rs @@ -1,9 +1,11 @@ use regex::Regex; -use std::cmp::{max, min}; +use std::{ + cmp::{max, min}, + sync::LazyLock, +}; -lazy_static! { - static ref FIELD_RANGE: Regex = Regex::new(r"^(?P-?\d+)?(?P\.\.)?(?P-?\d+)?$").unwrap(); -} +static FIELD_RANGE: LazyLock = + LazyLock::new(|| Regex::new(r"^(?P-?\d+)?(?P\.\.)?(?P-?\d+)?$").unwrap()); #[derive(PartialEq, Eq, Clone, Debug)] pub enum FieldRange { diff --git a/skim/src/global.rs b/skim/src/global.rs index 2517ac04..3dffc521 100644 --- a/skim/src/global.rs +++ b/skim/src/global.rs @@ -1,21 +1,19 @@ use std::collections::HashMap; use std::sync::atomic::{AtomicU32, Ordering}; -use std::sync::Mutex; +use std::sync::{LazyLock, Mutex}; // Consider that you invoke a command with different arguments several times // If you select some items each time, how will skim remember it? // => Well, we'll give each invocation a number, i.e. RUN_NUM // What if you invoke the same command and same arguments twice? // => We use NUM_MAP to specify the same run number. -lazy_static! { - static ref RUN_NUM: AtomicU32 = AtomicU32::new(0); - static ref SEQ: AtomicU32 = AtomicU32::new(1); - static ref NUM_MAP: Mutex> = { - let mut m = HashMap::new(); - m.insert("".to_string(), 0); - Mutex::new(m) - }; -} +static RUN_NUM: LazyLock = LazyLock::new(|| AtomicU32::new(0)); +static SEQ: LazyLock = LazyLock::new(|| AtomicU32::new(1)); +static NUM_MAP: LazyLock>> = LazyLock::new(|| { + let mut m = HashMap::new(); + m.insert("".to_string(), 0); + Mutex::new(m) +}); pub fn current_run_num() -> u32 { RUN_NUM.load(Ordering::SeqCst) diff --git a/skim/src/input.rs b/skim/src/input.rs index bf346751..abaa0f4e 100644 --- a/skim/src/input.rs +++ b/skim/src/input.rs @@ -3,6 +3,7 @@ use crate::event::{parse_event, Event}; use regex::Regex; use std::collections::HashMap; +use std::sync::LazyLock; use tuikit::event::Event as TermEvent; use tuikit::key::{from_keyname, Key}; @@ -87,14 +88,17 @@ type KeyActions<'a> = (&'a str, Vec<(&'a str, Option)>); /// parse key action string to `(key, action, argument)` tuple /// key_action is comma separated: 'ctrl-j:accept,ctrl-k:kill-line' pub fn parse_key_action(key_action: &str) -> Vec { - lazy_static! { - // match `key:action` or `key:action:arg` or `key:action(arg)` etc. - static ref RE: Regex = - Regex::new(r#"(?si)([^:]+?):((?:\+?[a-z-]+?(?:"[^"]*?"|'[^']*?'|\([^\)]*?\)|\[[^\]]*?\]|:[^:]*?)?\s*)+)(?:,|$)"#) - .unwrap(); - // grab key, action and arg out. - static ref RE_BIND: Regex = Regex::new(r#"(?si)([a-z-]+)("[^"]+?"|'[^']+?'|\([^\)]+?\)|\[[^\]]+?\]|:[^:]+?)?(?:\+|$)"#).unwrap(); - } + // match `key:action` or `key:action:arg` or `key:action(arg)` etc. + static RE: LazyLock = LazyLock::new(|| { + Regex::new( + r#"(?si)([^:]+?):((?:\+?[a-z-]+?(?:"[^"]*?"|'[^']*?'|\([^\)]*?\)|\[[^\]]*?\]|:[^:]*?)?\s*)+)(?:,|$)"#, + ) + .unwrap() + }); + // grab key, action and arg out. + static RE_BIND: LazyLock = LazyLock::new(|| { + Regex::new(r#"(?si)([a-z-]+)("[^"]+?"|'[^']+?'|\([^\)]+?\)|\[[^\]]+?\]|:[^:]+?)?(?:\+|$)"#).unwrap() + }); RE.captures_iter(key_action) .map(|caps| { diff --git a/skim/src/lib.rs b/skim/src/lib.rs index 1ecd22aa..d8dcf871 100644 --- a/skim/src/lib.rs +++ b/skim/src/lib.rs @@ -1,6 +1,4 @@ #[macro_use] -extern crate lazy_static; -#[macro_use] extern crate log; use std::any::Any; diff --git a/skim/src/model/mod.rs b/skim/src/model/mod.rs index b32dbd4e..59c8e631 100644 --- a/skim/src/model/mod.rs +++ b/skim/src/model/mod.rs @@ -8,7 +8,7 @@ use std::env; use std::process::Command; use std::rc::Rc; -use std::sync::Arc; +use std::sync::{Arc, LazyLock}; use std::time::Instant; use chrono::Duration as TimerDuration; @@ -39,10 +39,8 @@ use std::cmp::max; const REFRESH_DURATION: i64 = 100; -lazy_static! { - static ref RE_FIELDS: Regex = Regex::new(r"\\?(\{-?[0-9.,q]*?})").unwrap(); - static ref RE_PREVIEW_OFFSET: Regex = Regex::new(r"^\+([0-9]+|\{-?[0-9]+\})(-[0-9]+|-/[1-9][0-9]*)?$").unwrap(); -} +static RE_PREVIEW_OFFSET: LazyLock = + LazyLock::new(|| Regex::new(r"^\+([0-9]+|\{-?[0-9]+\})(-[0-9]+|-/[1-9][0-9]*)?$").unwrap()); struct ModelEnv { pub cmd: String, diff --git a/skim/src/model/status.rs b/skim/src/model/status.rs index 6710a610..0510c2eb 100644 --- a/skim/src/model/status.rs +++ b/skim/src/model/status.rs @@ -1,7 +1,6 @@ use std::sync::Arc; use std::time::Duration; -use regex::Regex; use tuikit::attr::{Attr, Effect}; use tuikit::canvas::Canvas; use tuikit::draw::{Draw, DrawResult}; @@ -18,11 +17,6 @@ const SPINNER_DURATION: u32 = 200; const SPINNERS_INLINE: [char; 2] = ['-', '<']; const SPINNERS_UNICODE: [char; 10] = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']; -lazy_static! { - static ref RE_FIELDS: Regex = Regex::new(r"\\?(\{-?[0-9.,q]*?})").unwrap(); - static ref RE_PREVIEW_OFFSET: Regex = Regex::new(r"^\+([0-9]+|\{-?[0-9]+\})(-[0-9]+|-/[1-9][0-9]*)?$").unwrap(); -} - #[derive(Clone)] pub(crate) struct Status { pub(crate) total: usize, diff --git a/skim/src/theme.rs b/skim/src/theme.rs index fd21e4b4..5de387b9 100644 --- a/skim/src/theme.rs +++ b/skim/src/theme.rs @@ -1,11 +1,10 @@ //! Handle the color theme +use std::sync::LazyLock; + use crate::options::SkimOptions; use tuikit::prelude::*; -#[rustfmt::skip] -lazy_static! { - pub static ref DEFAULT_THEME: ColorTheme = ColorTheme::dark256(); -} +pub static DEFAULT_THEME: LazyLock = LazyLock::new(|| ColorTheme::dark256()); /// The color scheme of skim's UI /// diff --git a/skim/src/util.rs b/skim/src/util.rs index 8d1e49c0..3e3d7c87 100644 --- a/skim/src/util.rs +++ b/skim/src/util.rs @@ -4,6 +4,7 @@ use std::fs::File; use std::io::{BufRead, BufReader}; use std::prelude::v1::*; use std::str::FromStr; +use std::sync::LazyLock; use regex::{Captures, Regex}; use tuikit::prelude::*; @@ -12,10 +13,8 @@ use unicode_width::UnicodeWidthChar; use crate::field::get_string_by_range; use crate::AnsiString; -lazy_static! { - static ref RE_ESCAPE: Regex = Regex::new(r"['\U{00}]").unwrap(); - static ref RE_NUMBER: Regex = Regex::new(r"[+|-]?\d+").unwrap(); -} +static RE_ESCAPE: LazyLock = LazyLock::new(|| Regex::new(r"['\U{00}]").unwrap()); +static RE_NUMBER: LazyLock = LazyLock::new(|| Regex::new(r"[+|-]?\d+").unwrap()); pub fn clear_canvas(canvas: &mut dyn Canvas) -> DrawResult<()> { let (screen_width, screen_height) = canvas.size()?; @@ -322,10 +321,8 @@ pub struct InjectContext<'a> { pub cmd_query: &'a str, } -lazy_static! { - static ref RE_ITEMS: Regex = Regex::new(r"\\?(\{ *-?[0-9.+]*? *})").unwrap(); - static ref RE_FIELDS: Regex = Regex::new(r"\\?(\{ *-?[0-9.,cq+n]*? *})").unwrap(); -} +static RE_ITEMS: LazyLock = LazyLock::new(|| Regex::new(r"\\?(\{ *-?[0-9.+]*? *})").unwrap()); +static RE_FIELDS: LazyLock = LazyLock::new(|| Regex::new(r"\\?(\{ *-?[0-9.,cq+n]*? *})").unwrap()); /// Check if a command depends on item /// e.g. contains `{}`, `{1..}`, `{+}`