mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 18:41:51 +00:00
add coloring for content_menu.xml.lua
This commit is contained in:
parent
ccc11232bb
commit
02f0e20010
3 changed files with 69 additions and 21 deletions
|
|
@ -1,5 +1,8 @@
|
|||
local search_utils = require("search_utils")
|
||||
|
||||
local MATCHED_COLOR = "[#4FC3F7]"
|
||||
local NORMAL_COLOR = "[#FFFFFF]"
|
||||
|
||||
local packs_installed = {}
|
||||
local pack_open = {}
|
||||
local PARSERS = {
|
||||
|
|
@ -20,10 +23,11 @@ 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 = packinfo.id.."*"
|
||||
packinfo.id_verbose = id_md.."\\*"
|
||||
else
|
||||
packinfo.id_verbose = packinfo.id
|
||||
packinfo.id_verbose = id_md
|
||||
end
|
||||
packinfo.callback = callback
|
||||
panel:add(gui.template("pack", packinfo))
|
||||
|
|
@ -39,16 +43,23 @@ function refresh_search()
|
|||
|
||||
local score_non_zero_num = 0
|
||||
local smart_score_cache = {}
|
||||
local colored_ids = {}
|
||||
local colored_titles = {}
|
||||
|
||||
local function smart_fuzzy_score(a)
|
||||
if smart_score_cache[a.id] then
|
||||
return smart_score_cache[a.id]
|
||||
end
|
||||
local res = math.max(
|
||||
search_utils.fuzzy_score(a.id, search_text),
|
||||
search_utils.fuzzy_score(a.title, search_text)
|
||||
)
|
||||
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 res = math.max(id_score, title_score)
|
||||
|
||||
smart_score_cache[a.id] = res
|
||||
colored_ids[a.id] = colored_id
|
||||
colored_titles[a.id] = colored_title
|
||||
|
||||
if res > 0 then
|
||||
score_non_zero_num = score_non_zero_num + 1
|
||||
end
|
||||
|
|
@ -67,19 +78,33 @@ function refresh_search()
|
|||
|
||||
table.sort(packs, cmp)
|
||||
|
||||
local contents = document.contents
|
||||
contents:clear()
|
||||
|
||||
for i, v in ipairs(packs) do
|
||||
local id = v.id
|
||||
|
||||
local packinfo = table.copy(packs[i])
|
||||
if colored_ids[id] then
|
||||
packinfo.id_md = colored_ids[id]
|
||||
end
|
||||
if colored_titles[id] then
|
||||
packinfo.title = colored_titles[id]
|
||||
end
|
||||
|
||||
place_pack(contents, packinfo, callback)
|
||||
|
||||
local content = document["pack_" .. id]
|
||||
local pos = content.pos
|
||||
local size = content.size
|
||||
|
||||
if i <= score_non_zero_num then
|
||||
content.enabled = true
|
||||
content.pos = {pos[1], visible * (size[2] + interval) - step}
|
||||
content.pos = { pos[1], visible * (size[2] + interval) - step }
|
||||
visible = visible + 1
|
||||
else
|
||||
content.enabled = false
|
||||
content.pos = {pos[1], (visible + #packs_installed - i) * (size[2] + interval) - step}
|
||||
content.pos = { pos[1], (visible + #packs_installed - i) * (size[2] + interval) - step }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -302,12 +327,9 @@ function refresh()
|
|||
local packinfos = pack.get_info(packs_installed)
|
||||
for i, id in ipairs(packs_installed) do
|
||||
local packinfo = packinfos[id]
|
||||
|
||||
packinfo.id = id
|
||||
packs_installed[i] = {
|
||||
id = packinfo.id,
|
||||
title = packinfo.title
|
||||
}
|
||||
packs_installed[i] = packinfo
|
||||
|
||||
local callback = string.format('open_pack("%s")', id)
|
||||
place_pack(contents, packinfo, callback)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
<container id='pack_%{id}' onclick='%{callback}' size='0,80' color='#00000040' hover-color='#00000080' position-func="%{position_func}" z-index="%{index}">
|
||||
<label color='#FFFFFF80' size='340,25' align='right' gravity='top-right'>
|
||||
<label color='#FFFFFF80' size='340,25' align='right' gravity='top-right' markup='md'>
|
||||
[%{id_verbose} %{version}]
|
||||
</label>
|
||||
<label pos='78,6'>%{title}</label>
|
||||
<label pos='78,6' markup='md'>%{title}</label>
|
||||
<label if='%{creator}' color='#CCFFE5B2' size='300,20' align='right'
|
||||
gravity='bottom-right'>
|
||||
%{creator}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,23 @@
|
|||
local M = {}
|
||||
|
||||
-- Оценивает схожесть строк и возвращает целое число,
|
||||
-- чем оно больше, тем больше совпадение
|
||||
function M.fuzzy_score(sample, pattern)
|
||||
-- Оценивает схожесть строк и возвращает совпадение (>= 0) и если переданы
|
||||
-- параметры color_matched и color_normal, возвращает также исходную строку,
|
||||
-- в которой совпадающие части обрамлены в color_matched и color_normal
|
||||
---@param sample str где ищем совпадения
|
||||
---@param pattern str какие совпадения ищем
|
||||
---@param color_matched? str
|
||||
---@param color_normal? str
|
||||
---@return int match
|
||||
---@return str? colored_sample
|
||||
function M.fuzzy_score(sample, pattern, color_matched, color_normal)
|
||||
if pattern == "" then return 0 end
|
||||
|
||||
local do_coloring = color_matched and color_normal
|
||||
local colored_sample
|
||||
if do_coloring then
|
||||
colored_sample = ""
|
||||
end
|
||||
|
||||
local sample_lower = string.lower(sample)
|
||||
local pattern_lower = string.lower(pattern)
|
||||
|
||||
|
|
@ -21,9 +34,13 @@ function M.fuzzy_score(sample, pattern)
|
|||
local last_match_pos = -1
|
||||
|
||||
for i = 1, sample_len do
|
||||
if pattern_idx <= pattern_len and
|
||||
sample_lower:sub(i, i) == pattern_lower:sub(pattern_idx, pattern_idx) then
|
||||
if pattern_idx <= pattern_len and (
|
||||
sample_lower:sub(i, i) ==
|
||||
pattern_lower:sub(pattern_idx, pattern_idx)) then
|
||||
matched_chars = matched_chars + 1
|
||||
if consecutive == 0 and do_coloring then
|
||||
colored_sample = colored_sample .. color_matched
|
||||
end
|
||||
consecutive = consecutive + 1
|
||||
if consecutive > max_consecutive then
|
||||
max_consecutive = consecutive
|
||||
|
|
@ -36,8 +53,17 @@ function M.fuzzy_score(sample, pattern)
|
|||
|
||||
pattern_idx = pattern_idx + 1
|
||||
else
|
||||
if consecutive ~= 0 and do_coloring then
|
||||
colored_sample = colored_sample .. color_normal
|
||||
end
|
||||
consecutive = 0
|
||||
end
|
||||
if do_coloring then
|
||||
colored_sample = colored_sample .. sample:sub(i, i)
|
||||
end
|
||||
end
|
||||
if consecutive ~= 0 and do_coloring then
|
||||
colored_sample = colored_sample .. color_normal
|
||||
end
|
||||
|
||||
if pattern_idx <= pattern_len then
|
||||
|
|
@ -66,7 +92,7 @@ function M.fuzzy_score(sample, pattern)
|
|||
score = score + 30
|
||||
end
|
||||
|
||||
return math.max(0, score)
|
||||
return math.max(0, score), colored_sample
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue