awful.rules: add does_match and matching_rules functions (FS#1224)
Signed-off-by: Daniel Hahler <git@thequod.de> Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
parent
a15e53deec
commit
d9aed1b21e
|
@ -105,7 +105,7 @@ local rules = {}
|
||||||
-- @name rules
|
-- @name rules
|
||||||
rules.rules = {}
|
rules.rules = {}
|
||||||
|
|
||||||
--- Check if a client match a rule.
|
--- Check if a client matches a rule.
|
||||||
-- @param c The client.
|
-- @param c The client.
|
||||||
-- @param rule The rule to check.
|
-- @param rule The rule to check.
|
||||||
-- @return True if it matches, false otherwise.
|
-- @return True if it matches, false otherwise.
|
||||||
|
@ -127,7 +127,7 @@ function rules.match(c, rule)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Check if a client match a rule. Multiple clients can be matched
|
--- Check if a client matches any part of a rule.
|
||||||
-- @param c The client.
|
-- @param c The client.
|
||||||
-- @param rule The rule to check.
|
-- @param rule The rule to check.
|
||||||
-- @return True if at least one rule is matched, false otherwise.
|
-- @return True if at least one rule is matched, false otherwise.
|
||||||
|
@ -147,23 +147,47 @@ function rules.match_any(c, rule)
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Get list of matching rules for a client.
|
||||||
|
-- @param c The client.
|
||||||
|
-- @param rule The rules to check. List with "rule", "rule_any", "except" and
|
||||||
|
-- "except_any" keys.
|
||||||
|
-- @return The list of matched rules.
|
||||||
|
function rules.matching_rules(c, _rules)
|
||||||
|
local result = {}
|
||||||
|
for _, entry in ipairs(_rules) do
|
||||||
|
if (rules.match(c, entry.rule) or rules.match_any(c, entry.rule_any)) and
|
||||||
|
(not rules.match(c, entry.except) and not rules.match_any(c, entry.except_any)) then
|
||||||
|
table.insert(result, entry)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Check if a client matches a given set of rules.
|
||||||
|
-- @param c The client.
|
||||||
|
-- @param rule The rules to check. List with "rule", "rule_any", "except" and
|
||||||
|
-- "except_any" keys.
|
||||||
|
-- @return True if at least one rule is matched, false otherwise.
|
||||||
|
function rules.does_match(c, rules)
|
||||||
|
local result = rules.matching_rules(c, rules)
|
||||||
|
return #result == 0 and false or result
|
||||||
|
end
|
||||||
|
|
||||||
--- Apply rules to a client.
|
--- Apply rules to a client.
|
||||||
-- @param c The client.
|
-- @param c The client.
|
||||||
function rules.apply(c)
|
function rules.apply(c)
|
||||||
local props = {}
|
local props = {}
|
||||||
local callbacks = {}
|
local callbacks = {}
|
||||||
for _, entry in ipairs(rules.rules) do
|
|
||||||
if (rules.match(c, entry.rule) or rules.match_any(c, entry.rule_any)) and
|
for _, entry in ipairs(rules.matching_rules(c, rules.rules)) do
|
||||||
(not rules.match(c, entry.except) and not rules.match_any(c, entry.except_any)) then
|
if entry.properties then
|
||||||
if entry.properties then
|
for property, value in pairs(entry.properties) do
|
||||||
for property, value in pairs(entry.properties) do
|
props[property] = value
|
||||||
props[property] = value
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if entry.callback then
|
|
||||||
table.insert(callbacks, entry.callback)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
if entry.callback then
|
||||||
|
table.insert(callbacks, entry.callback)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
for property, value in pairs(props) do
|
for property, value in pairs(props) do
|
||||||
|
|
Loading…
Reference in New Issue