gdritter repos knurling / 57ff80d
Allow configurable fonts and colors Getty Ritter 4 years ago
2 changed file(s) with 64 addition(s) and 7 deletion(s). Collapse all Expand all
11 use crate::widgets as w;
2
3 mod defaults {
4 pub const BG_COLOR: (f64, f64, f64) = (0.1, 0.1, 0.1);
5 pub const FG_COLOR: (f64, f64, f64) = (1.0, 1.0, 1.0);
6
7 pub const FONT_FAMILY: &'static str = "Fira Mono";
8 pub const FONT_SIZE: &'static str = "18";
9 }
210
311 pub struct Config {
412 left: Vec<Box<w::Widget>>,
513 right: Vec<Box<w::Widget>>,
14 bg_color: (f64, f64, f64),
15 fg_color: (f64, f64, f64),
16 font: String,
17 }
18
19 pub fn color_from_hex(input: &str) -> Result<(f64, f64, f64), failure::Error> {
20 let s = input.trim_start_matches("0x");
21 let s = s.trim_start_matches(|c| !"ABCDEFabcdef0123456789".contains(c));
22 match s.len() {
23 6 => {
24 let r = i64::from_str_radix(&s[0..2], 16)? as f64 / 255.0;
25 let g = i64::from_str_radix(&s[2..4], 16)? as f64 / 255.0;
26 let b = i64::from_str_radix(&s[4..6], 16)? as f64 / 255.0;
27 Ok((r, g, b))
28 }
29 3 => {
30 let r = i64::from_str_radix(&s[0..1], 16)? as f64 / 255.0;
31 let g = i64::from_str_radix(&s[1..2], 16)? as f64 / 255.0;
32 let b = i64::from_str_radix(&s[2..3], 16)? as f64 / 255.0;
33 Ok((r, g, b))
34 }
35 _ => bail!("Unable to parse {} as a hex color literal", input),
36 }
637 }
738
839 impl Config {
940 pub fn from_toml(input: toml::Value) -> Result<Config, failure::Error> {
10 let mut conf = Config { left: Vec::new(), right: Vec::new() };
11 let widgets = &input.as_table().ok_or(format_err!("invalid config"))?["widgets"];
41 let mut conf = Config {
42 left: Vec::new(),
43 right: Vec::new(),
44 bg_color: defaults::BG_COLOR,
45 fg_color: defaults::FG_COLOR,
46 font: format!("{} {}", defaults::FONT_FAMILY, defaults::FONT_SIZE),
47 };
48 let table = input.as_table().ok_or(format_err!("invalid config"))?;
49 let widgets = &table["widgets"];
1250 let mut target = &mut conf.left;
1351 for section in widgets.as_array().ok_or(format_err!("invalid config"))? {
1452 let section = section.as_table().ok_or(format_err!("invalid config"))?;
2058 "time" => target.push(Box::new(w::Time::new())),
2159 _ => (),
2260 }
61 }
62 if let Some(color) = table.get("background") {
63 conf.bg_color = color_from_hex(color.as_str().ok_or(format_err!("`background` not a str"))?)?;
64 }
65 if let Some(color) = table.get("foreground") {
66 conf.fg_color = color_from_hex(color.as_str().ok_or(format_err!("`foreground` not a str"))?)?;
67 }
68 if let Some(font) = table.get("font") {
69 conf.font = font.as_str().ok_or(format_err!("`font` not a str"))?.to_string();
2370 }
2471 conf.right.reverse();
2572 Ok(conf)
3986 }
4087
4188 pub fn draw(&self, ctx: &cairo::Context, layout: &pango::Layout, stdin: &str, size: w::Size) -> Result<(), failure::Error>{
42 // the background is... gray-ish? this'll be configurable eventually
43 ctx.set_source_rgb(0.1, 0.1, 0.1);
89 // paint the background
90 {
91 let (r, g, b) = self.bg_color;
92 ctx.set_source_rgb(r, g, b);
93 }
4494 ctx.paint();
4595
46 // and the text is white
47 ctx.set_source_rgb(1.0, 1.0, 1.0);
96 // set the foreground color for drawing
97 {
98 let (r, g, b) = self.fg_color;
99 ctx.set_source_rgb(r, g, b);
100 }
48101
49102 // set up a struct with everything that widgets need to draw
50103 let d = w::Drawing {
65118
66119 Ok(())
67120 }
121
122 pub fn font(&self) -> &str {
123 &self.font
124 }
68125 }
7474 // allow for the whole width of the bar, minus a small fixed amount
7575 layout.set_width((w.width - 20) * pango::SCALE);
7676 // this should also be configurable, but Fira Mono is a good font
77 let mut font = pango::FontDescription::from_string("Fira Mono 18");
77 let mut font = pango::FontDescription::from_string(config.font());
7878 font.set_weight(pango::Weight::Bold);
7979 layout.set_font_description(&font);
8080