summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.lua115
1 files changed, 100 insertions, 15 deletions
diff --git a/main.lua b/main.lua
index 71e313c..3b2a678 100644
--- a/main.lua
+++ b/main.lua
@@ -17,6 +17,10 @@ local depth = 0
local menus = {}
local menu_top = {}
local menu_pos = {}
+local menu_titles = {}
+
+local search_text = ''
+local search_active = false
local function load_json_file(path)
local f = io.open(script_dir .. '/' .. path, 'r')
@@ -49,9 +53,9 @@ local function update_osd()
local out = {}
if depth > 1 then
- for i = 1, depth - 1 do
- out[#out+1] = '{\\c&H999999&} » [' ..
- menus[i][menu_pos[i]].category_name .. ']{\\c}'
+ for i = 2, depth do
+ out[#out+1] = '{\\c&H999999&} » [' .. menu_titles[i] ..
+ ']{\\c}'
end
out[#out+1] = ' ' -- space character for correct line height
end
@@ -68,7 +72,7 @@ local function update_osd()
str = opt.name
end
- if i == menu_pos[depth] then
+ if i == menu_pos[depth] and not search_active then
str = '{\\c&HFF00&}* ' .. str .. '{\\c}'
elseif opt.type == 'category' then
str = '{\\c&H99DDFF&}' .. str .. '{\\c}'
@@ -128,7 +132,15 @@ local function last_option()
advance_cursor(math.huge)
end
-local function add_category_menu(category_id)
+local function push_menu(menu, title)
+ depth = depth + 1
+ menus[depth] = menu or {}
+ menu_top[depth] = 1
+ menu_pos[depth] = 1
+ menu_titles[depth] = title
+end
+
+local function add_category_menu(category_id, category_name)
local menu = {}
for _, v in ipairs(categories) do
if tostring(v.parent_id) == category_id then
@@ -141,10 +153,7 @@ local function add_category_menu(category_id)
end
end
- depth = depth + 1
- menus[depth] = menu
- menu_top[depth] = 1
- menu_pos[depth] = 1
+ push_menu(menu, category_name)
update_osd()
end
@@ -157,7 +166,7 @@ local function select_option()
local opt = menus[depth][menu_pos[depth]]
if opt.type == 'category' then
- add_category_menu(opt.category_id)
+ add_category_menu(opt.category_id, opt.category_name)
else
play_stream(opt.stream_id)
end
@@ -171,18 +180,91 @@ local function prev_menu()
end
local function bind_key(key, name, func, opts)
- key_bindings[#key_bindings+1] = name
+ key_bindings[key] = name
mp.add_forced_key_binding(key, name, func, opts)
end
local function unbind_keys()
- for _, name in ipairs(key_bindings) do
+ for _, name in pairs(key_bindings) do
mp.remove_key_binding(name)
end
key_bindings = {}
end
-local function bind_keys()
+local function search_update()
+ matches = {}
+ for _, v in ipairs(menus[depth-1]) do
+ local name
+ if v.type == 'category' then
+ name = v.category_name
+ else
+ name = v.name
+ end
+
+ if string.find(name, search_text, 0, true) then
+ matches[#matches+1] = v
+ end
+ end
+
+ menus[depth] = matches
+ menu_titles[depth] = 'Searching: ' .. search_text
+end
+
+local function search_input(event)
+ if not (event.event == 'down' or event.event == 'repeat') then
+ return
+ end
+
+ search_text = search_text .. event.key_text
+ search_update()
+ update_osd()
+end
+
+local function search_backspace()
+ search_text = search_text:sub(1, -2)
+ search_update()
+ update_osd()
+end
+
+local bind_search_keys
+local bind_menu_keys
+
+local function search_start()
+ search_active = true
+ push_menu()
+ search_text = ''
+ search_update()
+ update_osd()
+ bind_search_keys()
+end
+
+local function search_finish()
+ search_active = false
+ menu_titles[depth] = 'Search results: ' .. search_text
+ update_osd()
+ bind_menu_keys()
+end
+
+local function search_cancel()
+ search_active = false
+ search_text = ''
+ depth = depth - 1
+ update_osd()
+ bind_menu_keys()
+end
+
+function bind_search_keys()
+ unbind_keys()
+
+ 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)
+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)
@@ -192,6 +274,7 @@ local function bind_keys()
bind_key('END', 'last-option', last_option)
bind_key('ENTER', 'select-option', select_option)
bind_key('BS', 'prev-menu', prev_menu)
+ bind_key('/', 'start-search', search_start)
end
local function toggle_menu()
@@ -200,13 +283,15 @@ local function toggle_menu()
if osd.hidden then
unbind_keys()
+ elseif search_active then
+ bind_search_keys()
else
- bind_keys()
+ bind_menu_keys()
end
end
mp.add_forced_key_binding('TAB', 'toggle-menu', toggle_menu)
-bind_keys()
+bind_menu_keys()
load_data()
add_category_menu('0')
update_osd()