lua: Format with stylua

This commit is contained in:
Marty Oehme 2023-06-15 10:12:30 +02:00
parent e434c191c9
commit 5f93ecba7c
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A
33 changed files with 4062 additions and 3413 deletions

View file

@ -21,72 +21,110 @@ audio=yes
MAXENTRIES = 5000
local msg = require 'mp.msg'
local options = require 'mp.options'
local utils = require 'mp.utils'
local msg = require("mp.msg")
local options = require("mp.options")
local utils = require("mp.utils")
o = {
disabled = false,
images = true,
videos = true,
audio = true
disabled = false,
images = true,
videos = true,
audio = true,
}
options.read_options(o)
function Set (t)
local set = {}
for _, v in pairs(t) do set[v] = true end
return set
function Set(t)
local set = {}
for _, v in pairs(t) do
set[v] = true
end
return set
end
function SetUnion (a,b)
local res = {}
for k in pairs(a) do res[k] = true end
for k in pairs(b) do res[k] = true end
return res
function SetUnion(a, b)
local res = {}
for k in pairs(a) do
res[k] = true
end
for k in pairs(b) do
res[k] = true
end
return res
end
EXTENSIONS_VIDEO = Set {
'mkv', 'avi', 'mp4', 'ogv', 'webm', 'rmvb', 'flv', 'wmv', 'mpeg', 'mpg', 'm4v', '3gp'
}
EXTENSIONS_VIDEO = Set({
"mkv",
"avi",
"mp4",
"ogv",
"webm",
"rmvb",
"flv",
"wmv",
"mpeg",
"mpg",
"m4v",
"3gp",
})
EXTENSIONS_AUDIO = Set {
'mp3', 'wav', 'ogm', 'flac', 'm4a', 'wma', 'ogg', 'opus'
}
EXTENSIONS_AUDIO = Set({
"mp3",
"wav",
"ogm",
"flac",
"m4a",
"wma",
"ogg",
"opus",
})
EXTENSIONS_IMAGES = Set {
'jpg', 'jpeg', 'png', 'tif', 'tiff', 'gif', 'webp', 'svg', 'bmp'
}
EXTENSIONS_IMAGES = Set({
"jpg",
"jpeg",
"png",
"tif",
"tiff",
"gif",
"webp",
"svg",
"bmp",
})
EXTENSIONS = Set {}
if o.videos then EXTENSIONS = SetUnion(EXTENSIONS, EXTENSIONS_VIDEO) end
if o.audio then EXTENSIONS = SetUnion(EXTENSIONS, EXTENSIONS_AUDIO) end
if o.images then EXTENSIONS = SetUnion(EXTENSIONS, EXTENSIONS_IMAGES) end
EXTENSIONS = Set({})
if o.videos then
EXTENSIONS = SetUnion(EXTENSIONS, EXTENSIONS_VIDEO)
end
if o.audio then
EXTENSIONS = SetUnion(EXTENSIONS, EXTENSIONS_AUDIO)
end
if o.images then
EXTENSIONS = SetUnion(EXTENSIONS, EXTENSIONS_IMAGES)
end
function add_files_at(index, files)
index = index - 1
local oldcount = mp.get_property_number("playlist-count", 1)
for i = 1, #files do
mp.commandv("loadfile", files[i], "append")
mp.commandv("playlist-move", oldcount + i - 1, index + i - 1)
end
index = index - 1
local oldcount = mp.get_property_number("playlist-count", 1)
for i = 1, #files do
mp.commandv("loadfile", files[i], "append")
mp.commandv("playlist-move", oldcount + i - 1, index + i - 1)
end
end
function get_extension(path)
match = string.match(path, "%.([^%.]+)$" )
if match == nil then
return "nomatch"
else
return match
end
match = string.match(path, "%.([^%.]+)$")
if match == nil then
return "nomatch"
else
return match
end
end
table.filter = function(t, iter)
for i = #t, 1, -1 do
if not iter(t[i]) then
table.remove(t, i)
end
end
for i = #t, 1, -1 do
if not iter(t[i]) then
table.remove(t, i)
end
end
end
-- splitbynum and alnumcomp from alphanum.lua (C) Andre Bogus
@ -95,126 +133,136 @@ end
-- split a string into a table of number and string values
function splitbynum(s)
local result = {}
for x, y in (s or ""):gmatch("(%d*)(%D*)") do
if x ~= "" then table.insert(result, tonumber(x)) end
if y ~= "" then table.insert(result, y) end
end
return result
local result = {}
for x, y in (s or ""):gmatch("(%d*)(%D*)") do
if x ~= "" then
table.insert(result, tonumber(x))
end
if y ~= "" then
table.insert(result, y)
end
end
return result
end
function clean_key(k)
k = (' '..k..' '):gsub("%s+", " "):sub(2, -2):lower()
return splitbynum(k)
k = (" " .. k .. " "):gsub("%s+", " "):sub(2, -2):lower()
return splitbynum(k)
end
-- compare two strings
function alnumcomp(x, y)
local xt, yt = clean_key(x), clean_key(y)
for i = 1, math.min(#xt, #yt) do
local xe, ye = xt[i], yt[i]
if type(xe) == "string" then ye = tostring(ye)
elseif type(ye) == "string" then xe = tostring(xe) end
if xe ~= ye then return xe < ye end
end
return #xt < #yt
local xt, yt = clean_key(x), clean_key(y)
for i = 1, math.min(#xt, #yt) do
local xe, ye = xt[i], yt[i]
if type(xe) == "string" then
ye = tostring(ye)
elseif type(ye) == "string" then
xe = tostring(xe)
end
if xe ~= ye then
return xe < ye
end
end
return #xt < #yt
end
local autoloaded = nil
function find_and_add_entries()
local path = mp.get_property("path", "")
local dir, filename = utils.split_path(path)
msg.trace(("dir: %s, filename: %s"):format(dir, filename))
if o.disabled then
msg.verbose("stopping: autoload disabled")
return
elseif #dir == 0 then
msg.verbose("stopping: not a local path")
return
end
local path = mp.get_property("path", "")
local dir, filename = utils.split_path(path)
msg.trace(("dir: %s, filename: %s"):format(dir, filename))
if o.disabled then
msg.verbose("stopping: autoload disabled")
return
elseif #dir == 0 then
msg.verbose("stopping: not a local path")
return
end
local pl_count = mp.get_property_number("playlist-count", 1)
-- check if this is a manually made playlist
if (pl_count > 1 and autoloaded == nil) or
(pl_count == 1 and EXTENSIONS[string.lower(get_extension(filename))] == nil) then
msg.verbose("stopping: manually made playlist")
return
else
autoloaded = true
end
local pl_count = mp.get_property_number("playlist-count", 1)
-- check if this is a manually made playlist
if
(pl_count > 1 and autoloaded == nil)
or (pl_count == 1 and EXTENSIONS[string.lower(get_extension(filename))] == nil)
then
msg.verbose("stopping: manually made playlist")
return
else
autoloaded = true
end
local pl = mp.get_property_native("playlist", {})
local pl_current = mp.get_property_number("playlist-pos-1", 1)
msg.trace(("playlist-pos-1: %s, playlist: %s"):format(pl_current,
utils.to_string(pl)))
local pl = mp.get_property_native("playlist", {})
local pl_current = mp.get_property_number("playlist-pos-1", 1)
msg.trace(("playlist-pos-1: %s, playlist: %s"):format(pl_current, utils.to_string(pl)))
local files = utils.readdir(dir, "files")
if files == nil then
msg.verbose("no other files in directory")
return
end
table.filter(files, function (v, k)
if string.match(v, "^%.") then
return false
end
local ext = get_extension(v)
if ext == nil then
return false
end
return EXTENSIONS[string.lower(ext)]
end)
table.sort(files, alnumcomp)
local files = utils.readdir(dir, "files")
if files == nil then
msg.verbose("no other files in directory")
return
end
table.filter(files, function(v, k)
if string.match(v, "^%.") then
return false
end
local ext = get_extension(v)
if ext == nil then
return false
end
return EXTENSIONS[string.lower(ext)]
end)
table.sort(files, alnumcomp)
if dir == "." then
dir = ""
end
if dir == "." then
dir = ""
end
-- Find the current pl entry (dir+"/"+filename) in the sorted dir list
local current
for i = 1, #files do
if files[i] == filename then
current = i
break
end
end
if current == nil then
return
end
msg.trace("current file position in files: "..current)
-- Find the current pl entry (dir+"/"+filename) in the sorted dir list
local current
for i = 1, #files do
if files[i] == filename then
current = i
break
end
end
if current == nil then
return
end
msg.trace("current file position in files: " .. current)
local append = {[-1] = {}, [1] = {}}
for direction = -1, 1, 2 do -- 2 iterations, with direction = -1 and +1
for i = 1, MAXENTRIES do
local file = files[current + i * direction]
local pl_e = pl[pl_current + i * direction]
if file == nil or file[1] == "." then
break
end
local append = { [-1] = {}, [1] = {} }
for direction = -1, 1, 2 do -- 2 iterations, with direction = -1 and +1
for i = 1, MAXENTRIES do
local file = files[current + i * direction]
local pl_e = pl[pl_current + i * direction]
if file == nil or file[1] == "." then
break
end
local filepath = dir .. file
if pl_e then
-- If there's a playlist entry, and it's the same file, stop.
msg.trace(pl_e.filename.." == "..filepath.." ?")
if pl_e.filename == filepath then
break
end
end
local filepath = dir .. file
if pl_e then
-- If there's a playlist entry, and it's the same file, stop.
msg.trace(pl_e.filename .. " == " .. filepath .. " ?")
if pl_e.filename == filepath then
break
end
end
if direction == -1 then
if pl_current == 1 then -- never add additional entries in the middle
msg.info("Prepending " .. file)
table.insert(append[-1], 1, filepath)
end
else
msg.info("Adding " .. file)
table.insert(append[1], filepath)
end
end
end
if direction == -1 then
if pl_current == 1 then -- never add additional entries in the middle
msg.info("Prepending " .. file)
table.insert(append[-1], 1, filepath)
end
else
msg.info("Adding " .. file)
table.insert(append[1], filepath)
end
end
end
add_files_at(pl_current + 1, append[1])
add_files_at(pl_current, append[-1])
add_files_at(pl_current + 1, append[1])
add_files_at(pl_current, append[-1])
end
mp.register_event("start-file", find_and_add_entries)

View file

@ -1,5 +1,5 @@
-- If the laptop is on battery, the profile 'lq' will be loaded; otherwise 'hq' is used
local mp = require 'mp'
local mp = require("mp")
local SHOULD_ADJUST = false
@ -7,27 +7,31 @@ local lqprofile = "lowquality"
local hqprofile = "highquality"
local function powerstate()
local f = io.open("/sys/class/power_supply/AC/online")
if f == nil then return end
local t = f:read("*n")
f:close()
return t
local f = io.open("/sys/class/power_supply/AC/online")
if f == nil then
return
end
local t = f:read("*n")
f:close()
return t
end
local function adjust()
if not SHOULD_ADJUST then return end
if not SHOULD_ADJUST then
return
end
local state = powerstate()
-- this actually overrides automatically applied profiles
-- like 'protocol.http'
if state == 0 then
mp.set_property("profile", lqprofile)
mp.msg.info("[quality] running battery, setting low-quality options.")
mp.osd_message("[quality] LQ")
else
mp.set_property("profile", hqprofile)
mp.msg.info("[quality] running ac, setting high-quality options.")
mp.osd_message("[quality] HQ")
end
local state = powerstate()
-- this actually overrides automatically applied profiles
-- like 'protocol.http'
if state == 0 then
mp.set_property("profile", lqprofile)
mp.msg.info("[quality] running battery, setting low-quality options.")
mp.osd_message("[quality] LQ")
else
mp.set_property("profile", hqprofile)
mp.msg.info("[quality] running ac, setting high-quality options.")
mp.osd_message("[quality] HQ")
end
end
mp.add_hook("on_load", 1, adjust)

View file

@ -1,5 +1,5 @@
local mp = require 'mp'
require 'mp.msg'
local mp = require("mp")
require("mp.msg")
-- Copy the current time of the video to clipboard.
@ -8,73 +8,76 @@ UNIX = 3
KEY_BIND = "y"
local function platform_type()
local utils = require 'mp.utils'
local workdir = utils.to_string(mp.get_property_native("working-directory"))
if string.find(workdir, "\\") then
return WINDOWS
else
return UNIX
end
local utils = require("mp.utils")
local workdir = utils.to_string(mp.get_property_native("working-directory"))
if string.find(workdir, "\\") then
return WINDOWS
else
return UNIX
end
end
local function command_exists(cmd)
local pipe = io.popen("type " .. cmd .. " > /dev/null 2> /dev/null; printf \"$?\"", "r")
if not pipe then return end
local exists = pipe:read() == "0"
pipe:close()
return exists
local pipe = io.popen("type " .. cmd .. ' > /dev/null 2> /dev/null; printf "$?"', "r")
if not pipe then
return
end
local exists = pipe:read() == "0"
pipe:close()
return exists
end
local function get_clipboard_cmd()
if command_exists("xclip") then
return "xclip -silent -in -selection clipboard"
elseif command_exists("wl-copy") then
return "wl-copy"
elseif command_exists("pbcopy") then
return "pbcopy"
else
mp.msg.error("No supported clipboard command found")
return false
end
if command_exists("xclip") then
return "xclip -silent -in -selection clipboard"
elseif command_exists("wl-copy") then
return "wl-copy"
elseif command_exists("pbcopy") then
return "pbcopy"
else
mp.msg.error("No supported clipboard command found")
return false
end
end
local function divmod(a, b)
return a / b, a % b
return a / b, a % b
end
local function set_clipboard(text)
if platform == WINDOWS then
mp.commandv("run", "powershell", "set-clipboard", text)
return true
elseif (platform == UNIX and clipboard_cmd) then
local pipe = io.popen(clipboard_cmd, "w")
if not pipe then return end
pipe:write(text)
pipe:close()
return true
else
mp.msg.error("Set_clipboard error")
return false
end
if platform == WINDOWS then
mp.commandv("run", "powershell", "set-clipboard", text)
return true
elseif platform == UNIX and clipboard_cmd then
local pipe = io.popen(clipboard_cmd, "w")
if not pipe then
return
end
pipe:write(text)
pipe:close()
return true
else
mp.msg.error("Set_clipboard error")
return false
end
end
local function copyTime()
local time_pos = mp.get_property_number("time-pos")
local minutes, remainder = divmod(time_pos, 60)
local hours, minutes = divmod(minutes, 60)
local seconds = math.floor(remainder)
local milliseconds = math.floor((remainder - seconds) * 1000)
local time = string.format("%02d:%02d:%02d.%03d", hours, minutes, seconds, milliseconds)
if set_clipboard(time) then
mp.osd_message(string.format("[copytime] %s", time))
else
mp.osd_message("[copytime] failed")
end
local time_pos = mp.get_property_number("time-pos")
local minutes, remainder = divmod(time_pos, 60)
local hours, minutes = divmod(minutes, 60)
local seconds = math.floor(remainder)
local milliseconds = math.floor((remainder - seconds) * 1000)
local time = string.format("%02d:%02d:%02d.%03d", hours, minutes, seconds, milliseconds)
if set_clipboard(time) then
mp.osd_message(string.format("[copytime] %s", time))
else
mp.osd_message("[copytime] failed")
end
end
platform = platform_type()
if platform == UNIX then
clipboard_cmd = get_clipboard_cmd()
clipboard_cmd = get_clipboard_cmd()
end
mp.add_key_binding(KEY_BIND, "copyTime", copyTime)

View file

@ -7,27 +7,27 @@
-- e.g.
-- `mpv gallery-dl://https://imgur.com/....`
local mp = require 'mp'
local utils = require 'mp.utils'
local msg = require 'mp.msg'
local mp = require("mp")
local utils = require("mp.utils")
local msg = require("mp.msg")
local function exec(args)
local ret = utils.subprocess({ args = args })
return ret.status, ret.stdout, ret
local ret = utils.subprocess({ args = args })
return ret.status, ret.stdout, ret
end
mp.add_hook("on_load", 15, function()
local fn = mp.get_property("stream-open-filename", "")
if (fn:find("gdl://") ~= 1) then
msg.debug("not a gdl:// url: " .. fn)
return
end
local url = string.gsub(url, "gdl://", "")
local fn = mp.get_property("stream-open-filename", "")
if fn:find("gdl://") ~= 1 then
msg.debug("not a gdl:// url: " .. fn)
return
end
local url = string.gsub(url, "gdl://", "")
local es, urls, result = exec({ "gallery-dl", "-g", url })
if (es < 0) or (urls == nil) or (urls == "") then
msg.error("failed to get album list.")
end
local es, urls, result = exec({ "gallery-dl", "-g", url })
if (es < 0) or (urls == nil) or (urls == "") then
msg.error("failed to get album list.")
end
mp.commandv("loadlist", "memory://" .. urls)
mp.commandv("loadlist", "memory://" .. urls)
end)

File diff suppressed because it is too large Load diff

View file

@ -5,90 +5,93 @@
--
-- original from https://codeberg.org/jouni/mpv_sponsorblock_minimal
-- adapted for local playback skipping and some refactoring by me
local mp = require 'mp'
local mp = require("mp")
local options = {
API = "https://sponsor.ajay.app/api/skipSegments",
API = "https://sponsor.ajay.app/api/skipSegments",
-- Categories to fetch and skip
categories = '"sponsor","intro","outro","interaction","selfpromo"'
-- Categories to fetch and skip
categories = '"sponsor","intro","outro","interaction","selfpromo"',
}
local function getranges()
local args = {
"curl", "-s", "-d", "videoID=" .. Youtube_id, "-d",
"categories=[" .. options.categories .. "]", "-G", options.API
}
local sponsors = mp.command_native({
name = "subprocess",
capture_stdout = true,
playback_only = false,
args = args
})
local args = {
"curl",
"-s",
"-d",
"videoID=" .. Youtube_id,
"-d",
"categories=[" .. options.categories .. "]",
"-G",
options.API,
}
local sponsors = mp.command_native({
name = "subprocess",
capture_stdout = true,
playback_only = false,
args = args,
})
if string.match(sponsors.stdout, "%[(.-)%]") then
Ranges = {}
for i in string.gmatch(string.sub(sponsors.stdout, 2, -2), "%[(.-)%]") do
local k, v = string.match(i, "(%d+.?%d*),(%d+.?%d*)")
Ranges[k] = v
end
end
if string.match(sponsors.stdout, "%[(.-)%]") then
Ranges = {}
for i in string.gmatch(string.sub(sponsors.stdout, 2, -2), "%[(.-)%]") do
local k, v = string.match(i, "(%d+.?%d*),(%d+.?%d*)")
Ranges[k] = v
end
end
end
local function skip_ads(_, pos)
if pos ~= nil then
for k, v in pairs(Ranges) do
if tonumber(k) <= pos and tonumber(v) > pos then
-- this message may sometimes be wrong
-- it only seems to be a visual thing though
mp.osd_message("[sponsorblock] skipping forward " ..
math.floor(
tonumber(v) - mp.get_property("time-pos")) ..
"s")
-- need to do the +0.01 otherwise mpv will start spamming skip sometimes
-- example: https://www.youtube.com/watch?v=4ypMJzeNooo
mp.set_property("time-pos", tonumber(v) + 0.01)
return
end
end
end
if pos ~= nil then
for k, v in pairs(Ranges) do
if tonumber(k) <= pos and tonumber(v) > pos then
-- this message may sometimes be wrong
-- it only seems to be a visual thing though
mp.osd_message(
"[sponsorblock] skipping forward " .. math.floor(tonumber(v) - mp.get_property("time-pos")) .. "s"
)
-- need to do the +0.01 otherwise mpv will start spamming skip sometimes
-- example: https://www.youtube.com/watch?v=4ypMJzeNooo
mp.set_property("time-pos", tonumber(v) + 0.01)
return
end
end
end
end
local function file_loaded()
local video_path = mp.get_property("path")
local youtube_id1 = string.match(video_path,
"https?://youtu%.be/([%w-_]+).*")
local youtube_id2 = string.match(video_path,
"https?://w?w?w?%.?youtube%.com/v/([%w-_]+).*")
local youtube_id3 = string.match(video_path, "/watch.*[?&]v=([%w-_]+).*")
local youtube_id4 = string.match(video_path, "/embed/([%w-_]+).*")
local localytfile = string.match(video_path,
"-([%a%d%-_]+)%.[mw][kpe][v4b][m]?$")
Youtube_id = youtube_id1 or youtube_id2 or youtube_id3 or youtube_id4 or
localytfile
if not Youtube_id or string.len(Youtube_id) < 11 then return end
Youtube_id = string.sub(Youtube_id, 1, 11)
local video_path = mp.get_property("path")
local youtube_id1 = string.match(video_path, "https?://youtu%.be/([%w-_]+).*")
local youtube_id2 = string.match(video_path, "https?://w?w?w?%.?youtube%.com/v/([%w-_]+).*")
local youtube_id3 = string.match(video_path, "/watch.*[?&]v=([%w-_]+).*")
local youtube_id4 = string.match(video_path, "/embed/([%w-_]+).*")
local localytfile = string.match(video_path, "-([%a%d%-_]+)%.[mw][kpe][v4b][m]?$")
Youtube_id = youtube_id1 or youtube_id2 or youtube_id3 or youtube_id4 or localytfile
if not Youtube_id or string.len(Youtube_id) < 11 then
return
end
Youtube_id = string.sub(Youtube_id, 1, 11)
getranges()
if Ranges then
ON = true
mp.add_key_binding("b", "sponsorblock", toggle)
mp.observe_property("time-pos", "native", skip_ads)
end
return
getranges()
if Ranges then
ON = true
mp.add_key_binding("b", "sponsorblock", toggle)
mp.observe_property("time-pos", "native", skip_ads)
end
return
end
local function toggle()
if ON then
mp.unobserve_property(skip_ads)
mp.osd_message("[sponsorblock] off")
ON = false
return
end
mp.observe_property("time-pos", "native", skip_ads)
mp.osd_message("[sponsorblock] on")
ON = true
return
if ON then
mp.unobserve_property(skip_ads)
mp.osd_message("[sponsorblock] off")
ON = false
return
end
mp.observe_property("time-pos", "native", skip_ads)
mp.osd_message("[sponsorblock] on")
ON = true
return
end
mp.register_event("file-loaded", file_loaded)

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff