Skip to content

Commit

Permalink
Refactor to utilize from_hex color method
Browse files Browse the repository at this point in the history
  • Loading branch information
thled committed Sep 3, 2022
1 parent c7026fa commit 93ec0ab
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 41 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ version = "1.0.0"
edition = "2021"

[dependencies]
cnx = { git = "https://github.com/mjkillough/cnx", rev = "0f0406d750e583f6632a1d42945902ccd00379c4" }
cnx-contrib = { git = "https://github.com/mjkillough/cnx", rev = "0f0406d750e583f6632a1d42945902ccd00379c4" }
cnx = { git = "https://github.com/thled/cnx" }
cnx-contrib = { git = "https://github.com/thled/cnx" }
anyhow = "1.0.62"
xrdb = "0.1.1"
colors-transform = "0.2.11"
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ fn window_title() -> ActiveWindowTitle {

fn cpu() -> Result<cpu::Cpu, anyhow::Error> {
let render = Box::new(|load| {
let mut color = Color::white().to_hex();
let mut color = xcolor::foreground().to_hex();
if load > 10 {
color = Color::yellow().to_hex();
color = xcolor::yellow().to_hex();
}
if load > 50 {
color = Color::red().to_hex();
color = xcolor::red().to_hex();
}
format!("CPU <span foreground=\"{}\">{}%</span>", color, load)
});
Expand Down
54 changes: 22 additions & 32 deletions src/xcolor.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,37 @@
use cnx::text::Color;
use colors_transform::{Color as ColorTransform, Rgb};
use xrdb::Colors;

fn read_xresources_color(color_code: usize) -> Color {
let xcolors = thledbar_colors_from_xresources();
let hex = xcolors.colors[color_code].as_ref().unwrap();
let rgb = Rgb::from_hex_str(hex).unwrap();

Color::from_rgb(
rgb.get_red() as u8,
rgb.get_green() as u8,
rgb.get_blue() as u8,
)
pub fn background() -> Color {
let hex = xcolors().bg.unwrap();

Color::from_hex(&hex)
}

fn thledbar_colors_from_xresources() -> Colors {
Colors::new("thledbar").unwrap()
pub fn foreground() -> Color {
let hex = xcolors().fg.unwrap();

Color::from_hex(&hex)
}

pub fn red() -> Color {
read_xresources_color(1)
}

pub fn blue() -> Color {
read_xresources_color(4)
}

pub fn background() -> Color {
let xcolors = thledbar_colors_from_xresources();
let hex = xcolors.bg.unwrap();
let rgb = Rgb::from_hex_str(&hex).unwrap();

Color::from_rgb(
rgb.get_red() as u8,
rgb.get_green() as u8,
rgb.get_blue() as u8,
)
pub fn yellow() -> Color {
read_xresources_color(3)
}

fn xcolors() -> Colors {
Colors::new("thledbar").unwrap()
}

pub(crate) fn foreground() -> Color {
let xcolors = thledbar_colors_from_xresources();
let hex = xcolors.fg.unwrap();
let rgb = Rgb::from_hex_str(&hex).unwrap();
fn read_xresources_color(color_code: usize) -> Color {
let xcolor = xcolors();
let hex = xcolor.colors[color_code].as_ref().unwrap();

Color::from_rgb(
rgb.get_red() as u8,
rgb.get_green() as u8,
rgb.get_blue() as u8,
)
Color::from_hex(hex)
}

0 comments on commit 93ec0ab

Please sign in to comment.