oxwm

https://git.tonybtw.com/oxwm.git git://git.tonybtw.com/oxwm.git
1,271 bytes raw
1
use super::Block;
2
use crate::errors::BlockError;
3
use std::process::Command;
4
use std::time::Duration;
5
6
pub struct ShellBlock {
7
    format: String,
8
    command: String,
9
    interval: Duration,
10
    color: u32,
11
}
12
13
impl ShellBlock {
14
    pub fn new(format: &str, command: &str, interval_secs: u64, color: u32) -> Self {
15
        Self {
16
            format: format.to_string(),
17
            command: command.to_string(),
18
            interval: Duration::from_secs(interval_secs),
19
            color,
20
        }
21
    }
22
}
23
24
impl Block for ShellBlock {
25
    fn content(&mut self) -> Result<String, BlockError> {
26
        let output = Command::new("sh")
27
            .arg("-c")
28
            .arg(&self.command)
29
            .output()
30
            .map_err(|e| BlockError::CommandFailed(format!("Failed to execute command: {}", e)))?;
31
32
        if !output.status.success() {
33
            return Err(BlockError::CommandFailed(format!(
34
                "Command exited with status: {}",
35
                output.status
36
            )));
37
        }
38
39
        let result = String::from_utf8_lossy(&output.stdout).trim().to_string();
40
        Ok(self.format.replace("{}", &result))
41
    }
42
43
    fn interval(&self) -> Duration {
44
        self.interval
45
    }
46
47
    fn color(&self) -> u32 {
48
        self.color
49
    }
50
}