diff options
| author | Max Verevkin <maxxverrr@gmail.com> | 2022-04-01 21:50:19 +0300 |
|---|---|---|
| committer | Max Verevkin <maxxverrr@gmail.com> | 2022-04-01 21:50:19 +0300 |
| commit | 7bcaa341b8685ba411d206c1a89dc3ca77aa71bb (patch) | |
| tree | 917dec11c47ce71fe9486e56f94015bd12c496b7 /src/config.rs | |
| download | i3bar-mango-7bcaa341b8685ba411d206c1a89dc3ca77aa71bb.tar.gz | |
initial commit
Diffstat (limited to 'src/config.rs')
| -rw-r--r-- | src/config.rs | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..21ef9a2 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,114 @@ +use crate::color::Color; +use anyhow::{Context, Result}; +use dirs_next::config_dir; +use pangocairo::pango::FontDescription; +use serde::{de, Deserialize}; +use std::fmt; +use std::fs::read_to_string; +use std::ops::Deref; + +#[derive(Deserialize)] +#[serde(default)] +pub struct Config { + // colors + pub background: Color, + pub color: Color, + pub separator: Color, + pub tag_fg: Color, + pub tag_bg: Color, + pub tag_focused_fg: Color, + pub tag_focused_bg: Color, + pub tag_urgent_fg: Color, + pub tag_urgent_bg: Color, + // font and size + pub font: Font, + pub height: u32, + // command + pub command: Option<String>, +} + +impl Default for Config { + fn default() -> Self { + // A kind of gruvbox theme + Self { + background: Color::from_rgba_hex(0x282828ff), + color: Color::from_rgba_hex(0xffffffff), + separator: Color::from_rgba_hex(0x9a8a62ff), + tag_fg: Color::from_rgba_hex(0xd79921ff), + tag_bg: Color::from_rgba_hex(0x282828ff), + tag_focused_fg: Color::from_rgba_hex(0x1d2021ff), + tag_focused_bg: Color::from_rgba_hex(0x689d68ff), + tag_urgent_fg: Color::from_rgba_hex(0x282828ff), + tag_urgent_bg: Color::from_rgba_hex(0xcc241dff), + font: Font::new("monospace 10"), + height: 24, + command: None, + } + } +} + +impl Config { + pub fn new() -> Result<Self> { + Ok(match config_dir() { + Some(mut config_path) => { + config_path.push("i3bar-river"); + config_path.push("config.toml"); + if config_path.exists() { + let config = + read_to_string(config_path).context("Failed to read configuration")?; + toml::from_str(&config).context("Failed to deserialize configuration")? + } else { + info!("Using default configuration"); + Self::default() + } + } + None => { + warn!("Could not find the configuration path"); + info!("Using default configuration"); + Self::default() + } + }) + } +} + +pub struct Font(pub FontDescription); + +impl Font { + pub fn new(desc: &str) -> Self { + Self(FontDescription::from_string(desc)) + } +} + +impl Deref for Font { + type Target = FontDescription; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl<'de> de::Deserialize<'de> for Font { + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> + where + D: de::Deserializer<'de>, + { + struct FontVisitor; + + impl<'de> de::Visitor<'de> for FontVisitor { + type Value = Font; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("font description") + } + + fn visit_str<E>(self, s: &str) -> Result<Self::Value, E> + where + E: de::Error, + { + Ok(Font::new(s)) + } + } + + deserializer.deserialize_str(FontVisitor) + } +} |
