tony / oxwm

Restructured blocks to be more sane.. Browse files
tonybtw committed 2025-10-04 parent d5b3499 commit 06c6741a6ff5edf0188ad33a9b5d8c6e819cc80f
Showing 10 changed files with 280 additions and 123 deletions. UnifiedSplit
+12 −11 src/bar/bar.rs
@@ -1,9 +1,6 @@
1 -use super::blocks::{Block, Clock, Sep, Uname}; 1 +use super::blocks::Block;
2 use super::font::{Font, FontDraw}; 2 use super::font::{Font, FontDraw};
3 -use crate::config::{ 3 +use crate::config::{FONT, SCHEME_NORMAL, SCHEME_OCCUPIED, SCHEME_SELECTED, STATUS_BLOCKS, TAGS};
4 - CLOCK_COLOR, CLOCK_FORMAT, FONT, SCHEME_NORMAL, SCHEME_OCCUPIED, SCHEME_SELECTED, SEP_COLOR,
5 - SEPARATOR, TAGS, UNAME_COLOR, UNAME_PREFIX,
6 -};
7 use anyhow::Result; 4 use anyhow::Result;
8 use std::time::Instant; 5 use std::time::Instant;
9 use x11rb::COPY_DEPTH_FROM_PARENT; 6 use x11rb::COPY_DEPTH_FROM_PARENT;
@@ -42,7 +39,7 @@ impl Bar {
42 } 39 }
43 let font = Font::new(display, screen_num as i32, FONT)?; 40 let font = Font::new(display, screen_num as i32, FONT)?;
44 41
45 - let height = (font.height() as f32 * 1.25) as u16; 42 + let height = (font.height() as f32 * 1.5) as u16;
46 43
47 connection.create_window( 44 connection.create_window(
48 COPY_DEPTH_FROM_PARENT, 45 COPY_DEPTH_FROM_PARENT,
@@ -87,13 +84,17 @@ impl Bar {
87 }) 84 })
88 .collect(); 85 .collect();
89 86
90 - let blocks: Vec<Box<dyn Block>> = vec![ 87 + let blocks: Vec<Box<dyn Block>> = STATUS_BLOCKS
91 - Box::new(Uname::new(UNAME_PREFIX, UNAME_COLOR)), 88 + .iter()
92 - Box::new(Sep::new(SEPARATOR, SEP_COLOR)), 89 + .map(|config| config.to_block())
93 - Box::new(Clock::new(CLOCK_FORMAT, CLOCK_COLOR)), 90 + .collect();
94 - ];
95 91
96 let block_last_updates = vec![Instant::now(); blocks.len()]; 92 let block_last_updates = vec![Instant::now(); blocks.len()];
93 + // let blocks: Vec<Box<dyn Block>> = vec![
94 + // Box::new(Uname::new(UNAME_PREFIX, UNAME_COLOR)),
95 + // Box::new(Sep::new(SEPARATOR, SEP_COLOR)),
96 + // Box::new(Clock::new(CLOCK_FORMAT, CLOCK_COLOR)),
97 + // ];
97 98
98 Ok(Bar { 99 Ok(Bar {
99 window, 100 window,
+68 −0 src/bar/blocks/battery.rs new file mode 100644
@@ -0,0 +1,68 @@
1 +use super::Block;
2 +use anyhow::Result;
3 +use std::fs;
4 +use std::time::Duration;
5 +
6 +pub struct Battery {
7 + format_charging: String,
8 + format_discharging: String,
9 + format_full: String,
10 + interval: Duration,
11 + color: u32,
12 + battery_path: String,
13 +}
14 +
15 +impl Battery {
16 + pub fn new(
17 + format_charging: &str,
18 + format_discharging: &str,
19 + format_full: &str,
20 + interval_secs: u64,
21 + color: u32,
22 + ) -> Self {
23 + Self {
24 + format_charging: format_charging.to_string(),
25 + format_discharging: format_discharging.to_string(),
26 + format_full: format_full.to_string(),
27 + interval: Duration::from_secs(interval_secs),
28 + color,
29 + battery_path: "/sys/class/power_supply/BAT0".to_string(),
30 + }
31 + }
32 +
33 + fn read_file(&self, filename: &str) -> Result<String> {
34 + let path = format!("{}/{}", self.battery_path, filename);
35 + Ok(fs::read_to_string(path)?.trim().to_string())
36 + }
37 +
38 + fn get_capacity(&self) -> Result<u32> {
39 + Ok(self.read_file("capacity")?.parse()?)
40 + }
41 +
42 + fn get_status(&self) -> Result<String> {
43 + self.read_file("status")
44 + }
45 +}
46 +
47 +impl Block for Battery {
48 + fn content(&mut self) -> Result<String> {
49 + let capacity = self.get_capacity()?;
50 + let status = self.get_status()?;
51 +
52 + let format = match status.as_str() {
53 + "Charging" => &self.format_charging,
54 + "Full" => &self.format_full,
55 + _ => &self.format_discharging,
56 + };
57 +
58 + Ok(format.replace("{}", &capacity.to_string()))
59 + }
60 +
61 + fn interval(&self) -> Duration {
62 + self.interval
63 + }
64 +
65 + fn color(&self) -> u32 {
66 + self.color
67 + }
68 +}
+0 −30 src/bar/blocks/clock.rs deleted file mode 100644
@@ -1,30 +0,0 @@
1 -use super::Block;
2 -use anyhow::Result;
3 -use chrono::Local;
4 -use std::time::Duration;
5 -
6 -pub struct Clock {
7 - format: &'static str,
8 - color: u32,
9 -}
10 -
11 -impl Clock {
12 - pub fn new(format: &'static str, color: u32) -> Self {
13 - Self { format, color }
14 - }
15 -}
16 -
17 -impl Block for Clock {
18 - fn content(&mut self) -> Result<String> {
19 - let now = Local::now();
20 - Ok(now.format(self.format).to_string())
21 - }
22 -
23 - fn interval(&self) -> Duration {
24 - Duration::from_secs(1)
25 - }
26 -
27 - fn color(&self) -> u32 {
28 - self.color
29 - }
30 -}
+38 −0 src/bar/blocks/datetime.rs new file mode 100644
@@ -0,0 +1,38 @@
1 +use super::Block;
2 +use anyhow::Result;
3 +use chrono::Local;
4 +use std::time::Duration;
5 +
6 +pub struct DateTime {
7 + format_template: String,
8 + time_format: String,
9 + interval: Duration,
10 + color: u32,
11 +}
12 +
13 +impl DateTime {
14 + pub fn new(format_template: &str, time_format: &str, interval_secs: u64, color: u32) -> Self {
15 + Self {
16 + format_template: format_template.to_string(),
17 + time_format: time_format.to_string(),
18 + interval: Duration::from_secs(interval_secs),
19 + color,
20 + }
21 + }
22 +}
23 +
24 +impl Block for DateTime {
25 + fn content(&mut self) -> Result<String> {
26 + let now = Local::now();
27 + let time_str = now.format(&self.time_format).to_string();
28 + Ok(self.format_template.replace("{}", &time_str))
29 + }
30 +
31 + fn interval(&self) -> Duration {
32 + self.interval
33 + }
34 +
35 + fn color(&self) -> u32 {
36 + self.color
37 + }
38 +}
+84 −8 src/bar/blocks/mod.rs
@@ -1,20 +1,96 @@
1 use anyhow::Result; 1 use anyhow::Result;
2 use std::time::Duration; 2 use std::time::Duration;
3 3
4 -pub mod clock; 4 +mod battery;
5 -pub mod sep; 5 +mod datetime;
6 -pub mod uname; 6 +mod shell;
7 7
8 -pub use clock::Clock; 8 +use battery::Battery;
9 -pub use sep::Sep; 9 +use datetime::DateTime;
10 -pub use uname::Uname; 10 +use shell::ShellBlock;
11 11
12 pub trait Block { 12 pub trait Block {
13 fn content(&mut self) -> Result<String>; 13 fn content(&mut self) -> Result<String>;
14 fn interval(&self) -> Duration; 14 fn interval(&self) -> Duration;
15 fn color(&self) -> u32; 15 fn color(&self) -> u32;
16 +}
17 +
18 +pub struct BlockConfig {
19 + pub format: &'static str,
20 + pub command: BlockCommand,
21 + pub interval_secs: u64,
22 + pub color: u32,
23 +}
24 +
25 +pub enum BlockCommand {
26 + Shell(&'static str),
27 + DateTime(&'static str),
28 + Battery {
29 + format_charging: &'static str,
30 + format_discharging: &'static str,
31 + format_full: &'static str,
32 + },
33 + Static(&'static str),
34 +}
35 +
36 +impl BlockConfig {
37 + pub fn to_block(&self) -> Box<dyn Block> {
38 + match self.command {
39 + BlockCommand::Shell(cmd) => Box::new(ShellBlock::new(
40 + self.format,
41 + cmd,
42 + self.interval_secs,
43 + self.color,
44 + )),
45 + BlockCommand::DateTime(fmt) => Box::new(DateTime::new(
46 + self.format,
47 + fmt,
48 + self.interval_secs,
49 + self.color,
50 + )),
51 + BlockCommand::Battery {
52 + format_charging,
53 + format_discharging,
54 + format_full,
55 + } => Box::new(Battery::new(
56 + format_charging,
57 + format_discharging,
58 + format_full,
59 + self.interval_secs,
60 + self.color,
61 + )),
62 + BlockCommand::Static(text) => Box::new(StaticBlock::new(
63 + &format!("{}{}", self.format, text),
64 + self.color,
65 + )),
66 + }
67 + }
68 +}
69 +
70 +struct StaticBlock {
71 + text: String,
72 + color: u32,
73 +}
74 +
75 +impl StaticBlock {
76 + fn new(text: &str, color: u32) -> Self {
77 + Self {
78 + text: text.to_string(),
79 + color,
80 + }
81 + }
82 +}
83 +
84 +impl Block for StaticBlock {
85 + fn content(&mut self) -> Result<String> {
86 + Ok(self.text.clone())
87 + }
88 +
89 + fn interval(&self) -> Duration {
90 + Duration::from_secs(u64::MAX)
91 + }
16 92
17 - fn on_click(&mut self) -> Result<()> { 93 + fn color(&self) -> u32 {
18 - Ok(()) 94 + self.color
19 } 95 }
20 } 96 }
+0 −28 src/bar/blocks/sep.rs deleted file mode 100644
@@ -1,28 +0,0 @@
1 -use super::Block;
2 -use anyhow::Result;
3 -use std::time::Duration;
4 -
5 -pub struct Sep {
6 - text: &'static str,
7 - color: u32,
8 -}
9 -
10 -impl Sep {
11 - pub fn new(text: &'static str, color: u32) -> Self {
12 - Self { text, color }
13 - }
14 -}
15 -
16 -impl Block for Sep {
17 - fn content(&mut self) -> Result<String> {
18 - Ok(self.text.to_string())
19 - }
20 -
21 - fn interval(&self) -> Duration {
22 - Duration::from_secs(u64::MAX)
23 - }
24 -
25 - fn color(&self) -> u32 {
26 - self.color
27 - }
28 -}
+39 −0 src/bar/blocks/shell.rs new file mode 100644
@@ -0,0 +1,39 @@
1 +use super::Block;
2 +use anyhow::Result;
3 +use std::process::Command;
4 +use std::time::Duration;
5 +
6 +pub struct ShellBlock {
7 + format: String,
8 + command: String,
9 + interval: Duration,
10 + color: u32,
11 +}
12 +
13 +impl ShellBlock {
14 + pub fn new(format: &str, command: &str, interval_secs: u64, color: u32) -> Self {
15 + Self {
16 + format: format.to_string(),
17 + command: command.to_string(),
18 + interval: Duration::from_secs(interval_secs),
19 + color,
20 + }
21 + }
22 +}
23 +
24 +impl Block for ShellBlock {
25 + fn content(&mut self) -> Result<String> {
26 + let output = Command::new("sh").arg("-c").arg(&self.command).output()?;
27 +
28 + let result = String::from_utf8_lossy(&output.stdout).trim().to_string();
29 + Ok(self.format.replace("{}", &result))
30 + }
31 +
32 + fn interval(&self) -> Duration {
33 + self.interval
34 + }
35 +
36 + fn color(&self) -> u32 {
37 + self.color
38 + }
39 +}
+0 −38 src/bar/blocks/uname.rs deleted file mode 100644
@@ -1,38 +0,0 @@
1 -use super::Block;
2 -use anyhow::Result;
3 -use std::process::Command;
4 -use std::time::Duration;
5 -
6 -pub struct Uname {
7 - cached_version: String,
8 - prefix: &'static str,
9 - color: u32,
10 -}
11 -
12 -impl Uname {
13 - pub fn new(prefix: &'static str, color: u32) -> Self {
14 - Self {
15 - cached_version: String::new(),
16 - prefix,
17 - color,
18 - }
19 - }
20 -}
21 -
22 -impl Block for Uname {
23 - fn content(&mut self) -> Result<String> {
24 - if self.cached_version.is_empty() {
25 - let output = Command::new("uname").arg("-r").output()?;
26 - self.cached_version = String::from_utf8_lossy(&output.stdout).trim().to_string();
27 - }
28 - Ok(format!("{}{}", self.prefix, self.cached_version))
29 - }
30 -
31 - fn interval(&self) -> Duration {
32 - Duration::from_secs(u64::MAX)
33 - }
34 -
35 - fn color(&self) -> u32 {
36 - self.color
37 - }
38 -}
+1 −1 src/bar/mod.rs
@@ -1,9 +1,9 @@
1 mod bar; 1 mod bar;
2 mod blocks; 2 mod blocks;
3 mod font; 3 mod font;
4 -// mod widgets; // TODO: implement later
5 4
6 pub use bar::Bar; 5 pub use bar::Bar;
6 +pub use blocks::{BlockCommand, BlockConfig};
7 7
8 // Bar position (for future use) 8 // Bar position (for future use)
9 #[derive(Debug, Clone, Copy)] 9 #[derive(Debug, Clone, Copy)]
+38 −7 src/config.rs
@@ -1,3 +1,4 @@
1 +use crate::bar::{BlockCommand, BlockConfig};
1 use crate::keyboard::handlers::Key; 2 use crate::keyboard::handlers::Key;
2 use crate::keyboard::{Arg, KeyAction, keycodes}; 3 use crate::keyboard::{Arg, KeyAction, keycodes};
3 use x11rb::protocol::xproto::KeyButMask; 4 use x11rb::protocol::xproto::KeyButMask;
@@ -8,7 +9,7 @@ use x11rb::protocol::xproto::KeyButMask;
8 pub const BORDER_WIDTH: u32 = 1; 9 pub const BORDER_WIDTH: u32 = 1;
9 pub const BORDER_FOCUSED: u32 = 0x6dade3; 10 pub const BORDER_FOCUSED: u32 = 0x6dade3;
10 pub const BORDER_UNFOCUSED: u32 = 0xbbbbbb; 11 pub const BORDER_UNFOCUSED: u32 = 0xbbbbbb;
11 -pub const FONT: &str = "JetBrainsMono Nerd Font:style=Bold:size=12"; 12 +pub const FONT: &str = "JetBrainsMono Nerd Font:style=Bold:size=14";
12 13
13 // ======================================== 14 // ========================================
14 // DEFAULTS 15 // DEFAULTS
@@ -110,11 +111,41 @@ pub const KEYBINDINGS: &[Key] = &[
110 // ======================================== 111 // ========================================
111 // STATUS BAR BLOCKS 112 // STATUS BAR BLOCKS
112 // ======================================== 113 // ========================================
113 -pub const CLOCK_FORMAT: &str = " 󰸘 %a, %b %d - %-I:%M %P"; // "Fri, Oct 03 - 7:58 pm" 114 +pub const STATUS_BLOCKS: &[BlockConfig] = &[
114 -pub const SEPARATOR: &str = " | "; 115 + BlockConfig {
115 -pub const UNAME_COLOR: u32 = RED; 116 + format: "",
116 -pub const UNAME_PREFIX: &str = "  "; 117 + command: BlockCommand::Battery {
117 -pub const SEP_COLOR: u32 = GRAY_LIGHT; 118 + format_charging: " 󰂄 Bat: {}%",
118 -pub const CLOCK_COLOR: u32 = CYAN; 119 + format_discharging: " 󰁹 Bat:{}%",
120 + format_full: " 󰁹 Bat: {}%",
121 + },
122 + interval_secs: 30,
123 + color: BLUE,
124 + },
125 + BlockConfig {
126 + format: " | ",
127 + command: BlockCommand::Static(""),
128 + interval_secs: u64::MAX,
129 + color: GRAY_LIGHT,
130 + },
131 + BlockConfig {
132 + format: "  {}",
133 + command: BlockCommand::Shell("uname -r"),
134 + interval_secs: u64::MAX,
135 + color: RED,
136 + },
137 + BlockConfig {
138 + format: " | ",
139 + command: BlockCommand::Static(""),
140 + interval_secs: u64::MAX,
141 + color: GRAY_LIGHT,
142 + },
143 + BlockConfig {
144 + format: " 󰸘 {}",
145 + command: BlockCommand::DateTime("%a, %b %d - %-I:%M %P"),
146 + interval_secs: 1,
147 + color: CYAN,
148 + },
149 +];
119 150
120 const SHIFT: KeyButMask = KeyButMask::SHIFT; 151 const SHIFT: KeyButMask = KeyButMask::SHIFT;