use std::{fmt::Write, process::Stdio}; use anyhow::bail; use dim_sdk::{plugin_info, Context, DimPlugin, Message, Result}; mod config; plugin_info!( Plug, // Your main global structs name that implements `DimPlugin` "volume", // Plugin name "1.0.0", // Plugin Version (leave empty for none) "GPLv3" // Plugin license (leave empty for none) ); #[derive(Debug, Clone)] struct Plug { cfg: config::Config, ctx: Context } impl DimPlugin for Plug { fn init(ctx: Context) -> Result { let cfg; match config::Config::parse(&ctx.config_dir.join("config.toml").clone()) { Ok(c) => cfg = c, Err(e) => { log::error!("Failed to open config file: {e}"); // TODO: Add function to disable the plugin bail!("") } } Ok(Self { ctx, cfg }) } fn on_message(&mut self, msg: Message) -> Result<()> { self.ctx.buf.reset(); match msg.name.as_str() { "poll" => { let mut proc = { let mut p = std::process::Command::new("sh"); p.arg("-c"); p.arg(&self.cfg.commands.get_volume); p.stdout(Stdio::piped()); p }; let output = String::from_utf8(proc.output()?.stdout)?; let re = regex::Regex::new(&self.cfg.commands.get_volume_regex)?; if let Some(caps) = re.captures(&output) { let volume = &caps["vol"]; write!(self.ctx, "Vol: {volume}%")?; } } _ => () } Ok(()) } fn pre_reload(&mut self) -> Result<()> { Ok(()) } fn post_reload(&mut self) -> Result<()> { Ok(()) } fn free(&mut self) -> Result<()> { Ok(()) } }