Allow an optional extension to be passed to nextfile

This commit is contained in:
The Magician 2023-11-18 21:33:25 +00:00
parent 415a0f1d57
commit b0cadf63e4
2 changed files with 20 additions and 3 deletions

View File

@ -83,5 +83,13 @@ describe("nextfile", function()
assert.are.equal("/testdir/421za.txt", file) assert.are.equal("/testdir/421za.txt", file)
end) end)
it("should use the provided file extension if one is given", function()
prevfile.prevfile = function() return "/testdir/900.txt" end
local file = nextfile.nextfile("/testdir", "yaml")
assert.are.equal("/testdir/901.yaml", file)
end)
end) end)
end) end)

View File

@ -15,19 +15,28 @@ function nextfile.nextletter(letter)
return string.char(nextascii) return string.char(nextascii)
end end
function nextfile.nextfile(directory) function nextfile.nextfile(directory, extension)
local previous = prevfile.prevfile(directory) local previous = prevfile.prevfile(directory)
if previous == nil then return nil end if previous == nil then return nil end
local dir, name, ext = nextfile.splitfilename(previous) local dir, name, ext = nextfile.splitfilename(previous)
value = dir
if tonumber(name) == nil then if tonumber(name) == nil then
local lastletter = name:sub(#name) local lastletter = name:sub(#name)
local nextletter = nextfile.nextletter(lastletter) local nextletter = nextfile.nextletter(lastletter)
return dir .. name:gsub(lastletter .. "$", nextletter) .. ext value = value .. name:gsub(lastletter .. "$", nextletter)
else
value = value .. name + 1
end end
return dir .. name + 1 .. ext if extension ~= nil then
value = value .. "." .. extension
else
value = value .. ext
end
return value
end end
return nextfile return nextfile