Cleaned up consts to enum
Browse files
+51 −1 src/config/mod.rs
| @@ -4,6 +4,7 @@ use crate::keyboard::handlers::Key; | ||
| 4 | 4 | use crate::keyboard::keycodes; |
| 5 | 5 | use crate::keyboard::{Arg, KeyAction}; |
| 6 | 6 | use serde::Deserialize; |
| 7 | +use std::collections::HashMap; | |
| 7 | 8 | use x11rb::protocol::xproto::{KeyButMask, Keycode}; |
| 8 | 9 | |
| 9 | 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 | 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 | 211 | config_data_to_config(config_data) |
| 162 | 212 | } |
| 163 | 213 |
+12 −0 src/errors.rs
| @@ -25,6 +25,9 @@ pub enum ConfigError { | ||
| 25 | 25 | UnknownAction(String), |
| 26 | 26 | UnknownBlockCommand(String), |
| 27 | 27 | MissingCommandArg { command: String, field: String }, |
| 28 | + InvalidVariableName(String), | |
| 29 | + InvalidDefine(String), | |
| 30 | + UndefinedVariable(String), | |
| 28 | 31 | } |
| 29 | 32 | |
| 30 | 33 | impl std::fmt::Display for WmError { |
| @@ -67,6 +70,15 @@ impl std::fmt::Display for ConfigError { | ||
| 67 | 70 | Self::MissingCommandArg { command, field } => { |
| 68 | 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 | 2 | // OXWM Configuration File |
| 3 | 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 | 18 | border_width: 2, |
| 7 | - border_focused: 0x6dade3, | |
| 8 | - border_unfocused: 0xbbbbbb, | |
| 19 | + border_focused: $color_blue, | |
| 20 | + border_unfocused: $color_grey, | |
| 9 | 21 | font: "monospace:style=Bold:size=10", |
| 10 | - | |
| 22 | + | |
| 11 | 23 | gaps_enabled: true, |
| 12 | 24 | gap_inner_horizontal: 5, |
| 13 | 25 | gap_inner_vertical: 5, |
| 14 | 26 | gap_outer_horizontal: 5, |
| 15 | 27 | gap_outer_vertical: 5, |
| 16 | - | |
| 17 | - terminal: "st", | |
| 28 | + | |
| 29 | + terminal: $terminal, | |
| 18 | 30 | modkey: Mod4, |
| 19 | 31 | |
| 20 | 32 | tags: ["1", "2", "3", "4", "5", "6", "7", "8", "9"], |
| @@ -24,7 +36,7 @@ | ||
| 24 | 36 | ], |
| 25 | 37 | |
| 26 | 38 | keybindings: [ |
| 27 | - (modifiers: [Mod4], key: Return, action: Spawn, arg: "st"), | |
| 39 | + (modifiers: [Mod4], key: Return, action: Spawn, arg: $terminal), | |
| 28 | 40 | (modifiers: [Mod4], key: D, action: Spawn, arg: ["sh", "-c", "dmenu_run -l 10"]), |
| 29 | 41 | (modifiers: [Mod4], key: S, action: Spawn, arg: ["sh", "-c", "maim -s | xclip -selection clipboard -t image/png"]), |
| 30 | 42 | (modifiers: [Mod4], key: Q, action: KillClient), |
| @@ -73,15 +85,15 @@ | ||
| 73 | 85 | ], |
| 74 | 86 | |
| 75 | 87 | status_blocks: [ |
| 76 | - (format: "Ram: {used}/{total} GB", command: "Ram", interval_secs: 5, color: 0x7aa2f7, underline: true), | |
| 77 | - (format: " │ ", command: "Static", interval_secs: 18446744073709551615, color: 0xa9b1d6, underline: false), | |
| 78 | - (format: "Kernel: {}", command: "Shell", command_arg: "uname -r", interval_secs: 18446744073709551615, color: 0xf7768e, underline: true), | |
| 79 | - (format: " │ ", command: "Static", interval_secs: 18446744073709551615, color: 0xa9b1d6, underline: false), | |
| 80 | - (format: "{}", command: "DateTime", command_arg: "%a, %b %d - %-I:%M %P", interval_secs: 1, color: 0x0db9d7, underline: true), | |
| 88 | + (format: "Ram: {used}/{total} GB", command: "Ram", interval_secs: 5, color: $color_light_blue, underline: true), | |
| 89 | + (format: " │ ", command: "Static", interval_secs: 18446744073709551615, color: $color_lavender, underline: false), | |
| 90 | + (format: "Kernel: {}", command: "Shell", command_arg: "uname -r", interval_secs: 18446744073709551615, color: $color_red, underline: true), | |
| 91 | + (format: " │ ", command: "Static", interval_secs: 18446744073709551615, color: $color_lavender, underline: false), | |
| 92 | + (format: "{}", command: "DateTime", command_arg: "%a, %b %d - %-I:%M %P", interval_secs: 1, color: $color_cyan, underline: true), | |
| 81 | 93 | ], |
| 82 | - | |
| 83 | - scheme_normal: (foreground: 0xbbbbbb, background: 0x1a1b26, underline: 0x444444), | |
| 84 | - scheme_occupied: (foreground: 0x0db9d7, background: 0x1a1b26, underline: 0x0db9d7), | |
| 85 | - scheme_selected: (foreground: 0x0db9d7, background: 0x1a1b26, underline: 0xad8ee6), | |
| 94 | + | |
| 95 | + scheme_normal: (foreground: $color_fg, background: $color_bg, underline: 0x444444), | |
| 96 | + scheme_occupied: (foreground: $color_cyan, background: $color_bg, underline: $color_cyan), | |
| 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 | 2 | // OXWM Configuration File |
| 3 | 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 | 18 | border_width: 2, |
| 7 | - border_focused: 0x6dade3, | |
| 8 | - border_unfocused: 0xbbbbbb, | |
| 19 | + border_focused: $color_blue, | |
| 20 | + border_unfocused: $color_grey, | |
| 9 | 21 | font: "JetBrainsMono Nerd Font:style=Bold:size=12", |
| 10 | - | |
| 22 | + | |
| 11 | 23 | gaps_enabled: true, |
| 12 | 24 | gap_inner_horizontal: 5, |
| 13 | 25 | gap_inner_vertical: 5, |
| 14 | 26 | gap_outer_horizontal: 5, |
| 15 | 27 | gap_outer_vertical: 5, |
| 16 | - | |
| 17 | - terminal: "st", | |
| 28 | + | |
| 29 | + terminal: $terminal, | |
| 18 | 30 | modkey: Mod1, |
| 19 | 31 | tags: ["1", "2", "3", "4", "5", "6", "7", "8", "9"], |
| 20 | 32 | |
| @@ -30,7 +42,7 @@ | ||
| 30 | 42 | ], |
| 31 | 43 | |
| 32 | 44 | keybindings: [ |
| 33 | - (modifiers: [Mod1], key: Return, action: Spawn, arg: "st"), | |
| 45 | + (modifiers: [Mod1], key: Return, action: Spawn, arg: $terminal), | |
| 34 | 46 | (modifiers: [Mod1], key: D, action: Spawn, arg: ["sh", "-c", "dmenu_run -l 10"]), |
| 35 | 47 | (modifiers: [Mod1], key: S, action: Spawn, arg: ["sh", "-c", "maim -s | xclip -selection clipboard -t image/png"]), |
| 36 | 48 | (modifiers: [Mod1], key: Q, action: KillClient), |
| @@ -84,7 +96,7 @@ | ||
| 84 | 96 | full: " Bat: {}%", |
| 85 | 97 | ), |
| 86 | 98 | interval_secs: 30, |
| 87 | - color: 0x9ece6a, | |
| 99 | + color: $color_green, | |
| 88 | 100 | underline: true, |
| 89 | 101 | ), |
| 90 | 102 | |
| @@ -93,7 +105,7 @@ | ||
| 93 | 105 | format: " │ ", |
| 94 | 106 | command: "Static", |
| 95 | 107 | interval_secs: 18446744073709551615, |
| 96 | - color: 0xa9b1d6, | |
| 108 | + color: $color_lavender, | |
| 97 | 109 | underline: false, |
| 98 | 110 | ), |
| 99 | 111 | |
| @@ -102,7 +114,7 @@ | ||
| 102 | 114 | format: " {used}/{total} GB", |
| 103 | 115 | command: "Ram", |
| 104 | 116 | interval_secs: 5, |
| 105 | - color: 0x7aa2f7, | |
| 117 | + color: $color_light_blue, | |
| 106 | 118 | underline: true, |
| 107 | 119 | ), |
| 108 | 120 | |
| @@ -111,7 +123,7 @@ | ||
| 111 | 123 | format: " │ ", |
| 112 | 124 | command: "Static", |
| 113 | 125 | interval_secs: 18446744073709551615, |
| 114 | - color: 0xa9b1d6, | |
| 126 | + color: $color_lavender, | |
| 115 | 127 | underline: false, |
| 116 | 128 | ), |
| 117 | 129 | |
| @@ -121,7 +133,7 @@ | ||
| 121 | 133 | command: "Shell", |
| 122 | 134 | command_arg: "uname -r", |
| 123 | 135 | interval_secs: 18446744073709551615, |
| 124 | - color: 0xf7768e, | |
| 136 | + color: $color_red, | |
| 125 | 137 | underline: true, |
| 126 | 138 | ), |
| 127 | 139 | |
| @@ -130,7 +142,7 @@ | ||
| 130 | 142 | format: " │ ", |
| 131 | 143 | command: "Static", |
| 132 | 144 | interval_secs: 18446744073709551615, |
| 133 | - color: 0xa9b1d6, | |
| 145 | + color: $color_lavender, | |
| 134 | 146 | underline: false, |
| 135 | 147 | ), |
| 136 | 148 | |
| @@ -140,13 +152,13 @@ | ||
| 140 | 152 | command: "DateTime", |
| 141 | 153 | command_arg: "%a, %b %d - %-I:%M %P", |
| 142 | 154 | interval_secs: 1, |
| 143 | - color: 0x0db9d7, | |
| 155 | + color: $color_cyan, | |
| 144 | 156 | underline: true, |
| 145 | 157 | ), |
| 146 | 158 | ], |
| 147 | 159 | |
| 148 | - scheme_normal: (foreground: 0xbbbbbb, background: 0x1a1b26, underline: 0x444444), | |
| 149 | - scheme_occupied: (foreground: 0x0db9d7, background: 0x1a1b26, underline: 0x0db9d7), | |
| 150 | - scheme_selected: (foreground: 0x0db9d7, background: 0x1a1b26, underline: 0xad8ee6), | |
| 160 | + scheme_normal: (foreground: $color_fg, background: $color_bg, underline: 0x444444), | |
| 161 | + scheme_occupied: (foreground: $color_cyan, background: $color_bg, underline: $color_cyan), | |
| 162 | + scheme_selected: (foreground: $color_cyan, background: $color_bg, underline: $color_purple), | |
| 151 | 163 | ) |
| 152 | 164 |