diff options
| author | giskard <rtgiskard@gmail.com> | 2024-05-29 05:37:24 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-28 23:37:24 +0200 |
| commit | ebf258788e46cd0320fb17138ba413f719180418 (patch) | |
| tree | d7b96e24caab26cb3d46b2857eebf90ad475b2a9 /src/helpers | |
| parent | 73b133d0155d376f35833cd992f821399a42f76b (diff) | |
config: add tag dispacther and window rule (#6211)
Diffstat (limited to 'src/helpers')
| -rw-r--r-- | src/helpers/TagKeeper.cpp | 40 | ||||
| -rw-r--r-- | src/helpers/TagKeeper.hpp | 18 |
2 files changed, 58 insertions, 0 deletions
diff --git a/src/helpers/TagKeeper.cpp b/src/helpers/TagKeeper.cpp new file mode 100644 index 00000000..f960accb --- /dev/null +++ b/src/helpers/TagKeeper.cpp @@ -0,0 +1,40 @@ +#include "TagKeeper.hpp" + +bool CTagKeeper::isTagged(const std::string& tag, bool strict) { + return m_tags.contains(tag) || (!strict && m_tags.contains(tag + "*")); +} + +bool CTagKeeper::applyTag(const std::string& tag, bool dynamic) { + + std::string tagReal = tag; + + if (dynamic && !tag.ends_with("*")) + tagReal += "*"; + + bool changed = true; + bool setTag = true; + + if (tagReal.starts_with("-")) { // unset + tagReal = tagReal.substr(1); + changed = isTagged(tagReal, true); + setTag = false; + } else if (tagReal.starts_with("+")) { // set + tagReal = tagReal.substr(1); + changed = !isTagged(tagReal, true); + } else // toggle if without prefix + setTag = !isTagged(tagReal, true); + + if (!changed) + return false; + + if (setTag) + m_tags.emplace(tagReal); + else + m_tags.erase(tagReal); + + return true; +} + +bool CTagKeeper::removeDynamicTags() { + return std::erase_if(m_tags, [](const auto& tag) { return tag.ends_with("*"); }); +} diff --git a/src/helpers/TagKeeper.hpp b/src/helpers/TagKeeper.hpp new file mode 100644 index 00000000..deeabf48 --- /dev/null +++ b/src/helpers/TagKeeper.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include <string> +#include <set> + +class CTagKeeper { + public: + bool isTagged(const std::string& tag, bool strict = false); + bool applyTag(const std::string& tag, bool dynamic = false); + bool removeDynamicTags(); + + inline const auto& getTags() { + return m_tags; + }; + + private: + std::set<std::string> m_tags; +}; |
