summaryrefslogtreecommitdiff
path: root/src/helpers/TagKeeper.cpp
blob: 80bf3957da895e6f7bc397905e956e3cf0f06615 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "TagKeeper.hpp"

bool CTagKeeper::isTagged(const std::string& tag, bool strict) const {
    const bool NEGATIVE = tag.starts_with("negative");
    const auto MATCH    = NEGATIVE ? tag.substr(9) : tag;
    const bool TAGGED   = m_tags.contains(MATCH) || (!strict && m_tags.contains(MATCH + "*"));
    return NEGATIVE ? !TAGGED : TAGGED;
}

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::clearTags() {
    if (!m_tags.empty()) {
        m_tags.clear();
        return true;
    }

    return false;
}

bool CTagKeeper::removeDynamicTag(const std::string& s) {
    return std::erase_if(m_tags, [&s](const auto& tag) { return tag == s + "*"; });
}