From d6b3b689f40c7ce0e502af811a62b1ca71f11174 Mon Sep 17 00:00:00 2001 From: Yago Iglesias Date: Wed, 30 Aug 2023 19:04:36 +0200 Subject: [PATCH] feat: wallpaper changing + current wallpaper --- src/lib.rs | 1 + src/wallpaper.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/wallpaper.rs diff --git a/src/lib.rs b/src/lib.rs index d01d024..85a7ab4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,3 @@ pub mod cli; pub mod configuration; +pub mod wallpaper; diff --git a/src/wallpaper.rs b/src/wallpaper.rs new file mode 100644 index 0000000..a7b2dd8 --- /dev/null +++ b/src/wallpaper.rs @@ -0,0 +1,52 @@ +use std::process::Command; + +use crate::configuration::Settings; + +/// Gets the current wallpaper. It assumes that the user is only using feh to set the wallpaper. +/// TODO: allow user to choose other wallpaper getter +/// +/// +/// The .fehbg file is a shell script that is run when the user logs in. It is used to set the +/// wallpaper. It uses the following format: +/// ``` +/// #!/bin/sh +/// feh --no-fehbg --bg-fill /path/to/wallpaper +/// ``` +/// This function parses the .fehbg file and returns the path to the current wallpaper. +/// +/// # Panics +/// +/// Panics if the file $HOME/.fehbg does not exist. +/// +pub fn get_current_wallpaper() -> String { + let feh_raw = std::fs::read_to_string("~/.fehbg").expect("failed to open .fehbg file"); + + feh_raw + .lines() + .nth(1) + .expect("failed to parse .fehbg file, it should contain at least 2 lines") + .split(' ') + .last() + .expect( + "failed to parse .fehbg file, the last line should contain the path to the wallpaper", + ) + .to_string() +} + +pub fn update_wallpaper(settings: &Settings, path: &str) { + // TODO: allow user to choose other wallpaper setter + Command::new("feh") + .arg("--bg-fill") + .arg(path) + .output() + .expect("failed to call feh"); + + // Updates the betterlockscreen wallpaper + if settings.betterlockscreen { + Command::new("betterlockscreen") + .arg("-u") + .arg(path) + .output() + .expect("failed to call betterlockscreen"); + } +}