1 | 1 |
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 |
}
|
2 | 10 |
|
3 | 11 |
pub struct Config {
|
4 | 12 |
left: Vec<Box<w::Widget>>,
|
5 | 13 |
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 |
}
|
6 | 37 |
}
|
7 | 38 |
|
8 | 39 |
impl Config {
|
9 | 40 |
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"];
|
12 | 50 |
let mut target = &mut conf.left;
|
13 | 51 |
for section in widgets.as_array().ok_or(format_err!("invalid config"))? {
|
14 | 52 |
let section = section.as_table().ok_or(format_err!("invalid config"))?;
|
|
20 | 58 |
"time" => target.push(Box::new(w::Time::new())),
|
21 | 59 |
_ => (),
|
22 | 60 |
}
|
| 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();
|
23 | 70 |
}
|
24 | 71 |
conf.right.reverse();
|
25 | 72 |
Ok(conf)
|
|
39 | 86 |
}
|
40 | 87 |
|
41 | 88 |
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 |
}
|
44 | 94 |
ctx.paint();
|
45 | 95 |
|
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 |
}
|
48 | 101 |
|
49 | 102 |
// set up a struct with everything that widgets need to draw
|
50 | 103 |
let d = w::Drawing {
|
|
65 | 118 |
|
66 | 119 |
Ok(())
|
67 | 120 |
}
|
| 121 |
|
| 122 |
pub fn font(&self) -> &str {
|
| 123 |
&self.font
|
| 124 |
}
|
68 | 125 |
}
|