summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.lua124
1 files changed, 63 insertions, 61 deletions
diff --git a/main.lua b/main.lua
index 586d536..184e98a 100644
--- a/main.lua
+++ b/main.lua
@@ -3,7 +3,7 @@ local utils = require('mp.utils')
local script_dir = mp.get_script_directory()
local stream_prefix = mp.get_opt('iptv_menu.stream_prefix')
-function load_json_file(path)
+local function load_json_file(path)
local f = io.open(script_dir .. '/' .. path, 'r')
local data = f:read('*all')
f:close()
@@ -21,11 +21,11 @@ local menus = {categories}
local menu_top = {1}
local menu_pos = {1}
-function osd_menu_lines()
+local function osd_menu_lines()
return osd_lines - depth + 1
end
-function update_osd()
+local function update_osd()
local out = {}
for i = 1, depth - 1 do
out[#out+1] = '{\\c&H999999&}[' ..
@@ -46,49 +46,52 @@ function update_osd()
osd:update()
end
-function handle_up()
- if menu_pos[depth] <= 1 then
- return
+local function advance_cursor(n, opts)
+ pos = math.max(1, math.min(menu_pos[depth] + n, #menus[depth]))
+ top = menu_top[depth]
+ if opts and opts.advance_page then
+ top = top + n
end
- menu_pos[depth] = menu_pos[depth] - 1
- if menu_pos[depth] < menu_top[depth] then
- menu_top[depth] = menu_top[depth] - 1
+ -- move page to keep selected option visible
+ if pos < top then
+ top = pos
+ elseif pos > top + osd_menu_lines() - 1 then
+ top = pos - osd_menu_lines() + 1
end
+ top = math.max(1, math.min(top, #menus[depth] - osd_menu_lines() + 1))
+
+ menu_pos[depth] = pos
+ menu_top[depth] = top
update_osd()
end
-function handle_down()
- if menu_pos[depth] >= #menus[depth] then
- return
- end
-
- menu_pos[depth] = menu_pos[depth] + 1
- if menu_pos[depth] > menu_top[depth] + osd_menu_lines() - 1 then
- menu_top[depth] = menu_top[depth] + 1
- end
+local function next_option()
+ advance_cursor(1)
+end
- update_osd()
+local function prev_option()
+ advance_cursor(-1)
end
-function handle_page_up()
- menu_pos[depth] = math.max(menu_pos[depth] - osd_menu_lines(), 1)
- menu_top[depth] = math.max(menu_top[depth] - osd_menu_lines(), 1)
+local function next_page()
+ advance_cursor(osd_menu_lines(), {advance_page=true})
+end
- update_osd()
+local function prev_page()
+ advance_cursor(-osd_menu_lines(), {advance_page=true})
end
-function handle_page_down()
- menu_pos[depth] = math.min(menu_pos[depth] + osd_menu_lines(),
- #menus[depth])
- menu_top[depth] = math.min(menu_top[depth] + osd_menu_lines(),
- math.max(1, #menus[depth] - osd_menu_lines() + 1))
+local function first_option()
+ advance_cursor(-math.huge)
+end
- update_osd()
+local function last_option()
+ advance_cursor(math.huge)
end
-function select_category()
+local function select_category()
local cat = categories[menu_pos[depth]]
local menu = {}
for _, v in ipairs(streams) do
@@ -101,16 +104,15 @@ function select_category()
menus[depth] = menu
menu_top[depth] = 1
menu_pos[depth] = 1
-
update_osd()
end
-function select_stream()
+local function select_stream()
local url = stream_prefix .. menus[depth][menu_pos[depth]]['stream_id']
mp.commandv('loadfile', url)
end
-function handle_enter()
+local function select_option()
if depth == 1 then
select_category()
else
@@ -118,50 +120,50 @@ function handle_enter()
end
end
-function handle_backspace()
+local function prev_menu()
if depth > 1 then
depth = depth - 1
- end
-
- update_osd()
-end
-
-function toggle_menu()
- osd.hidden = not osd.hidden
- osd:update()
-
- if osd.hidden then
- unbind_keys()
- else
- bind_keys()
+ update_osd()
end
end
local key_bindings = {}
-function bind_key(key, func, opts)
- key_bindings[#key_bindings+1] = key
- mp.add_forced_key_binding(key, key, func, opts)
+local function bind_key(key, name, func, opts)
+ key_bindings[#key_bindings+1] = name
+ mp.add_forced_key_binding(key, name, func, opts)
end
-function unbind_keys()
- for _, key in ipairs(key_bindings) do
- mp.remove_key_binding(key)
+local function unbind_keys()
+ for _, name in ipairs(key_bindings) do
+ mp.remove_key_binding(name)
end
key_bindings = {}
end
-function bind_keys()
+local function bind_keys()
local repeatable = {repeatable=true}
- bind_key('UP', handle_up, repeatable)
- bind_key('DOWN', handle_down, repeatable)
- bind_key('PGUP', handle_page_up, repeatable)
- bind_key('PGDWN', handle_page_down, repeatable)
- bind_key('ENTER', handle_enter)
- bind_key('BS', handle_backspace)
+ 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)
+ bind_key('HOME', 'first-option', first_option)
+ bind_key('END', 'last-option', last_option)
+ bind_key('ENTER', 'select-option', select_option)
+ bind_key('BS', 'prev-menu', prev_menu)
+end
+
+local function toggle_menu()
+ osd.hidden = not osd.hidden
+ osd:update()
+
+ if osd.hidden then
+ unbind_keys()
+ else
+ bind_keys()
+ end
end
mp.add_forced_key_binding('TAB', 'toggle-menu', toggle_menu)
bind_keys()
-
update_osd()