Created
February 25, 2017 15:37
-
-
Save wiiaboo/a8f355905aaa2da5f11f5202d9a0d099 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- autoloadtxt.lua | |
-- autoload .txt subtitles respecting --sub-auto setting | |
-- hacked from autoload.lua | |
local msg = require 'mp.msg' | |
local options = require 'mp.options' | |
local utils = require 'mp.utils' | |
o = { disabled = false } | |
options.read_options(o) | |
function split_ext(path) | |
return string.match(path, "^(.+)%.([^%.]+)$") | |
end | |
table.filter = function(t, iter) | |
for i = #t, 1, -1 do | |
if not iter(t[i]) then | |
table.remove(t, i) | |
end | |
end | |
end | |
function find_and_add_entries() | |
local path = mp.get_property("path", "") | |
local dir, filename = utils.split_path(path) | |
local name, ext = string.match(filename, "(.+)%.([^%.]+)$") | |
local fuzz = mp.get_property("sub-auto", "no") | |
if o.disabled or #dir == 0 or fuzz == "no" then | |
return | |
end | |
local files = utils.readdir(dir, "files") | |
if not files then | |
return | |
end | |
local logdir, logfile = utils.split_path(mp.get_property("log-file", "")) | |
table.filter(files, function (v, k) | |
local sname, sext = split_ext(v:lower()) | |
local name = name:lower() | |
if fuzz=="all" and logfile and | |
logdir:lower()==dir:lower() and | |
logfile:lower()==v then | |
return false | |
end | |
return (sext:lower() == "txt") and | |
( | |
(fuzz=="exact" and sname==name) or | |
(fuzz=="fuzzy" and sname:find(name)==1) or | |
fuzz=="all" | |
) | |
end) | |
table.sort(files, function (a, b) | |
local len = string.len(a) - string.len(b) | |
if len ~= 0 then -- case for ordering filename ending with such as X.Y.Z | |
local _, ext = split_ext(a) | |
local ext = string.len(ext) + 1 | |
return string.sub(a, 1, -ext) < string.sub(b, 1, -ext) | |
end | |
return string.lower(a) < string.lower(b) | |
end) | |
msg.verbose("candidates:", utils.to_string(files)) | |
if dir == "." then | |
dir = "" | |
end | |
for i = 1,#files do | |
local subpath = files[i] | |
local subname = split_ext(subpath) | |
if subname:lower() == name:lower() then | |
mp.commandv('sub-add', utils.join_path(dir,subpath), 'select') | |
elseif fuzz == "all" or | |
(fuzz == "fuzzy" and subname:lower():find(name:lower()) == 1) then | |
mp.commandv('sub-add', utils.join_path(dir,subpath), 'auto') | |
end | |
end | |
end | |
mp.register_event("start-file", find_and_add_entries) |
It was not working for me.
confirmed, thanks for the fix
diff: wiiaboo vs pgasior
diff --git a/autoloadtxt.lua b/autoloadtxt.lua
index a32fe26..58ecefb 100644
--- a/autoloadtxt.lua
+++ b/autoloadtxt.lua
@@ -50,7 +50,7 @@ function find_and_add_entries()
return (sext:lower() == "txt") and
(
(fuzz=="exact" and sname==name) or
- (fuzz=="fuzzy" and sname:find(name)==1) or
+ (fuzz=="fuzzy" and sname:find(name, nil, true)==1) or
fuzz=="all"
)
end)
@@ -76,7 +76,7 @@ function find_and_add_entries()
if subname:lower() == name:lower() then
mp.commandv('sub-add', utils.join_path(dir,subpath), 'select')
elseif fuzz == "all" or
- (fuzz == "fuzzy" and subname:lower():find(name:lower()) == 1) then
+ (fuzz == "fuzzy" and subname:lower():find(name:lower(), nil, true) == 1) then
mp.commandv('sub-add', utils.join_path(dir,subpath), 'auto')
end
end
this fails on files without file extension
error: attempt to index local 'sext' (a nil value)
fix:
- return (sext:lower() == "txt") and
+ return sext and (sext:lower() == "txt") and
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It was not working for me. By default string:find expects pattern string, so [ and ] have special meaning. Fixed version: