select.lua: select from the watch history with g-h

Implement selection of the entries in the watch history.

The last entry in the selector deletes the history file.
This commit is contained in:
Guido Cella
2025-01-26 08:42:58 +01:00
committed by Kacper Michajłow
parent b75ed73f4f
commit e2090533cf
4 changed files with 94 additions and 1 deletions

View File

@@ -18,6 +18,13 @@ License along with mpv. If not, see <http://www.gnu.org/licenses/>.
local utils = require "mp.utils"
local input = require "mp.input"
local options = {
history_date_format = "%Y-%m-%d %H:%M:%S",
hide_history_duplicates = true,
}
require "mp.options".read_options(options, nil, function () end)
local function show_warning(message)
mp.msg.warn(message)
if mp.get_property_native("vo-configured") then
@@ -353,6 +360,87 @@ mp.add_key_binding(nil, "select-audio-device", function ()
})
end)
local function format_history_entry(entry)
local status
status, entry.time = pcall(os.date, options.history_date_format, entry.time)
if not status then
mp.msg.warn(entry.time)
end
local item = "(" .. entry.time .. ") "
if entry.title then
return item .. entry.title .. " (" .. entry.path .. ")"
end
if entry.path:find("://") then
return item .. entry.path
end
local directory, filename = utils.split_path(entry.path)
return item .. filename .. " (" .. directory .. ")"
end
mp.add_key_binding(nil, "select-watch-history", function ()
local history_file_path = mp.command_native(
{"expand-path", mp.get_property("watch-history-path")})
local history_file, error_message = io.open(history_file_path)
if not history_file then
show_warning(mp.get_property_native("save-watch-history")
and error_message
or "Enable --save-watch-history")
return
end
local all_entries = {}
local line_num = 1
for line in history_file:lines() do
local entry = utils.parse_json(line)
if entry and entry.path then
all_entries[#all_entries + 1] = entry
else
mp.msg.warn(history_file_path .. ": Parse error at line " .. line_num)
end
line_num = line_num + 1
end
history_file:close()
local entries = {}
local items = {}
local seen = {}
for i = #all_entries, 1, -1 do
local entry = all_entries[i]
if not seen[entry.path] or not options.hide_history_duplicates then
seen[entry.path] = true
entries[#entries + 1] = entry
items[#items + 1] = format_history_entry(entry)
end
end
items[#items+1] = "Clear history"
input.select({
prompt = "Select a file:",
items = items,
submit = function (i)
if entries[i] then
mp.commandv("loadfile", entries[i].path)
return
end
error_message = select(2, os.remove(history_file_path))
if error_message then
show_error(error_message)
else
mp.osd_message("History cleared.")
end
end,
})
end)
mp.add_key_binding(nil, "select-watch-later", function ()
local watch_later_dir = mp.get_property("current-watch-later-dir")