tony / oxwm

Branch: 35a51b2
Switch branches/tags
master
tonybanters Added ram block 93d1e6e 2025-10-07
101 lines · 2.3 KB Raw Blame History
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 pub struct BlockConfig {
21 pub format: &'static str,
22 pub command: BlockCommand,
23 pub interval_secs: u64,
24 pub color: u32,
25 pub underline: bool,
26 }
27
28 pub enum BlockCommand {
29 Shell(&'static str),
30 DateTime(&'static str),
31 Battery {
32 format_charging: &'static str,
33 format_discharging: &'static str,
34 format_full: &'static str,
35 },
36 Ram,
37 Static(&'static str),
38 }
39
40 impl BlockConfig {
41 pub fn to_block(&self) -> Box<dyn Block> {
42 match self.command {
43 BlockCommand::Shell(cmd) => Box::new(ShellBlock::new(
44 self.format,
45 cmd,
46 self.interval_secs,
47 self.color,
48 )),
49 BlockCommand::DateTime(fmt) => Box::new(DateTime::new(
50 self.format,
51 fmt,
52 self.interval_secs,
53 self.color,
54 )),
55 BlockCommand::Battery {
56 format_charging,
57 format_discharging,
58 format_full,
59 } => Box::new(Battery::new(
60 format_charging,
61 format_discharging,
62 format_full,
63 self.interval_secs,
64 self.color,
65 )),
66 BlockCommand::Ram => Box::new(Ram::new(self.format, self.interval_secs, self.color)),
67 BlockCommand::Static(text) => Box::new(StaticBlock::new(
68 &format!("{}{}", self.format, text),
69 self.color,
70 )),
71 }
72 }
73 }
74
75 struct StaticBlock {
76 text: String,
77 color: u32,
78 }
79
80 impl StaticBlock {
81 fn new(text: &str, color: u32) -> Self {
82 Self {
83 text: text.to_string(),
84 color,
85 }
86 }
87 }
88
89 impl Block for StaticBlock {
90 fn content(&mut self) -> Result<String> {
91 Ok(self.text.clone())
92 }
93
94 fn interval(&self) -> Duration {
95 Duration::from_secs(u64::MAX)
96 }
97
98 fn color(&self) -> u32 {
99 self.color
100 }
101 }