replace lazy_static with once_cell (#7502)

replacing the dependence on `lazy_static` with `once_cell`, this will
ensure that variables are initialized when needed instead of startup
time.
This commit is contained in:
Jaffar Ashoor
2022-12-17 21:30:04 +03:00
committed by GitHub
parent 90849a067f
commit c3c41a61b0
11 changed files with 69 additions and 99 deletions

View File

@ -54,7 +54,6 @@ indexmap = { version="1.7", features=["serde-1"] }
Inflector = "0.11"
is-root = "0.1.2"
itertools = "0.10.0"
lazy_static = "1.4.0"
log = "0.4.14"
lscolors = { version = "0.12.0", features = ["crossterm"], default-features = false }
md5 = { package = "md-5", version = "0.10.0" }

View File

@ -1,10 +1,10 @@
use lazy_static::lazy_static;
use nu_ansi_term::*;
use nu_engine::CallExt;
use nu_protocol::{
ast::Call, engine::Command, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData,
PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use once_cell::sync::Lazy;
use std::collections::HashMap;
#[derive(Clone)]
@ -16,8 +16,8 @@ struct AnsiCode {
code: String,
}
lazy_static! {
static ref CODE_LIST: Vec<AnsiCode> = vec!{
#[rustfmt::skip]
static CODE_LIST: Lazy<Vec<AnsiCode>> = Lazy::new(|| { vec![
AnsiCode{ short_name: Some("g"), long_name: "green", code: Color::Green.prefix().to_string()},
AnsiCode{ short_name: Some("gb"), long_name: "green_bold", code: Color::Green.bold().prefix().to_string()},
AnsiCode{ short_name: Some("gu"), long_name: "green_underline", code: Color::Green.underline().prefix().to_string()},
@ -476,10 +476,12 @@ lazy_static! {
// Returns terminal size like "[<r>;<c>R" where r is rows and c is columns
// This should work assuming your terminal is not greater than 999x999
AnsiCode{ short_name: None, long_name:"size", code: "\x1b[s\x1b[999;999H\x1b[6n\x1b[u".to_string()},};
AnsiCode{ short_name: None, long_name:"size", code: "\x1b[s\x1b[999;999H\x1b[6n\x1b[u".to_string()}
]
});
static ref CODE_MAP: HashMap<&'static str, &'static str > = build_ansi_hashmap(&CODE_LIST);
}
static CODE_MAP: Lazy<HashMap<&'static str, &'static str>> =
Lazy::new(|| build_ansi_hashmap(&CODE_LIST));
impl Command for AnsiCommand {
fn name(&self) -> &str {
@ -783,10 +785,10 @@ fn generate_ansi_code_list(
.into_pipeline_data(engine_state.ctrlc.clone()));
}
fn build_ansi_hashmap(v: &'static [AnsiCode]) -> HashMap<&'static str, &'static str> {
fn build_ansi_hashmap(v: &[AnsiCode]) -> HashMap<&str, &str> {
let mut result = HashMap::new();
for code in v.iter() {
let value: &'static str = &code.code;
let value: &str = &code.code;
if let Some(sn) = code.short_name {
result.insert(sn, value);
}

View File

@ -1,11 +1,11 @@
use indexmap::indexmap;
use indexmap::map::IndexMap;
use lazy_static::lazy_static;
use nu_engine::CallExt;
use nu_protocol::{
ast::Call, engine::Command, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData,
PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use once_cell::sync::Lazy;
// Character used to separate directories in a Path Environment variable on windows is ";"
#[cfg(target_family = "windows")]
@ -17,8 +17,8 @@ const ENV_PATH_SEPARATOR_CHAR: char = ':';
#[derive(Clone)]
pub struct Char;
lazy_static! {
static ref CHAR_MAP: IndexMap<&'static str, String> = indexmap! {
static CHAR_MAP: Lazy<IndexMap<&'static str, String>> = Lazy::new(|| {
indexmap! {
// These are some regular characters that either can't be used or
// it's just easier to use them like this.
@ -146,8 +146,8 @@ lazy_static! {
"unit_separator" => '\x1f'.to_string(),
"unit_sep" => '\x1f'.to_string(),
"us" => '\x1f'.to_string(),
};
}
}
});
impl Command for Char {
fn name(&self) -> &str {

View File

@ -1,5 +1,5 @@
use lazy_static::lazy_static;
use nu_protocol::{ShellError, Span};
use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::path::Path;
@ -85,50 +85,47 @@ impl Icons {
// .unwrap_or_default()
// }
lazy_static! {
static ref MAP_BY_NAME: HashMap<&'static str, char> = {
let mut m = HashMap::new();
m.insert(".Trash", '\u{f1f8}'); //
m.insert(".atom", '\u{e764}'); //
m.insert(".bashprofile", '\u{e615}'); //
m.insert(".bashrc", '\u{f489}'); //
m.insert(".git", '\u{f1d3}'); // 
m.insert(".gitattributes", '\u{f1d3}'); // 
m.insert(".gitconfig", '\u{f1d3}'); //
m.insert(".github", '\u{f408}'); //
m.insert(".gitignore", '\u{f1d3}'); // 
m.insert(".gitmodules", '\u{f1d3}'); //
m.insert(".rvm", '\u{e21e}'); //
m.insert(".vimrc", '\u{e62b}'); //
m.insert(".vscode", '\u{e70c}'); //
m.insert(".zshrc", '\u{f489}'); //
m.insert("Cargo.lock", '\u{e7a8}'); //
m.insert("bin", '\u{e5fc}'); // 
m.insert("config", '\u{e5fc}'); //
m.insert("docker-compose.yml", '\u{f308}'); // 
m.insert("Dockerfile", '\u{f308}'); //
m.insert("ds_store", '\u{f179}'); //
m.insert("gitignore_global", '\u{f1d3}'); //
m.insert("gradle", '\u{e70e}'); //
m.insert("gruntfile.coffee", '\u{e611}'); // 
m.insert("gruntfile.js", '\u{e611}'); // 
m.insert("gruntfile.ls", '\u{e611}'); //
m.insert("gulpfile.coffee", '\u{e610}'); // 
m.insert("gulpfile.js", '\u{e610}'); // 
m.insert("gulpfile.ls", '\u{e610}'); //
m.insert("hidden", '\u{f023}'); //
m.insert("include", '\u{e5fc}'); //
m.insert("lib", '\u{f121}'); //
m.insert("localized", '\u{f179}'); //
m.insert("Makefile", '\u{e779}'); //
m.insert("node_modules", '\u{e718}'); //
m.insert("npmignore", '\u{e71e}'); //
m.insert("rubydoc", '\u{e73b}'); //
m.insert("yarn.lock", '\u{e718}'); // 
m
};
}
static MAP_BY_NAME: Lazy<HashMap<&'static str, char>> = Lazy::new(|| {
HashMap::from([
(".Trash", '\u{f1f8}'), // 
(".atom", '\u{e764}'), //
(".bashprofile", '\u{e615}'), //
(".bashrc", '\u{f489}'), //
(".git", '\u{f1d3}'), //
(".gitattributes", '\u{f1d3}'), // 
(".gitconfig", '\u{f1d3}'), // 
(".github", '\u{f408}'), //
(".gitignore", '\u{f1d3}'), //
(".gitmodules", '\u{f1d3}'), // 
(".rvm", '\u{e21e}'), //
(".vimrc", '\u{e62b}'), //
(".vscode", '\u{e70c}'), //
(".zshrc", '\u{f489}'), //
("Cargo.lock", '\u{e7a8}'), //
("bin", '\u{e5fc}'), //
("config", '\u{e5fc}'), // 
("docker-compose.yml", '\u{f308}'), //
("Dockerfile", '\u{f308}'), // 
("ds_store", '\u{f179}'), //
("gitignore_global", '\u{f1d3}'), //
("gradle", '\u{e70e}'), //
("gruntfile.coffee", '\u{e611}'), //
("gruntfile.js", '\u{e611}'), // 
("gruntfile.ls", '\u{e611}'), // 
("gulpfile.coffee", '\u{e610}'), //
("gulpfile.js", '\u{e610}'), // 
("gulpfile.ls", '\u{e610}'), // 
("hidden", '\u{f023}'), //
("include", '\u{e5fc}'), //
("lib", '\u{f121}'), //
("localized", '\u{f179}'), //
("Makefile", '\u{e779}'), //
("node_modules", '\u{e718}'), //
("npmignore", '\u{e71e}'), //
("rubydoc", '\u{e73b}'), //
("yarn.lock", '\u{e718}'), //
])
});
pub fn icon_for_file(file_path: &Path, span: Span) -> Result<char, ShellError> {
let extensions = Box::new(FileExtensions);