Integrate mkdirp function in common module (#265)
Move the function mkdirp into common to be generally available. Use the new common.mkdirp from create_user_directory() from core/init.lua replacing previous parent directory creation code within the function. The previous mkdirp function did not work on Windows where absolute paths starts with a drive letter. The code from create_user_directory() did not have this problem but was wrong in the way it was creating the nested directories. The new implementation in common.mkdirp fix both problems.
This commit is contained in:
@@ -1,37 +1,11 @@
|
||||
local core = require "core"
|
||||
local command = require "core.command"
|
||||
|
||||
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
|
||||
local common = require "core.common"
|
||||
|
||||
command.add(nil, {
|
||||
["files:create-directory"] = function()
|
||||
core.command_view:enter("New directory name", function(text)
|
||||
local success, err, path = mkdirp(text)
|
||||
local success, err, path = common.mkdirp(text)
|
||||
if not success then
|
||||
core.error("cannot create directory %q: %s", path, err)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user