oxwm

https://git.tonybtw.com/oxwm.git git://git.tonybtw.com/oxwm.git
2,302 bytes raw
1
use anyhow::Result;
2
use std::time::Duration;
3
4
mod battery;
5
mod datetime;
6
mod ram;
7
mod shell;
8
9
use battery::Battery;
10
use datetime::DateTime;
11
use ram::Ram;
12
use shell::ShellBlock;
13
14
pub trait Block {
15
    fn content(&mut self) -> Result<String>;
16
    fn interval(&self) -> Duration;
17
    fn color(&self) -> u32;
18
}
19
20
#[derive(Clone)]
21
pub struct BlockConfig {
22
    pub format: String,
23
    pub command: BlockCommand,
24
    pub interval_secs: u64,
25
    pub color: u32,
26
    pub underline: bool,
27
}
28
29
#[derive(Clone)]
30
pub enum BlockCommand {
31
    Shell(String),
32
    DateTime(String),
33
    Battery {
34
        format_charging: String,
35
        format_discharging: String,
36
        format_full: String,
37
    },
38
    Ram,
39
    Static(String),
40
}
41
42
impl BlockConfig {
43
    pub fn to_block(&self) -> Box<dyn Block> {
44
        match &self.command {
45
            BlockCommand::Shell(cmd) => Box::new(ShellBlock::new(
46
                &self.format,
47
                cmd,
48
                self.interval_secs,
49
                self.color,
50
            )),
51
            BlockCommand::DateTime(fmt) => Box::new(DateTime::new(
52
                &self.format,
53
                fmt,
54
                self.interval_secs,
55
                self.color,
56
            )),
57
            BlockCommand::Battery {
58
                format_charging,
59
                format_discharging,
60
                format_full,
61
            } => Box::new(Battery::new(
62
                format_charging,
63
                format_discharging,
64
                format_full,
65
                self.interval_secs,
66
                self.color,
67
            )),
68
            BlockCommand::Ram => Box::new(Ram::new(&self.format, self.interval_secs, self.color)),
69
            BlockCommand::Static(text) => Box::new(StaticBlock::new(
70
                &format!("{}{}", self.format, text),
71
                self.color,
72
            )),
73
        }
74
    }
75
}
76
77
struct StaticBlock {
78
    text: String,
79
    color: u32,
80
}
81
82
impl StaticBlock {
83
    fn new(text: &str, color: u32) -> Self {
84
        Self {
85
            text: text.to_string(),
86
            color,
87
        }
88
    }
89
}
90
91
impl Block for StaticBlock {
92
    fn content(&mut self) -> Result<String> {
93
        Ok(self.text.clone())
94
    }
95
96
    fn interval(&self) -> Duration {
97
        Duration::from_secs(u64::MAX)
98
    }
99
100
    fn color(&self) -> u32 {
101
        self.color
102
    }
103
}