summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2025-05-06 17:48:08 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2025-05-06 17:48:08 -0700
commit8700155980e3a742064d095c9958fa5a843e2582 (patch)
tree2903628f7f7ef78e3d3a774704db84993b1387be
parentc329d7c8f923588da3a4adc711bac6f790958580 (diff)
downloadmpv-iptv-menu-8700155980e3a742064d095c9958fa5a843e2582.tar.gz
mpv-iptv-menu-8700155980e3a742064d095c9958fa5a843e2582.tar.xz
add cursor to search input
-rw-r--r--main.lua95
1 files changed, 76 insertions, 19 deletions
diff --git a/main.lua b/main.lua
index ac321bf..8d7f40f 100644
--- a/main.lua
+++ b/main.lua
@@ -10,6 +10,10 @@ local font_size = 20
local osd = mp.create_osd_overlay('ass-events')
local osd_lines = math.floor((720 / font_size) + 0.5) - 1
local osd_padding = math.floor((720 - (osd_lines * font_size)) / 2)
+local osd_cursor_glyph = '{\\p1\\pbo' .. math.floor(font_size / 5) .. '}' ..
+ 'm 0 0 l ' .. math.ceil(font_size / 32) .. ' 0 ' ..
+ math.ceil(font_size / 32) .. ' ' .. font_size ..
+ ' 0 ' .. font_size .. '{\\p0}'
local osd_bg = mp.create_osd_overlay('ass-events')
osd_bg.z = -1
osd_bg.data = '{\\alpha&H50&\\c&H&\\pos(0,0)}' ..
@@ -20,7 +24,8 @@ local streams = {}
local depth = 0
local menus = {}
-local search_text = ''
+local search_text
+local search_text_cursor
local search_active = false
local key_bindings = {}
@@ -58,8 +63,12 @@ local function update_osd()
if depth > 1 then
for i = 2, depth do
- out[#out+1] = '{\\c&H999999&} » [' .. menus[i].title ..
- ']{\\c}'
+ local col = '999999'
+ if search_active and i == depth then
+ col = 'FF00'
+ end
+ out[#out+1] = '{\\c&H' .. col .. '&} » [' ..
+ menus[i].title .. ']{\\c}'
end
out[#out+1] = ' ' -- space character for correct line height
end
@@ -202,6 +211,17 @@ local function unbind_keys()
key_bindings = {}
end
+local function split_search_text()
+ return search_text:sub(1, math.max(0, search_text_cursor - 1)),
+ search_text:sub(search_text_cursor)
+end
+
+local function search_update_osd()
+ local bc, ac = split_search_text()
+ menus[depth].title = 'Searching: ' .. bc .. osd_cursor_glyph .. ac
+ update_osd()
+end
+
local function search_update()
local matches = {}
for _, v in ipairs(menus[depth-1].options) do
@@ -210,9 +230,8 @@ local function search_update()
end
end
- local menu = menus[depth]
- menu.options = matches
- menu.title = 'Searching: ' .. search_text
+ menus[depth].options = matches
+ search_update_osd()
end
local function search_input(event)
@@ -220,15 +239,49 @@ local function search_input(event)
return
end
- search_text = search_text .. event.key_text
+ local bc, ac = split_search_text()
+ search_text = bc .. event.key_text .. ac
+ search_text_cursor = search_text_cursor + 1
search_update()
- update_osd()
end
local function search_backspace()
- search_text = search_text:sub(1, -2)
+ if search_text_cursor == 1 then
+ return
+ end
+
+ local bc, ac = split_search_text()
+ search_text = bc:sub(1, -2) .. ac
+ search_text_cursor = search_text_cursor - 1
search_update()
- update_osd()
+end
+
+local function search_delete()
+ if search_text_cursor == #search_text + 1 then
+ return
+ end
+
+ local bc, ac = split_search_text()
+ search_text = bc .. ac:sub(2)
+ search_update()
+end
+
+local function search_cursor_left()
+ if search_text_cursor == 1 then
+ return
+ end
+
+ search_text_cursor = search_text_cursor - 1
+ search_update_osd()
+end
+
+local function search_cursor_right()
+ if search_text_cursor == #search_text + 1 then
+ return
+ end
+
+ search_text_cursor = search_text_cursor + 1
+ search_update_osd()
end
local bind_search_keys
@@ -236,10 +289,11 @@ local bind_menu_keys
local function search_start()
search_active = true
- push_menu()
search_text = ''
+ search_text_cursor = 1
+
+ push_menu()
search_update()
- update_osd()
bind_search_keys()
end
@@ -252,7 +306,6 @@ end
local function search_cancel()
search_active = false
- search_text = ''
depth = depth - 1
update_osd()
bind_menu_keys()
@@ -261,20 +314,24 @@ end
function bind_search_keys()
unbind_keys()
+ local r = {repeatable=true}
bind_key('ANY_UNICODE', 'search-input', search_input, {complex=true})
bind_key('ENTER', 'finish-search', search_finish)
bind_key('ESC', 'cancel-search', search_cancel)
- bind_key('BS', 'search-backspace', search_backspace)
+ bind_key('BS', 'search-backspace', search_backspace, r)
+ bind_key('DEL', 'search-delete', search_delete, r)
+ bind_key('LEFT', 'search-cursor-left', search_cursor_left, r)
+ bind_key('RIGHT', 'search-cursor-right', search_cursor_right, r)
end
function bind_menu_keys()
unbind_keys()
- local repeatable = {repeatable=true}
- bind_key('DOWN', 'next-option', next_option, repeatable)
- bind_key('UP', 'prev-option', prev_option, repeatable)
- bind_key('PGDWN', 'next-page', next_page, repeatable)
- bind_key('PGUP', 'prev-page', prev_page, repeatable)
+ local r = {repeatable=true}
+ bind_key('DOWN', 'next-option', next_option, r)
+ bind_key('UP', 'prev-option', prev_option, r)
+ bind_key('PGDWN', 'next-page', next_page, r)
+ bind_key('PGUP', 'prev-page', prev_page, r)
bind_key('HOME', 'first-option', first_option)
bind_key('END', 'last-option', last_option)
bind_key('ENTER', 'select-option', select_option)