use crate::error::{ Error, Result }; use crate::facility::{ FacilityMap, FacilityType }; use crate::facility::envsys::EnvSys; use crate::module::{ CreatableModule, Module }; use std::time::Duration; pub struct BatteryModule { is_visible: bool, } impl CreatableModule for BatteryModule { const REQUIRED_FACILITIES: FacilityType = FacilityType::ENVSYS; fn new(facilities: &mut FacilityMap) -> Result { let envsys: &mut EnvSys = facilities.get().ok_or(Error::custom( "Battery module can't access envsys facility."))?; let mut is_visible = false; for name in envsys.device_iter() { if Self::is_battery_device(name) { is_visible = true; } } Ok(BatteryModule { is_visible }) } } impl Module for BatteryModule { fn format(&mut self, facilities: &mut FacilityMap) -> Result { let envsys: &mut EnvSys = facilities.get().ok_or(Error::custom( "Battery module can't access envsys facility."))?; /* When there's more than one battery, we want to present them in an * order which is both stable and predictable. Their device names are also * user-visible via envstat(8), so lexicographic ordering will be fine for * both needs. Anyway, to make that happen, we collect and sort the ones * we're interested in in advance. */ let mut devices = Vec::new(); for name in envsys.device_iter() { if Self::is_battery_device(name) { devices.push(name); } } devices.sort(); self.is_visible = !devices.is_empty(); let mut text = String::new(); for device in &devices { if !text.is_empty() { text.push_str(" "); } text.push_str(device); } Ok(text) } fn sleep_duration(&self) -> Option { None } fn is_visible(&self) -> bool { self.is_visible } } impl BatteryModule { fn is_battery_device(name: &str) -> bool { name.starts_with("acpibat") } }