add fuzzy score

This commit is contained in:
neveix 2026-08-31 00:53:45 +05:00
parent 02f0e20010
commit 1e69b6df13
4 changed files with 92 additions and 59 deletions

View file

@ -1,5 +1,7 @@
local search_utils = require("search_utils")
local score_func = search_utils.fuzzy_score
function on_open(params)
if params then
mode = params.mode
@ -65,6 +67,18 @@ function reposition_func(_pack)
return pos[1], pos[2]
end
local function update_pack_id_title(packinfo)
local id_el = document["pack_"..packinfo.id.."_id"]
local title_el = document["pack_"..packinfo.id.."_title"]
local id_md = packinfo.id_md or packinfo.id
if packinfo.has_indices then
id_md = id_md .. "\\*"
end
id_el.text = string.format("[%s %s]", id_md, packinfo.version)
title_el.text = packinfo.title_md or packinfo.title
end
function refresh_search()
local search_text = document.search_textbox.text:lower()
@ -73,22 +87,27 @@ function refresh_search()
local new_excluded = table.copy(packs_excluded)
local smart_score_cache = {}
local colored_ids = {}
local colored_titles = {}
local function smart_fuzzy_score(id)
local function smart_score(id)
if smart_score_cache[id] then
return smart_score_cache[id]
end
local res = math.max(
search_utils.fuzzy_score(id, search_text),
search_utils.fuzzy_score(packs_info[id].title, search_text)
)
local id_score, colored_id = score_func(id, search_text, true)
local title_score, colored_title = score_func(
packs_info[id].title, search_text, true)
local res = math.max(id_score, title_score)
smart_score_cache[id] = res
colored_ids[id] = colored_id
colored_titles[id] = colored_title
return res
end
local function cmp(a, b)
local score_a = smart_fuzzy_score(a)
local score_b = smart_fuzzy_score(b)
local score_a = smart_score(a)
local score_b = smart_score(b)
if score_a ~= score_b then
return score_a > score_b
@ -106,6 +125,10 @@ function refresh_search()
for _, id in ipairs(table.merge(table.copy(packs_included), packs_excluded)) do
local content = document["pack_" .. id]
content:reposition()
local packinfo = pack.get_info(id)
packinfo.id_md = colored_ids[id]
packinfo.title_md = colored_titles[id]
update_pack_id_title(packinfo)
end
end
@ -174,21 +197,20 @@ function move_right()
refresh_changes()
end
function place_pack(panel, packinfo, callback, position_func)
if packinfo.error then
callback = nil
end
if packinfo.has_indices then
packinfo.id_verbose = packinfo.id.."*"
else
packinfo.id_verbose = packinfo.id
end
packinfo.id_verbose = ""
packinfo.callback = callback
packinfo.position_func = position_func or function () end
panel:add(gui.template("pack", packinfo))
if not callback then
document["pack_"..packinfo.id].enabled = false
end
update_pack_id_title(packinfo)
end
local Version = {}