From 37e9e30103d8b1cdcdacc07f457dd6c787919f0b Mon Sep 17 00:00:00 2001 From: The Mist Date: Sat, 18 Oct 2025 20:46:46 +0800 Subject: [PATCH] [Fix] cannot parse correct home dir on windows --- Cargo.lock | 7 ++++--- core/Cargo.toml | 1 + core/src/themes.rs | 2 +- core/src/utils/path.rs | 18 +++++++----------- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b4d919a..17f75bf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -419,6 +419,7 @@ dependencies = [ "chrono", "cosmic-text", "derive_builder", + "home", "hyperpolyglot_fork", "include_dir", "mime_guess", @@ -972,11 +973,11 @@ checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" [[package]] name = "home" -version = "0.5.9" +version = "0.5.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" +checksum = "589533453244b0995c858700322199b2becb13b627df2851f64a2775d024abcf" dependencies = [ - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] diff --git a/core/Cargo.toml b/core/Cargo.toml index bb6b667..7008fd5 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -34,6 +34,7 @@ url = "2.5.4" hyperpolyglot_fork = { version = "0.1.7", optional = true } theme-converter = "0.1.2" plist = "1.8.0" +home = "0.5.11" [features] default = [] diff --git a/core/src/themes.rs b/core/src/themes.rs index 0db1906..ef20640 100644 --- a/core/src/themes.rs +++ b/core/src/themes.rs @@ -52,7 +52,7 @@ pub async fn parse_remote_code_theme(path: &str, code_theme: &str) -> anyhow::Re } pub async fn parse_code_theme(code_theme: &str) -> anyhow::Result { - let remote_theme_folder_path = get_config_home_path()?.join("remote_themes"); + let remote_theme_folder_path = get_config_home_path().join("remote_themes"); if !remote_theme_folder_path.exists() { fs::create_dir_all(&remote_theme_folder_path)?; diff --git a/core/src/utils/path.rs b/core/src/utils/path.rs index cf06137..7529a64 100644 --- a/core/src/utils/path.rs +++ b/core/src/utils/path.rs @@ -18,20 +18,16 @@ fn create_temp_file_name() -> String { format!("CodeSnap_{}", formatted_time) } -pub fn parse_home_variable(path: &str) -> Result { - if cfg!(windows) { - return Ok(path.to_string()); - } - - let home_path = var("HOME")?; +pub fn parse_home_variable(path: &str) -> String { + let home_path = home::home_dir().unwrap().to_string_lossy().to_string(); let regex = Regex::new(r"(~|$HOME)").unwrap(); let path = regex.replace_all(path, home_path); - Ok(path.to_string()) + path.to_string() } pub fn parse_file_name(path: &str) -> Result { - let path_str = parse_home_variable(path)?; + let path_str = parse_home_variable(path); let path = Path::new(&path_str); let parsed_path = if path.is_dir() { path.join(create_temp_file_name()) @@ -45,13 +41,13 @@ pub fn parse_file_name(path: &str) -> Result { Ok(parsed_path) } -pub fn get_config_home_path() -> Result { - let home = parse_home_variable("~")?; +pub fn get_config_home_path() -> PathBuf { + let home = parse_home_variable("~"); let config_home_path = Path::new(&home).join(".config").join("codesnap"); if !config_home_path.exists() { std::fs::create_dir_all(&config_home_path).unwrap(); } - Ok(config_home_path) + config_home_path }