1
0
mirror of https://github.com/mpv-player/mpv synced 2025-02-11 09:27:26 +00:00

TOOLS/autoload.lua: fix lint warnings

This commit is contained in:
Guido Cella 2024-05-27 23:01:50 +02:00 committed by Kacper Michajłow
parent 44d7100296
commit 843161d96e
2 changed files with 54 additions and 56 deletions

View File

@ -110,7 +110,6 @@ max_line_length = 100
-- TODO: Remove everything below this line -- TODO: Remove everything below this line
local todo = { local todo = {
"player/lua/osc.lua", "player/lua/osc.lua",
"TOOLS/lua/autoload.lua",
"TOOLS/lua/command-test.lua", "TOOLS/lua/command-test.lua",
"TOOLS/lua/cycle-deinterlace-pullup.lua", "TOOLS/lua/cycle-deinterlace-pullup.lua",
"TOOLS/lua/nan-test.lua", "TOOLS/lua/nan-test.lua",

View File

@ -32,14 +32,14 @@ ignore_patterns=^~,^bak-,%.bak$
--]] --]]
MAXENTRIES = 5000 local MAXENTRIES = 5000
MAXDIRSTACK = 20 local MAXDIRSTACK = 20
local msg = require 'mp.msg' local msg = require 'mp.msg'
local options = require 'mp.options' local options = require 'mp.options'
local utils = require 'mp.utils' local utils = require 'mp.utils'
o = { local o = {
disabled = false, disabled = false,
images = true, images = true,
videos = true, videos = true,
@ -52,37 +52,46 @@ o = {
directory_mode = "auto", directory_mode = "auto",
ignore_patterns = "" ignore_patterns = ""
} }
options.read_options(o, nil, function(list)
split_option_exts(list.additional_video_exts, list.additional_audio_exts, list.additional_image_exts)
if list.videos or list.additional_video_exts or
list.audio or list.additional_audio_exts or
list.images or list.additional_image_exts then
create_extensions()
end
if list.directory_mode then
validate_directory_mode()
end
end)
function Set (t) local function Set (t)
local set = {} local set = {}
for _, v in pairs(t) do set[v] = true end for _, v in pairs(t) do set[v] = true end
return set return set
end end
function SetUnion (a,b) local EXTENSIONS_VIDEO_DEFAULT = Set {
'3g2', '3gp', 'avi', 'flv', 'm2ts', 'm4v', 'mj2', 'mkv', 'mov',
'mp4', 'mpeg', 'mpg', 'ogv', 'rmvb', 'webm', 'wmv', 'y4m'
}
local EXTENSIONS_AUDIO_DEFAULT = Set {
'aiff', 'ape', 'au', 'flac', 'm4a', 'mka', 'mp3', 'oga', 'ogg',
'ogm', 'opus', 'wav', 'wma'
}
local EXTENSIONS_IMAGES_DEFAULT = Set {
'avif', 'bmp', 'gif', 'j2k', 'jp2', 'jpeg', 'jpg', 'jxl', 'png',
'svg', 'tga', 'tif', 'tiff', 'webp'
}
local EXTENSIONS = {}
local EXTENSIONS_VIDEO = {}
local EXTENSIONS_AUDIO = {}
local EXTENSIONS_IMAGES = {}
local function SetUnion (a,b)
for k in pairs(b) do a[k] = true end for k in pairs(b) do a[k] = true end
return a return a
end end
-- Returns first and last positions in string or past-to-end indices -- Returns first and last positions in string or past-to-end indices
function FindOrPastTheEnd (string, pattern, start_at) local function FindOrPastTheEnd (string, pattern, start_at)
local pos1, pos2 = string.find(string, pattern, start_at) local pos1, pos2 = string.find(string, pattern, start_at)
return pos1 or #string + 1, return pos1 or #string + 1,
pos2 or #string + 1 pos2 or #string + 1
end end
function Split (list) local function Split (list)
local set = {} local set = {}
local item_pos = 1 local item_pos = 1
@ -117,38 +126,19 @@ function Split (list)
return set return set
end end
EXTENSIONS_VIDEO_DEFAULT = Set { local function split_option_exts(video, audio, image)
'3g2', '3gp', 'avi', 'flv', 'm2ts', 'm4v', 'mj2', 'mkv', 'mov',
'mp4', 'mpeg', 'mpg', 'ogv', 'rmvb', 'webm', 'wmv', 'y4m'
}
EXTENSIONS_AUDIO_DEFAULT = Set {
'aiff', 'ape', 'au', 'flac', 'm4a', 'mka', 'mp3', 'oga', 'ogg',
'ogm', 'opus', 'wav', 'wma'
}
EXTENSIONS_IMAGES_DEFAULT = Set {
'avif', 'bmp', 'gif', 'j2k', 'jp2', 'jpeg', 'jpg', 'jxl', 'png',
'svg', 'tga', 'tif', 'tiff', 'webp'
}
function split_option_exts(video, audio, image)
if video then o.additional_video_exts = Split(o.additional_video_exts) end if video then o.additional_video_exts = Split(o.additional_video_exts) end
if audio then o.additional_audio_exts = Split(o.additional_audio_exts) end if audio then o.additional_audio_exts = Split(o.additional_audio_exts) end
if image then o.additional_image_exts = Split(o.additional_image_exts) end if image then o.additional_image_exts = Split(o.additional_image_exts) end
end end
split_option_exts(true, true, true) split_option_exts(true, true, true)
function split_patterns() local function split_patterns()
o.ignore_patterns = Split(o.ignore_patterns) o.ignore_patterns = Split(o.ignore_patterns)
end end
split_patterns() split_patterns()
function create_extensions() local function create_extensions()
EXTENSIONS = {}
EXTENSIONS_VIDEO = {}
EXTENSIONS_AUDIO = {}
EXTENSIONS_IMAGES = {}
if o.videos then if o.videos then
SetUnion(SetUnion(EXTENSIONS_VIDEO, EXTENSIONS_VIDEO_DEFAULT), o.additional_video_exts) SetUnion(SetUnion(EXTENSIONS_VIDEO, EXTENSIONS_VIDEO_DEFAULT), o.additional_video_exts)
SetUnion(EXTENSIONS, EXTENSIONS_VIDEO) SetUnion(EXTENSIONS, EXTENSIONS_VIDEO)
@ -164,14 +154,28 @@ function create_extensions()
end end
create_extensions() create_extensions()
function validate_directory_mode() local function validate_directory_mode()
if o.directory_mode ~= "recursive" and o.directory_mode ~= "lazy" and o.directory_mode ~= "ignore" then if o.directory_mode ~= "recursive" and o.directory_mode ~= "lazy"
and o.directory_mode ~= "ignore" then
o.directory_mode = nil o.directory_mode = nil
end end
end end
validate_directory_mode() validate_directory_mode()
function add_files(files) options.read_options(o, nil, function(list)
split_option_exts(list.additional_video_exts, list.additional_audio_exts,
list.additional_image_exts)
if list.videos or list.additional_video_exts or
list.audio or list.additional_audio_exts or
list.images or list.additional_image_exts then
create_extensions()
end
if list.directory_mode then
validate_directory_mode()
end
end)
local function add_files(files)
local oldcount = mp.get_property_number("playlist-count", 1) local oldcount = mp.get_property_number("playlist-count", 1)
for i = 1, #files do for i = 1, #files do
mp.commandv("loadfile", files[i][1], "append") mp.commandv("loadfile", files[i][1], "append")
@ -179,16 +183,11 @@ function add_files(files)
end end
end end
function get_extension(path) local function get_extension(path)
match = string.match(path, "%.([^%.]+)$" ) return string.match(path, "%.([^%.]+)$" ) or "nomatch"
if match == nil then
return "nomatch"
else
return match
end
end end
function is_ignored(file) local function is_ignored(file)
for pattern, _ in pairs(o.ignore_patterns) do for pattern, _ in pairs(o.ignore_patterns) do
if string.match(file, pattern) then if string.match(file, pattern) then
return true return true
@ -201,7 +200,7 @@ end
-- alphanum sorting for humans in Lua -- alphanum sorting for humans in Lua
-- http://notebook.kulchenko.com/algorithms/alphanumeric-natural-sorting-for-humans-in-lua -- http://notebook.kulchenko.com/algorithms/alphanumeric-natural-sorting-for-humans-in-lua
function alphanumsort(filenames) local function alphanumsort(filenames)
local function padnum(n, d) local function padnum(n, d)
return #d > 0 and ("%03d%s%.12f"):format(#n, n, tonumber(d) / (10 ^ #d)) return #d > 0 and ("%03d%s%.12f"):format(#n, n, tonumber(d) / (10 ^ #d))
or ("%03d%s"):format(#n, n) or ("%03d%s"):format(#n, n)
@ -222,7 +221,7 @@ local autoloaded = nil
local added_entries = {} local added_entries = {}
local autoloaded_dir = nil local autoloaded_dir = nil
function scan_dir(path, current_file, dir_mode, separator, dir_depth, total_files, extensions) local function scan_dir(path, current_file, dir_mode, separator, dir_depth, total_files, extensions)
if dir_depth == MAXDIRSTACK then if dir_depth == MAXDIRSTACK then
return return
end end
@ -287,7 +286,7 @@ function scan_dir(path, current_file, dir_mode, separator, dir_depth, total_file
end end
end end
function find_and_add_entries() local function find_and_add_entries()
local aborted = mp.get_property_native("playback-abort") local aborted = mp.get_property_native("playback-abort")
if aborted then if aborted then
msg.debug("stopping: playback aborted") msg.debug("stopping: playback aborted")
@ -306,7 +305,7 @@ function find_and_add_entries()
end end
local pl_count = mp.get_property_number("playlist-count", 1) local pl_count = mp.get_property_number("playlist-count", 1)
this_ext = get_extension(filename) local this_ext = get_extension(filename)
-- check if this is a manually made playlist -- check if this is a manually made playlist
if (pl_count > 1 and autoloaded == nil) or if (pl_count > 1 and autoloaded == nil) or
(pl_count == 1 and EXTENSIONS[string.lower(this_ext)] == nil) then (pl_count == 1 and EXTENSIONS[string.lower(this_ext)] == nil) then