summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2025-05-12 13:23:46 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2025-05-12 13:23:46 -0700
commitd81631f40bf94aef69369a9f9b06667cf6d44168 (patch)
tree043f9a11f060c7e495889384ab2a8f4f4c3078ab
parent3143b989984c8c9462c70933a0851d0480475750 (diff)
downloadmpv-iptv-menu-d81631f40bf94aef69369a9f9b06667cf6d44168.tar.gz
mpv-iptv-menu-d81631f40bf94aef69369a9f9b06667cf6d44168.tar.xz
support utf8 search input
-rw-r--r--main.lua45
1 files changed, 33 insertions, 12 deletions
diff --git a/main.lua b/main.lua
index 560a376..74f7178 100644
--- a/main.lua
+++ b/main.lua
@@ -57,6 +57,23 @@ local function strip(str)
return (str:gsub('^%s*(.-)%s*$', '%1'))
end
+local function utf8_seek(str, pos, n)
+ local step = n > 0 and 1 or -1
+ local test = n > 0
+ and function() return pos > #str end
+ or function() return pos <= 1 end
+
+ while n ~= 0 and not test() do
+ repeat
+ pos = pos + step
+ until test() or bit.band(str:byte(pos), 0xc0) ~= 0x80
+
+ n = n - step
+ end
+
+ return pos
+end
+
local function read_json_file(fn)
local f = io.open(script_dir .. '/' .. fn, 'r')
if not f then
@@ -553,7 +570,7 @@ local function update_search_matches()
update_search_osd()
end
-local function search_input_text(event)
+local function search_input_char(event)
if event.event ~= 'down' and event.event ~= 'repeat' then
return
end
@@ -561,30 +578,32 @@ local function search_input_text(event)
local menu = menus[depth]
local bc, ac = split_search_text()
menu.search_text = bc .. event.key_text .. ac
- menu.search_cursor = menu.search_cursor + 1
+ menu.search_cursor = menu.search_cursor + #event.key_text
update_search_matches()
end
local function search_input_bs()
local menu = menus[depth]
- if menu.search_cursor == 1 then
+ if menu.search_cursor <= 1 then
return
end
- local bc, ac = split_search_text()
- menu.search_text = bc:sub(1, -2) .. ac
- menu.search_cursor = menu.search_cursor - 1
+ local pos = utf8_seek(menu.search_text, menu.search_cursor, -1)
+ menu.search_text = menu.search_text:sub(1, pos - 1) ..
+ menu.search_text:sub(menu.search_cursor)
+ menu.search_cursor = pos
update_search_matches()
end
local function search_input_del()
local menu = menus[depth]
- if menu.search_cursor == #menu.search_text + 1 then
+ if menu.search_cursor > #menu.search_text then
return
end
- local bc, ac = split_search_text()
- menu.search_text = bc .. ac:sub(2)
+ menu.search_text = menu.search_text:sub(1, menu.search_cursor - 1) ..
+ menu.search_text:sub(
+ utf8_seek(menu.search_text, menu.search_cursor, 1))
update_search_matches()
end
@@ -600,11 +619,13 @@ local function set_search_cursor(pos)
end
local function search_cursor_left()
- set_search_cursor(menus[depth].search_cursor - 1)
+ local menu = menus[depth]
+ set_search_cursor(utf8_seek(menu.search_text, menu.search_cursor, -1))
end
local function search_cursor_right()
- set_search_cursor(menus[depth].search_cursor + 1)
+ local menu = menus[depth]
+ set_search_cursor(utf8_seek(menu.search_text, menu.search_cursor, 1))
end
local function search_cursor_start()
@@ -674,7 +695,7 @@ end
function bind_search_keys()
unbind_keys()
- bind_key('ANY_UNICODE', search_input_text, {complex=true})
+ bind_key('ANY_UNICODE', search_input_char, {complex=true})
bind_key('BS', search_input_bs, {repeatable=true})
bind_key('DEL', search_input_del, {repeatable=true})