diff --git a/.gitignore b/.gitignore index 9c954c4..12ef810 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ .direnv/ waycast_cache waycast.toml +# Using locally to test .config, .cache, and data dir +xdg diff --git a/Cargo.lock b/Cargo.lock index 7e4d97b..98d1b74 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1460,6 +1460,7 @@ dependencies = [ "redb", "serde", "tempfile", + "waycast-config", ] [[package]] diff --git a/waycast-config/src/lib.rs b/waycast-config/src/lib.rs index dbd01db..e103fca 100644 --- a/waycast-config/src/lib.rs +++ b/waycast-config/src/lib.rs @@ -1,20 +1,33 @@ -use config::Config; +use config::{Config, Environment}; use directories::ProjectDirs; use serde::de::DeserializeOwned; use serde::{Deserialize, Serialize}; use std::path::Path; use std::sync::OnceLock; -use std::{fs::File, path::PathBuf}; +use std::{env, fs::File, path::PathBuf}; #[derive(Debug, Deserialize, Serialize)] pub struct WaycastConfig {} static CONFIG_SINGLETON: OnceLock = OnceLock::new(); +fn is_development_mode() -> bool { + // Check if we're in development by looking for Cargo.toml in current directory + env::current_dir() + .map(|dir| dir.join("Cargo.toml").exists()) + .unwrap_or(false) +} + pub fn project_dirs() -> Option { ProjectDirs::from("dev.thegrind", "The Grind", "waycast") } pub fn config_dir() -> Option { + if is_development_mode() { + return env::current_dir() + .map(|d| d.join("xdg").join(".config").join("waycast")) + .ok(); + } + if let Some(dirs) = project_dirs() { return Some(dirs.config_dir().to_path_buf()); } @@ -23,6 +36,12 @@ pub fn config_dir() -> Option { } pub fn cache_dir() -> Option { + if is_development_mode() { + return env::current_dir() + .ok() + .map(|d| d.join("xdg").join(".cache")); + } + if let Some(dirs) = project_dirs() { return Some(dirs.cache_dir().to_path_buf()); } @@ -31,6 +50,12 @@ pub fn cache_dir() -> Option { } pub fn data_dir() -> Option { + if is_development_mode() { + return env::current_dir() + .ok() + .map(|d| d.join("xdg").join(".local/share")); + } + if let Some(dirs) = project_dirs() { return Some(dirs.data_dir().to_path_buf()); } @@ -46,6 +71,14 @@ pub fn config_path>(file: P) -> Option { None } +pub fn cache_path>(file: P) -> Option { + if let Some(p) = cache_dir() { + return Some(p.join(file)); + } + + None +} + pub fn config_file() -> &'static Config { CONFIG_SINGLETON.get_or_init(|| init()) } @@ -57,14 +90,18 @@ pub fn get(key: &str) -> Result { fn init() -> Config { let mut cfg = Config::builder(); - cfg = cfg.add_source(config::File::with_name("waycast.toml").required(false)); // Local file for dev - - // Production version in ~/.config + println!("Config: {}", config_path("waycast.toml").unwrap().display()); if let Some(path) = config_path("waycast.toml") { cfg = cfg.add_source(config::File::with_name(&path.to_string_lossy()).required(false)); } - cfg = cfg.add_source(config::Environment::with_prefix("WAYCAST")); + cfg = cfg.add_source(Environment::with_prefix("WAYCAST")); + + println!("Using directories"); + println!("---"); + println!("{}", config_dir().unwrap().display()); + println!("{}", cache_dir().unwrap().display()); + println!("{}", data_dir().unwrap().display()); cfg.build().unwrap() } diff --git a/waycast-core/Cargo.toml b/waycast-core/Cargo.toml index a2f1a35..945c30e 100644 --- a/waycast-core/Cargo.toml +++ b/waycast-core/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" bincode = "1.3" redb = "3.0.1" serde = { version = "1.0.219", features = ["derive"] } +waycast-config = { path = "../waycast-config" } [dev-dependencies] tempfile = "3.21.0" diff --git a/waycast-core/src/cache/mod.rs b/waycast-core/src/cache/mod.rs index 81b77f0..d94e6be 100644 --- a/waycast-core/src/cache/mod.rs +++ b/waycast-core/src/cache/mod.rs @@ -33,7 +33,19 @@ pub struct Cache { } pub fn get() -> &'static Cache { - CACHE_SINGLETON.get_or_init(|| new("waycast_cache").expect("Failed to initialize cache :(")) + CACHE_SINGLETON.get_or_init(|| { + let cache_path = waycast_config::cache_path("waycast_cache.db") + .unwrap_or_else(|| std::env::current_dir().unwrap().join("waycast_cache.db")); + + // Ensure cache directory exists + if let Some(parent) = cache_path.parent() { + if let Err(e) = std::fs::create_dir_all(parent) { + eprintln!("Warning: Failed to create cache directory {}: {}", parent.display(), e); + } + } + + new(cache_path).expect("Failed to initialize cache :(") + }) } // Get an existing cache at the given path or diff --git a/waycast-plugins/src/file_search.rs b/waycast-plugins/src/file_search.rs index 7b1e446..21f15ee 100644 --- a/waycast-plugins/src/file_search.rs +++ b/waycast-plugins/src/file_search.rs @@ -178,6 +178,7 @@ impl FileSearchPlugin { let mut local_files = Vec::new(); for path in &self.search_paths { + println!("Scanning: {}", path.display()); let walker = WalkDir::new(path).into_iter(); for entry in walker .filter_entry(|e| {