History

TOOLS/lua/autoload: add recursive directory loading

Recursion depth has been limited to 20, the same depth the demuxer uses.

Christoph Heinrich committed 3 years ago

Commit 5100e7a1 parent 5de1f5a1 file · +63 −36Browse files

Unified · Split

TOOLS/lua/autoload.lua+63−36
@@ -25,6 +25,7 @@ same_type=yes · 24 unmodified lines
2525 --]]
2626
2727 MAXENTRIES = 5000
28+MAXDIRSTACK = 20
2829
2930 local msg = require 'mp.msg'
3031 local options = require 'mp.options'
@@ -151,6 +152,51 @@ end · 120 unmodified lines
151152
152153 local autoloaded = nil
153154 local added_entries = {}
155+local autoloaded_dir = nil
156+
157+function scan_dir(path, current_file, dir_mode, separator, dir_depth, total_files)
158+ if dir_depth == MAXDIRSTACK then
159+ return
160+ end
161+ msg.trace("scanning: " .. path)
162+ local files = utils.readdir(path, "files") or {}
163+ local dirs = dir_mode ~= "ignore" and utils.readdir(path, "dirs") or {}
164+ local prefix = path == "." and "" or path
165+ table.filter(files, function (v)
166+ -- The current file could be a hidden file, ignoring it doesn't load other
167+ -- files from the current directory.
168+ if (o.ignore_hidden and not (prefix .. v == current_file) and string.match(v, "^%.")) then
169+ return false
170+ end
171+ local ext = get_extension(v)
172+ if ext == nil then
173+ return false
174+ end
175+ return EXTENSIONS[string.lower(ext)]
176+ end)
177+ table.filter(dirs, function(d)
178+ return not ((o.ignore_hidden and string.match(d, "^%.")))
179+ end)
180+ alphanumsort(files)
181+ alphanumsort(dirs)
182+
183+ for i, file in ipairs(files) do
184+ files[i] = prefix .. file
185+ end
186+
187+ table.append(total_files, files)
188+ if dir_mode == "recursive" then
189+ for _, dir in ipairs(dirs) do
190+ scan_dir(prefix .. dir .. separator, current_file, dir_mode,
191+ separator, dir_depth + 1, total_files)
192+ end
193+ else
194+ for i, dir in ipairs(dirs) do
195+ dirs[i] = prefix .. dir
196+ end
197+ table.append(total_files, dirs)
198+ end
199+end
154200
155201 function find_and_add_entries()
156202 local path = mp.get_property("path", "")
@@ -174,6 +220,7 @@ function find_and_add_entries() · 17 unmodified lines
174220 else
175221 if pl_count == 1 then
176222 autoloaded = true
223+ autoloaded_dir = dir
177224 added_entries = {}
178225 end
179226 end
@@ -195,41 +242,22 @@ function find_and_add_entries() · 15 unmodified lines
195242 msg.trace(("playlist-pos-1: %s, playlist: %s"):format(pl_current,
196243 utils.to_string(pl)))
197244
198− local files = utils.readdir(dir, "files")
199− local load_dirs = mp.get_property("directory-mode") ~= "ignore"
200− local dirs = load_dirs and utils.readdir(dir, "dirs") or nil
201− if files == nil and dirs == nil then
202− msg.verbose("no other files or directories in directory")
203− return
245+ local files = {}
246+ do
247+ local dir_mode = mp.get_property("directory-mode")
248+ local separator = mp.get_property_native("platform") == "windows" and "\\" or "/"
249+ scan_dir(autoloaded_dir, path, dir_mode, separator, 0, files)
204250 end
205− files, dirs = files or {}, dirs or {}
206− table.filter(files, function (v, k)
207− -- The current file could be a hidden file, ignoring it doesn't load other
208− -- files from the current directory.
209− if (o.ignore_hidden and not (v == filename) and string.match(v, "^%.")) then
210− return false
211− end
212− local ext = get_extension(v)
213− if ext == nil then
214− return false
215− end
216− return EXTENSIONS_TARGET[string.lower(ext)]
217− end)
218− table.filter(dirs, function(d)
219− return not ((o.ignore_hidden and string.match(d, "^%.")))
220− end)
221− alphanumsort(files)
222− alphanumsort(dirs)
223− table.append(files, dirs)
224251
225− if dir == "." then
226− dir = ""
252+ if next(files) == nil then
253+ msg.verbose("no other files or directories in directory")
254+ return
227255 end
228256
229257 -- Find the current pl entry (dir+"/"+filename) in the sorted dir list
230258 local current
231259 for i = 1, #files do
232− if files[i] == filename then
260+ if files[i] == path then
233261 current = i
234262 break
235263 end
@@ -254,22 +282,21 @@ function find_and_add_entries() · 18 unmodified lines
254282 break
255283 end
256284
257− local filepath = dir .. file
258285 -- skip files that are/were already in the playlist
259− if not added_entries[filepath] then
286+ if not added_entries[file] then
260287 if direction == -1 then
261− msg.info("Prepending " .. filepath)
262− table.insert(append[-1], 1, {filepath, pl_current + i * direction + 1})
288+ msg.info("Prepending " .. file)
289+ table.insert(append[-1], 1, {file, pl_current + i * direction + 1})
263290 else
264− msg.info("Adding " .. filepath)
291+ msg.info("Adding " .. file)
265292 if pl_count > 1 then
266− table.insert(append[1], {filepath, pl_current + i * direction - 1})
293+ table.insert(append[1], {file, pl_current + i * direction - 1})
267294 else
268− mp.commandv("loadfile", filepath, "append")
295+ mp.commandv("loadfile", file, "append")
269296 end
270297 end
271298 end
272− added_entries[filepath] = true
299+ added_entries[file] = true
273300 end
274301 if pl_count == 1 and direction == -1 and #append[-1] > 0 then
275302 for i = 1, #append[-1] do

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t