From 50eabe842cb7895bb4eb5a7c4216209927bdbe9e Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Fri, 12 Jan 2024 23:36:42 +0800 Subject: [PATCH] add support for reverse check --- README.md | 1 + lua/dyn_menu.lua | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 40e17b2..b1543ba 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ The menu syntax is similar to [mpv.net](https://github.com/mpvnet-player/mpv.net - `number`: not zero - `table`: not empty - none of above: not `nil` + - `#@prop:check!` is the reverse form of `#@prop:check` - use `_` if no keybinding - use `ignore` if no command diff --git a/lua/dyn_menu.lua b/lua/dyn_menu.lua index c21ffe9..85e0a0a 100644 --- a/lua/dyn_menu.lua +++ b/lua/dyn_menu.lua @@ -16,7 +16,8 @@ -- #@playlist playlist -- #@profiles profile list -- --- #@prop:check check menu item based on property value +-- #@prop:check check menu item if property is true +-- #@prop:check! check menu item if property is false local opts = require('mp.options') local utils = require('mp.utils') @@ -339,7 +340,7 @@ end -- handle #@prop:check function update_check_status(item, keyword) - local prop = keyword:match('^([%w-]+):check$') + local prop, e = keyword:match('^([%w-]+):check(!?)$') if not prop then return false end local function check(v) @@ -351,7 +352,9 @@ function update_check_status(item, keyword) return v ~= nil end mp.observe_property(prop, 'native', function(name, value) - item.state = check(value) and { 'checked' } or {} + local ok = check(value) + if e == '!' then ok = not ok end + item.state = ok and { 'checked' } or {} menu_items_dirty = true end)