Skip to content

Commit

Permalink
chore: remove lazy_static
Browse files Browse the repository at this point in the history
  • Loading branch information
LoricAndre committed Jan 22, 2025
1 parent bf49f68 commit 3bdbf32
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 54 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion skim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 3 additions & 5 deletions skim/src/engine/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Regex> = LazyLock::new(|| Regex::new(r"([^ |]+( +\| +[^ |]*)+)|( +)").unwrap());
static RE_OR: LazyLock<Regex> = LazyLock::new(|| Regex::new(r" +\| +").unwrap());
//------------------------------------------------------------------------------
// Exact engine factory
pub struct ExactOrFuzzyEngineFactory {
Expand Down
10 changes: 6 additions & 4 deletions skim/src/field.rs
Original file line number Diff line number Diff line change
@@ -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<left>-?\d+)?(?P<sep>\.\.)?(?P<right>-?\d+)?$").unwrap();
}
static FIELD_RANGE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^(?P<left>-?\d+)?(?P<sep>\.\.)?(?P<right>-?\d+)?$").unwrap());

#[derive(PartialEq, Eq, Clone, Debug)]
pub enum FieldRange {
Expand Down
18 changes: 8 additions & 10 deletions skim/src/global.rs
Original file line number Diff line number Diff line change
@@ -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<HashMap<String, u32>> = {
let mut m = HashMap::new();
m.insert("".to_string(), 0);
Mutex::new(m)
};
}
static RUN_NUM: LazyLock<AtomicU32> = LazyLock::new(|| AtomicU32::new(0));
static SEQ: LazyLock<AtomicU32> = LazyLock::new(|| AtomicU32::new(1));
static NUM_MAP: LazyLock<Mutex<HashMap<String, u32>>> = 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)
Expand Down
20 changes: 12 additions & 8 deletions skim/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -87,14 +88,17 @@ type KeyActions<'a> = (&'a str, Vec<(&'a str, Option<String>)>);
/// 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<KeyActions> {
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<Regex> = LazyLock::new(|| {
Regex::new(
r#"(?si)([^:]+?):((?:\+?[a-z-]+?(?:"[^"]*?"|'[^']*?'|\([^\)]*?\)|\[[^\]]*?\]|:[^:]*?)?\s*)+)(?:,|$)"#,
)
.unwrap()
});
// grab key, action and arg out.
static RE_BIND: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r#"(?si)([a-z-]+)("[^"]+?"|'[^']+?'|\([^\)]+?\)|\[[^\]]+?\]|:[^:]+?)?(?:\+|$)"#).unwrap()
});

RE.captures_iter(key_action)
.map(|caps| {
Expand Down
2 changes: 0 additions & 2 deletions skim/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;

use std::any::Any;
Expand Down
8 changes: 3 additions & 5 deletions skim/src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Regex> =
LazyLock::new(|| Regex::new(r"^\+([0-9]+|\{-?[0-9]+\})(-[0-9]+|-/[1-9][0-9]*)?$").unwrap());

struct ModelEnv {
pub cmd: String,
Expand Down
6 changes: 0 additions & 6 deletions skim/src/model/status.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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,
Expand Down
7 changes: 3 additions & 4 deletions skim/src/theme.rs
Original file line number Diff line number Diff line change
@@ -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<ColorTheme> = LazyLock::new(|| ColorTheme::dark256());

/// The color scheme of skim's UI
///
Expand Down
13 changes: 5 additions & 8 deletions skim/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -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<Regex> = LazyLock::new(|| Regex::new(r"['\U{00}]").unwrap());
static RE_NUMBER: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[+|-]?\d+").unwrap());

pub fn clear_canvas(canvas: &mut dyn Canvas) -> DrawResult<()> {
let (screen_width, screen_height) = canvas.size()?;
Expand Down Expand Up @@ -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<Regex> = LazyLock::new(|| Regex::new(r"\\?(\{ *-?[0-9.+]*? *})").unwrap());
static RE_FIELDS: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\\?(\{ *-?[0-9.,cq+n]*? *})").unwrap());

/// Check if a command depends on item
/// e.g. contains `{}`, `{1..}`, `{+}`
Expand Down

0 comments on commit 3bdbf32

Please sign in to comment.