diff --git a/extra/config.toml b/extra/config.toml index 0558866..f2980bc 100644 --- a/extra/config.toml +++ b/extra/config.toml @@ -48,6 +48,10 @@ main_log_path = "/var/log/lemurs.log" # Window Manager for Xorg, the Compositor for Wayland and the Shell for TTY. client_log_path = "/var/log/lemurs.client.log" +# At which point to point the cache. If you want to disable the cache globally +# you can use `/dev/null`. +cache_path = "/var/cache/lemurs"; + # Disable all logging. This is overwritten by the `--no-log` flag. do_log = true diff --git a/src/config.rs b/src/config.rs index fa04391..27a11eb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -213,6 +213,7 @@ toml_config_struct! { Config, PartialConfig, RoughConfig, main_log_path => String, client_log_path => String, + cache_path => String, do_log => bool, diff --git a/src/info_caching.rs b/src/info_caching.rs index 774c5cb..06e9225 100644 --- a/src/info_caching.rs +++ b/src/info_caching.rs @@ -1,7 +1,8 @@ use log::{info, warn}; use std::fs::{read_to_string, write}; -pub const CACHE_PATH: &str = "/var/cache/lemurs"; +use crate::config::Config; + const USERNAME_LENGTH_LIMIT: usize = 32; // Saved in the /var/cache/lemurs file as @@ -48,13 +49,12 @@ impl CachedInfo { } } -pub fn get_cached_information() -> CachedInfo { - info!( - "Attempting to get a cached information from '{}'", - CACHE_PATH - ); +pub fn get_cached_information(config: &Config) -> CachedInfo { + let cache_path = &config.cache_path; - match read_to_string(CACHE_PATH) { + info!("Attempting to get a cached information from '{cache_path}'",); + + match read_to_string(cache_path) { Ok(cached) => { // Remove any line feeds let cached = cached.trim().to_string(); @@ -102,8 +102,10 @@ pub fn get_cached_information() -> CachedInfo { } } -pub fn set_cache(environment: Option<&str>, username: Option<&str>) { - info!("Attempting to set cache"); +pub fn set_cache(environment: Option<&str>, username: Option<&str>, config: &Config) { + let cache_path = &config.cache_path; + + info!("Attempting to set cache: {cache_path}"); let username = if let Some(username) = username { // Username length check @@ -129,9 +131,9 @@ pub fn set_cache(environment: Option<&str>, username: Option<&str>) { username.unwrap_or_default() ); - match write(CACHE_PATH, cache_file_content) { + match write(cache_path, cache_file_content) { Err(err) => { - warn!("Failed to set username to cache file. Reason: '{}'", err); + warn!("Failed to set username to cache file. Reason: '{err}'"); } _ => { info!("Successfully set username in cache file"); diff --git a/src/main.rs b/src/main.rs index ede46b7..4648d8c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -151,14 +151,14 @@ fn main() -> Result<(), Box> { } } Commands::Cache => { - let cached_info = info_caching::get_cached_information(); + let cached_info = info_caching::get_cached_information(&config); let environment = cached_info.environment().unwrap_or("No cached value"); let username = cached_info.username().unwrap_or("No cached value"); println!( "Information currently cached within '{}'\n", - info_caching::CACHE_PATH + config.cache_path ); println!("environment: '{environment}'"); diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 793e364..5b057d9 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -257,14 +257,14 @@ impl LoginForm { .then_some(self.widgets.get_username()); info!("Setting cached information"); - set_cache(selected_env.as_deref(), username.as_deref()); + set_cache(selected_env.as_deref(), username.as_deref(), &self.config); } fn load_cache(&self) { let env_remember = self.config.environment_switcher.remember; let username_remember = self.config.username_field.remember; - let cached = get_cached_information(); + let cached = get_cached_information(&self.config); if username_remember { if let Some(username) = cached.username() {