Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455use std::{cell::RefCell, path::PathBuf, rc::Rc};
use smol::{LocalExecutor, channel::Sender};
use crate::bar::{component::Value, module::Data};
pub struct Battery;
impl Battery { const PREFIX: &str = "/sys/class/power_supply";
pub fn run( target: &str, executor: &LocalExecutor<'static>, data: Rc<RefCell<Data>>, _tx: Sender<()>, ) { let key_prefix = String::from("battery.") + target + "."; let target = [Self::PREFIX, target, "uevent"].iter().collect::<PathBuf>(); let future = async move { loop { let events = std::fs::read_to_string(&target)?; let mut data = data.borrow_mut(); data.update_tick(); for line in events.lines() { let (key, value) = line.split_once('=').ok_or("Invalid format")?; let key = key_prefix.clone() + &key .strip_prefix("POWER_SUPPLY_") .unwrap_or(key) .to_lowercase();
let value_fn = || { value .parse::<f32>() .map(Value::Num) .unwrap_or(Value::String(value.to_string())) };
data.update_value(&key, value_fn); } drop(data);
// tx.try_send(()).unwrap(); smol::Timer::after(std::time::Duration::from_secs(1)).await; }
#[expect(unreachable_code, reason = "type inference")] Ok::<(), Box<dyn std::error::Error>>(()) };
executor.spawn(future).detach(); }}