menubar: Refactoring and cleanup

This commit is contained in:
modk 2016-07-17 17:23:35 +02:00 committed by Emmanuel Lepage Vallee
parent 1a40333b9e
commit 12ea5e2318
1 changed files with 44 additions and 33 deletions

View File

@ -113,7 +113,7 @@ local function load_count_table()
local count_file = io.open (count_file_name, "r") local count_file = io.open (count_file_name, "r")
local count_table = {} local count_table = {}
-- read count file -- read weight file
if count_file then if count_file then
io.input (count_file) io.input (count_file)
for line in io.lines() do for line in io.lines() do
@ -135,7 +135,7 @@ local function write_count_table(count_table)
if count_file then if count_file then
io.output (count_file) io.output (count_file)
for name,count in pairs(count_table) do for name, count in pairs(count_table) do
local str = string.format("%s;%d\n", name, count) local str = string.format("%s;%d\n", name, count)
io.write(str) io.write(str)
end end
@ -225,40 +225,46 @@ local function menulist_update(query, scr)
query = query or "" query = query or ""
shownitems = {} shownitems = {}
local pattern = awful.util.query_to_pattern(query) local pattern = awful.util.query_to_pattern(query)
local match_inside = {}
-- First we add entries which names match the command from the -- All entries are added to a list that will be sorted
-- beginning to the table shownitems, and the ones that contain -- according to the priority (first) and weight (second) of its
-- command in the middle to the table match_inside. -- entries.
-- If categories are used in the menu, we add the entries matching
-- the current query with high priority as to ensure they are
-- displayed first. Afterwards the non-category entries are added.
-- All entries are weighted according to the number of times they
-- have been executed previously (stored in count_table).
local count_table = load_count_table() local count_table = load_count_table()
local command_list = {} local command_list = {}
local PRIO_NONE = 0 local PRIO_NONE = 0
local PRIO_HIG = 3 local PRIO_CATEGORY_MATCH = 2
local PRIO_LOW = 1
local PRIO_NORMAL = 2
-- Add the categories -- Add the categories
if menubar.show_categories then if menubar.show_categories then
for _, v in pairs(menubar.menu_gen.all_categories) do for _, v in pairs(menubar.menu_gen.all_categories) do
v.focused = false v.focused = false
if not current_category and v.use then if not current_category and v.use then
if string.match(v.name, pattern) then
if string.match(v.name, "^" .. pattern) then
v.count = PRIO_NONE
v.prio = PRIO_NORMAL
-- use count from count_table if present -- check if current query matches a category
if string.match(v.name, pattern) then
v.weight = 0
v.prio = PRIO_CATEGORY_MATCH
-- get use count from count_table if present
-- and use it as weight
if string.len(pattern) > 0 and count_table[v.name] ~= nil then if string.len(pattern) > 0 and count_table[v.name] ~= nil then
v.count = tonumber(count_table[v.name]) v.weight = tonumber(count_table[v.name])
end end
if string.match(v.name, "^" .. pattern) -- check for prefix match
or string.match(v.cmdline, "^" .. pattern) then if string.match(v.name, "^" .. pattern) then
v.prio = PRIO_HIGH -- increase default priority
v.prio = PRIO_CATEGORY_MATCH + 1
else else
v.prio = PRIO_NORMAL v.prio = PRIO_CATEGORY_MATCH
end end
table.insert (command_list, v) table.insert (command_list, v)
@ -266,26 +272,31 @@ local function menulist_update(query, scr)
end end
end end
end end
end
-- Add the applications according to their name and cmdline -- Add the applications according to their name and cmdline
for _, v in ipairs(menubar.menu_entries) do for _, v in ipairs(menubar.menu_entries) do
v.focused = false v.focused = false
if not current_category or v.category == current_category then if not current_category or v.category == current_category then
-- check if the query matches either the name or the commandline
-- of some entry
if string.match(v.name, pattern) if string.match(v.name, pattern)
or string.match(v.cmdline, pattern) then or string.match(v.cmdline, pattern) then
v.count = 0 v.weight = 0
v.prio = PRIO_NONE v.prio = PRIO_NONE
-- use count from count_table if present -- get use count from count_table if present
-- and use it as weight
if string.len(pattern) > 0 and count_table[v.name] ~= nil then if string.len(pattern) > 0 and count_table[v.name] ~= nil then
v.count = tonumber(count_table[v.name]) v.weight = tonumber(count_table[v.name])
end end
-- check for prefix match
if string.match(v.name, "^" .. pattern) if string.match(v.name, "^" .. pattern)
or string.match(v.cmdline, "^" .. pattern) then or string.match(v.cmdline, "^" .. pattern) then
v.prio = PRIO_LOW -- increase default priority
v.prio = PRIO_NONE + 1
else else
v.prio = PRIO_NONE v.prio = PRIO_NONE
end end
@ -295,14 +306,14 @@ local function menulist_update(query, scr)
end end
end end
local function compare_counts(a,b) local function compare_counts(a, b)
if a.count == b.count then if a.prio == b.prio then
return a.weight > b.weight
end
return a.prio > b.prio return a.prio > b.prio
end end
return a.count > b.count
end
-- sort command_list by count (highest first) -- sort command_list by weight (highest first)
table.sort(command_list, compare_counts) table.sort(command_list, compare_counts)
-- copy into showitems -- copy into showitems
shownitems = command_list shownitems = command_list