diff --git a/src/blocks/battery.rs b/src/blocks/battery.rs new file mode 100644 index 0000000..f9d1840 --- /dev/null +++ b/src/blocks/battery.rs @@ -0,0 +1,57 @@ +use systemstat::Platform; + +use crate::{ + blocks::{BlockInfo, Color}, + Block, +}; + +use super::{BlockEvent, BlockUpdate}; + +pub struct BatteryBlock { + pub id: usize, +} + +impl Block for BatteryBlock { + fn id(&self) -> usize { + self.id + } + + fn name(&self) -> &str { + "battery" + } + + fn run(&mut self, _event_r: flume::Receiver, update_s: flume::Sender) { + let sys = systemstat::System::new(); + if let Ok(battery) = sys.battery_life() { + loop { + let battery_percentage = 100.0 * battery.remaining_capacity; + let battery_rem_hours = battery.remaining_time.as_secs() / 3600; + let battery_rem_mins = battery.remaining_time.as_secs() % 60; + let mut fg_color = Color::Default; + let icon = if battery_percentage > 90.0 { + // Tailwind green 500 + fg_color = Color::Rgb(16, 160, 91); + "\u{f240}" + } else if battery_percentage > 65.0 { + "\u{f241}" + } else if battery_percentage > 35.0 { + "\u{f242}" + } else if battery_percentage > 10.0 { + "\u{f243}" + } else { + // Tailwind red 500 + fg_color = Color::Rgb(239, 68, 68); + "\u{f244}" + }; + let formatted = format!( + " {}{:.1}% {}:{:02} ", + icon, battery_percentage, battery_rem_hours, battery_rem_mins + ); + let update = + BlockUpdate::Single(BlockInfo::from_main(formatted).fg_color(fg_color).build()); + update_s.send(update).unwrap(); + std::thread::sleep(std::time::Duration::from_millis(1000)); + } + } + } +} diff --git a/src/blocks/mod.rs b/src/blocks/mod.rs index 37bc518..b33aa80 100644 --- a/src/blocks/mod.rs +++ b/src/blocks/mod.rs @@ -1,3 +1,4 @@ +pub mod battery; pub mod cpu; pub mod memory; pub mod time; @@ -69,7 +70,7 @@ impl BlockInfoBuilder { self } - pub fn _fg_color(mut self, fg_color: Color) -> Self { + pub fn fg_color(mut self, fg_color: Color) -> Self { self.inner.fg_color = fg_color; self } @@ -153,7 +154,7 @@ pub struct BlockBorder { #[derive(Clone, Copy, Debug)] pub enum Color { Default, - _Rgb(u8, u8, u8), + Rgb(u8, u8, u8), _Rgba(u8, u8, u8, u8), } @@ -161,7 +162,7 @@ impl Color { pub fn to_hex(self) -> Option { match self { Color::Default => None, - Color::_Rgb(r, g, b) => Some(format!("#{:02x}{:02x}{:02x}", r, g, b)), + Color::Rgb(r, g, b) => Some(format!("#{:02x}{:02x}{:02x}", r, g, b)), Color::_Rgba(r, g, b, a) => Some(format!("#{:02x}{:02x}{:02x}{:02x}", r, g, b, a)), } } diff --git a/src/main.rs b/src/main.rs index b833f6a..0296e4f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,8 +15,9 @@ use crate::{ fn main() { let blocks = vec![ Box::new(blocks::time::TimeBlock { id: 0 }) as Box, - Box::new(blocks::memory::MemoryBlock { id: 1 }) as Box, - Box::new(blocks::cpu::CpuBlock { id: 2 }) as Box, + Box::new(blocks::battery::BatteryBlock { id: 1 }) as Box, + Box::new(blocks::memory::MemoryBlock { id: 2 }) as Box, + Box::new(blocks::cpu::CpuBlock { id: 3 }) as Box, ]; let (update_s, update_r) = flume::unbounded();