madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

menu_iterator: generic menu creator function
authorSimon Désaulniers <sim.desaulniers@gmail.com>
Wed, 14 Feb 2018 22:04:06 +0000 (17:04 -0500)
committerSimon Désaulniers <sim.desaulniers@gmail.com>
Fri, 16 Feb 2018 21:14:13 +0000 (16:14 -0500)
A simple example usage is given over on the amh project~[1]

[1]: https://github.com/sim590/amh/blob/dcf101c8ef74921de513c0b1dee294606fc6befe/exec/mpv.lua#L32

util/menu_iterator.lua

index 071f16f513d6c94a9e5cc5b94b2806630c4c00bb..913482a2829f0c974d62dfccf3e3f15960d57916 100644 (file)
@@ -10,6 +10,7 @@
 -- Menu iterator using naughty.notify
 
 local naughty = require("naughty")
+local util = require("lain.util")
 
 local state = { cid = nil }
 
@@ -63,4 +64,76 @@ local function iterate(menu, timeout, icon)
     }).id
 end
 
-return { iterate = iterate }
+-- Generates a menu compatible with the iterate function argument and suitable
+-- for the following cases:
+-- * all possible choices individually.
+-- * all possible choices are all the possible subsets of the set of individual
+--   choices (the powerset)
+--
+-- The following describes the function arguments:
+-- * args: an array containing the following members:
+--   * choices:       the list of choices from which to generate the menu
+--   * name:          the displayed name of the menu (in the form "name: choices")
+--   * selected_cb:   the callback to execute for each selected choice. Takes
+--                    the choice as a string argument. The function
+--                    menu_iterator.naughty_destroy_callback will handle nil
+--                    callbacks. It is then fine to pass nil callbacks.
+--   * rejected_cb:   the callback to execute for each rejected choice (in the
+--                    set of possible choices, but not selected). Takes the
+--                    choice as a string argument. The function
+--                    menu_iterator.naughty_destroy_callback will handle nil
+--                    callbacks. It is then fine to pass nil callbacks.
+--   * extra_choices: an array of pairs { choice_text, cb } for extra choices to
+--                    be added to the menu. The function
+--                    menu_iterator.naughty_destroy_callback will handle nil
+--                    callbacks. It is then fine to pass nil callbacks.
+--   * combination:   the combination of choice to generate. Possible choices
+--                    are "powerset" and "single" (the default).
+local function menu(args)
+    local choices     = assert(args.choices or args[1])
+    local name        = assert(args.name or args[2])
+    local selected_cb = args.selected_cb
+    local rejected_cb = args.rejected_cb
+    local extra_choices = args.extra_choices or {}
+
+    local ch_combinations = args.combination == "powerset" and helpers.powerset(choices) or helpers.trivial_partition_set(choices)
+    for _,c in pairs(extra_choices) do
+        ch_combinations = awful.util.table.join(ch_combinations, {{c[1]}})
+    end
+
+    local m = {}
+    for _,c in pairs(ch_combinations) do
+        if #c > 0 then
+            local cbs = {}
+            -- selected choices
+            for _,ch in pairs(c) do
+                if awful.util.table.hasitem(choices, ch) then
+                    cbs[#cbs + 1] = selected_cb and function() selected_cb(ch) end or nil
+                end
+            end
+
+            -- rejected choices
+            for _,ch in pairs(choices) do
+                if not awful.util.table.hasitem(c, ch) and awful.util.table.hasitem(choices, ch) then
+                    cbs[#cbs + 1] = rejected_cb and function() rejected_cb(ch) end or nil
+                end
+            end
+
+            -- add user extra choices (like the choice "None" for e.g.)
+            for _,x in pairs(extra_choices) do
+                if x[1] == c[1] then
+                    cbs[#cbs + 1] = x[2]
+                end
+            end
+
+            m[#m + 1] = { name .. ": " .. table.concat(c, " + "), cbs }
+        end
+    end
+
+    return m
+end
+
+return {
+    iterate = iterate,
+    menu    = menu
+}