From 44644dfb4e3e19438c6499a6b49458fc8b382ac1 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Wed, 7 Jan 2026 23:25:48 -0800 Subject: more images cleanup --- catalogue.lua | 12 +++++----- main.lua | 44 ++++++++++++++-------------------- osd.lua | 76 ++++++++++++++++++++++++++++++++++++----------------------- xc.lua | 4 ++-- 4 files changed, 72 insertions(+), 64 deletions(-) diff --git a/catalogue.lua b/catalogue.lua index 47a1b7a..d781cf4 100644 --- a/catalogue.lua +++ b/catalogue.lua @@ -7,13 +7,13 @@ local mt = {} mt.__index = mt function catalogue.new() - o = setmetatable({data = {}}, mt) + local t = setmetatable({data = {}}, mt) - o:add({ + t:add({ type = 'group', id = 'root', }) - o:add({ + t:add({ type = 'group', id = 'favourites', parent_id = 'root', @@ -21,7 +21,7 @@ function catalogue.new() lazy = true, -- prevent infinite recursion on search }) - return o + return t end function mt:get(id) @@ -95,14 +95,14 @@ function mt:load_xc_section(section) vv.group_type = 'series' vv.id = section.id .. ':series:' .. v.series_id vv.series_id = v.series_id - vv.image = util.strip_ne(v.cover) + vv.img_url = util.strip_ne(v.cover) vv.lazy = true -- avoid API calls on search else vv.type = 'stream' vv.id = section.id .. ':stream:' .. v.stream_id vv.stream_type = v.stream_type vv.stream_id = v.stream_id - vv.image = util.strip_ne(v.stream_icon) + vv.img_url = util.strip_ne(v.stream_icon) vv.epg_channel_id = util.strip_ne(v.epg_channel_id) end diff --git a/main.lua b/main.lua index d0d2489..6e04001 100644 --- a/main.lua +++ b/main.lua @@ -42,7 +42,20 @@ xc = cacher.wrap(xc, { local catalogue = _catalogue.new() local epg = _epg.new() -local osd = _osd.new() +local osd = _osd.new({ + img_path_func = function(url, cb) + local path = mp_utils.join_path( + config.img_dir, url:gsub('%W', '_')) + + if not mp_utils.file_info(path) and cb then + downloader:schedule(url, path, function(_, path) + cb(path) + end) + end + + return path + end, +}) local key_bindings = {} @@ -54,27 +67,6 @@ local function osd_menu_lines() return osd:menu_lines(state) end --- FIXME leaving this here as a global for now since the image is downloaded --- during osd redraw but this function has other dependencies -function get_image_path(url, dl) - local path = mp_utils.join_path(config.img_dir, url:gsub('%W', '_')) - - local f = mp_utils.file_info(path) - if f then - return path - end - - if dl then - downloader:schedule(url, path, function(_, file) - if osd.img and file == osd.img.path then - osd:draw_image() - end - end) - - return path - end -end - local function load_data() local arr = { {id = 'live', name = 'Live TV', type = 'live'}, @@ -310,12 +302,12 @@ local function series_group_menu_options(series) id = series.section .. ':stream:' .. episode.id, stream_id = episode.id, - image = util.strip_ne( + img_url = util.strip_ne( epinfo.movie_image), } t.info_data = { name = t.name, - cover = t.image, + cover = t.img_url, description = epinfo.plot, releasedate = epinfo.releasedate, duration = epinfo.duration, @@ -338,12 +330,12 @@ local function series_group_menu_options(series) children = episodes, name = util.strip(season.name), info = count, - image = util.strip_ne(season.cover_big) or + img_url = util.strip_ne(season.cover_big) or util.strip_ne(season.cover), } t.info_data = { name = t.name, - cover = t.image, + cover = t.img_url, description = season.overview, releasedate = season.air_date, num_episodes = count, diff --git a/osd.lua b/osd.lua index 7d9089e..2bcfce2 100644 --- a/osd.lua +++ b/osd.lua @@ -34,9 +34,9 @@ local function asscape(str) return mp.command_native({'escape-ass', (str:gsub('\n', ' '))}) end -function osd.new() +function osd.new(init) local lines = math.floor((720 / config.font_size) + 0.5) - 1 - local t = { + local t = setmetatable({ fg = mp.create_osd_overlay('ass-events'), bg = mp.create_osd_overlay('ass-events'), width = 0, @@ -45,10 +45,14 @@ function osd.new() lines = lines, padding = math.floor((720 - (lines * config.font_size)) / 2), img = nil, - } + }, mt) t.bg.z = -1 - return setmetatable(t, mt) + for k, v in pairs(init or {}) do + t[k] = v + end + + return t end function mt:resize(w, h) @@ -167,26 +171,33 @@ function mt:option_path(opt, info) return str end -function mt:load_image() +function mt:load_img() local f = mp_utils.file_info(self.img.path) if not f then return false end - local cmd = 'magick \'' .. self.img.path .. '\'' .. - ' -background none' .. - ' -gravity northwest' .. - ' -depth 8' .. - ' -resize ' .. self.img.cmd.w .. 'x' .. self.img.cmd.h .. - ' -extent ' .. self.img.cmd.w .. 'x' .. self.img.cmd.h .. - ' tmp.bgra' - print('exec: ' .. cmd) - os.execute(cmd) - self.img.loaded = true - return true + local cmd = { + 'magick', self.img.path, + '-background', 'none', + '-gravity', 'northwest', + '-depth', '8', + '-resize', self.img.cmd.w .. 'x' .. self.img.cmd.h, + '-extent', self.img.cmd.w .. 'x' .. self.img.cmd.h, + 'tmp.bgra'} + print('exec: ' .. mp_utils.to_string(cmd)) + local res = mp.command_native({ + name = 'subprocess', + args = cmd, + playback_only = false, + }) + + local loaded = res and res.status == 0 + self.img.loaded = loaded + return loaded end -function mt:clear_image(purge) +function mt:clear_img(purge) if not self.img then return end @@ -197,13 +208,13 @@ function mt:clear_image(purge) end end -function mt:draw_image() +function mt:draw_img() if not self.img then return end - if not (self.img.loaded or self:load_image()) then - self:clear_image() + if not (self.img.loaded or self:load_img()) then + self:clear_img() return end @@ -212,7 +223,7 @@ function mt:draw_image() end end -function mt:update_image(path, menu_res) +function mt:set_img(path, menu_res) -- images require *real* dimensions and coordinates, unlike other OSD -- functions which scale the given values automatically local padding = self.scale * self.padding @@ -329,8 +340,8 @@ function mt:redraw(state) self:option_text(opt, info) .. self:option_path(opt, info) - if selected and opt.image then - img = opt.image + if selected and opt.img_url then + img = opt.img_url end end @@ -349,12 +360,17 @@ function mt:redraw(state) self.fg.compute_bounds = not not img local res = self.fg:update() self.bg:update() - if img then - local path = get_image_path(img, true) - self:update_image(path, res) - self:draw_image() + if img and self.img_path_func then + local path = self.img_path_func(img, function(path) + if self.img and path == self.img.path then + self:draw_img() + end + end) + + self:set_img(path, res) + self:draw_img() else - self:clear_image(true) + self:clear_img(true) end end @@ -367,9 +383,9 @@ function mt:toggle_hidden() self.bg:update() if hidden then - self:clear_image() + self:clear_img() else - self:draw_image() + self:draw_img() end end diff --git a/xc.lua b/xc.lua index 723f9cb..e1f37d7 100644 --- a/xc.lua +++ b/xc.lua @@ -31,7 +31,7 @@ function mt:get(path, params) capture_stdout = true, playback_only = false, }) - if res.status == 0 then + if res and res.status == 0 then return res.stdout end end @@ -103,7 +103,7 @@ json.dump( capture_stdout = true, playback_only = false, }) - if res.status == 0 then + if res and res.status == 0 then return mp_utils.parse_json(res.stdout) end end -- cgit v1.2.3-70-g09d2