lazy_static => once_cell

main
Tassilo Horn 3 years ago
parent b778869ca3
commit 59fa701ab5
  1. 2
      Cargo.lock
  2. 2
      Cargo.toml
  3. 19
      src/cmds.rs
  4. 13
      src/fmt_replace.rs
  5. 28
      src/tree.rs
  6. 23
      src/util.rs

2
Cargo.lock generated

@ -554,8 +554,8 @@ dependencies = [
"clap", "clap",
"directories", "directories",
"env_logger", "env_logger",
"lazy_static",
"log", "log",
"once_cell",
"rand", "rand",
"regex", "regex",
"rt-format", "rt-format",

@ -17,7 +17,6 @@ swayipc = "3.0.0"
toml = "0.5.8" toml = "0.5.8"
directories = "4.0" directories = "4.0"
regex = "1.5.5" regex = "1.5.5"
lazy_static = "1.4.0"
rand = "0.8.4" rand = "0.8.4"
rt-format = "0.3.0" rt-format = "0.3.0"
log = "0.4" log = "0.4"
@ -25,6 +24,7 @@ env_logger = { version = "0.9.0", default-features = false, features = ["termcol
swaybar-types = "3.0.0" swaybar-types = "3.0.0"
chrono = "0.4" chrono = "0.4"
sysinfo = "0.23" sysinfo = "0.23"
once_cell = "1.10.0"
[profile.release] [profile.release]
lto = "thin" lto = "thin"

@ -21,8 +21,9 @@ use crate::tree as t;
use crate::tree::NodeMethods; use crate::tree::NodeMethods;
use crate::util; use crate::util;
use crate::util::DisplayFormat; use crate::util::DisplayFormat;
use lazy_static::lazy_static; use once_cell::sync::Lazy;
use rand::prelude::SliceRandom; use rand::prelude::SliceRandom;
use regex::Regex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::atomic; use std::sync::atomic;
@ -537,10 +538,8 @@ pub fn focus_win_if_not_focused(
} }
} }
lazy_static! { static DIGIT_AND_NAME: Lazy<Regex> =
static ref DIGIT_AND_NAME: regex::Regex = Lazy::new(|| Regex::new(r"^(\d):(.*)").unwrap());
regex::Regex::new(r"^(\d):(.*)").unwrap();
}
fn create_workspace(ws_name: &str) { fn create_workspace(ws_name: &str) {
if DIGIT_AND_NAME.is_match(ws_name) { if DIGIT_AND_NAME.is_match(ws_name) {
@ -550,12 +549,10 @@ fn create_workspace(ws_name: &str) {
} }
} }
lazy_static! { static SPECIAL_WORKSPACE: Lazy<Regex> =
static ref SPECIAL_WORKSPACE: regex::Regex = Lazy::new(|| Regex::new(r"^#*w:(.*)").unwrap());
regex::Regex::new(r"^#*w:(.*)").unwrap(); static SPECIAL_SWAY: Lazy<regex::Regex> =
static ref SPECIAL_SWAY: regex::Regex = Lazy::new(|| Regex::new(r"^#*s:(.*)").unwrap());
regex::Regex::new(r"^#*s:(.*)").unwrap();
}
fn chop_workspace_shortcut(input: &str) -> &str { fn chop_workspace_shortcut(input: &str) -> &str {
match SPECIAL_WORKSPACE.captures(input) { match SPECIAL_WORKSPACE.captures(input) {

@ -13,14 +13,15 @@
// You should have received a copy of the GNU General Public License along with // You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/>. // this program. If not, see <https://www.gnu.org/licenses/>.
use lazy_static::lazy_static; use once_cell::sync::Lazy;
use regex::Regex;
lazy_static! { pub static PLACEHOLDER_RX: Lazy<Regex> = Lazy::new(|| {
pub static ref PLACEHOLDER_RX: regex::Regex = regex::Regex::new( Regex::new(
r"\{(?P<name>[^}:]+)(?::(?P<fmtstr>\{[^}]*\})(?P<clipstr>[^}]*))?\}" r"\{(?P<name>[^}:]+)(?::(?P<fmtstr>\{[^}]*\})(?P<clipstr>[^}]*))?\}",
) )
.unwrap(); .unwrap()
} });
pub fn maybe_html_escape(do_it: bool, text: String) -> String { pub fn maybe_html_escape(do_it: bool, text: String) -> String {
if do_it { if do_it {

@ -18,26 +18,34 @@
use crate::config; use crate::config;
use crate::util; use crate::util;
use crate::util::DisplayFormat; use crate::util::DisplayFormat;
use lazy_static::lazy_static; use once_cell::sync::Lazy;
use regex::Regex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::cell::RefCell; use std::cell::RefCell;
use std::cmp; use std::cmp;
use std::collections::HashMap; use std::collections::HashMap;
use std::rc::Rc; use std::rc::Rc;
use std::sync::Mutex;
use swayipc as s; use swayipc as s;
static SWAY_IPC_CONNECTION: Lazy<Mutex<RefCell<s::Connection>>> =
Lazy::new(|| {
Mutex::new(RefCell::new(
s::Connection::new().expect("Could not open sway IPC connection."),
))
});
pub fn get_root_node(include_scratchpad: bool) -> s::Node { pub fn get_root_node(include_scratchpad: bool) -> s::Node {
match s::Connection::new() { let mut root = match SWAY_IPC_CONNECTION.lock() {
Ok(mut con) => { Ok(cell) => cell.borrow_mut().get_tree().expect("Couldn't get tree"),
let mut root = con.get_tree().expect("Got no root node"); Err(err) => panic!("{}", err),
};
if !include_scratchpad { if !include_scratchpad {
root.nodes.retain(|o| !o.is_scratchpad()); root.nodes.retain(|o| !o.is_scratchpad());
} }
root root
} }
Err(err) => panic!("{}", err),
}
}
/// Immutable Node Iterator /// Immutable Node Iterator
/// ///
@ -441,10 +449,8 @@ pub fn get_tree<'a>(
} }
} }
lazy_static! { static APP_NAME_AND_VERSION_RX: Lazy<Regex> =
static ref APP_NAME_AND_VERSION_RX: regex::Regex = Lazy::new(|| Regex::new("(.+)(-[0-9.]+)").unwrap());
regex::Regex::new("(.+)(-[0-9.]+)").unwrap();
}
fn format_marks(marks: &[String]) -> String { fn format_marks(marks: &[String]) -> String {
if marks.is_empty() { if marks.is_empty() {

@ -15,12 +15,15 @@
//! Utility functions including selection between choices using a menu program. //! Utility functions including selection between choices using a menu program.
use once_cell::sync::Lazy;
use regex::Regex;
use crate::config as cfg; use crate::config as cfg;
use lazy_static::lazy_static;
use std::collections::HashMap; use std::collections::HashMap;
use std::io::{BufRead, Write}; use std::io::{BufRead, Write};
use std::path as p; use std::path as p;
use std::process as proc; use std::process as proc;
use std::sync::Mutex;
pub fn get_swayr_socket_path() -> String { pub fn get_swayr_socket_path() -> String {
// We prefer checking the env variable instead of // We prefer checking the env variable instead of
@ -120,12 +123,10 @@ fn find_icon(icon_name: &str, icon_dirs: &[String]) -> Option<Box<p::Path>> {
None None
} }
lazy_static! { static WM_CLASS_OR_ICON_RX: Lazy<Regex> =
static ref WM_CLASS_OR_ICON_RX: regex::Regex = Lazy::new(|| Regex::new(r"(StartupWMClass|Icon)=(.+)").unwrap());
regex::Regex::new(r"(StartupWMClass|Icon)=(.+)").unwrap(); static REV_DOMAIN_NAME_RX: Lazy<Regex> =
static ref REV_DOMAIN_NAME_RX: regex::Regex = Lazy::new(|| Regex::new(r"^(?:[a-zA-Z0-9-]+\.)+([a-zA-Z0-9-]+)$").unwrap());
regex::Regex::new(r"^(?:[a-zA-Z0-9-]+\.)+([a-zA-Z0-9-]+)$").unwrap();
}
fn get_app_id_to_icon_map( fn get_app_id_to_icon_map(
icon_dirs: &[String], icon_dirs: &[String],
@ -195,10 +196,10 @@ fn get_app_id_to_icon_map(
map map
} }
lazy_static! { // Well, this type definition is pretty useless since it's only used in
static ref APP_ID_TO_ICON_MAP: std::sync::Mutex<Option<HashMap<String, Box<p::Path>>>> = // get_icon anyway but clippy suggested it...
std::sync::Mutex::new(None); type AppIdToIconMap = Lazy<Mutex<Option<HashMap<String, Box<p::Path>>>>>;
} static APP_ID_TO_ICON_MAP: AppIdToIconMap = Lazy::new(|| Mutex::new(None));
pub fn get_icon(app_id: &str, icon_dirs: &[String]) -> Option<Box<p::Path>> { pub fn get_icon(app_id: &str, icon_dirs: &[String]) -> Option<Box<p::Path>> {
let mut opt = APP_ID_TO_ICON_MAP.lock().unwrap(); let mut opt = APP_ID_TO_ICON_MAP.lock().unwrap();

Loading…
Cancel
Save