Skip to content

Instantly share code, notes, and snippets.

@Shudouken
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shudouken/be8c3b34ebd3ead1b2b8 to your computer and use it in GitHub Desktop.
Save Shudouken/be8c3b34ebd3ead1b2b8 to your computer and use it in GitHub Desktop.
history.lua
--logs filename and timeposition to a history file when closing mpv
--needs wc for word counting
local msg = require 'mp.msg'
-- default options, .conf is read
local options = {
history_file = os.getenv("HOME") .. "/.mpv/history",
line_limit = 20, --set to 0 for unlimited history logging
--by default it will log the last 20 files
}
function append_history()
file_in = mp.get_property("path", "")
file_in = string.gsub(file_in, "'", "'\\''")
timepos = mp.get_property_osd("time-pos")
if timepos == "" then
timepos = "00:00:00"
end
local handle = io.popen('wc -l ' .. options.history_file)
local wc = handle:read("*a")
handle:close()
if wc == "" then
lines = 0
else
lines = string.gsub(wc, "(.*)( )(.*)", "%1")
lines = tonumber(lines)
end
msg.info("lines: " .. lines)
if string.sub(file_in,1,4) == "http" then
title = mp.get_property("media-title")
title = string.gsub(title, "'", "'\\''")
command = "echo '" .. timepos .. " - " .. title .. " " .. file_in .. "' >> " .. options.history_file
else
command = "echo '" .. timepos .. " - " .. file_in .. "' >> " .. options.history_file
end
if options.line_limit ~= 0 and lines > (options.line_limit-1) then
shift = 'echo -e "$(sed \'1d\' ' .. options.history_file .. ')" > ' .. options.history_file
msg.info(shift)
os.execute(shift)
end
msg.info(command)
os.execute(command)
end
mp.add_hook("on_unload", 50, append_history)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment