Skip to content

Commit

Permalink
add mpv-menu-plugin support
Browse files Browse the repository at this point in the history
  • Loading branch information
dyphire committed Jun 9, 2024
1 parent 52f6465 commit 584ac4d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,36 @@ Logs played files to a history log file with an interactive 'recently played' me
* Disabling `auto_save` makes it only save with a keybind
* No save key is bound by default, see `script-opts`
* See comments in the script for more info

### uosc integration

**[tomasklaen/uosc](https://github.com/tomasklaen/uosc) is required.**

[Menu](https://github.com/tomasklaen/uosc#adding-items-to-menu) - add following to `input.conf`.

```ini
KEY script-binding recent/display-recent #! Recently played
```

[Controls](https://github.com/tomasklaen/uosc#set-prop-value) - add following to `uosc.conf#controls`.

```ini
command:history:script-message-to recent display-recent?Recently played
```

Play most recent one.

```ini
KEY script-binding recent/display-recent
```

### mpv-menu-plugin integration

**[mpv-menu-plugin](https://github.com/tsl0922/mpv-menu-plugin) is required.**

[Menu](https://github.com/tsl0922/mpv-menu-plugin?tab=readme-ov-file#messages) - add following to `input.conf`.

```ini
KEY script-binding recent/display-recent #! Recently played #@recent
```

57 changes: 54 additions & 3 deletions recent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ local o = {
hi_color = "H46CFFF",
-- Draw ellipsis at start/end denoting ommited entries
ellipsis = false,
--Change maximum number to show items on uosc-submenu
--Change maximum number to show items on submenu
list_show_amount = 20,
}
(require "mp.options").read_options(o)
Expand Down Expand Up @@ -69,6 +69,14 @@ local function utf8_sub(s, i, j)
return table.concat(t)
end

function split_ext(filename)
local idx = filename:match(".+()%.%w+$")
if idx then
filename = filename:sub(1, idx - 1)
end
return filename
end

function striptitle(str)
if o.slice_longfilenames and str:len() > o.slice_longfilenames_amount + 5 then
str = utf8_sub(str, 1, o.slice_longfilenames_amount) .. "..."
Expand All @@ -84,6 +92,11 @@ function get_ext(path)
end
end

function get_filename(path)
local dir, filename = utils.split_path(path)
return filename
end

function get_path()
local path = mp.get_property("path")
local title = mp.get_property("media-title"):gsub("\"", "")
Expand Down Expand Up @@ -142,6 +155,34 @@ function read_log_table()
end)
end

local dyn_menu = {
ready = false,
type = 'submenu',
submenu = {}
}

function update_dyn_menu_items()
local lists = read_log_table()
if not lists or not lists[1] then
return
end
local menu = {}
if #lists > o.list_show_amount then
length = o.list_show_amount
else
length = #lists
end
for i = 1, length do
menu[#menu + 1] = {
title = string.format('%s\t%s', o.show_paths and striptitle(split_ext(get_filename(lists[#lists-i+1].path)))
or striptitle(split_ext(lists[#lists-i+1].title)), get_ext(lists[#lists-i+1].path)),
cmd = string.format("loadfile '%s'", lists[#lists-i+1].path),
}
end
dyn_menu.submenu = menu
mp.commandv('script-message-to', 'dyn_menu', 'update', 'recent', utils.format_json(dyn_menu))
end

-- Write path to log on file end
-- removing duplicates along the way
function write_log(delete)
Expand All @@ -163,6 +204,9 @@ function write_log(delete)
f:write(("[%s] \"%s\" | %s\n"):format(os.date(o.date_format), cur_title, cur_path))
end
f:close()
if dyn_menu.ready then
update_dyn_menu_items()
end
end

-- Display list on OSD and terminal
Expand All @@ -183,7 +227,7 @@ function draw_list(list, start, choice)
local p
if o.show_paths then
if o.split_paths and not is_protocol(list[size-start-i+1].path) then
_, p = utils.split_path(list[size-start-i+1].path)
p = get_filename(list[size-start-i+1].path)
else
p = list[size-start-i+1].title or ""
end
Expand Down Expand Up @@ -262,7 +306,8 @@ function open_menu(lists)
end
for i = 1, length do
menu.items[i] = {
title = o.show_paths and striptitle(lists[#lists-i+1].path) or striptitle(lists[#lists-i+1].title),
title = o.show_paths and striptitle(split_ext(get_filename(lists[#lists-i+1].path)))
or striptitle(split_ext(lists[#lists-i+1].title)),
hint = get_ext(lists[#lists-i+1].path),
value = { "loadfile", lists[#lists-i+1].path, "replace" },
}
Expand Down Expand Up @@ -351,6 +396,12 @@ else
end)
end

-- mpv-menu-plugin integration
mp.register_script_message('menu-ready', function()
dyn_menu.ready = true
update_dyn_menu_items()
end)

-- check if uosc is running
mp.register_script_message('uosc-version', function(version)
uosc_available = true
Expand Down

0 comments on commit 584ac4d

Please sign in to comment.