From 27e44c34c33ed1de7a2c3e25099bb0c25f92a9bf Mon Sep 17 00:00:00 2001 From: Yago Iglesias Date: Sat, 23 Dec 2023 13:36:28 +0100 Subject: [PATCH] feat: Save current background in a file This allows to not depend on feh for getting the current background. We still need it for setting the background, but we can use other programs for that. --- src/wallpaper.rs | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/wallpaper.rs b/src/wallpaper.rs index 4b0d592..4951a98 100644 --- a/src/wallpaper.rs +++ b/src/wallpaper.rs @@ -26,27 +26,11 @@ use crate::{ /// Panics if the file $HOME/.fehbg does not exist. /// pub fn get_current_wallpaper() -> Option { - let feh_raw = read_to_string(format!( - "{}/.fehbg", - home::home_dir() - .expect("Unable to get home path") - .to_str() - .unwrap() + let wallpaper_path = read_to_string(format!( + "{}/.local/share/wallshift/.current_wallpaper", + home::home_dir()?.to_str()? )) - .expect("failed to open .fehbg file"); - - let wallpaper_path = feh_raw - .lines() - // We take the second line because the first line is the shebang - .nth(1) - .expect("failed to parse .fehbg file, it should contain at least 2 lines") - // We take the last word - .split(' ') - .nth(3) - .expect("failed to parse .fehbg file, it should contain at least 4 words") - // Remove the single quotes - .trim_matches('\'') - .to_string(); + .ok()?; File::try_from(wallpaper_path).ok() } @@ -222,4 +206,21 @@ pub fn update_wallpaper(settings: &Settings, path: &str) { .output() .expect("failed to call betterlockscreen"); } + + // Saves the current wallpaper + + std::fs::create_dir_all(format!( + "{}/.local/share/wallshift", + home::home_dir().unwrap().to_str().unwrap() + )) + .expect("failed to create directory"); + + std::fs::write( + format!( + "{}/.local/share/wallshift/.current_wallpaper", + home::home_dir().unwrap().to_str().unwrap() + ), + path, + ) + .expect("failed to save current wallpaper"); }