aboutsummaryrefslogtreecommitdiffstats
path: root/src/border.rs
blob: c0c06d5e48cac6bb9684e3eed662176217306a67 (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
use std::collections::BTreeMap;

use crate::render::FormattedPart;

const DEFAULT_CHAR: &str = "─";

#[derive(Default, PartialEq, Debug)]
pub enum BorderPosition {
    #[default]
    Top,
    Bottom,
}

#[derive(Debug)]
pub struct BorderConfig {
    pub enabled: bool,
    pub char: String,
    pub format: FormattedPart,
    pub position: BorderPosition,
}

impl Default for BorderConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            char: DEFAULT_CHAR.to_owned(),
            format: FormattedPart::default(),
            position: BorderPosition::default(),
        }
    }
}

impl BorderConfig {
    pub fn draw(&self, cols: usize) -> String {
        self.format.format_string(&self.char.repeat(cols))
    }
}

pub fn parse_border_config(config: &BTreeMap<String, String>) -> Option<BorderConfig> {
    let enabled = match config.get("border_enabled") {
        Some(e) => matches!(e.as_str(), "true"),
        None => {
            return None;
        }
    };

    let char = match config.get("border_char") {
        Some(bc) => bc,
        None => DEFAULT_CHAR,
    };

    let format = match config.get("border_format") {
        Some(bfs) => bfs,
        None => "",
    };

    let position = match config.get("border_position") {
        Some(pos) => match pos.to_owned().as_str() {
            "bottom" => BorderPosition::Bottom,
            _ => BorderPosition::Top,
        },
        None => BorderPosition::Top,
    };

    Some(BorderConfig {
        enabled,
        char: char.to_owned(),
        format: FormattedPart::from_format_string(format, config),
        position,
    })
}