Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Remove reload_apps-related functionality and add a config.wallpaper section #113

Merged
merged 3 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{util::config::Config, wallpaper};
use crate::wallpaper;
use color_eyre::{eyre::Result, Report};
use log::LevelFilter;
use matugen::color::color::Source;
Expand Down Expand Up @@ -56,24 +56,17 @@ pub fn setup_logging(args: &Cli) -> Result<(), Report> {
Ok(())
}

pub fn set_wallpaper(source: &Source, config: &Config) -> Result<(), Report> {
pub fn set_wallpaper(source: &Source, wallpaper_cfg: wallpaper::Wallpaper) -> Result<(), Report> {
let path = match &source {
Source::Image { path } => path,
Source::Color { .. } => return Ok(()),
#[cfg(feature = "web-image")]
Source::WebImage { .. } => return Ok(()),
};
#[cfg(any(target_os = "linux", target_os = "netbsd"))]
let wallpaper_tool = match &config.wallpaper_tool {
Some(wallpaper_tool) => wallpaper_tool,
None => {
if cfg!(windows) {
return Ok(());
}
return Ok(warn!(
"<d>Wallpaper tool not set, not setting wallpaper...</>"
));
}
if !wallpaper_cfg.set {
return Ok(warn!(
"<d>Wallpaper setting disabled, not setting wallpaper...</>"
));
};
#[cfg(target_os = "windows")]
wallpaper::windows::set(path)?;
Expand All @@ -82,9 +75,7 @@ pub fn set_wallpaper(source: &Source, config: &Config) -> Result<(), Report> {
#[cfg(any(target_os = "linux", target_os = "netbsd"))]
wallpaper::unix::set(
path,
wallpaper_tool,
&config.feh_options,
&config.swww_options,
wallpaper_cfg
)?;
Ok(())
}
10 changes: 2 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ extern crate pretty_env_logger;
extern crate paris_log;

mod helpers;
mod reload;
pub mod template;
mod util;
mod wallpaper;
Expand Down Expand Up @@ -77,13 +76,8 @@ fn main() -> Result<(), Report> {
config_path,
)?;

if config.config.reload_apps == Some(true) {
#[cfg(any(target_os = "linux", target_os = "netbsd"))]
reload::unix::reload(&args, &config)?;
}

if config.config.set_wallpaper == Some(true) {
set_wallpaper(&args.source, &config.config)?;
if let Some(wallpaper_cfg) = config.config.wallpaper {
set_wallpaper(&args.source, wallpaper_cfg)?;
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/reload/mod.rs

This file was deleted.

73 changes: 0 additions & 73 deletions src/reload/unix.rs

This file was deleted.

22 changes: 17 additions & 5 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ pub struct Template {
pub compare_to: Option<String>,
pub pre_hook: Option<String>,
pub post_hook: Option<String>,
pub expr_prefix: Option<String>,
pub expr_postfix: Option<String>,
pub block_prefix: Option<String>,
pub block_postfix: Option<String>,
}

#[allow(clippy::manual_strip)]
Expand Down Expand Up @@ -78,11 +82,6 @@ impl Template {
) -> Result<(), Report> {
info!("Loaded <b><cyan>{}</> templates.", &templates.len());

let syntax = Syntax::builder().expr("{{", "}}").block("<*", "*>").build();
let mut engine = Engine::with_syntax(syntax);

add_engine_filters(&mut engine);

let image = match &source {
Source::Image { path } => Some(path),
#[cfg(feature = "web-image")]
Expand All @@ -99,6 +98,19 @@ impl Template {
)?;

for (i, (name, template)) in templates.iter().enumerate() {
let expr_prefix = template.expr_prefix.as_deref().unwrap_or("{{");
let expr_postfix = template.expr_postfix.as_deref().unwrap_or("}}");
let block_prefix = template.block_prefix.as_deref().unwrap_or("<*");
let block_postfix = template.block_postfix.as_deref().unwrap_or("*>");

let syntax = Syntax::builder()
.expr(expr_prefix, expr_postfix)
.block(block_prefix, block_postfix)
.build();
let mut engine = Engine::with_syntax(syntax);

add_engine_filters(&mut engine);

let (input_path_absolute, output_path_absolute) =
get_absolute_paths(&config_path, template)?;

Expand Down
25 changes: 2 additions & 23 deletions src/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,18 @@ use serde::{Deserialize, Serialize};

use super::arguments::Cli;
use crate::Template;

#[derive(Serialize, Deserialize, Debug)]
pub enum WallpaperTool {
Swaybg,
Swww,
Nitrogen,
Feh,
}
use crate::wallpaper::Wallpaper;

#[derive(Serialize, Deserialize, Debug)]
pub struct Config {
pub reload_apps: Option<bool>,
pub version_check: Option<bool>,
pub reload_apps_list: Option<Apps>,
pub set_wallpaper: Option<bool>,
pub wallpaper_tool: Option<WallpaperTool>,
pub wallpaper: Option<Wallpaper>,
// TODO: Add a `Command` struct
pub swww_options: Option<Vec<String>>,
pub feh_options: Option<Vec<String>>,
pub prefix: Option<String>,
pub custom_keywords: Option<HashMap<String, String>>,
pub custom_colors: Option<HashMap<String, matugen::color::color::OwnCustomColor>>,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct Apps {
pub kitty: Option<bool>,
pub waybar: Option<bool>,
pub gtk_theme: Option<bool>,
pub dunst: Option<bool>,
pub mako: Option<bool>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ConfigFile {
pub config: Config,
Expand Down
12 changes: 12 additions & 0 deletions src/wallpaper/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Wallpaper {
pub set: bool,
/// Useful for, for example, killing the wallpaper daemon
pub pre_hook: Option<String>,
pub command: String,
/// The last argument will be the image path
pub arguments: Option<Vec<String>>,
}

#[cfg(any(target_os = "linux", target_os = "netbsd"))]
pub mod unix;

Expand Down
110 changes: 25 additions & 85 deletions src/wallpaper/unix.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,35 @@
use color_eyre::Report;
use std::process::Command;
use std::process::Stdio;

use crate::reload::unix::reload_app;
use crate::util::config::WallpaperTool;
use std::process::{Command, Stdio};
use crate::wallpaper::Wallpaper;
use execute::Execute;

#[cfg(any(target_os = "linux", target_os = "netbsd"))]
pub fn set(
path: &String,
wallpaper_tool: &WallpaperTool,
feh_options: &Option<Vec<String>>,
swww_options: &Option<Vec<String>>,
Wallpaper { pre_hook, command, arguments, .. }: Wallpaper,
) -> Result<(), Report> {
info!("Setting wallpaper...");

match &wallpaper_tool {
WallpaperTool::Swaybg => set_wallaper_swaybg(path),
WallpaperTool::Swww => set_wallaper_swww(path, swww_options),
WallpaperTool::Nitrogen => set_wallaper_nitrogen(path),
WallpaperTool::Feh => set_wallaper_feh(path, feh_options),
if let Some(hook) = pre_hook {
spawn_hook(hook)?
}
}

#[cfg(any(target_os = "linux", target_os = "netbsd"))]
fn set_wallaper_swaybg(path: &String) -> Result<(), Report> {
reload_app("swaybg", "SIGUSR1")?;
let mut binding = Command::new(&command);
let cmd = binding
.stdout(Stdio::null())
.stderr(Stdio::null());

let mut binding = Command::new("swaybg");
let cmd = binding.stdout(Stdio::null()).stderr(Stdio::null());
cmd.arg("-i");
if let Some(args) = arguments {
cmd.args(args);
}
cmd.arg(path);


match cmd.spawn() {
Ok(_) => info!("Successfully set the wallpaper with <blue>swaybg</>"),
Ok(_) => info!("Successfully set the wallpaper with <blue>{command}</>"),
Err(e) => {
if let std::io::ErrorKind::NotFound = e.kind() {
error!("Failed to set wallpaper, the program <red>swaybg</> was not found in PATH!")
error!("Failed to set wallpaper, the program <red>{command}</> was not found in PATH!")
} else {
error!("Some error(s) occured while setting wallpaper!");
}
Expand All @@ -44,77 +38,23 @@ fn set_wallaper_swaybg(path: &String) -> Result<(), Report> {
Ok(())
}

#[cfg(any(target_os = "linux", target_os = "netbsd"))]
fn set_wallaper_swww(path: &String, swww_options: &Option<Vec<String>>) -> Result<(), Report> {
let mut binding = Command::new("swww");
let cmd = binding.stdout(Stdio::null()).stderr(Stdio::null());
cmd.arg("img");
cmd.arg(path);

if let Some(options) = &swww_options {
if !options[0].is_empty() {
cmd.args(options);
}
}

match cmd.spawn() {
Ok(_) => info!("Successfully set the wallpaper with <blue>swww</>"),
Err(e) => {
if let std::io::ErrorKind::NotFound = e.kind() {
error!("Failed to set wallpaper, the program <red>swww</> was not found in PATH!")
} else {
error!("Some error(s) occured while setting wallpaper!");
}
}
};
Ok(())
}

#[cfg(any(target_os = "linux", target_os = "netbsd"))]
fn set_wallaper_nitrogen(path: &String) -> Result<(), Report> {
let mut binding = Command::new("nitrogen");
let cmd = binding.stdout(Stdio::null()).stderr(Stdio::null());
cmd.arg(path);
fn spawn_hook(hook: String) -> Result<(), Report> {
let mut command = execute::shell(&hook);

match cmd.spawn() {
Ok(_) => info!("Successfully set the wallpaper with <blue>nitrogen</>"),
Err(e) => {
if let std::io::ErrorKind::NotFound = e.kind() {
error!(
"Failed to set wallpaper, the program <red>nitrogen</> was not found in PATH!"
)
} else {
error!("Some error(s) occured while setting wallpaper!");
}
}
};
Ok(())
}
command.stdout(Stdio::inherit());

#[cfg(any(target_os = "linux", target_os = "netbsd"))]
fn set_wallaper_feh(path: &String, feh_options: &Option<Vec<String>>) -> Result<(), Report> {
let mut binding = Command::new("feh");
let cmd = binding.stdout(Stdio::null()).stderr(Stdio::null());
let output = command.execute_output()?;

if let Some(options) = &feh_options {
if !options[0].is_empty() {
cmd.args(options);
} else {
cmd.arg("--bg-scale");
if let Some(exit_code) = output.status.code() {
if exit_code != 0 {
error!("Failed executing command: {:?}", hook)
}
} else {
eprintln!("Interrupted!");
}

cmd.arg(path);

match cmd.spawn() {
Ok(_) => info!("Successfully set the wallpaper with <blue>feh</>"),
Err(e) => {
if let std::io::ErrorKind::NotFound = e.kind() {
error!("Failed to set wallpaper, the program <red>feh</> was not found in PATH!")
} else {
error!("Some error(s) occured while setting wallpaper!");
}
}
};
Ok(())
}