| 1 | use super::Block; |
| 2 | use anyhow::Result; |
| 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> { |
| 26 | let output = Command::new("sh").arg("-c").arg(&self.command).output()?; |
| 27 | |
| 28 | let result = String::from_utf8_lossy(&output.stdout).trim().to_string(); |
| 29 | Ok(self.format.replace("{}", &result)) |
| 30 | } |
| 31 | |
| 32 | fn interval(&self) -> Duration { |
| 33 | self.interval |
| 34 | } |
| 35 | |
| 36 | fn color(&self) -> u32 { |
| 37 | self.color |
| 38 | } |
| 39 | } |