parent
650e5f021e
commit
e14841ff2a
@ -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)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue