oxwm

https://git.tonybtw.com/oxwm.git git://git.tonybtw.com/oxwm.git
2,457 bytes raw
1
use crate::errors::BlockError;
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, BlockError>;
16
    fn interval(&self) -> Duration;
17
    fn color(&self) -> u32;
18
}
19
20
#[derive(Debug, 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(Debug, 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
        battery_name: Option<String>,
38
    },
39
    Ram,
40
    Static(String),
41
}
42
43
impl BlockConfig {
44
    pub fn to_block(&self) -> Box<dyn Block> {
45
        match &self.command {
46
            BlockCommand::Shell(cmd) => Box::new(ShellBlock::new(
47
                &self.format,
48
                cmd,
49
                self.interval_secs,
50
                self.color,
51
            )),
52
            BlockCommand::DateTime(fmt) => Box::new(DateTime::new(
53
                &self.format,
54
                fmt,
55
                self.interval_secs,
56
                self.color,
57
            )),
58
            BlockCommand::Battery {
59
                format_charging,
60
                format_discharging,
61
                format_full,
62
                battery_name,
63
            } => Box::new(Battery::new(
64
                format_charging,
65
                format_discharging,
66
                format_full,
67
                self.interval_secs,
68
                self.color,
69
                battery_name.clone(),
70
            )),
71
            BlockCommand::Ram => Box::new(Ram::new(&self.format, self.interval_secs, self.color)),
72
            BlockCommand::Static(text) => Box::new(StaticBlock::new(
73
                &format!("{}{}", self.format, text),
74
                self.color,
75
            )),
76
        }
77
    }
78
}
79
80
struct StaticBlock {
81
    text: String,
82
    color: u32,
83
}
84
85
impl StaticBlock {
86
    fn new(text: &str, color: u32) -> Self {
87
        Self {
88
            text: text.to_string(),
89
            color,
90
        }
91
    }
92
}
93
94
impl Block for StaticBlock {
95
    fn content(&mut self) -> Result<String, BlockError> {
96
        Ok(self.text.clone())
97
    }
98
99
    fn interval(&self) -> Duration {
100
        Duration::from_secs(u64::MAX)
101
    }
102
103
    fn color(&self) -> u32 {
104
        self.color
105
    }
106
}