TOOLS/lua/autoload: support directories

Adds support for adding directories to the playlist in addition to
files. The propertiy `directory-mode` controls if directories get added.
Recursive directory loading will get added in a later commit.

Directories get sorted after files to behave the same way mpv
behaves when it loads directories directly.
This commit is contained in:
Christoph Heinrich 2023-02-09 17:24:29 +01:00 committed by Dudemanguy
parent 662650bde3
commit 6714f6b820
1 changed files with 22 additions and 8 deletions

View File

@ -122,6 +122,13 @@ table.filter = function(t, iter)
end end
end end
table.append = function(t1, t2)
local t1_size = #t1
for i = 1, #t2 do
t1[t1_size + i] = t2[i]
end
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
@ -147,8 +154,7 @@ local autoloaded = nil
function get_playlist_filenames(playlist) function get_playlist_filenames(playlist)
local filenames = {} local filenames = {}
for i = 1, #playlist do for i = 1, #playlist do
local _, file = utils.split_path(playlist[i].filename) filenames[playlist[i].filename] = true
filenames[file] = true
end end
return filenames return filenames
end end
@ -194,10 +200,13 @@ function find_and_add_entries()
utils.to_string(pl))) utils.to_string(pl)))
local files = utils.readdir(dir, "files") local files = utils.readdir(dir, "files")
if files == nil then local load_dirs = mp.get_property("directory-mode") ~= "ignore"
msg.verbose("no other files in directory") local dirs = load_dirs and utils.readdir(dir, "dirs") or nil
if files == nil and dirs == nil then
msg.verbose("no other files or directories in directory")
return return
end end
files, dirs = files or {}, dirs or {}
table.filter(files, function (v, k) table.filter(files, function (v, k)
-- The current file could be a hidden file, ignoring it doesn't load other -- The current file could be a hidden file, ignoring it doesn't load other
-- files from the current directory. -- files from the current directory.
@ -210,7 +219,12 @@ function find_and_add_entries()
end end
return EXTENSIONS_TARGET[string.lower(ext)] return EXTENSIONS_TARGET[string.lower(ext)]
end) end)
table.filter(dirs, function(d)
return not ((o.ignore_hidden and string.match(d, "^%.")))
end)
alphanumsort(files) alphanumsort(files)
alphanumsort(dirs)
table.append(files, dirs)
if dir == "." then if dir == "." then
dir = "" dir = ""
@ -243,12 +257,12 @@ function find_and_add_entries()
-- skip files already in playlist -- skip files already in playlist
if not filenames[file] then if not filenames[file] then
if direction == -1 then if direction == -1 then
msg.info("Prepending " .. file) msg.info("Prepending " .. filepath)
table.insert(append[-1], 1, {filepath, pos - 1}) table.insert(append[-1], 1, {filepath, pl_current + i * direction + 1})
else else
msg.info("Adding " .. file) msg.info("Adding " .. filepath)
if pl_count > 1 then if pl_count > 1 then
table.insert(append[1], {filepath, pos - 1}) table.insert(append[1], {filepath, pl_current + i * direction - 1})
else else
mp.commandv("loadfile", filepath, "append") mp.commandv("loadfile", filepath, "append")
end end