History

autoload.lua: cosmetic changes

Kacper Michajłow committed 2 years ago

Commit 8dcd5ea1 parent 7d471971 file · +36 −45Browse files

Unified · Split

TOOLS/lua/autoload.lua+36−45
@@ -1,5 +1,5 @@
11 -- This script automatically loads playlist entries before and after the
2−-- the currently played file. It does so by scanning the directory a file is
2+-- currently played file. It does so by scanning the directory a file is
33 -- located in when starting playback. It sorts the directory entries
44 -- alphabetically, and adds entries before and after the current file to
55 -- the internal playlist. (It stops if it would add an already existing
@@ -32,8 +32,8 @@ ignore_patterns=^~,^bak-,%.bak$ · 26 unmodified lines
3232
3333 --]]
3434
35−local MAXENTRIES = 5000
36−local MAXDIRSTACK = 20
35+local MAX_ENTRIES = 5000
36+local MAX_DIR_STACK = 20
3737
3838 local msg = require 'mp.msg'
3939 local options = require 'mp.options'
@@ -53,7 +53,7 @@ local o = { · 13 unmodified lines
5353 ignore_patterns = ""
5454 }
5555
56−local function Set (t)
56+local function Set(t)
5757 local set = {}
5858 for _, v in pairs(t) do set[v] = true end
5959 return set
@@ -74,24 +74,21 @@ local EXTENSIONS_IMAGES_DEFAULT = Set { · 14 unmodified lines
7474 'svg', 'tga', 'tif', 'tiff', 'webp'
7575 }
7676
77−local EXTENSIONS
78−local EXTENSIONS_VIDEO
79−local EXTENSIONS_AUDIO
80−local EXTENSIONS_IMAGES
77+local EXTENSIONS, EXTENSIONS_VIDEO, EXTENSIONS_AUDIO, EXTENSIONS_IMAGES
8178
82−local function SetUnion (a,b)
79+local function SetUnion(a, b)
8380 for k in pairs(b) do a[k] = true end
8481 return a
8582 end
8683
8784 -- Returns first and last positions in string or past-to-end indices
88−local function FindOrPastTheEnd (string, pattern, start_at)
89− local pos1, pos2 = string.find(string, pattern, start_at)
85+local function FindOrPastTheEnd(string, pattern, start_at)
86+ local pos1, pos2 = string:find(pattern, start_at)
9087 return pos1 or #string + 1,
9188 pos2 or #string + 1
9289 end
9390
94−local function Split (list)
91+local function Split(list)
9592 local set = {}
9693
9794 local item_pos = 1
@@ -192,16 +189,15 @@ local function add_files(files) · 94 unmodified lines
192189 end
193190
194191 local function get_extension(path)
195− return string.match(path, "%.([^%.]+)$" ) or "nomatch"
192+ return path:match("%.([^%.]+)$") or "nomatch"
196193 end
197194
198195 local function is_ignored(file)
199− for pattern, _ in pairs(o.ignore_patterns) do
200− if string.match(file, pattern) then
196+ for pattern in pairs(o.ignore_patterns) do
197+ if file:match(pattern) then
201198 return true
202199 end
203200 end
204−
205201 return false
206202 end
207203
@@ -225,12 +221,12 @@ local function alphanumsort(filenames) · 17 unmodified lines
225221 return filenames
226222 end
227223
228−local autoloaded = nil
224+local autoloaded
229225 local added_entries = {}
230−local autoloaded_dir = nil
226+local autoloaded_dir
231227
232228 local function scan_dir(path, current_file, dir_mode, separator, dir_depth, total_files, extensions)
233− if dir_depth == MAXDIRSTACK then
229+ if dir_depth == MAX_DIR_STACK then
234230 return
235231 end
236232 msg.trace("scanning: " .. path)
@@ -238,7 +234,7 @@ local function scan_dir(path, current_file, dir_mode, separator, dir_depth, tota · 1 unmodified line
238234 local dirs = dir_mode ~= "ignore" and utils.readdir(path, "dirs") or {}
239235 local prefix = path == "." and "" or path
240236
241− local filter = function(t, iter)
237+ local function filter(t, iter)
242238 for i = #t, 1, -1 do
243239 if not iter(t[i]) then
244240 table.remove(t, i)
@@ -246,11 +242,11 @@ local function scan_dir(path, current_file, dir_mode, separator, dir_depth, tota · 1 unmodified line
246242 end
247243 end
248244
249− filter(files, function (v)
245+ filter(files, function(v)
250246 -- The current file could be a hidden file, ignoring it doesn't load other
251247 -- files from the current directory.
252248 local current = prefix .. v == current_file
253− if o.ignore_hidden and not current and string.match(v, "^%.") then
249+ if o.ignore_hidden and not current and v:match("^%.") then
254250 return false
255251 end
256252 if not current and is_ignored(v) then
@@ -258,13 +254,10 @@ local function scan_dir(path, current_file, dir_mode, separator, dir_depth, tota · 1 unmodified line
258254 end
259255
260256 local ext = get_extension(v)
261− if ext == nil then
262− return false
263− end
264− return extensions[string.lower(ext)]
257+ return ext and extensions[ext:lower()]
265258 end)
266259 filter(dirs, function(d)
267− return not ((o.ignore_hidden and string.match(d, "^%.")))
260+ return not (o.ignore_hidden and d:match("^%."))
268261 end)
269262 alphanumsort(files)
270263 alphanumsort(dirs)
@@ -273,7 +266,7 @@ local function scan_dir(path, current_file, dir_mode, separator, dir_depth, tota · 2 unmodified lines
273266 files[i] = prefix .. file
274267 end
275268
276− local append = function(t1, t2)
269+ local function append(t1, t2)
277270 local t1_size = #t1
278271 for i = 1, #t2 do
279272 t1[t1_size + i] = t2[i]
@@ -316,15 +309,13 @@ local function find_and_add_entries() · 36 unmodified lines
316309 local this_ext = get_extension(filename)
317310 -- check if this is a manually made playlist
318311 if (pl_count > 1 and autoloaded == nil) or
319− (pl_count == 1 and EXTENSIONS[string.lower(this_ext)] == nil) then
312+ (pl_count == 1 and EXTENSIONS[this_ext:lower()] == nil) then
320313 msg.debug("stopping: manually made playlist")
321314 return
322− else
323− if pl_count == 1 then
324− autoloaded = true
325− autoloaded_dir = dir
326− added_entries = {}
327− end
315+ elseif pl_count == 1 then
316+ autoloaded = true
317+ autoloaded_dir = dir
318+ added_entries = {}
328319 end
329320
330321 local extensions
@@ -346,11 +337,10 @@ local function find_and_add_entries() · 15 unmodified lines
346337 utils.to_string(pl)))
347338
348339 local files = {}
349− do
350− local dir_mode = o.directory_mode or mp.get_property("directory-mode", "lazy")
351− local separator = mp.get_property_native("platform") == "windows" and "\\" or "/"
352− scan_dir(autoloaded_dir, path, dir_mode, separator, 0, files, extensions)
353− end
340+ scan_dir(autoloaded_dir, path,
341+ o.directory_mode or mp.get_property("directory-mode", "lazy"),
342+ mp.get_property_native("platform") == "windows" and "\\" or "/",
343+ 0, files, extensions)
354344
355345 if next(files) == nil then
356346 msg.debug("no other files or directories in directory")
@@ -365,7 +355,7 @@ local function find_and_add_entries() · 8 unmodified lines
365355 break
366356 end
367357 end
368− if current == nil then
358+ if not current then
369359 return
370360 end
371361 msg.trace("current file position in files: "..current)
@@ -378,7 +368,7 @@ local function find_and_add_entries() · 6 unmodified lines
378368
379369 local append = {[-1] = {}, [1] = {}}
380370 for direction = -1, 1, 2 do -- 2 iterations, with direction = -1 and +1
381− for i = 1, MAXENTRIES do
371+ for i = 1, MAX_ENTRIES do
382372 local pos = current + i * direction
383373 local file = files[pos]
384374 if file == nil or file[1] == "." then
@@ -398,12 +388,13 @@ local function find_and_add_entries() · 13 unmodified lines
398388 mp.commandv("loadfile", file, "append")
399389 end
400390 end
391+ added_entries[file] = true
401392 end
402− added_entries[file] = true
403393 end
404394 if pl_count == 1 and direction == -1 and #append[-1] > 0 then
405− for i = 1, #append[-1] do
406− mp.commandv("loadfile", append[-1][i][1], "append")
395+ local load = append[-1]
396+ for i = 1, #load do
397+ mp.commandv("loadfile", load[i][1], "append")
407398 end
408399 mp.commandv("playlist-move", 0, current)
409400 end

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