Add battery block

master
Rasmus Rosengren 3 years ago
parent 650e5f021e
commit e14841ff2a
Signed by: rsrp
GPG Key ID: A13BC7BC4F81CF5F
  1. 57
      src/blocks/battery.rs
  2. 7
      src/blocks/mod.rs
  3. 5
      src/main.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<BlockEvent>, update_s: flume::Sender<BlockUpdate>) {
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));
}
}
}
}

@ -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<String> {
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)),
}
}

@ -15,8 +15,9 @@ use crate::{
fn main() {
let blocks = vec![
Box::new(blocks::time::TimeBlock { id: 0 }) as Box<dyn Block + Send>,
Box::new(blocks::memory::MemoryBlock { id: 1 }) as Box<dyn Block + Send>,
Box::new(blocks::cpu::CpuBlock { id: 2 }) as Box<dyn Block + Send>,
Box::new(blocks::battery::BatteryBlock { id: 1 }) as Box<dyn Block + Send>,
Box::new(blocks::memory::MemoryBlock { id: 2 }) as Box<dyn Block + Send>,
Box::new(blocks::cpu::CpuBlock { id: 3 }) as Box<dyn Block + Send>,
];
let (update_s, update_r) = flume::unbounded();

Loading…
Cancel
Save