awful.rules: factor out `matches`
This factors out `matches` and uses it in `matches_list` (renamed from `does_match`) to short-circuit the successful case - where not all rules have to get checked. Closes https://github.com/awesomeWM/awesome/pull/431.
This commit is contained in:
parent
285f24d234
commit
a251331683
|
@ -130,6 +130,16 @@ function rules.match_any(c, rule)
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Does a given rule entry match a client?
|
||||||
|
-- @client c The client.
|
||||||
|
-- @tab entry Rule entry (with keys `rule`, `rule_any`, `except` and/or
|
||||||
|
-- `except_any`).
|
||||||
|
-- @treturn bool
|
||||||
|
function rules.matches(c, entry)
|
||||||
|
return (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))
|
||||||
|
end
|
||||||
|
|
||||||
--- Get list of matching rules for a client.
|
--- Get list of matching rules for a client.
|
||||||
-- @client c The client.
|
-- @client c The client.
|
||||||
-- @tab _rules The rules to check. List with "rule", "rule_any", "except" and
|
-- @tab _rules The rules to check. List with "rule", "rule_any", "except" and
|
||||||
|
@ -138,8 +148,7 @@ end
|
||||||
function rules.matching_rules(c, _rules)
|
function rules.matching_rules(c, _rules)
|
||||||
local result = {}
|
local result = {}
|
||||||
for _, entry in ipairs(_rules) do
|
for _, entry in ipairs(_rules) do
|
||||||
if (rules.match(c, entry.rule) or rules.match_any(c, entry.rule_any)) and
|
if (rules.matches(c, entry)) then
|
||||||
(not rules.match(c, entry.except) and not rules.match_any(c, entry.except_any)) then
|
|
||||||
table.insert(result, entry)
|
table.insert(result, entry)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -148,12 +157,16 @@ end
|
||||||
|
|
||||||
--- Check if a client matches a given set of rules.
|
--- Check if a client matches a given set of rules.
|
||||||
-- @client c The client.
|
-- @client c The client.
|
||||||
-- @tab _rules The rules to check. List with "rule", "rule_any", "except" and
|
-- @tab _rules The rules to check. List of tables with `rule`, `rule_any`,
|
||||||
-- "except_any" keys.
|
-- `except` and `except_any` keys.
|
||||||
-- @treturn bool True if at least one rule is matched, false otherwise.
|
-- @treturn bool True if at least one rule is matched, false otherwise.
|
||||||
function rules.does_match(c, _rules)
|
function rules.matches_list(c, _rules)
|
||||||
local result = rules.matching_rules(c, _rules)
|
for _, entry in ipairs(_rules) do
|
||||||
return #result == 0 and false or result
|
if (rules.matches(c, entry)) then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Apply awful.rules.rules to a client.
|
--- Apply awful.rules.rules to a client.
|
||||||
|
|
Loading…
Reference in New Issue