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
25--]]25--]]
26 26
27MAXENTRIES = 500027MAXENTRIES = 5000
28MAXDIRSTACK = 20
28 29
29local msg = require 'mp.msg'30local msg = require 'mp.msg'
30local options = require 'mp.options'31local options = require 'mp.options'
@@ -151,6 +152,51 @@ end · 120 unmodified lines
151 152
152local autoloaded = nil153local autoloaded = nil
153local added_entries = {}154local added_entries = {}
155local autoloaded_dir = nil
156
157function 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
199end
154 200
155function find_and_add_entries()201function find_and_add_entries()
156 local path = mp.get_property("path", "")202 local path = mp.get_property("path", "")
@@ -174,6 +220,7 @@ function find_and_add_entries() · 17 unmodified lines
174 else220 else
175 if pl_count == 1 then221 if pl_count == 1 then
176 autoloaded = true222 autoloaded = true
223 autoloaded_dir = dir
177 added_entries = {}224 added_entries = {}
178 end225 end
179 end226 end
@@ -195,41 +242,22 @@ function find_and_add_entries() · 15 unmodified lines
195 msg.trace(("playlist-pos-1: %s, playlist: %s"):format(pl_current,242 msg.trace(("playlist-pos-1: %s, playlist: %s"):format(pl_current,
196 utils.to_string(pl)))243 utils.to_string(pl)))
197 244
198 local files = utils.readdir(dir, "files")245 local files = {}
199 local load_dirs = mp.get_property("directory-mode") ~= "ignore"246 do
200 local dirs = load_dirs and utils.readdir(dir, "dirs") or nil247 local dir_mode = mp.get_property("directory-mode")
201 if files == nil and dirs == nil then248 local separator = mp.get_property_native("platform") == "windows" and "\\" or "/"
202 msg.verbose("no other files or directories in directory")249 scan_dir(autoloaded_dir, path, dir_mode, separator, 0, files)
203 return
204 end250 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)
224 251
225 if dir == "." then252 if next(files) == nil then
226 dir = ""253 msg.verbose("no other files or directories in directory")
254 return
227 end255 end
228 256
229 -- Find the current pl entry (dir+"/"+filename) in the sorted dir list257 -- Find the current pl entry (dir+"/"+filename) in the sorted dir list
230 local current258 local current
231 for i = 1, #files do259 for i = 1, #files do
232 if files[i] == filename then260 if files[i] == path then
233 current = i261 current = i
234 break262 break
235 end263 end
@@ -254,22 +282,21 @@ function find_and_add_entries() · 18 unmodified lines
254 break282 break
255 end283 end
256 284
257 local filepath = dir .. file
258 -- skip files that are/were already in the playlist285 -- skip files that are/were already in the playlist
259 if not added_entries[filepath] then286 if not added_entries[file] then
260 if direction == -1 then287 if direction == -1 then
261 msg.info("Prepending " .. filepath)288 msg.info("Prepending " .. file)
262 table.insert(append[-1], 1, {filepath, pl_current + i * direction + 1})289 table.insert(append[-1], 1, {file, pl_current + i * direction + 1})
263 else290 else
264 msg.info("Adding " .. filepath)291 msg.info("Adding " .. file)
265 if pl_count > 1 then292 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})
267 else294 else
268 mp.commandv("loadfile", filepath, "append")295 mp.commandv("loadfile", file, "append")
269 end296 end
270 end297 end
271 end298 end
272 added_entries[filepath] = true299 added_entries[file] = true
273 end300 end
274 if pl_count == 1 and direction == -1 and #append[-1] > 0 then301 if pl_count == 1 and direction == -1 and #append[-1] > 0 then
275 for i = 1, #append[-1] do302 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