mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-10 21:41:50 +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 search_utils = require("search_utils")
|
||||||
|
|
||||||
|
local MATCHED_COLOR = "[#4FC3F7]"
|
||||||
|
local NORMAL_COLOR = "[#FFFFFF]"
|
||||||
|
|
||||||
local packs_installed = {}
|
local packs_installed = {}
|
||||||
local pack_open = {}
|
local pack_open = {}
|
||||||
local PARSERS = {
|
local PARSERS = {
|
||||||
|
|
@ -20,10 +23,11 @@ function place_pack(panel, packinfo, callback)
|
||||||
if packinfo.error then
|
if packinfo.error then
|
||||||
callback = nil
|
callback = nil
|
||||||
end
|
end
|
||||||
|
local id_md = packinfo.id_md or packinfo.id
|
||||||
if packinfo.has_indices then
|
if packinfo.has_indices then
|
||||||
packinfo.id_verbose = packinfo.id.."*"
|
packinfo.id_verbose = id_md.."\\*"
|
||||||
else
|
else
|
||||||
packinfo.id_verbose = packinfo.id
|
packinfo.id_verbose = id_md
|
||||||
end
|
end
|
||||||
packinfo.callback = callback
|
packinfo.callback = callback
|
||||||
panel:add(gui.template("pack", packinfo))
|
panel:add(gui.template("pack", packinfo))
|
||||||
|
|
@ -39,16 +43,23 @@ function refresh_search()
|
||||||
|
|
||||||
local score_non_zero_num = 0
|
local score_non_zero_num = 0
|
||||||
local smart_score_cache = {}
|
local smart_score_cache = {}
|
||||||
|
local colored_ids = {}
|
||||||
|
local colored_titles = {}
|
||||||
|
|
||||||
local function smart_fuzzy_score(a)
|
local function smart_fuzzy_score(a)
|
||||||
if smart_score_cache[a.id] then
|
if smart_score_cache[a.id] then
|
||||||
return smart_score_cache[a.id]
|
return smart_score_cache[a.id]
|
||||||
end
|
end
|
||||||
local res = math.max(
|
local id_score, colored_id = search_utils.fuzzy_score(a.id, search_text,
|
||||||
search_utils.fuzzy_score(a.id, search_text),
|
MATCHED_COLOR, NORMAL_COLOR)
|
||||||
search_utils.fuzzy_score(a.title, search_text)
|
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
|
smart_score_cache[a.id] = res
|
||||||
|
colored_ids[a.id] = colored_id
|
||||||
|
colored_titles[a.id] = colored_title
|
||||||
|
|
||||||
if res > 0 then
|
if res > 0 then
|
||||||
score_non_zero_num = score_non_zero_num + 1
|
score_non_zero_num = score_non_zero_num + 1
|
||||||
end
|
end
|
||||||
|
|
@ -67,19 +78,33 @@ function refresh_search()
|
||||||
|
|
||||||
table.sort(packs, cmp)
|
table.sort(packs, cmp)
|
||||||
|
|
||||||
|
local contents = document.contents
|
||||||
|
contents:clear()
|
||||||
|
|
||||||
for i, v in ipairs(packs) do
|
for i, v in ipairs(packs) do
|
||||||
local id = v.id
|
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 content = document["pack_" .. id]
|
||||||
local pos = content.pos
|
local pos = content.pos
|
||||||
local size = content.size
|
local size = content.size
|
||||||
|
|
||||||
if i <= score_non_zero_num then
|
if i <= score_non_zero_num then
|
||||||
content.enabled = true
|
content.enabled = true
|
||||||
content.pos = {pos[1], visible * (size[2] + interval) - step}
|
content.pos = { pos[1], visible * (size[2] + interval) - step }
|
||||||
visible = visible + 1
|
visible = visible + 1
|
||||||
else
|
else
|
||||||
content.enabled = false
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -302,12 +327,9 @@ function refresh()
|
||||||
local packinfos = pack.get_info(packs_installed)
|
local packinfos = pack.get_info(packs_installed)
|
||||||
for i, id in ipairs(packs_installed) do
|
for i, id in ipairs(packs_installed) do
|
||||||
local packinfo = packinfos[id]
|
local packinfo = packinfos[id]
|
||||||
|
|
||||||
packinfo.id = id
|
packinfo.id = id
|
||||||
packs_installed[i] = {
|
packs_installed[i] = packinfo
|
||||||
id = packinfo.id,
|
|
||||||
title = packinfo.title
|
|
||||||
}
|
|
||||||
local callback = string.format('open_pack("%s")', id)
|
local callback = string.format('open_pack("%s")', id)
|
||||||
place_pack(contents, packinfo, callback)
|
place_pack(contents, packinfo, callback)
|
||||||
end
|
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}">
|
<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}]
|
[%{id_verbose} %{version}]
|
||||||
</label>
|
</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'
|
<label if='%{creator}' color='#CCFFE5B2' size='300,20' align='right'
|
||||||
gravity='bottom-right'>
|
gravity='bottom-right'>
|
||||||
%{creator}
|
%{creator}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,23 @@
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
-- Оценивает схожесть строк и возвращает целое число,
|
-- Оценивает схожесть строк и возвращает совпадение (>= 0) и если переданы
|
||||||
-- чем оно больше, тем больше совпадение
|
-- параметры color_matched и color_normal, возвращает также исходную строку,
|
||||||
function M.fuzzy_score(sample, pattern)
|
-- в которой совпадающие части обрамлены в 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
|
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 sample_lower = string.lower(sample)
|
||||||
local pattern_lower = string.lower(pattern)
|
local pattern_lower = string.lower(pattern)
|
||||||
|
|
||||||
|
|
@ -21,9 +34,13 @@ function M.fuzzy_score(sample, pattern)
|
||||||
local last_match_pos = -1
|
local last_match_pos = -1
|
||||||
|
|
||||||
for i = 1, sample_len do
|
for i = 1, sample_len do
|
||||||
if pattern_idx <= pattern_len and
|
if pattern_idx <= pattern_len and (
|
||||||
sample_lower:sub(i, i) == pattern_lower:sub(pattern_idx, pattern_idx) then
|
sample_lower:sub(i, i) ==
|
||||||
|
pattern_lower:sub(pattern_idx, pattern_idx)) then
|
||||||
matched_chars = matched_chars + 1
|
matched_chars = matched_chars + 1
|
||||||
|
if consecutive == 0 and do_coloring then
|
||||||
|
colored_sample = colored_sample .. color_matched
|
||||||
|
end
|
||||||
consecutive = consecutive + 1
|
consecutive = consecutive + 1
|
||||||
if consecutive > max_consecutive then
|
if consecutive > max_consecutive then
|
||||||
max_consecutive = consecutive
|
max_consecutive = consecutive
|
||||||
|
|
@ -36,8 +53,17 @@ function M.fuzzy_score(sample, pattern)
|
||||||
|
|
||||||
pattern_idx = pattern_idx + 1
|
pattern_idx = pattern_idx + 1
|
||||||
else
|
else
|
||||||
|
if consecutive ~= 0 and do_coloring then
|
||||||
|
colored_sample = colored_sample .. color_normal
|
||||||
|
end
|
||||||
consecutive = 0
|
consecutive = 0
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
if pattern_idx <= pattern_len then
|
if pattern_idx <= pattern_len then
|
||||||
|
|
@ -66,7 +92,7 @@ function M.fuzzy_score(sample, pattern)
|
||||||
score = score + 30
|
score = score + 30
|
||||||
end
|
end
|
||||||
|
|
||||||
return math.max(0, score)
|
return math.max(0, score), colored_sample
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue