From 8c6b648db4fdea4af8527dc277459be9c35a4346 Mon Sep 17 00:00:00 2001 From: Araxeus <78568641+Araxeus@users.noreply.github.com> Date: Wed, 29 Mar 2023 22:35:59 +0300 Subject: [PATCH] create cached `fn get_computer_name()` --- Cargo.lock | 153 ++++++++++++++++++++++++++++++++- Cargo.toml | 1 + src/main.rs | 7 +- src/structs/prompt_renderer.rs | 8 +- src/utils.rs | 5 ++ 5 files changed, 163 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 38151b4..9882e26 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -141,6 +141,12 @@ dependencies = [ "syn", ] +[[package]] +name = "async_once" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ce4f10ea3abcd6617873bae9f91d1c5332b4a778bd9ce34d0cd517474c1de82" + [[package]] name = "atomic-waker" version = "1.1.0" @@ -215,6 +221,44 @@ version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +[[package]] +name = "cached" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e5877db5d1af7fae60d06b5db9430b68056a69b3582a0be8e3691e87654aeb6" +dependencies = [ + "async-trait", + "async_once", + "cached_proc_macro", + "cached_proc_macro_types", + "futures", + "hashbrown 0.13.2", + "instant", + "lazy_static", + "once_cell", + "thiserror", + "tokio", +] + +[[package]] +name = "cached_proc_macro" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e10ca87c81aaa3a949dbbe2b5e6c2c45dbc94ba4897e45ea31ff9ec5087be3dc" +dependencies = [ + "cached_proc_macro_types", + "darling", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "cached_proc_macro_types" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a4f925191b4367301851c6d99b09890311d74b0d43f274c0b34c86d308a3663" + [[package]] name = "cc" version = "1.0.78" @@ -394,6 +438,41 @@ dependencies = [ "syn", ] +[[package]] +name = "darling" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b750cb3417fd1b327431a470f388520309479ab0bf5e323505daf0290cd3850" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "109c1ca6e6b7f82cc233a97004ea8ed7ca123a9af07a8230878fcfda9b158bf0" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn", +] + +[[package]] +name = "darling_macro" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e" +dependencies = [ + "darling_core", + "quote", + "syn", +] + [[package]] name = "derivative" version = "2.2.0" @@ -528,6 +607,36 @@ dependencies = [ "instant", ] +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "futures" +version = "0.3.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38390104763dc37a5145a53c29c63c1290b5d316d6086ec32c293f6736051bb0" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52ba265a92256105f45b719605a571ffe2d1f0fea3807304b522c1d778f79eed" +dependencies = [ + "futures-core", + "futures-sink", +] + [[package]] name = "futures-core" version = "0.3.25" @@ -625,6 +734,12 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +[[package]] +name = "hashbrown" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" + [[package]] name = "heck" version = "0.3.3" @@ -686,6 +801,12 @@ dependencies = [ "cxx-build", ] +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "indexmap" version = "1.9.2" @@ -693,7 +814,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" dependencies = [ "autocfg", - "hashbrown", + "hashbrown 0.12.3", ] [[package]] @@ -806,6 +927,7 @@ dependencies = [ name = "ls-interactive" version = "1.6.0" dependencies = [ + "cached", "console", "crossterm", "fuzzy-matcher", @@ -1356,6 +1478,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "strum" version = "0.22.0" @@ -1489,6 +1617,29 @@ dependencies = [ "serde_json", ] +[[package]] +name = "tokio" +version = "1.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03201d01c3c27a29c8a5cee5b55a93ddae1ccf6f08f65365c2c918f8c1b76f64" +dependencies = [ + "autocfg", + "pin-project-lite", + "tokio-macros", + "windows-sys 0.45.0", +] + +[[package]] +name = "tokio-macros" +version = "1.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d266c00fde287f55d3f1c3e96c500c362a2b8c695076ec180f27918820bc6df8" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "toml" version = "0.7.2" diff --git a/Cargo.toml b/Cargo.toml index 0c186a3..09d8d26 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ fuzzy-matcher = "0.3.7" crossterm = "0.26.1" unicode-segmentation = "1.10.1" tiny_update_notifier = "2.2.0" +cached = "0.42.0" [target.'cfg(windows)'.dependencies] windows = { version = "0.44.0", features = ["Win32_Storage_FileSystem"] } diff --git a/src/main.rs b/src/main.rs index 8430248..8a29891 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,9 +6,6 @@ use std::{fs, path::Path}; use structs::{Entry, Filetype, Icons}; use utils::{display_choices, err, get_first_arg, pretty_path, resolve_lnk, KeyModifiers}; -#[cfg(windows)] -use utils::get_logical_drives; - use tiny_update_notifier::check_github; fn main() { @@ -75,7 +72,7 @@ fn get_choices(entry: &Entry) -> Vec { #[cfg(windows)] // Open Drives View on Windows if entry.filetype == Filetype::DriveView { - match get_logical_drives() { + match utils::get_logical_drives() { Ok(drives) => { for drive in drives { result_vector.push(Entry { @@ -106,7 +103,7 @@ fn get_choices(entry: &Entry) -> Vec { // .. Open Drives View on Windows result_vector.push(Entry { name: String::from(".."), - path: env!("COMPUTERNAME").to_string(), + path: utils::get_computer_name(), icon: &Icons::PC, filetype: Filetype::DriveView, }); diff --git a/src/structs/prompt_renderer.rs b/src/structs/prompt_renderer.rs index ef3be37..903bc97 100644 --- a/src/structs/prompt_renderer.rs +++ b/src/structs/prompt_renderer.rs @@ -1,10 +1,10 @@ //! Customizes the rendering of the elements. -use std::{env, fmt, io}; +use std::{fmt, io}; use console::{style, Style, StyledObject, Term}; use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher}; -use crate::utils::{link, link_with_label, pretty_path}; +use crate::utils::{get_computer_name, link, link_with_label, pretty_path}; use super::Entry; @@ -183,9 +183,7 @@ impl Theme { cursor_pos: usize, ) -> fmt::Result { if !prompt.is_empty() { - let link_text = if cfg!(windows) - && prompt == env::var("COMPUTERNAME").unwrap_or("My Computer".to_string()) - { + let link_text = if cfg!(windows) && prompt == get_computer_name() { link_with_label("shell:MyComputerFolder", prompt) } else { link(prompt) diff --git a/src/utils.rs b/src/utils.rs index df4b33d..02c908a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -100,3 +100,8 @@ fn bitmask_to_vec(bitmask: u32) -> Vec { } vec } + +#[cached::proc_macro::once] +pub fn get_computer_name() -> String { + env::var("COMPUTERNAME").unwrap_or_else(|_| String::from("My Computer")) +}