dim/dim_plugins/volume/src/lib.rs

69 lines
1.7 KiB
Rust
Raw Normal View History

2024-06-23 11:16:03 +00:00
use std::{fmt::Write, path::PathBuf, process::Stdio};
use dim_sdk::{plugin_info, Context, DimPlugin};
use std::fs::read_to_string;
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,
}
impl Plug {
pub fn new() -> Self {
Self {
cfg: config::Config::default()
}
}
}
impl DimPlugin for Plug {
fn init(&mut self, ctx: Context) {
match config::Config::parse(&ctx.config_dir.join("config.toml")) {
Ok(c) => self.cfg = c,
Err(e) => {
eprintln!("ERROR: Failed to open config file: {e}");
// TODO: Add function to disable the plugin
return;
}
}
}
fn poll(&mut self, f: &mut dim_sdk::CBuffer) -> dim_sdk::Result<()> {
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!(f, "Vol: {volume}%")?;
}
Ok(())
}
fn pre_reload(&mut self) {
}
fn post_reload(&mut self) {
}
fn free(&mut self) {
}
}