diff options
| -rw-r--r-- | config.lua | 6 | ||||
| -rw-r--r-- | main.lua | 133 | ||||
| -rw-r--r-- | osd.lua | 2 |
3 files changed, 114 insertions, 27 deletions
@@ -27,7 +27,9 @@ config.colours = { icon_missing = 'ff0000', } -config.cache_dir = mp_utils.join_path(mp.get_script_directory(), 'cache') -config.img_dir = mp_utils.join_path(mp.get_script_directory(), 'img') +local script_dir = mp.get_script_directory() +config.cache_dir = mp_utils.join_path(script_dir, 'cache') +config.img_dir = mp_utils.join_path(script_dir, 'img') +config.favourites_file = mp_utils.join_path(script_dir, 'favourites.json') return config @@ -92,12 +92,20 @@ local function load_data() epg:load_xc_data(xc:get_epg()) - favourites = util.read_json_file( - mp_utils.join_path(script_dir, 'favourites.json')) - -- json loading/dumping breaks when the table is empty, so we need a - -- dummy value to prevent that - if next(favourites) == nil then - favourites = {oi = true} + local t = util.read_json_file(config.favourites_file) + favourites = t.favourites or {} +end + +local function save_favourites() + util.write_json_file(config.favourites_file, {favourites = favourites}) +end + +-- returns index or nil +function favourited(id) + for i, v in ipairs(favourites) do + if v == id then + return i + end end end @@ -124,7 +132,10 @@ local function set_cursor(pos, opts) menu.cursor = pos menu.view_top = top - update_osd() + + if not (opts and opts.skip_redraw) then + update_osd() + end end local function cursor_up() @@ -162,6 +173,76 @@ local function cursor_to_object(id) end end +-- inserts the given id into the favourites array before the next favourited +-- menu option, starting from the next cursor position, or the end if no such +-- option is found. this is meant for in-place favouriting from the favourites +-- menu. +local function insert_favourite_before_next_in_menu(id) + local menu = menus[depth] + for i = menu.cursor+1, #menu.options do + local ind = favourited(menu.options[i].id) + if ind then + table.insert(favourites, ind, id) + return + end + end + + favourites[#favourites+1] = id +end + +local function move_option(pos, opts) + local menu = menus[depth] + if menu.group_id ~= 'favourites' or menu.sorted then + return + end + + local prev_cursor = menu.cursor + local opts = opts or {} + opts.skip_redraw = true + set_cursor(pos, opts) + if menu.cursor == prev_cursor then + return + end + + local opt = table.remove(menu.options, prev_cursor) + table.insert(menu.options, menu.cursor, opt) + + local ind = favourited(opt.id) + if ind then + table.remove(favourites, ind) + insert_favourite_before_next_in_menu(opt.id) + save_favourites() + end + + update_osd() +end + +local function move_option_up() + move_option(menus[depth].cursor - 1) +end + +local function move_option_down() + move_option(menus[depth].cursor + 1) +end + +local function move_option_start() + move_option(0) +end + +local function move_option_end() + move_option(#menus[depth].options) +end + +local function move_option_page_up() + move_option( + menus[depth].cursor - osd_menu_lines(), {keep_offset = true}) +end + +local function move_option_page_down() + move_option( + menus[depth].cursor + osd_menu_lines(), {keep_offset = true}) +end + local function push_menu(t) local menu = { options = {}, @@ -184,7 +265,7 @@ local function sort_options(options) if v.missing then score = score - 4 end - if favourites[v.id] then + if favourited(v.id) then score = score + 2 end if v.type == 'group' and v.group_type ~= 'series' then @@ -228,21 +309,15 @@ local function group_count(group) end return count elseif group.id == 'favourites' then - -- count number of favourited items, not recursive - local count = 0 - for k in pairs(favourites) do - if k ~= 'oi' then - count = count + 1 - end - end - return count + -- not recursive + return #favourites end end local function favourites_group_menu_options(group) local options = {} local time = os.time() - for id in pairs(favourites) do + for _, id in ipairs(favourites) do local obj = catalogue:get(id) if obj then local path = {} @@ -264,7 +339,7 @@ local function favourites_group_menu_options(group) obj.path = path end options[#options+1] = obj - elseif id ~= 'oi' then -- ignore dummy value + else -- display missing favourites so that they can be -- removed options[#options+1] = { @@ -395,15 +470,16 @@ local function favourite_option() return end - local id = opt.id - if favourites[id] then - favourites[id] = nil + local ind = favourited(opt.id) + if ind then + table.remove(favourites, ind) + elseif menu.group_id == 'favourites' then + insert_favourite_before_next_in_menu(opt.id) else - favourites[id] = true + favourites[#favourites+1] = opt.id end - util.write_json_file(mp_utils.join_path(script_dir, 'favourites.json'), - favourites) + save_favourites() update_osd() end @@ -941,6 +1017,15 @@ function bind_menu_keys() bind_key('END', cursor_end) bind_key('PGUP', cursor_page_up, {repeatable = true}) bind_key('PGDWN', cursor_page_down, {repeatable = true}) + + bind_key('J', move_option_down, {repeatable = true}) + bind_key('K', move_option_up, {repeatable = true}) + bind_key('Shift+UP', move_option_up, {repeatable = true}) + bind_key('Shift+DOWN', move_option_down, {repeatable = true}) + bind_key('Shift+HOME', move_option_start) + bind_key('Shift+END', move_option_end) + bind_key('Shift+PGUP', move_option_page_up, {repeatable = true}) + bind_key('Shift+PGDWN', move_option_page_down, {repeatable = true}) end -- uses enable-section and disable-section to disable builtin key bindings @@ -279,7 +279,7 @@ function mt:redraw(menus, depth, favourites, playing_id) empty = (opt.type == 'group' and not opt.lazy and #opt.children == 0), playing = not not (opt.id and opt.id == playing_id), - favourited = not not (opt.id and favourites[opt.id]), + favourited = not not (opt.id and favourited(opt.id)), } out[#out+1] = self:option_icons(opt, info) .. self:option_text(opt, info) .. |
