Skip to content

Commit

Permalink
create cached fn get_computer_name()
Browse files Browse the repository at this point in the history
  • Loading branch information
Araxeus committed Mar 29, 2023
1 parent 3c0d1eb commit 8c6b648
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 11 deletions.
153 changes: 152 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
7 changes: 2 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -75,7 +72,7 @@ fn get_choices(entry: &Entry) -> Vec<Entry> {
#[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 {
Expand Down Expand Up @@ -106,7 +103,7 @@ fn get_choices(entry: &Entry) -> Vec<Entry> {
// .. 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,
});
Expand Down
8 changes: 3 additions & 5 deletions src/structs/prompt_renderer.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,8 @@ fn bitmask_to_vec(bitmask: u32) -> Vec<char> {
}
vec
}

#[cached::proc_macro::once]
pub fn get_computer_name() -> String {
env::var("COMPUTERNAME").unwrap_or_else(|_| String::from("My Computer"))
}

0 comments on commit 8c6b648

Please sign in to comment.