add unrule

This commit is contained in:
emilis 2024-03-01 19:50:32 +00:00
parent 22925fc738
commit aaf053e070
3 changed files with 46 additions and 6 deletions

View File

@ -27,7 +27,7 @@ use crate::{
command::{CommandError, HlwmCommand}, command::{CommandError, HlwmCommand},
hook::Hook, hook::Hook,
key::{Key, KeyParseError, KeyUnbind, Keybind, MouseButton, Mousebind, MousebindAction}, key::{Key, KeyParseError, KeyUnbind, Keybind, MouseButton, Mousebind, MousebindAction},
rule::{Condition, Consequence, FloatPlacement, Rule}, rule::{Condition, Consequence, FloatPlacement, Rule, Unrule},
setting::{FrameLayout, Setting, ShowFrameDecoration, SmartFrameSurroundings}, setting::{FrameLayout, Setting, ShowFrameDecoration, SmartFrameSurroundings},
theme::ThemeAttr, theme::ThemeAttr,
window::Window, window::Window,
@ -298,11 +298,9 @@ impl Config {
} }
fn rule_command_set(&self) -> Vec<HlwmCommand> { fn rule_command_set(&self) -> Vec<HlwmCommand> {
info!("loading rule command set"); [HlwmCommand::Unrule(Unrule::All)]
self.rules
.clone()
.into_iter() .into_iter()
.map(|r| HlwmCommand::Rule(r)) .chain(self.rules.clone().into_iter().map(|r| HlwmCommand::Rule(r)))
.collect() .collect()
} }

View File

@ -12,7 +12,7 @@ use super::{
hook::Hook, hook::Hook,
key::{KeyUnbind, Keybind, Mousebind}, key::{KeyUnbind, Keybind, Mousebind},
pad::Pad, pad::Pad,
rule::Rule, rule::{Rule, Unrule},
setting::{FrameLayout, Setting, SettingName}, setting::{FrameLayout, Setting, SettingName},
split, split,
window::Window, window::Window,
@ -199,6 +199,9 @@ pub enum HlwmCommand {
}, },
#[strum(serialize = "sprintf")] #[strum(serialize = "sprintf")]
Sprintf(Vec<String>), Sprintf(Vec<String>),
/// Removes all rules either with the given label, or all of them
/// (See [Unrule])
Unrule(Unrule),
} }
impl FromStr for Box<HlwmCommand> { impl FromStr for Box<HlwmCommand> {
@ -573,6 +576,7 @@ impl HlwmCommand {
}) })
} }
HlwmCommand::Sprintf(_) => parse!([Vec<String>] => Sprintf), HlwmCommand::Sprintf(_) => parse!([Vec<String>] => Sprintf),
HlwmCommand::Unrule(_) => parse!(FromStr => Unrule),
}?; }?;
assert_eq!(command.to_string(), parsed_command.to_string()); assert_eq!(command.to_string(), parsed_command.to_string());
@ -833,6 +837,7 @@ impl ToCommandString for HlwmCommand {
.chain(args.into_iter().cloned()) .chain(args.into_iter().cloned())
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join("\t"), .join("\t"),
HlwmCommand::Unrule(unrule) => format!("{self}\t{unrule}"),
}; };
if let Some(s) = cmd_string.strip_suffix('\t') { if let Some(s) = cmd_string.strip_suffix('\t') {
return s.to_string(); return s.to_string();
@ -1149,6 +1154,10 @@ mod test {
), ),
"sprintf\tX\ttag=%s\ttags.focus.name\trule\tonce\tX".into(), "sprintf\tX\ttag=%s\ttags.focus.name\trule\tonce\tX".into(),
), ),
HlwmCommand::Unrule(_) => (
HlwmCommand::Unrule(super::Unrule::All),
"unrule\t--all".into(),
),
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
for (command, expected_string) in commands { for (command, expected_string) in commands {

View File

@ -1,5 +1,6 @@
use std::{ use std::{
borrow::BorrowMut, borrow::BorrowMut,
convert::Infallible,
fmt::{Display, Write}, fmt::{Display, Write},
str::FromStr, 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<Self, Self::Err> {
match s {
"--all" | "-F" => Ok(Self::All),
_ => Ok(Self::Rule(s.to_string())),
}
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};