Begin reworks of project manager

This commit is contained in:
Francesco Abbate 2020-12-07 18:06:58 +01:00
parent 48ad8a8392
commit a3adfeb8f6
2 changed files with 30 additions and 85 deletions

View File

@ -102,6 +102,21 @@ function common.path_suggest(text)
end
function common.dir_path_suggest(text)
local path, name = text:match("^(.-)([^/\\]*)$")
local files = system.list_dir(path == "" and "." or path) or {}
local res = {}
for _, file in ipairs(files) do
file = path .. file
local info = system.get_file_info(file)
if info and info.type == "dir" and file:lower():find(text:lower(), nil, true) == 1 then
table.insert(res, file)
end
end
return res
end
function common.match_pattern(text, pattern, ...)
if type(pattern) == "string" then
return text:find(pattern, ...)

View File

@ -5,13 +5,13 @@ local command = require "core.command"
local common = require "core.common"
local keymap = require "core.keymap"
local projects_file = ".lite_projects.lua"
local recent_projects_file = "recent_projects.lua"
project_manager.projects = {}
project_manager.recents = {}
local function load_projects()
local ok, t = pcall(dofile, USERDIR .. "/" .. projects_file)
if ok then project_manager.projects = t end
local ok, t = pcall(dofile, USERDIR .. "/" .. recent_projects_file)
if ok then project_manager.recents = t end
end
load_projects()
@ -30,9 +30,9 @@ local function serialize(val)
end
local function save_projects()
local fp = io.open(USERDIR .. "/" .. projects_file, "w")
local fp = io.open(USERDIR .. "/" .. recent_projects_file, "w")
if fp then
fp:write("return ", serialize(project_manager.projects), "\n")
fp:write("return ", serialize(project_manager.recents), "\n")
fp:close()
end
end
@ -42,89 +42,19 @@ local function path_base_name(str)
return str:match(pattern)
end
function project_manager.add_project()
local proj_dir = system.absolute_path(".")
local proj_name = path_base_name(proj_dir)
core.command_view:set_text(proj_name)
core.command_view:enter("Project Name",
function(text)
if text then
project_manager.projects[text] = proj_dir
save_projects()
end
end)
end
local function get_project_names()
local t = {}
for k, v in pairs(project_manager.projects) do table.insert(t, k) end
return t
end
local function project_lister(func)
local projects = get_project_names();
core.command_view:enter("Open Project", func, function(text)
local res = common.fuzzy_match(projects, text)
for i, name in ipairs(res) do
res[i] = {
text = name,
info = project_manager.projects[name],
}
end
return res
end)
end
function project_manager.rename_project(func)
project_lister(function(text, item)
if item then
core.command_view:set_text(item.text)
core.command_view:enter("Rename ".. item.text,
function(_text)
if _text then
project_manager.projects[_text] = project_manager.projects[item.text]
project_manager.projects[item.text] = nil
save_projects()
end
end)
end
end)
end
function project_manager.open_project()
project_lister(function(text, item)
if item then
system.exec(string.format("%q %q", EXEFILE, item.info))
end
end)
end
function project_manager.switch_project()
project_lister(function(text, item)
if item then
if core.confirm_close_all() then
core.root_view:close_all_docviews()
core.switch_project = item.info
end
end
end)
end
function project_manager.remove_project()
project_lister(function(text, item)
if item then
project_manager.projects[item.text] = nil
function project_manager.open_folder()
core.command_view:enter("Open Folder", function(text)
if core.confirm_close_all() then
core.root_view:close_all_docviews()
table.insert(project_manager.recents, text)
save_projects()
core.switch_project = text
end
end)
end, common.dir_path_suggest)
end
command.add(nil, {
["project-manager:open-project"] = project_manager.open_project,
["project-manager:switch-project"] = project_manager.switch_project,
["project-manager:add-project"] = project_manager.add_project,
["project-manager:remove-project"] = project_manager.remove_project,
["project-manager:rename-project"] = project_manager.rename_project,
})
["project-manager:open-folder"] = project_manager.open_folder,
})
return project_manager