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,7 +1,6 @@
local search_utils = require("search_utils")
local MATCHED_COLOR = "[#4FC3F7]"
local NORMAL_COLOR = "[#FFFFFF]"
local score_func = search_utils.fuzzy_score
local packs_installed = {}
local pack_open = {}
@ -19,18 +18,28 @@ function on_open()
document.search_textbox.focused = true
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 place_pack(panel, packinfo, callback)
if packinfo.error then
callback = nil
end
local id_md = packinfo.id_md or packinfo.id
if packinfo.has_indices then
packinfo.id_verbose = id_md.."\\*"
else
packinfo.id_verbose = id_md
end
packinfo.id_verbose = ""
packinfo.callback = callback
panel:add(gui.template("pack", packinfo))
update_pack_id_title(packinfo)
end
function refresh_search()
@ -46,14 +55,12 @@ function refresh_search()
local colored_ids = {}
local colored_titles = {}
local function smart_fuzzy_score(a)
local function smart_score(a)
if smart_score_cache[a.id] then
return smart_score_cache[a.id]
end
local id_score, colored_id = search_utils.fuzzy_score(a.id, search_text,
MATCHED_COLOR, NORMAL_COLOR)
local title_score, colored_title = search_utils.fuzzy_score(a.title, search_text,
MATCHED_COLOR, NORMAL_COLOR)
local id_score, colored_id = score_func(a.id, search_text, true)
local title_score, colored_title = score_func(a.title, search_text, true)
local res = math.max(id_score, title_score)
smart_score_cache[a.id] = res
@ -67,8 +74,8 @@ function refresh_search()
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 a.title > b.title
else
@ -78,9 +85,6 @@ function refresh_search()
table.sort(packs, cmp)
local contents = document.contents
contents:clear()
for i, v in ipairs(packs) do
local id = v.id
@ -89,10 +93,9 @@ function refresh_search()
packinfo.id_md = colored_ids[id]
end
if colored_titles[id] then
packinfo.title = colored_titles[id]
packinfo.title_md = colored_titles[id]
end
place_pack(contents, packinfo, callback)
update_pack_id_title(packinfo)
local content = document["pack_" .. id]
local pos = content.pos
@ -132,7 +135,7 @@ local function create_trackbar(id, name, val)
"<label id='%s'>%s (%s)</label>",
id .. "_label", name:gsub("^%l", string.upper), val
))
document.configs:add(string.format(
document.configs:add(string.format(
"<trackbar id='%s' consumer='function(x) set_value(\"%s\", \"%s\", x) end' value='%s' min='0' max='1000' step='10'>%s</trackbar>",
id, name, id, val, name
))
@ -154,7 +157,7 @@ end
local function create_config(i, config, name, path)
create_label(name, ('**[' .. name .. ']**'):upper(), "#FFFFFF")
pack_open[2][i] = {elements = {}, path = path}
pack_open[2][i] = { elements = {}, path = path }
for _, a in ipairs(config.checkboxes) do
create_checkbox(i .. '_' .. a[1], a[1], a[2])
pack_open[2][i]["elements"][a[1]] = type(a[2])
@ -203,7 +206,6 @@ local function has_valid_config(id)
end
local function load_config_file(path)
local extension = path:match("^.+%.(.+)$")
local name = path:match("([^/]+)%.([^%.]+)$")
@ -220,10 +222,11 @@ end
local function load_config(path)
if file.exists(path) then
local value = load_config_file(path)
local config = {checkboxes = {},
trackbars = {},
textboxes = {}
}
local config = {
checkboxes = {},
trackbars = {},
textboxes = {}
}
if not value then
return
@ -231,11 +234,11 @@ local function load_config(path)
for i, v in pairs(value) do
if type(v) == "string" then
table.insert(config.textboxes, {i, v})
table.insert(config.textboxes, { i, v })
elseif type(v) == "boolean" then
table.insert(config.checkboxes, {i, v})
table.insert(config.checkboxes, { i, v })
elseif type(v) == "number" then
table.insert(config.trackbars, {i, v})
table.insert(config.trackbars, { i, v })
end
end
@ -275,16 +278,18 @@ end
function open_pack(id)
local packinfo = pack.get_info(id)
pack_open = {id, {}}
pack_open = { id, {} }
document.configs.visible = false
document.content_info.visible = true
if has_valid_config(id) then document.open_config.enabled = true else document.open_config.enabled = false end
if packinfo['dependencies'] == nil then document.dependencies.text = 'None' else document.dependencies.text = table.tostring(packinfo['dependencies']) end
if packinfo['dependencies'] == nil then document.dependencies.text = 'None' else document.dependencies.text = table
.tostring(packinfo['dependencies']) end
if packinfo['creator'] == '' then document.author.text = 'None' else document.author.text = packinfo['creator'] end
if packinfo['version'] == nil then document.version.text = 'None' else document.version.text = packinfo['version'] end
if packinfo['description'] == nil then document.description.text = 'None' else document.description.text = packinfo['description'] end
if packinfo['description'] == nil then document.description.text = 'None' else document.description.text = packinfo
['description'] end
end
function open_config()