From 6ef9d5a2b77297a1be730e7f34bbbf63aef2de9b Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Thu, 8 May 2025 19:22:38 -0700 Subject: colour matching text when searching --- main.lua | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 13 deletions(-) (limited to 'main.lua') diff --git a/main.lua b/main.lua index b3d9d9e..422afb9 100644 --- a/main.lua +++ b/main.lua @@ -76,23 +76,35 @@ local function update_osd() menu.page_pos + osd_menu_lines() - 1, #menu.options) do local opt = menu.options[i] local str = opt.name - local col + local col = '{\\c}' local icons = '' - if opt.type == 'category' then - str = '[' .. str .. ']' - col = '99DDFF' - elseif opt.stream_id == playing_stream_id then - icons = icons .. '{\\c&HFF6633&&}\226\143\186{\\c} ' + if i == menu.cursor_pos and not menu.search_active then + col = '{\\c&HFF00&}' + icons = col .. '› ' .. icons + elseif opt.type == 'category' then + col = '{\\c&H99DDFF&}' end - if i == menu.cursor_pos and not menu.search_active then - col = 'FF00' - icons = '{\\c&H' .. col .. '&}›{\\c} ' .. icons + if opt.hl then + local buf = '' + local n = 0 + for _, hl in ipairs(opt.hl) do + buf = buf .. col .. + str:sub(n + 1, hl.m_start - 1) .. + '{\\c&HFFDD}' .. + str:sub(hl.m_start, hl.m_end) + n = hl.m_end + end + str = buf .. col .. str:sub(n + 1) + else + str = col .. str end - if col then - str = '{\\c&H' .. col .. '&}' .. str .. '{\\c}' + if opt.type == 'category' then + str = col .. '[' .. str .. ']' + elseif opt.stream_id == playing_stream_id then + icons = icons .. '{\\c&HFF6633&&}\226\143\186 ' end out[#out+1] = icons .. str @@ -273,13 +285,40 @@ local function search_menu_options(options) return ret end +local function copy_table(t) + local u = {} + for k, v in pairs(t) do + u[k] = v + end + return u +end + local function search_update() local menu = menus[depth] + if #menu.search_text == 0 then + menu.options = menu.search_options + search_update_osd() + return + end + local matches = {} for _, v in ipairs(menu.search_options) do - if v.name:find(menu.search_text, 0, true) then - matches[#matches+1] = v + local hl = {} + + local i, j = 0, 0 + while true do + i, j = v.name:find(menu.search_text, j + 1, true) + if not i then + break + end + hl[#hl+1] = {m_start=i, m_end=j} + end + + if #hl > 0 then + local t = copy_table(v) + t.hl = hl + matches[#matches+1] = t end end -- cgit v1.2.3-70-g09d2