Move project manager into core init file

This commit is contained in:
Francesco Abbate 2020-12-08 16:57:24 +01:00
parent 9c7e477cd1
commit e88adc0567
5 changed files with 64 additions and 67 deletions

View File

@ -111,4 +111,19 @@ command.add(nil, {
doc:save(filename)
end
end,
["core:open-folder"] = function()
core.command_view:enter("Open Folder", function(text)
local path_stat = system.get_file_info(text)
if not path_stat or path_stat.type ~= 'dir' then
core.error("Cannot open folder %q", text)
return
end
if core.confirm_close_all() then
core.open_folder_project(text)
end
end, function(text)
return text == "" and core.recent_projects or common.dir_path_suggest(text)
end)
end,
})

View File

@ -11,6 +11,48 @@ local Doc
local core = {}
local function table_serialize(t)
local ls = {"{"}
for i = 1, #t do
ls[#ls + 1] = string.format(" %q,", t[i])
end
ls[#ls + 1] = "}"
return table.concat(ls, "\n")
end
local function load_projects()
local ok, t = pcall(dofile, USERDIR .. "/recent_projects.lua")
core.recent_projects = (ok and t or {})
end
local function add_project_to_recents(dirname)
dirname = system.absolute_path(dirname)
if not dirname then return end
local recents = core.recent_projects
local n = #recents
for i = 1, n do
if dirname == recents[i] then return end
end
recents[n + 1] = dirname
end
local function save_projects()
local fp = io.open(USERDIR .. "/recent_projects.lua", "w")
if fp then
local _, err = fp:write("return ", table_serialize(core.recent_projects), "\n")
if err then
core.error("Error saving recent projects, %d entries", #core.recent_projects)
end
fp:close()
end
end
function core.open_folder_project(dirname)
core.root_view:close_all_docviews()
add_project_to_recents(dirname)
save_projects()
core.switch_project = dirname
end
local function project_scan_thread()
local function diff_files(a, b)
@ -157,7 +199,9 @@ function core.init()
CommandView = require "core.commandview"
Doc = require "core.doc"
local project_dir = "."
load_projects()
local project_dir = #core.recent_projects > 0 and core.recent_projects[#core.recent_projects] or "."
local files = {}
for i = 2, #ARGS do
local info = system.get_file_info(ARGS[i]) or {}
@ -165,6 +209,8 @@ function core.init()
table.insert(files, system.absolute_path(ARGS[i]))
elseif info.type == "dir" then
project_dir = ARGS[i]
add_project_to_recents(project_dir)
save_projects()
end
end

View File

@ -181,6 +181,7 @@ keymap.add {
["ctrl+shift+end"] = "doc:select-to-end-of-doc",
["shift+pageup"] = "doc:select-to-previous-page",
["shift+pagedown"] = "doc:select-to-next-page",
["ctrl+shift+o"] = "core:open-folder",
}
return keymap

View File

@ -19,7 +19,7 @@ local function draw_text(x, y, color)
local lines = {
{ fmt = "%s to run a command", cmd = "core:find-command" },
{ fmt = "%s to open a file from the project", cmd = "core:find-file" },
{ fmt = "%s to open a folder", cmd = "project-manager:open-folder" },
{ fmt = "%s to open a folder", cmd = "core:open-folder" },
}
th = style.font:get_height()
y = y + (dh - th * 2 - style.padding.y) / 2

View File

@ -1,65 +0,0 @@
local project_manager = {}
local core = require "core"
local command = require "core.command"
local common = require "core.common"
local keymap = require "core.keymap"
local recent_projects_file = "recent_projects.lua"
project_manager.recents = {}
local function load_projects()
local ok, t = pcall(dofile, USERDIR .. "/" .. recent_projects_file)
if ok then project_manager.recents = t end
end
load_projects()
local function serialize(val)
local ls = {"{"}
for i = 1, #val do
ls[#ls + 1] = " " .. string.format("%q", val[i]) .. ","
end
ls[#ls + 1] = "}"
return table.concat(ls, "\n")
end
local function save_projects()
local fp = io.open(USERDIR .. "/" .. recent_projects_file, "w")
if fp then
fp:write("return ", serialize(project_manager.recents), "\n")
fp:close()
end
end
local function path_base_name(str)
local pattern = "[\\/]?([^\\/]+)[\\/]?$"
return str:match(pattern)
end
function project_manager.open_folder()
core.command_view:enter("Open Folder", function(text)
local path_stat = system.get_file_info(text)
if not path_stat or path_stat.type ~= 'dir' then
core.error("Cannot open folder %q", text)
return
end
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, function(text)
return text == "" and project_manager.recents or common.dir_path_suggest(text)
end)
end
command.add(nil, {
["project-manager:open-folder"] = project_manager.open_folder,
})
keymap.add { ["ctrl+shift+o"] = "project-manager:open-folder" }
return project_manager