summaryrefslogtreecommitdiff
path: root/src/config.rs
blob: 48855c874b1da153fb6a1d58a2f9e42bfbdcd300 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use crate::color::Color;
use crate::protocol::{zwlr_layer_shell_v1, zwlr_layer_surface_v1};
use anyhow::{Context, Result};
use pangocairo::pango::FontDescription;
use serde::{de, Deserialize};
use std::collections::HashMap;
use std::fs::read_to_string;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::{env, fmt};

#[derive(Deserialize, Debug)]
#[serde(deny_unknown_fields, default)]
pub struct Config {
    // command
    pub command: Option<String>,
    // 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,
    pub tag_inactive_fg: Color,
    pub tag_inactive_bg: Color,
    // font and size
    pub font: Font,
    pub height: u32,
    pub margin_top: i32,
    pub margin_bottom: i32,
    pub margin_left: i32,
    pub margin_right: i32,
    pub separator_width: f64,
    pub tags_r: f64,
    pub tags_padding: f64,
    pub tags_margin: f64,
    pub blocks_r: f64,
    pub blocks_overlap: f64,
    // misc
    pub position: Position,
    pub layer: Layer,
    pub hide_inactive_tags: bool,
    pub invert_touchpad_scrolling: bool,
    pub show_tags: bool,
    pub show_layout_name: bool,
    pub blend: bool,
    pub show_mode: bool,
    // wm-specific
    pub wm: WmConfig,
    // overrides
    pub output: HashMap<String, OutputOverrides>,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            command: None,

            // A kind of gruvbox theme
            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),
            tag_inactive_fg: Color::from_rgba_hex(0xd79921ff),
            tag_inactive_bg: Color::from_rgba_hex(0x282828ff),

            font: Font::new("monospace 10"),
            height: 24,
            margin_top: 0,
            margin_bottom: 0,
            margin_left: 0,
            margin_right: 0,
            separator_width: 2.0,
            tags_r: 0.0,
            tags_padding: 25.0,
            tags_margin: 0.0,
            blocks_r: 0.0,
            blocks_overlap: 0.0,

            position: Position::Top,
            layer: Layer::Top,
            hide_inactive_tags: true,
            invert_touchpad_scrolling: true,
            show_tags: true,
            show_layout_name: true,
            blend: true,
            show_mode: true,

            wm: WmConfig {
                river: RiverConfig { max_tag: 9 },
            },

            output: HashMap::new(),
        }
    }
}

impl Config {
    pub fn new(path: Option<&Path>) -> Result<Self> {
        let buf;

        let path = match path {
            Some(path) => Some(path),
            None => {
                buf = config_path();
                buf.as_deref()
            }
        };

        Ok(match path {
            Some(config_path) => {
                let config = read_to_string(config_path).context("Failed to read configuration")?;
                toml::from_str(&config).context("Failed to deserialize configuration")?
            }
            None => {
                eprintln!("Could not find the configuration path");
                eprintln!("Using default configuration");
                Self::default()
            }
        })
    }

    pub fn output_enabled(&self, output: &str) -> bool {
        self.output
            .get(output)
            .and_then(|o| o.enable)
            .unwrap_or(true)
    }
}

fn config_dir() -> Option<PathBuf> {
    env::var_os("XDG_CONFIG_HOME")
        .map(PathBuf::from)
        .or_else(|| Some(PathBuf::from(env::var_os("HOME")?).join(".config")))
}

fn config_path() -> Option<PathBuf> {
    let mut path = config_dir()?;
    path.push("i3bar-river");
    path.push("config.toml");
    path.exists().then_some(path)
}

#[derive(Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum Position {
    Top,
    Bottom,
}

impl From<Position> for zwlr_layer_surface_v1::Anchor {
    fn from(position: Position) -> Self {
        match position {
            Position::Top => Self::Top | Self::Left | Self::Right,
            Position::Bottom => Self::Bottom | Self::Left | Self::Right,
        }
    }
}

#[derive(Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum Layer {
    Background,
    Bottom,
    Top,
    Overlay,
}

impl From<Layer> for zwlr_layer_shell_v1::Layer {
    fn from(layer: Layer) -> Self {
        match layer {
            Layer::Background => Self::Background,
            Layer::Bottom => Self::Bottom,
            Layer::Top => Self::Top,
            Layer::Overlay => Self::Overlay,
        }
    }
}

#[derive(Debug, Deserialize)]
pub struct WmConfig {
    pub river: RiverConfig,
}

#[derive(Debug, Deserialize)]
pub struct RiverConfig {
    pub max_tag: u8,
}

#[derive(Debug, Deserialize)]
pub struct OutputOverrides {
    #[serde(default)]
    enable: Option<bool>,
}

#[derive(Debug)]
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::Visitor<'_> 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)
    }
}