Skip to content

Commit

Permalink
add playlist support
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Jan 12, 2024
1 parent 32bd75c commit 2521a33
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The menu syntax is similar to [mpv.net](https://github.com/mpvnet-player/mpv.net
- `#@chapters`: chapter list
- `#@editions`: edition list
- `#@audio-devices`: audio device list
- `#@playlist`: playlist
- `#@profiles`: profile list
- use `_` if no keybinding
- use `ignore` if no command
Expand Down
35 changes: 35 additions & 0 deletions lua/dyn_menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
-- #@chapters: chapter list
-- #@editions: edition list
-- #@audio-devices: audio device list
-- #@playlist: playlist
-- #@profiles: profile list

local opts = require('mp.options')
local utils = require('mp.utils')

-- user options
local o = {
Expand Down Expand Up @@ -257,6 +259,37 @@ local function update_audio_devices_menu(submenu)
end)
end

-- build playlist item title
local function build_playlist_title(item, id)
local title = item.title or ''
local ext = ''
if item.filename and item.filename ~= '' then
local _, filename = utils.split_path(item.filename)
local n, e = filename:match('^(.+)%.([%w-_]+)$')
if title == '' then title = n and n or filename end
if e then ext = e end
end
title = title ~= '' and abbr_title(title) or 'Item ' .. id
return ext ~= '' and title .. "\t" .. ext:upper() or title
end

-- handle #@playlist menu update
local function update_playlist_menu(submenu)
mp.observe_property('playlist', 'native', function(_, playlist)
for i = #submenu, 1, -1 do table.remove(submenu, i) end
menu_items_dirty = true
if not playlist or #playlist == 0 then return end

for id, item in ipairs(playlist) do
submenu[#submenu + 1] = {
title = build_playlist_title(item, id - 1),
cmd = string.format('playlist-play-index %d', id - 1),
state = item.current and { 'checked' } or {},
}
end
end)
end

-- handle #@profiles menu update
local function update_profiles_menu(submenu)
mp.observe_property('profile-list', 'native', function(_, profile_list)
Expand Down Expand Up @@ -299,6 +332,8 @@ local function dyn_menu_update(item, keyword)
update_editions_menu(item.submenu)
elseif keyword == 'audio-devices' then
update_audio_devices_menu(item.submenu)
elseif keyword == 'playlist' then
update_playlist_menu(item.submenu)
elseif keyword == 'profiles' then
update_profiles_menu(item.submenu)
end
Expand Down

0 comments on commit 2521a33

Please sign in to comment.