Skip to content

Commit cd2a959

Browse files
committed
✨ feat: execute script on change
1 parent 0c730e9 commit cd2a959

6 files changed

Lines changed: 52 additions & 7 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Expression is a highly customizable, time-aware wallpaper engine for Linux. It a
1717
- [x] Distribute wallpapers from a group evenly across the hour
1818
- [x] Override with special wallpaper based on a timetable (e.g., lunch, sleep)
1919
- [x] Per group config overrides
20+
- [x] Execute custom scripts on wallpaper change
2021

2122
## 🚀 Installation
2223

@@ -106,6 +107,10 @@ backend = "swww"
106107
enable_special = true
107108
# Way to select wallpaper from a group: random, spread
108109
group_selection_strategy = "random"
110+
# Command to execute on wallpaper change
111+
# Examples:
112+
# execute_on_change = "~/.scripts/custom_script.sh"
113+
execute_on_change = "notify-send 'Wallpaper Changed'"
109114

110115
[directories]
111116
# Default wallpaper directory

config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
backend = "swww"
33
enable_special = true
44
group_selection_strategy = "random" # random, spread
5+
# execute_on_change = "/path/to/script_or_executable"
56

67
[directories]
78
wallpaper = "~/Pictures/"

src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub struct GeneralConfig {
2323
pub backend: String,
2424
pub enable_special: bool,
2525
pub group_selection_strategy: GroupSelectionStrategy,
26+
pub execute_on_change: Option<String>,
2627
}
2728

2829
#[derive(Debug, Deserialize)]

src/main.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use colored::Colorize;
33
use ctrlc;
44
use expression::backends::get_backend;
55
use expression::config::{get_group_config, Config, GroupSelectionStrategy};
6-
use expression::utils::{calc, logger, wallpaper};
6+
use expression::utils::{calc, cmd, logger, wallpaper};
77
use log2::{debug, error, info, warn};
88
use std::process;
99
use std::sync::atomic::{AtomicBool, Ordering};
@@ -39,6 +39,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
3939
let config_special_entries = config.special_entries;
4040
let config_special_enabled = config.general.enable_special;
4141
let mut config_group_selection = config.general.group_selection_strategy;
42+
let exec_cmd = config.general.execute_on_change;
4243

4344
let mut selected_item = Vec::new();
4445

@@ -232,13 +233,18 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
232233
);
233234
}
234235

235-
// REFRESH
236+
// REFRESH LOOP
236237
match refresh_strategy {
237-
RefreshStrategy::Sleep => {
238-
calc::sleep(wait_seconds);
239-
}
240-
RefreshStrategy::Log2Sleep => {
241-
calc::refresh_tlog2(interval, now, wait_seconds);
238+
RefreshStrategy::Sleep => calc::sleep(wait_seconds),
239+
RefreshStrategy::Log2Sleep => calc::refresh_tlog2(interval, now, wait_seconds),
240+
}
241+
242+
// EXECUTE SCRIPT
243+
if exec_cmd.is_some() {
244+
let result = cmd::execute(exec_cmd.as_ref().unwrap());
245+
match result {
246+
Ok(_) => {}
247+
Err(err) => error!("Error executing command: {}", err),
242248
}
243249
}
244250
}

src/utils/cmd.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use std::io;
2+
use std::process::{Command, Output};
3+
4+
/// Execute a shell command and return the result
5+
/// # Arguments
6+
/// * `cmd` - The command string to execute
7+
/// # Returns
8+
/// * `Result<Output, io::Error>` - The command output or an error
9+
/// # Example
10+
/// ```
11+
/// use expression::utils::command::execute_shell_command;
12+
///
13+
/// let result = execute("ls -la");
14+
/// match result {
15+
/// Ok(output) => {
16+
/// println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
17+
/// println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
18+
/// println!("status: {}", output.status);
19+
/// }
20+
/// Err(e) => eprintln!("Error executing command: {}", e),
21+
/// }
22+
/// ```
23+
pub fn execute(cmd: &str) -> Result<Output, io::Error> {
24+
let (shell, flag) = if cfg!(target_os = "windows") {
25+
("cmd", "/C")
26+
} else {
27+
("bash", "-c")
28+
};
29+
30+
Command::new(shell).arg(flag).arg(cmd).output()
31+
}

src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod wallpaper;
22
pub mod calc;
33
pub mod logger;
4+
pub mod cmd;

0 commit comments

Comments
 (0)