dim/dim_plugins/volume/src/lib.rs

72 lines
2.0 KiB
Rust
Raw Normal View History

use std::{fmt::Write, process::Stdio};
use anyhow::bail;
use dim_sdk::{plugin_info, Context, DimPlugin, Message, Result};
2024-06-23 11:16:03 +00:00
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)
);
2024-06-23 11:16:03 +00:00
#[derive(Debug, Clone)]
struct Plug {
cfg: config::Config,
ctx: Context
2024-06-23 11:16:03 +00:00
}
impl DimPlugin for Plug {
fn init(ctx: Context) -> Result<Self> {
let cfg;
match config::Config::parse(&ctx.config_dir.join("config.toml").clone()) {
Ok(c) => cfg = c,
2024-06-23 11:16:03 +00:00
Err(e) => {
log::error!("Failed to open config file: {e}");
2024-06-23 11:16:03 +00:00
// TODO: Add function to disable the plugin
bail!("")
2024-06-23 11:16:03 +00:00
}
}
Ok(Self { ctx, cfg })
2024-06-23 11:16:03 +00:00
}
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
};
2024-06-23 11:16:03 +00:00
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}%")?;
}
}
_ => ()
2024-06-23 11:16:03 +00:00
}
Ok(())
}
fn pre_reload(&mut self) -> Result<()> {
Ok(())
2024-06-23 11:16:03 +00:00
}
fn post_reload(&mut self) -> Result<()> {
Ok(())
2024-06-23 11:16:03 +00:00
}
fn free(&mut self) -> Result<()> {
Ok(())
2024-06-23 11:16:03 +00:00
}
}