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 with watch later config files. Requires
--write-filename-in-watch-later-config.
  • Loading branch information
guidocella committed Dec 5, 2024
1 parent 1b1a8a3 commit 4d4f455
Show file tree
Hide file tree
Showing 3 changed files with 52 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
46 changes: 46 additions & 0 deletions player/lua/select.lua
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,52 @@ 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("watch-later-dir")
watch_later_dir = mp.command_native({"expand-path",
watch_later_dir ~= "" and watch_later_dir or "~~state/watch_later"})
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

table.sort(watch_later_files, function (i, j)
return utils.file_info(i).mtime > utils.file_info(j).mtime
end)

local recent_files = {}
for _, watch_later_file in ipairs(watch_later_files) do
watch_later_file = io.open(watch_later_file)
if watch_later_file then
local line = watch_later_file:read()
if line and line ~= "# redirect entry" and line:find("^#") then
recent_files[#recent_files + 1] = line:sub(3)
end
watch_later_file: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

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 4d4f455

Please sign in to comment.