tony / oxwm

Cleaned up consts to enum Browse files
tonybanters committed 2025-10-29 parents dd010d0 e8f6a3c commit 8dc9d0d37fb63657074dc2911be1233fdfe0fbc8
Showing 4 changed files with 118 additions and 32 deletions. UnifiedSplit
+51 −1 src/config/mod.rs
@@ -4,6 +4,7 @@ use crate::keyboard::handlers::Key;
4 use crate::keyboard::keycodes; 4 use crate::keyboard::keycodes;
5 use crate::keyboard::{Arg, KeyAction}; 5 use crate::keyboard::{Arg, KeyAction};
6 use serde::Deserialize; 6 use serde::Deserialize;
7 +use std::collections::HashMap;
7 use x11rb::protocol::xproto::{KeyButMask, Keycode}; 8 use x11rb::protocol::xproto::{KeyButMask, Keycode};
8 9
9 #[derive(Debug, Deserialize)] 10 #[derive(Debug, Deserialize)]
@@ -156,8 +157,57 @@ impl KeyData {
156 } 157 }
157 } 158 }
158 159
160 +fn preprocess_variables(input: &str) -> Result<String, ConfigError> {
161 + let mut variables: HashMap<String, String> = HashMap::new();
162 + let mut result = String::new();
163 +
164 + for line in input.lines() {
165 + let trimmed = line.trim();
166 +
167 + if trimmed.starts_with("#DEFINE") {
168 + let rest = trimmed.strip_prefix("#DEFINE").unwrap().trim();
169 +
170 + if let Some(eq_pos) = rest.find('=') {
171 + let var_name = rest[..eq_pos].trim();
172 + let value = rest[eq_pos + 1..].trim().trim_end_matches(',');
173 +
174 + if !var_name.starts_with('$') {
175 + return Err(ConfigError::InvalidVariableName(var_name.to_string()));
176 + }
177 +
178 + variables.insert(var_name.to_string(), value.to_string());
179 + } else {
180 + return Err(ConfigError::InvalidDefine(trimmed.to_string()));
181 + }
182 +
183 + result.push('\n');
184 + } else {
185 + let mut processed_line = line.to_string();
186 + for (var_name, value) in &variables {
187 + processed_line = processed_line.replace(var_name, value);
188 + }
189 + result.push_str(&processed_line);
190 + result.push('\n');
191 + }
192 + }
193 +
194 + for line in result.lines() {
195 + if let Some(var_start) = line.find('$') {
196 + let rest = &line[var_start..];
197 + let var_end = rest[1..]
198 + .find(|c: char| !c.is_alphanumeric() && c != '_')
199 + .unwrap_or(rest.len() - 1)
200 + + 1;
201 + let undefined_var = &rest[..var_end];
202 + return Err(ConfigError::UndefinedVariable(undefined_var.to_string()));
203 + }
204 + }
205 + Ok(result)
206 +}
207 +
159 pub fn parse_config(input: &str) -> Result<crate::Config, ConfigError> { 208 pub fn parse_config(input: &str) -> Result<crate::Config, ConfigError> {
160 - let config_data: ConfigData = ron::from_str(input)?; 209 + let preprocessed = preprocess_variables(input)?;
210 + let config_data: ConfigData = ron::from_str(&preprocessed)?;
161 config_data_to_config(config_data) 211 config_data_to_config(config_data)
162 } 212 }
163 213
+12 −0 src/errors.rs
@@ -25,6 +25,9 @@ pub enum ConfigError {
25 UnknownAction(String), 25 UnknownAction(String),
26 UnknownBlockCommand(String), 26 UnknownBlockCommand(String),
27 MissingCommandArg { command: String, field: String }, 27 MissingCommandArg { command: String, field: String },
28 + InvalidVariableName(String),
29 + InvalidDefine(String),
30 + UndefinedVariable(String),
28 } 31 }
29 32
30 impl std::fmt::Display for WmError { 33 impl std::fmt::Display for WmError {
@@ -67,6 +70,15 @@ impl std::fmt::Display for ConfigError {
67 Self::MissingCommandArg { command, field } => { 70 Self::MissingCommandArg { command, field } => {
68 write!(f, "{} command requires {}", command, field) 71 write!(f, "{} command requires {}", command, field)
69 } 72 }
73 + Self::InvalidVariableName(name) => {
74 + write!(f, "Invalid variable name '{}': must start with $", name)
75 + }
76 + Self::InvalidDefine(line) => {
77 + write!(f, "Invalid #DEFINE syntax: '{}'. Expected: #DEFINE $var_name = value", line)
78 + }
79 + Self::UndefinedVariable(var) => {
80 + write!(f, "Undefined variable '{}': define it with #DEFINE before use", var)
81 + }
70 } 82 }
71 } 83 }
72 } 84 }
+27 −15 templates/config.ron
@@ -2,19 +2,31 @@
2 // OXWM Configuration File 2 // OXWM Configuration File
3 // Edit this file and reload with Mod+Shift+R (no compilation needed!) 3 // Edit this file and reload with Mod+Shift+R (no compilation needed!)
4 4
5 +#DEFINE $terminal = "st"
6 +#DEFINE $color_blue = 0x6dade3
7 +#DEFINE $color_grey = 0xbbbbbb
8 +#DEFINE $color_green = 0x9ece6a
9 +#DEFINE $color_red = 0xf7768e
10 +#DEFINE $color_cyan = 0x0db9d7
11 +#DEFINE $color_purple = 0xad8ee6
12 +#DEFINE $color_lavender = 0xa9b1d6
13 +#DEFINE $color_bg = 0x1a1b26
14 +#DEFINE $color_fg = 0xbbbbbb
15 +#DEFINE $color_light_blue = 0x7aa2f7
16 +
5 ( 17 (
6 border_width: 2, 18 border_width: 2,
7 - border_focused: 0x6dade3, 19 + border_focused: $color_blue,
8 - border_unfocused: 0xbbbbbb, 20 + border_unfocused: $color_grey,
9 font: "monospace:style=Bold:size=10", 21 font: "monospace:style=Bold:size=10",
10 - 22 +
11 gaps_enabled: true, 23 gaps_enabled: true,
12 gap_inner_horizontal: 5, 24 gap_inner_horizontal: 5,
13 gap_inner_vertical: 5, 25 gap_inner_vertical: 5,
14 gap_outer_horizontal: 5, 26 gap_outer_horizontal: 5,
15 gap_outer_vertical: 5, 27 gap_outer_vertical: 5,
16 - 28 +
17 - terminal: "st", 29 + terminal: $terminal,
18 modkey: Mod4, 30 modkey: Mod4,
19 31
20 tags: ["1", "2", "3", "4", "5", "6", "7", "8", "9"], 32 tags: ["1", "2", "3", "4", "5", "6", "7", "8", "9"],
@@ -24,7 +36,7 @@
24 ], 36 ],
25 37
26 keybindings: [ 38 keybindings: [
27 - (modifiers: [Mod4], key: Return, action: Spawn, arg: "st"), 39 + (modifiers: [Mod4], key: Return, action: Spawn, arg: $terminal),
28 (modifiers: [Mod4], key: D, action: Spawn, arg: ["sh", "-c", "dmenu_run -l 10"]), 40 (modifiers: [Mod4], key: D, action: Spawn, arg: ["sh", "-c", "dmenu_run -l 10"]),
29 (modifiers: [Mod4], key: S, action: Spawn, arg: ["sh", "-c", "maim -s | xclip -selection clipboard -t image/png"]), 41 (modifiers: [Mod4], key: S, action: Spawn, arg: ["sh", "-c", "maim -s | xclip -selection clipboard -t image/png"]),
30 (modifiers: [Mod4], key: Q, action: KillClient), 42 (modifiers: [Mod4], key: Q, action: KillClient),
@@ -73,15 +85,15 @@
73 ], 85 ],
74 86
75 status_blocks: [ 87 status_blocks: [
76 - (format: "Ram: {used}/{total} GB", command: "Ram", interval_secs: 5, color: 0x7aa2f7, underline: true), 88 + (format: "Ram: {used}/{total} GB", command: "Ram", interval_secs: 5, color: $color_light_blue, underline: true),
77 - (format: " │ ", command: "Static", interval_secs: 18446744073709551615, color: 0xa9b1d6, underline: false), 89 + (format: " │ ", command: "Static", interval_secs: 18446744073709551615, color: $color_lavender, underline: false),
78 - (format: "Kernel: {}", command: "Shell", command_arg: "uname -r", interval_secs: 18446744073709551615, color: 0xf7768e, underline: true), 90 + (format: "Kernel: {}", command: "Shell", command_arg: "uname -r", interval_secs: 18446744073709551615, color: $color_red, underline: true),
79 - (format: " │ ", command: "Static", interval_secs: 18446744073709551615, color: 0xa9b1d6, underline: false), 91 + (format: " │ ", command: "Static", interval_secs: 18446744073709551615, color: $color_lavender, underline: false),
80 - (format: "{}", command: "DateTime", command_arg: "%a, %b %d - %-I:%M %P", interval_secs: 1, color: 0x0db9d7, underline: true), 92 + (format: "{}", command: "DateTime", command_arg: "%a, %b %d - %-I:%M %P", interval_secs: 1, color: $color_cyan, underline: true),
81 ], 93 ],
82 - 94 +
83 - scheme_normal: (foreground: 0xbbbbbb, background: 0x1a1b26, underline: 0x444444), 95 + scheme_normal: (foreground: $color_fg, background: $color_bg, underline: 0x444444),
84 - scheme_occupied: (foreground: 0x0db9d7, background: 0x1a1b26, underline: 0x0db9d7), 96 + scheme_occupied: (foreground: $color_cyan, background: $color_bg, underline: $color_cyan),
85 - scheme_selected: (foreground: 0x0db9d7, background: 0x1a1b26, underline: 0xad8ee6), 97 + scheme_selected: (foreground: $color_cyan, background: $color_bg, underline: $color_purple),
86 ) 98 )
87 99
+28 −16 test-config.ron
@@ -2,19 +2,31 @@
2 // OXWM Configuration File 2 // OXWM Configuration File
3 // Edit this file and reload with Mod+Shift+R (no compilation needed!) 3 // Edit this file and reload with Mod+Shift+R (no compilation needed!)
4 4
5 +#DEFINE $terminal = "alacritty"
6 +#DEFINE $color_blue = 0x6dade3
7 +#DEFINE $color_grey = 0xbbbbbb
8 +#DEFINE $color_green = 0x9ece6a
9 +#DEFINE $color_red = 0xf7768e
10 +#DEFINE $color_cyan = 0x0db9d7
11 +#DEFINE $color_purple = 0xad8ee6
12 +#DEFINE $color_lavender = 0xa9b1d6
13 +#DEFINE $color_bg = 0x1a1b26
14 +#DEFINE $color_fg = 0xbbbbbb
15 +#DEFINE $color_light_blue = 0x7aa2f7
16 +
5 ( 17 (
6 border_width: 2, 18 border_width: 2,
7 - border_focused: 0x6dade3, 19 + border_focused: $color_blue,
8 - border_unfocused: 0xbbbbbb, 20 + border_unfocused: $color_grey,
9 font: "JetBrainsMono Nerd Font:style=Bold:size=12", 21 font: "JetBrainsMono Nerd Font:style=Bold:size=12",
10 - 22 +
11 gaps_enabled: true, 23 gaps_enabled: true,
12 gap_inner_horizontal: 5, 24 gap_inner_horizontal: 5,
13 gap_inner_vertical: 5, 25 gap_inner_vertical: 5,
14 gap_outer_horizontal: 5, 26 gap_outer_horizontal: 5,
15 gap_outer_vertical: 5, 27 gap_outer_vertical: 5,
16 - 28 +
17 - terminal: "st", 29 + terminal: $terminal,
18 modkey: Mod1, 30 modkey: Mod1,
19 tags: ["1", "2", "3", "4", "5", "6", "7", "8", "9"], 31 tags: ["1", "2", "3", "4", "5", "6", "7", "8", "9"],
20 32
@@ -30,7 +42,7 @@
30 ], 42 ],
31 43
32 keybindings: [ 44 keybindings: [
33 - (modifiers: [Mod1], key: Return, action: Spawn, arg: "st"), 45 + (modifiers: [Mod1], key: Return, action: Spawn, arg: $terminal),
34 (modifiers: [Mod1], key: D, action: Spawn, arg: ["sh", "-c", "dmenu_run -l 10"]), 46 (modifiers: [Mod1], key: D, action: Spawn, arg: ["sh", "-c", "dmenu_run -l 10"]),
35 (modifiers: [Mod1], key: S, action: Spawn, arg: ["sh", "-c", "maim -s | xclip -selection clipboard -t image/png"]), 47 (modifiers: [Mod1], key: S, action: Spawn, arg: ["sh", "-c", "maim -s | xclip -selection clipboard -t image/png"]),
36 (modifiers: [Mod1], key: Q, action: KillClient), 48 (modifiers: [Mod1], key: Q, action: KillClient),
@@ -84,7 +96,7 @@
84 full: "󰁹 Bat: {}%", 96 full: "󰁹 Bat: {}%",
85 ), 97 ),
86 interval_secs: 30, 98 interval_secs: 30,
87 - color: 0x9ece6a, 99 + color: $color_green,
88 underline: true, 100 underline: true,
89 ), 101 ),
90 102
@@ -93,7 +105,7 @@
93 format: " │ ", 105 format: " │ ",
94 command: "Static", 106 command: "Static",
95 interval_secs: 18446744073709551615, 107 interval_secs: 18446744073709551615,
96 - color: 0xa9b1d6, 108 + color: $color_lavender,
97 underline: false, 109 underline: false,
98 ), 110 ),
99 111
@@ -102,7 +114,7 @@
102 format: "󰍛 {used}/{total} GB", 114 format: "󰍛 {used}/{total} GB",
103 command: "Ram", 115 command: "Ram",
104 interval_secs: 5, 116 interval_secs: 5,
105 - color: 0x7aa2f7, 117 + color: $color_light_blue,
106 underline: true, 118 underline: true,
107 ), 119 ),
108 120
@@ -111,7 +123,7 @@
111 format: " │ ", 123 format: " │ ",
112 command: "Static", 124 command: "Static",
113 interval_secs: 18446744073709551615, 125 interval_secs: 18446744073709551615,
114 - color: 0xa9b1d6, 126 + color: $color_lavender,
115 underline: false, 127 underline: false,
116 ), 128 ),
117 129
@@ -121,7 +133,7 @@
121 command: "Shell", 133 command: "Shell",
122 command_arg: "uname -r", 134 command_arg: "uname -r",
123 interval_secs: 18446744073709551615, 135 interval_secs: 18446744073709551615,
124 - color: 0xf7768e, 136 + color: $color_red,
125 underline: true, 137 underline: true,
126 ), 138 ),
127 139
@@ -130,7 +142,7 @@
130 format: " │ ", 142 format: " │ ",
131 command: "Static", 143 command: "Static",
132 interval_secs: 18446744073709551615, 144 interval_secs: 18446744073709551615,
133 - color: 0xa9b1d6, 145 + color: $color_lavender,
134 underline: false, 146 underline: false,
135 ), 147 ),
136 148
@@ -140,13 +152,13 @@
140 command: "DateTime", 152 command: "DateTime",
141 command_arg: "%a, %b %d - %-I:%M %P", 153 command_arg: "%a, %b %d - %-I:%M %P",
142 interval_secs: 1, 154 interval_secs: 1,
143 - color: 0x0db9d7, 155 + color: $color_cyan,
144 underline: true, 156 underline: true,
145 ), 157 ),
146 ], 158 ],
147 159
148 - scheme_normal: (foreground: 0xbbbbbb, background: 0x1a1b26, underline: 0x444444), 160 + scheme_normal: (foreground: $color_fg, background: $color_bg, underline: 0x444444),
149 - scheme_occupied: (foreground: 0x0db9d7, background: 0x1a1b26, underline: 0x0db9d7), 161 + scheme_occupied: (foreground: $color_cyan, background: $color_bg, underline: $color_cyan),
150 - scheme_selected: (foreground: 0x0db9d7, background: 0x1a1b26, underline: 0xad8ee6), 162 + scheme_selected: (foreground: $color_cyan, background: $color_bg, underline: $color_purple),
151 ) 163 )
152 164