Skip to content

Commit

Permalink
select.lua: select recent files with g-h
Browse files Browse the repository at this point in the history
Implement selection of files from watch later config files. Requires
--write-filename-in-watch-later-config.
  • Loading branch information
guidocella committed Dec 10, 2024
1 parent 42f6ea3 commit 496b2d9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
5 changes: 5 additions & 0 deletions DOCS/man/mpv.rst
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ g-l
g-d
Select an audio device.

g-h
Select a file from the history of files whose playback position has been
saved (see `RESUMING PLAYBACK`_). Requires
``--write-filename-in-watch-later-config``.

g-b
Select a defined input binding.

Expand Down
1 change: 1 addition & 0 deletions etc/input.conf
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
#g-e script-binding select/select-edition
#g-l script-binding select/select-subtitle-line
#g-d script-binding select/select-audio-device
#g-h script-binding select/select-history
#g-b script-binding select/select-binding
#g-r script-binding select/show-properties

Expand Down
54 changes: 54 additions & 0 deletions player/lua/select.lua
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,60 @@ mp.add_key_binding(nil, "select-audio-device", function ()
})
end)

mp.add_key_binding(nil, "select-history", function ()
local watch_later_dir = mp.get_property("current-watch-later-dir")

if not watch_later_dir then
show_error("No watch later files found.")
return
end

local watch_later_files = {}

for i, file in ipairs(utils.readdir(watch_later_dir, "files") or {}) do
watch_later_files[i] = watch_later_dir .. "/" .. file
end

if #watch_later_files == 0 then
show_error("No watch later files found.")
return
end

local recent_files = {}
for _, watch_later_file in pairs(watch_later_files) do
local file_handle = io.open(watch_later_file)
if file_handle then
local line = file_handle:read()
if line and line ~= "# redirect entry" and line:find("^#") then
recent_files[#recent_files + 1] = {watch_later_file, line:sub(3)}
end
file_handle:close()
end
end

if #recent_files == 0 then
show_error("No watch later files with media paths found. " ..
"Enable --write-filename-in-watch-later-config to select recent files.")
return
end

table.sort(recent_files, function (i, j)
return utils.file_info(i[1]).mtime > utils.file_info(j[1]).mtime
end)

for i, recent_file in ipairs(recent_files) do
recent_files[i] = recent_file[2]
end

input.select({
prompt = "Select a recent file:",
items = recent_files,
submit = function (i)
mp.commandv("loadfile", recent_files[i])
end,
})
end)

mp.add_key_binding(nil, "select-binding", function ()
local bindings = {}

Expand Down

0 comments on commit 496b2d9

Please sign in to comment.