2020-12-28 16:18:03 +01:00
|
|
|
local core = require "core"
|
|
|
|
local command = require "core.command"
|
|
|
|
|
2021-06-07 22:44:35 +02:00
|
|
|
local function mkdirp(path)
|
|
|
|
local segments = {}
|
|
|
|
local pos = 1
|
|
|
|
while true do
|
|
|
|
local s, e = string.find(path, "[/\\]+", pos)
|
|
|
|
if not s then break end
|
|
|
|
table.insert(segments, string.sub(str, pos, s - 1))
|
|
|
|
pos = e + 1
|
|
|
|
end
|
|
|
|
table.insert(segments, string.sub(str, pos))
|
|
|
|
if segments[#segments] == '' then
|
|
|
|
table.remove(segments)
|
|
|
|
end
|
|
|
|
|
|
|
|
for i = 1, #segments do
|
|
|
|
local p = table.concat(segments, PATHSEP, 1, i)
|
|
|
|
local stat = system.get_file_info(p)
|
|
|
|
if stat and stat.type == "file" then
|
|
|
|
return nil, "path exists", p
|
|
|
|
end
|
|
|
|
local success, err = system.mkdir(p)
|
|
|
|
if not success then
|
|
|
|
return nil, err, p
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-28 16:18:03 +01:00
|
|
|
command.add(nil, {
|
|
|
|
["files:create-directory"] = function()
|
|
|
|
core.command_view:enter("New directory name", function(text)
|
2021-06-07 22:44:35 +02:00
|
|
|
local success, err, path = mkdirp(text)
|
2020-12-28 16:18:03 +01:00
|
|
|
if not success then
|
2021-06-07 22:44:35 +02:00
|
|
|
core.error("cannot create directory %q: %s", path, err)
|
2020-12-28 16:18:03 +01:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end,
|
|
|
|
})
|