Browse Source

Allow configurable fonts and colors

Getty Ritter 4 years ago
parent
commit
57ff80d8dc
2 changed files with 64 additions and 7 deletions
  1. 63 6
      src/config.rs
  2. 1 1
      src/main.rs

+ 63 - 6
src/config.rs

@@ -1,14 +1,52 @@
 use crate::widgets as w;
 
+mod defaults {
+    pub const BG_COLOR: (f64, f64, f64) = (0.1, 0.1, 0.1);
+    pub const FG_COLOR: (f64, f64, f64) = (1.0, 1.0, 1.0);
+
+    pub const FONT_FAMILY: &'static str = "Fira Mono";
+    pub const FONT_SIZE: &'static str = "18";
+}
+
 pub struct Config {
     left: Vec<Box<w::Widget>>,
     right: Vec<Box<w::Widget>>,
+    bg_color: (f64, f64, f64),
+    fg_color: (f64, f64, f64),
+    font: String,
+}
+
+pub fn color_from_hex(input: &str) -> Result<(f64, f64, f64), failure::Error> {
+    let s = input.trim_start_matches("0x");
+    let s = s.trim_start_matches(|c| !"ABCDEFabcdef0123456789".contains(c));
+    match s.len() {
+        6 => {
+            let r = i64::from_str_radix(&s[0..2], 16)? as f64 / 255.0;
+            let g = i64::from_str_radix(&s[2..4], 16)? as f64 / 255.0;
+            let b = i64::from_str_radix(&s[4..6], 16)? as f64 / 255.0;
+            Ok((r, g, b))
+        }
+        3 => {
+            let r = i64::from_str_radix(&s[0..1], 16)? as f64 / 255.0;
+            let g = i64::from_str_radix(&s[1..2], 16)? as f64 / 255.0;
+            let b = i64::from_str_radix(&s[2..3], 16)? as f64 / 255.0;
+            Ok((r, g, b))
+        }
+        _ => bail!("Unable to parse {} as a hex color literal", input),
+    }
 }
 
 impl Config {
     pub fn from_toml(input: toml::Value) -> Result<Config, failure::Error> {
-        let mut conf = Config { left: Vec::new(), right: Vec::new() };
-        let widgets = &input.as_table().ok_or(format_err!("invalid config"))?["widgets"];
+        let mut conf = Config {
+            left: Vec::new(),
+            right: Vec::new(),
+            bg_color: defaults::BG_COLOR,
+            fg_color: defaults::FG_COLOR,
+            font: format!("{} {}", defaults::FONT_FAMILY, defaults::FONT_SIZE),
+        };
+        let table = input.as_table().ok_or(format_err!("invalid config"))?;
+        let widgets = &table["widgets"];
         let mut target = &mut conf.left;
         for section in widgets.as_array().ok_or(format_err!("invalid config"))? {
             let section = section.as_table().ok_or(format_err!("invalid config"))?;
@@ -21,6 +59,15 @@ impl Config {
                 _ => (),
             }
         }
+        if let Some(color) = table.get("background") {
+            conf.bg_color = color_from_hex(color.as_str().ok_or(format_err!("`background` not a str"))?)?;
+        }
+        if let Some(color) = table.get("foreground") {
+            conf.fg_color = color_from_hex(color.as_str().ok_or(format_err!("`foreground` not a str"))?)?;
+        }
+        if let Some(font) = table.get("font") {
+            conf.font = font.as_str().ok_or(format_err!("`font` not a str"))?.to_string();
+        }
         conf.right.reverse();
         Ok(conf)
     }
@@ -39,12 +86,18 @@ impl Config {
     }
 
     pub fn draw(&self, ctx: &cairo::Context, layout: &pango::Layout, stdin: &str, size: w::Size) -> Result<(), failure::Error>{
-        // the background is... gray-ish? this'll be configurable eventually
-        ctx.set_source_rgb(0.1, 0.1, 0.1);
+        // paint the background
+        {
+            let (r, g, b) = self.bg_color;
+            ctx.set_source_rgb(r, g, b);
+        }
         ctx.paint();
 
-        // and the text is white
-        ctx.set_source_rgb(1.0, 1.0, 1.0);
+        // set the foreground color for drawing
+        {
+            let (r, g, b) = self.fg_color;
+            ctx.set_source_rgb(r, g, b);
+        }
 
         // set up a struct with everything that widgets need to draw
         let d = w::Drawing {
@@ -65,4 +118,8 @@ impl Config {
 
         Ok(())
     }
+
+    pub fn font(&self) -> &str {
+        &self.font
+    }
 }

+ 1 - 1
src/main.rs

@@ -74,7 +74,7 @@ fn main() -> Result<(), failure::Error> {
         // allow for the whole width of the bar, minus a small fixed amount
         layout.set_width((w.width - 20) * pango::SCALE);
         // this should also be configurable, but Fira Mono is a good font
-        let mut font = pango::FontDescription::from_string("Fira Mono 18");
+        let mut font = pango::FontDescription::from_string(config.font());
         font.set_weight(pango::Weight::Bold);
         layout.set_font_description(&font);