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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use std::{collections::HashMap, path::Path};

use anyhow::{anyhow, Context, Result};
use config::{Config, File, FileFormat, Value};
use lazy_static::lazy_static;
use tokio::sync::OnceCell;

#[cfg(feature = "battery")]
use crate::panels::Battery;
#[cfg(feature = "cpu")]
use crate::panels::Cpu;
#[cfg(feature = "custom")]
use crate::panels::Custom;
#[cfg(feature = "fanotify")]
use crate::panels::Fanotify;
#[cfg(feature = "github")]
use crate::panels::Github;
#[cfg(feature = "inotify")]
use crate::panels::Inotify;
#[cfg(feature = "memory")]
use crate::panels::Memory;
#[cfg(feature = "mpd")]
use crate::panels::Mpd;
#[cfg(feature = "network")]
use crate::panels::Network;
#[cfg(feature = "ping")]
use crate::panels::Ping;
#[cfg(feature = "pulseaudio")]
use crate::panels::Pulseaudio;
#[cfg(feature = "separator")]
use crate::panels::Separator;
#[cfg(feature = "temp")]
use crate::panels::Temp;
#[cfg(feature = "xwindow")]
use crate::panels::XWindow;
#[cfg(feature = "xworkspaces")]
use crate::panels::XWorkspaces;
#[cfg(feature = "clock")]
use crate::panels::{
    precision::{Days, Hours, Minutes, Seconds},
    Clock,
};
use crate::{
    builders::BarConfigBuilder, cleanup, get_table_from_config,
    remove_string_from_config, Alignment, Attrs, BarConfig, Margins,
    PanelConfig, Position,
};

lazy_static! {
    /// The `attrs` table from the global [`Config`].
    pub static ref ATTRS: OnceCell<HashMap<String, Value>> =
        OnceCell::new();
    /// The `ramps` table from the global [`Config`].
    pub static ref RAMPS: OnceCell<HashMap<String, Value>> =
        OnceCell::new();
    /// The `bgs` table from the global [`Config`].
    pub static ref BGS: OnceCell<HashMap<String, Value>> =
        OnceCell::new();
    /// The `consts` table from the global [`Config`].
    pub static ref CONSTS: OnceCell<HashMap<String, Value>> =
        OnceCell::new();
    /// The `images` table from the global [`Config`].
    pub static ref IMAGES: OnceCell<HashMap<String, Value>> =
        OnceCell::new();
}

/// Parses a bar with a given name from the global [`Config`]
///
/// Configuration options:
/// - `position`: `top` or `bottom`
/// - `height`: the height in pixels of the bar
/// - `transparent`: `true` or `false`. If `bg` isn't transparent, the bar won't
///   be either.
/// - `bg`: the background color. See [`csscolorparser::parse`].
/// - `margins`: See [`Margins`]. Keys are `margin_left`, `margin_right`, and
///   `margin_internal`.
/// - `reverse_scroll`: `true` or `false`. Whether to reverse scrolling.
/// - `ipc`: `true` or `false`. Whether to enable inter-process communication.
///
/// See [`Attrs::parse`] for more parsing details.
pub fn parse(bar_name: &str, config: &Path) -> Result<BarConfig> {
    let config = Config::builder()
        .add_source(
            File::new(
                config.to_str().unwrap_or_else(|| {
                    log::error!("Invalid config path");
                    cleanup::exit(None, 101)
                }),
                FileFormat::Toml,
            )
            .required(true),
        )
        .build()
        .unwrap_or_else(|e| {
            log::error!("Error parsing config file: {e}");
            cleanup::exit(None, 101)
        });
    log::info!("Read config file");

    ATTRS
        .set(config.get_table("attrs").unwrap_or_default())
        .unwrap();

    RAMPS
        .set(config.get_table("ramps").unwrap_or_default())
        .unwrap();

    BGS.set(config.get_table("bgs").unwrap_or_default())
        .unwrap();

    CONSTS
        .set(config.get_table("consts").unwrap_or_default())
        .unwrap();

    IMAGES
        .set(config.get_table("images").unwrap_or_default())
        .unwrap();

    let mut bars_table = config
        .get_table("bars")
        .context("`bars` doesn't exist or isn't a table")?;
    log::trace!("got bars table from config");

    let mut bar_table = bars_table
        .remove(bar_name)
        .with_context(|| format!("`{bar_name}` doesn't exist"))?
        .into_table()
        .with_context(|| format!("`{bar_name}` isn't a table"))?;
    log::trace!("got bar table {bar_name} from config");

    let mut bar = BarConfigBuilder::default()
        .name(bar_name.to_owned())
        .position({
            let val = match bar_table
                .remove("position")
                .unwrap_or_default()
                .into_string()
                .unwrap_or_default()
                .as_str()
            {
                "top" => Position::Top,
                "bottom" => Position::Bottom,
                _ => Position::Top,
            };
            log::trace!("got bar position: {val:?}");
            val
        })
        .height({
            let val = bar_table
                .remove("height")
                .unwrap_or_default()
                .into_uint()
                .unwrap_or(24) as u16;
            log::trace!("got bar height: {val}");
            val
        })
        .transparent({
            let val = bar_table
                .remove("transparent")
                .unwrap_or_default()
                .into_bool()
                .unwrap_or_default();
            log::trace!("got bar transparency: {val}");
            val
        })
        .bg({
            let val = bar_table
                .remove("bg")
                .unwrap_or_default()
                .into_string()
                .unwrap_or_default()
                .parse()
                .unwrap_or_default();
            log::trace!("got bar background: {val}");
            val
        })
        .margins({
            let val = Margins::new(
                bar_table
                    .remove("margin_left")
                    .unwrap_or_default()
                    .into_float()
                    .unwrap_or_default(),
                bar_table
                    .remove("margin_internal")
                    .unwrap_or_default()
                    .into_float()
                    .unwrap_or_default(),
                bar_table
                    .remove("margin_right")
                    .unwrap_or_default()
                    .into_float()
                    .unwrap_or_default(),
            );
            log::trace!("got bar margins: {val:?}");
            val
        })
        .reverse_scroll({
            let val = bar_table
                .remove("reverse_scroll")
                .unwrap_or_default()
                .into_bool()
                .unwrap_or_default();
            log::trace!("got bar reverse scroll: {val}");
            val
        })
        .ipc({
            let val = bar_table
                .remove("ipc")
                .unwrap_or_default()
                .into_bool()
                .unwrap_or_default();
            log::trace!("got bar ipc: {val}");
            val
        })
        .attrs({
            let val =
                remove_string_from_config("default_attrs", &mut bar_table)
                    .map_or_else(Attrs::default, Attrs::parse_global);
            log::trace!("got bar attrs: {val:?}");
            val
        })
        .monitor({
            let val = remove_string_from_config("monitor", &mut bar_table);
            log::trace!("got bar monitor: {val:?}");
            val
        })
        .left(Vec::new())
        .center(Vec::new())
        .right(Vec::new())
        .build()?;

    let mut left_final = Vec::new();
    let mut center_final = Vec::new();
    let mut right_final = Vec::new();

    let panels_left = bar_table.remove("panels_left");
    if let Some(pl) = panels_left {
        let panel_list =
            pl.into_array().context("`panels_left` isn't an array")?;
        for p in panel_list {
            if let Ok(name) = p.clone().into_string() {
                log::debug!("Adding left panel {name}");
                left_final.push(name);
            } else {
                log::warn!("Ignoring non-string value {p:?} in `panels_left`");
            }
        }
    }

    let panels_center = bar_table.remove("panels_center");
    if let Some(pc) = panels_center {
        let panel_list =
            pc.into_array().context("`panels_center` isn't an array")?;
        for p in panel_list {
            if let Ok(name) = p.clone().into_string() {
                log::debug!("Adding center panel {name}");
                center_final.push(name);
            } else {
                log::warn!(
                    "Ignoring non-string value {p:?} in `panels_center`"
                );
            }
        }
    }

    let panels_right = bar_table.remove("panels_right");
    if let Some(pr) = panels_right {
        let panel_list =
            pr.into_array().context("`panels_right` isn't an array")?;
        for p in panel_list {
            if let Ok(name) = p.clone().into_string() {
                log::debug!("Adding right panel {name}");
                right_final.push(name);
            } else {
                log::warn!("Ignoring non-string value {p:?} in `panels_right`");
            }
        }
    }

    let panels_table = config
        .get_table("panels")
        .context("`panels` doesn't exist or isn't a table")?;
    log::trace!("got panels table");

    // leak panel names so that we can use &'static str instead of String
    left_final
        .into_iter()
        .filter_map(|p| parse_panel(p.leak(), &panels_table, &config))
        .for_each(|p| bar.add_panel(p, Alignment::Left));
    log::debug!("left panels added");
    center_final
        .into_iter()
        .filter_map(|p| parse_panel(p.leak(), &panels_table, &config))
        .for_each(|p| bar.add_panel(p, Alignment::Center));
    log::debug!("center panels added");
    right_final
        .into_iter()
        .filter_map(|p| parse_panel(p.leak(), &panels_table, &config))
        .for_each(|p| bar.add_panel(p, Alignment::Right));
    log::debug!("right panels added");

    Ok(bar)
}

fn parse_panel(
    p: &'static str,
    panels_table: &HashMap<String, Value>,
    config: &Config,
) -> Option<Box<dyn PanelConfig>> {
    if let Some(mut table) = get_table_from_config(p, panels_table) {
        if let Some(s) = remove_string_from_config("type", &mut table) {
            log::debug!("parsing {s} panel");
            return match s.as_str() {
                #[cfg(feature = "battery")]
                "battery" => {
                    Battery::parse(p, &mut table, config)
                        .map::<Box<dyn PanelConfig>, _>(|p| Box::new(p))
                }
                #[cfg(feature = "clock")]
                "clock" => {
                    if let Some(precision) = &mut table.remove("precision") {
                        if let Ok(precision) = precision.clone().into_string() {
                            match precision.as_str() {
                                "days" => {
                                    Clock::<Days>::parse(p, &mut table, config)
                                        .map::<Box<dyn PanelConfig>, _>(|p| {
                                        Box::new(p)
                                    })
                                }
                                "hours" => {
                                    Clock::<Hours>::parse(p, &mut table, config)
                                        .map::<Box<dyn PanelConfig>, _>(|p| {
                                            Box::new(p)
                                        })
                                }
                                "minutes" => Clock::<Minutes>::parse(
                                    p, &mut table, config,
                                )
                                .map::<Box<dyn PanelConfig>, _>(|p| {
                                    Box::new(p)
                                }),
                                "seconds" | _ => Clock::<Seconds>::parse(
                                    p, &mut table, config,
                                )
                                .map::<Box<dyn PanelConfig>, _>(|p| {
                                    Box::new(p)
                                }),
                            }
                        } else {
                            log::warn!(
                                "Ignoring non-string value {precision:?} \
                                 (location attempt: {:?})",
                                precision.origin()
                            );
                            Clock::<Seconds>::parse(p, &mut table, config)
                                .map::<Box<dyn PanelConfig>, _>(|p| Box::new(p))
                        }
                    } else {
                        Clock::<Seconds>::parse(p, &mut table, config)
                            .map::<Box<dyn PanelConfig>, _>(|p| Box::new(p))
                    }
                }
                #[cfg(feature = "cpu")]
                "cpu" => Cpu::parse(p, &mut table, config)
                    .map::<Box<dyn PanelConfig>, _>(|p| Box::new(p)),
                #[cfg(feature = "custom")]
                "custom" => {
                    Custom::parse(p, &mut table, config)
                        .map::<Box<dyn PanelConfig>, _>(|p| Box::new(p))
                }
                #[cfg(feature = "fanotify")]
                "fanotify" => {
                    Fanotify::parse(p, &mut table, config)
                        .map::<Box<dyn PanelConfig>, _>(|p| Box::new(p))
                }
                #[cfg(feature = "github")]
                "github" => {
                    Github::parse(p, &mut table, config)
                        .map::<Box<dyn PanelConfig>, _>(|p| Box::new(p))
                }
                #[cfg(feature = "inotify")]
                "inotify" => {
                    Inotify::parse(p, &mut table, config)
                        .map::<Box<dyn PanelConfig>, _>(|p| Box::new(p))
                }
                #[cfg(feature = "memory")]
                "memory" => {
                    Memory::parse(p, &mut table, config)
                        .map::<Box<dyn PanelConfig>, _>(|p| Box::new(p))
                }
                #[cfg(feature = "mpd")]
                "mpd" => Mpd::parse(p, &mut table, config)
                    .map::<Box<dyn PanelConfig>, _>(|p| Box::new(p)),
                #[cfg(feature = "network")]
                "network" => {
                    Network::parse(p, &mut table, config)
                        .map::<Box<dyn PanelConfig>, _>(|p| Box::new(p))
                }
                #[cfg(feature = "ping")]
                "ping" => {
                    Ping::parse(p, &mut table, config)
                        .map::<Box<dyn PanelConfig>, _>(|p| Box::new(p))
                }
                #[cfg(feature = "pulseaudio")]
                "pulseaudio" => Pulseaudio::parse(p, &mut table, config)
                    .map::<Box<dyn PanelConfig>, _>(|p| Box::new(p)),
                #[cfg(feature = "separator")]
                "separator" => Separator::parse(p, &mut table, config)
                    .map::<Box<dyn PanelConfig>, _>(|p| Box::new(p)),
                #[cfg(feature = "temp")]
                "temp" => {
                    Temp::parse(p, &mut table, config)
                        .map::<Box<dyn PanelConfig>, _>(|p| Box::new(p))
                }
                #[cfg(feature = "xwindow")]
                "xwindow" => {
                    XWindow::parse(p, &mut table, config)
                        .map::<Box<dyn PanelConfig>, _>(|p| Box::new(p))
                }
                #[cfg(feature = "xworkspaces")]
                "xworkspaces" => XWorkspaces::parse(p, &mut table, config)
                    .map::<Box<dyn PanelConfig>, _>(|p| Box::new(p)),
                s => Err(anyhow!("Unknown panel type {s}")),
            }
            .map_err(|e| {
                log::error!("{e}");
                e
            })
            .ok();
        }
    }
    None
}