refact search

This commit is contained in:
Xertis 2025-01-19 12:53:32 +03:00
parent dc83e9b466
commit 6c3dd941ce
4 changed files with 81 additions and 37 deletions

View File

@ -52,6 +52,11 @@ table.shuffle(t: table) -> table
Перемешивает значения в таблице.
```lua
table.merge(t1: table, t2: table) -> table
```
Возвращает объединённую таблицу t1 с t2.
```lua
table.tostring(t: table) -> string

View File

@ -34,34 +34,60 @@ function apply()
end
end
function refresh_search()
function reposition_func(pack)
local INTERVAL = 2
local STEP = -1
local SIZE = 80
local tbl = packs_excluded
if table.has(packs_included, pack) then
tbl = packs_included
end
local indx = table.index(tbl, pack) - 1
local pos = {0, (SIZE + INTERVAL) * indx - STEP}
return pos
end
function refresh_search()
local search_text = document.search_textbox.text:lower()
local interval = 2
local step = -1
for _, packs in ipairs({packs_excluded, packs_included}) do
local visible = 0
local new_included = table.copy(packs_included)
local new_excluded = table.copy(packs_excluded)
for i, v in ipairs(packs) do
local info = packs_info[v]
local function score(package_name)
if package_name:lower():find(search_text) then
return 1
end
return 0
end
local id = info[1]
local title = info[2]
local function sorting(a, b)
local score_a = score(packs_info[a][2])
local score_b = score(packs_info[b][2])
local content = document["pack_" .. id]
local pos = content.pos
local size = content.size
if title:lower():find(search_text) or search_text == '' then
content.pos = {pos[1], visible * (size[2] + interval) - step}
visible = visible + 1
else
content.pos = {pos[1], (visible + #packs - i) * (size[2] + interval) - step}
end
if score_a ~= score_b then
return score_a > score_b
else
return packs_info[a][2] < packs_info[b][2]
end
end
table.sort(new_included, sorting)
table.sort(new_excluded, sorting)
packs_included = new_included
packs_excluded = new_excluded
for _, id in ipairs(table.merge(table.copy(packs_included), packs_excluded)) do
local content = document["pack_" .. id]
content:reposition()
end
end
function refresh_changes()
document.apply_btn.enabled = (#add_packs>0) or (#rem_packs>0)
refresh_search()
@ -126,7 +152,7 @@ function move_right()
refresh_changes()
end
function place_pack(panel, packinfo, callback)
function place_pack(panel, packinfo, callback, position_func)
if packinfo.error then
callback = nil
end
@ -136,6 +162,7 @@ function place_pack(panel, packinfo, callback)
packinfo.id_verbose = packinfo.id
end
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
@ -191,22 +218,6 @@ function refresh()
end
local packinfos = pack.get_info(packids)
for i,id in ipairs(packs_installed) do
local packinfo = packinfos[id]
packinfo.index = i
callback = not table.has(base_packs, id) and string.format('move_pack("%s")', id) or nil
packinfo.error = check_dependencies(packinfo)
place_pack(packs_cur, packinfo, callback)
end
for i,id in ipairs(packs_available) do
local packinfo = packinfos[id]
packinfo.index = i
callback = string.format('move_pack("%s")', id)
packinfo.error = check_dependencies(packinfo)
place_pack(packs_add, packinfo, callback)
end
for _,id in ipairs(base_packs) do
local packinfo = pack.get_info(id)
packs_info[id] = {packinfo.id, packinfo.title}
@ -226,6 +237,22 @@ function refresh()
if #packs_excluded == 0 then packs_excluded = table.copy(packs_available) end
if #packs_included == 0 then packs_included = table.copy(packs_installed) end
for i,id in ipairs(packs_installed) do
local packinfo = packinfos[id]
packinfo.index = i
callback = not table.has(base_packs, id) and string.format('move_pack("%s")', id) or nil
packinfo.error = check_dependencies(packinfo)
place_pack(packs_cur, packinfo, callback, string.format('reposition_func("%s")[1],reposition_func("%s")[2]', packinfo.id, packinfo.id))
end
for i,id in ipairs(packs_available) do
local packinfo = packinfos[id]
packinfo.index = i
callback = string.format('move_pack("%s")', id)
packinfo.error = check_dependencies(packinfo)
place_pack(packs_add, packinfo, callback, string.format('reposition_func("%s")[1],reposition_func("%s")[2]', packinfo.id, packinfo.id))
end
apply_movements(packs_cur, packs_add)
refresh_changes()
end

View File

@ -1,4 +1,4 @@
<container id='pack_%{id}' onclick='%{callback}' size='0,80' color='#00000040' hover-color='#00000080' z-index="%{index}">
<container id='pack_%{id}' onclick='%{callback}' size='0,80' color='#00000040' hover-color='#00000080' z-index="%{index}" position-func="%{position_func}">
<label color='#FFFFFF80' size='300,25' align='right' gravity='top-right'>
[%{id_verbose}]
</label>

View File

@ -113,6 +113,18 @@ function table.shuffle(t)
return t
end
function table.merge(t1, t2)
for i, v in pairs(t2) do
if type(i) == "number" then
t1[#t1 + 1] = v
elseif t1[i] == nil then
t1[i] = v
end
end
return t1
end
----------------------------------------------
local pattern_escape_replacements = {