mirror of
https://github.com/mpv-player/mpv
synced 2024-11-14 22:48:35 +01:00
auto_profiles.lua: apply profiles when conditions are truthy
Instead of erroring when values returned by profile-cond expressions aren't booleans, apply the relative profiles as long as the return values are truthy. This allows shortening conditions like profile-cond=path:match('foo') ~= nil to profile-cond=path:match('foo')
This commit is contained in:
parent
b7ffe0d16e
commit
41372c5e1f
@ -793,9 +793,10 @@ Conditional auto profiles
|
|||||||
|
|
||||||
Profiles which have the ``profile-cond`` option set are applied automatically
|
Profiles which have the ``profile-cond`` option set are applied automatically
|
||||||
if the associated condition matches (unless auto profiles are disabled). The
|
if the associated condition matches (unless auto profiles are disabled). The
|
||||||
option takes a string, which is interpreted as Lua condition. If evaluating the
|
option takes a string, which is interpreted as Lua expression. If the
|
||||||
expression returns true, the profile is applied, if it returns false, it is
|
expression evaluates as truthy, the profile is applied. If the expression
|
||||||
ignored. This Lua code execution is not sandboxed.
|
errors or evaluates as falsy, the profile is not applied. This Lua code
|
||||||
|
execution is not sandboxed.
|
||||||
|
|
||||||
Any variables in condition expressions can reference properties. If an
|
Any variables in condition expressions can reference properties. If an
|
||||||
identifier is not already defined by Lua or mpv, it is interpreted as property.
|
identifier is not already defined by Lua or mpv, it is interpreted as property.
|
||||||
@ -816,13 +817,13 @@ cause errors if used in expressions. These are logged in verbose mode, and the
|
|||||||
expression is considered to be false.
|
expression is considered to be false.
|
||||||
|
|
||||||
Whenever a property referenced by a profile condition changes, the condition
|
Whenever a property referenced by a profile condition changes, the condition
|
||||||
is re-evaluated. If the return value of the condition changes from false or
|
is re-evaluated. If the return value of the condition changes from falsy or
|
||||||
error to true, the profile is applied.
|
error to truthy, the profile is applied.
|
||||||
|
|
||||||
This mechanism tries to "unapply" profiles once the condition changes from true
|
This mechanism tries to "unapply" profiles once the condition changes from
|
||||||
to false. If you want to use this, you need to set ``profile-restore`` for the
|
truthy to falsy or error. If you want to use this, you need to set
|
||||||
profile. Another possibility it to create another profile with an inverse
|
``profile-restore`` for the profile. Another possibility it to create another
|
||||||
condition to undo the other profile.
|
profile with an inverse condition to undo the other profile.
|
||||||
|
|
||||||
Recursive profiles can be used. But it is discouraged to reference other
|
Recursive profiles can be used. But it is discouraged to reference other
|
||||||
conditional profiles in a conditional profile, since this can lead to tricky
|
conditional profiles in a conditional profile, since this can lead to tricky
|
||||||
|
@ -25,11 +25,8 @@ local function evaluate(profile)
|
|||||||
-- errors can be "normal", e.g. in case properties are unavailable
|
-- errors can be "normal", e.g. in case properties are unavailable
|
||||||
msg.verbose("Profile condition error on evaluating: " .. res)
|
msg.verbose("Profile condition error on evaluating: " .. res)
|
||||||
res = false
|
res = false
|
||||||
elseif type(res) ~= "boolean" then
|
|
||||||
msg.verbose("Profile condition did not return a boolean, but "
|
|
||||||
.. type(res) .. ".")
|
|
||||||
res = false
|
|
||||||
end
|
end
|
||||||
|
res = not not res
|
||||||
if res ~= profile.status then
|
if res ~= profile.status then
|
||||||
if res == true then
|
if res == true then
|
||||||
msg.info("Applying auto profile: " .. profile.name)
|
msg.info("Applying auto profile: " .. profile.name)
|
||||||
|
Loading…
Reference in New Issue
Block a user