From aaf053e070c053bbad4526fcc69585e30ea07698 Mon Sep 17 00:00:00 2001 From: emilis Date: Fri, 1 Mar 2024 19:50:32 +0000 Subject: [PATCH] add unrule --- src/config.rs | 8 +++----- src/hlwm/command.rs | 11 ++++++++++- src/hlwm/rule.rs | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/config.rs b/src/config.rs index 5e9976e..e23b7b4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -27,7 +27,7 @@ use crate::{ command::{CommandError, HlwmCommand}, hook::Hook, key::{Key, KeyParseError, KeyUnbind, Keybind, MouseButton, Mousebind, MousebindAction}, - rule::{Condition, Consequence, FloatPlacement, Rule}, + rule::{Condition, Consequence, FloatPlacement, Rule, Unrule}, setting::{FrameLayout, Setting, ShowFrameDecoration, SmartFrameSurroundings}, theme::ThemeAttr, window::Window, @@ -298,11 +298,9 @@ impl Config { } fn rule_command_set(&self) -> Vec { - info!("loading rule command set"); - self.rules - .clone() + [HlwmCommand::Unrule(Unrule::All)] .into_iter() - .map(|r| HlwmCommand::Rule(r)) + .chain(self.rules.clone().into_iter().map(|r| HlwmCommand::Rule(r))) .collect() } diff --git a/src/hlwm/command.rs b/src/hlwm/command.rs index 0a8faf8..c760c9b 100644 --- a/src/hlwm/command.rs +++ b/src/hlwm/command.rs @@ -12,7 +12,7 @@ use super::{ hook::Hook, key::{KeyUnbind, Keybind, Mousebind}, pad::Pad, - rule::Rule, + rule::{Rule, Unrule}, setting::{FrameLayout, Setting, SettingName}, split, window::Window, @@ -199,6 +199,9 @@ pub enum HlwmCommand { }, #[strum(serialize = "sprintf")] Sprintf(Vec), + /// Removes all rules either with the given label, or all of them + /// (See [Unrule]) + Unrule(Unrule), } impl FromStr for Box { @@ -573,6 +576,7 @@ impl HlwmCommand { }) } HlwmCommand::Sprintf(_) => parse!([Vec] => Sprintf), + HlwmCommand::Unrule(_) => parse!(FromStr => Unrule), }?; assert_eq!(command.to_string(), parsed_command.to_string()); @@ -833,6 +837,7 @@ impl ToCommandString for HlwmCommand { .chain(args.into_iter().cloned()) .collect::>() .join("\t"), + HlwmCommand::Unrule(unrule) => format!("{self}\t{unrule}"), }; if let Some(s) = cmd_string.strip_suffix('\t') { return s.to_string(); @@ -1149,6 +1154,10 @@ mod test { ), "sprintf\tX\ttag=%s\ttags.focus.name\trule\tonce\tX".into(), ), + HlwmCommand::Unrule(_) => ( + HlwmCommand::Unrule(super::Unrule::All), + "unrule\t--all".into(), + ), }) .collect::>(); for (command, expected_string) in commands { diff --git a/src/hlwm/rule.rs b/src/hlwm/rule.rs index c7c2b0d..cdba58b 100644 --- a/src/hlwm/rule.rs +++ b/src/hlwm/rule.rs @@ -1,5 +1,6 @@ use std::{ borrow::BorrowMut, + convert::Infallible, fmt::{Display, Write}, str::FromStr, }; @@ -634,6 +635,38 @@ macro_rules! rule { } } +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum Unrule { + All, + Rule(String), +} + +impl Display for Unrule { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Unrule::All => f.write_str("--all"), + Unrule::Rule(rule) => f.write_str(&rule), + } + } +} + +impl Default for Unrule { + fn default() -> Self { + Unrule::All + } +} + +impl FromStr for Unrule { + type Err = Infallible; + + fn from_str(s: &str) -> Result { + match s { + "--all" | "-F" => Ok(Self::All), + _ => Ok(Self::Rule(s.to_string())), + } + } +} + #[cfg(test)] mod test { use serde::{Deserialize, Serialize};