use std::path::Path;
use std::process::{Command, Stdio};
use crate::utils::path;
use crate::utils::system::extract_stdout;
fn load_wallpaper(monitor: &str, wallpaper: &Path) -> Result<(), String> {
let mut hyprctl = Command::new("hyprctl");
let capture_mode = Stdio::piped;
let formatted_input =
format!("{},{}", monitor, wallpaper.to_str().unwrap());
let result = hyprctl
.args(["hyprpaper", "reload", formatted_input.as_str()])
.stdout(capture_mode())
.stdout(capture_mode())
.output();
match result {
Ok(output) => {
let stdout = extract_stdout(&output)?;
let cleaned_stdout = stdout.trim();
match cleaned_stdout {
"ok" => Ok(()),
_ => Err(cleaned_stdout.to_string()),
}
}
Err(error) => Err(error.to_string()),
}
}
pub fn apply_wallpaper(
monitor_name: &str,
wallpaper_path: &str,
) -> Result<(), String> {
let monitor = match monitor_name {
"*" => "",
_ => monitor_name,
};
let expanded_wallpaper_path = path::expand_path(wallpaper_path)?;
let wallpaper_search = path::deduce_path(&expanded_wallpaper_path);
match wallpaper_search {
Some(wallpaper) => load_wallpaper(monitor, wallpaper),
None => Err(format!("file {} does not exist", wallpaper_path)),
}
}