power: set color when battery power low.

This commit is contained in:
Bill Thiede 2021-12-12 11:24:58 -08:00
parent 9c94122d1d
commit 793f1f63e9
4 changed files with 33 additions and 17 deletions

15
src/colors.rs Normal file
View File

@ -0,0 +1,15 @@
use i3monkit::ColorRGB;
pub const WHITE: ColorRGB = ColorRGB(255, 255, 255);
pub fn lerp(c1: ColorRGB, c2: ColorRGB, alpha: f32) -> ColorRGB {
ColorRGB(
(c1.0 as f32 * (1. - alpha) + c2.0 as f32 * alpha) as u8,
(c1.1 as f32 * (1. - alpha) + c2.1 as f32 * alpha) as u8,
(c1.2 as f32 * (1. - alpha) + c2.2 as f32 * alpha) as u8,
)
}
pub fn to_string(c: ColorRGB) -> String {
format!("#{:02x}{:02x}{:02x}", c.0, c.1, c.2)
}

View File

@ -1,2 +1,3 @@
pub mod colors;
pub mod spark; pub mod spark;
pub mod widgets; pub mod widgets;

View File

@ -4,7 +4,7 @@ use std::io::{BufRead, BufReader, Result};
use i3monkit::{Block, ColorRGB, Widget, WidgetUpdate}; use i3monkit::{Block, ColorRGB, Widget, WidgetUpdate};
use crate::spark; use crate::{colors, spark};
#[derive(Debug)] #[derive(Debug)]
struct Stat { struct Stat {
@ -20,18 +20,6 @@ pub struct CpuWidget {
history: Vec<VecDeque<f32>>, history: Vec<VecDeque<f32>>,
} }
fn color_lerp(c1: ColorRGB, c2: ColorRGB, alpha: f32) -> ColorRGB {
ColorRGB(
(c1.0 as f32 * (1. - alpha) + c2.0 as f32 * alpha) as u8,
(c1.1 as f32 * (1. - alpha) + c2.1 as f32 * alpha) as u8,
(c1.2 as f32 * (1. - alpha) + c2.2 as f32 * alpha) as u8,
)
}
fn color_to_string(c: ColorRGB) -> String {
format!("#{:02x}{:02x}{:02x}", c.0, c.1, c.2)
}
impl CpuWidget { impl CpuWidget {
fn read_status() -> Result<Vec<Stat>> { fn read_status() -> Result<Vec<Stat>> {
let file = File::open("/proc/stat")?; let file = File::open("/proc/stat")?;
@ -92,10 +80,10 @@ impl CpuWidget {
.iter() .iter()
.zip(sg.chars()) .zip(sg.chars())
.map(|(v, g)| { .map(|(v, g)| {
let c = color_lerp(ColorRGB::green(), ColorRGB::red(), *v); let c = colors::lerp(ColorRGB::green(), ColorRGB::red(), *v);
format!( format!(
r##"<span foreground="{}">{}</span>"##, r##"<span foreground="{}">{}</span>"##,
color_to_string(c), colors::to_string(c),
g g
) )
}) })

View File

@ -13,9 +13,11 @@
use std::collections::HashMap; use std::collections::HashMap;
use glob::glob; use glob::glob;
use i3monkit::{Block, Widget, WidgetUpdate}; use i3monkit::{Block, ColorRGB, Widget, WidgetUpdate};
use thiserror::Error; use thiserror::Error;
use crate::colors;
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct PowerSupply {} pub struct PowerSupply {}
@ -71,14 +73,24 @@ impl PowerSupply {
} else { } else {
BATT_0 BATT_0
}; };
let color = if cap >= 50 {
colors::WHITE
} else if cap >= 25 {
ColorRGB::yellow()
} else {
ColorRGB::red()
};
res.push(format!( res.push(format!(
"{} {}: <span face=\"pango:Font Awesome 5 Free\">{}</span>", r##"{} {}: {}% <span color="{}" face="pango:Font Awesome 5 Free">{}</span>"##,
values values
.get("POWER_SUPPLY_MANUFACTURER") .get("POWER_SUPPLY_MANUFACTURER")
.unwrap_or(&"Unknown mfg"), .unwrap_or(&"Unknown mfg"),
values values
.get("POWER_SUPPLY_MODEL_NAME") .get("POWER_SUPPLY_MODEL_NAME")
.unwrap_or(&"Unknown model"), .unwrap_or(&"Unknown model"),
cap,
colors::to_string(color),
power, power,
)); ));
} }