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},
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<HlwmCommand> {
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()
}

View File

@ -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<String>),
/// Removes all rules either with the given label, or all of them
/// (See [Unrule])
Unrule(Unrule),
}
impl FromStr for Box<HlwmCommand> {
@ -573,6 +576,7 @@ impl HlwmCommand {
})
}
HlwmCommand::Sprintf(_) => parse!([Vec<String>] => 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::<Vec<_>>()
.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::<Vec<_>>();
for (command, expected_string) in commands {

View File

@ -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<Self, Self::Err> {
match s {
"--all" | "-F" => Ok(Self::All),
_ => Ok(Self::Rule(s.to_string())),
}
}
}
#[cfg(test)]
mod test {
use serde::{Deserialize, Serialize};