1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
local project_manager = {}
local core = require "core"
local command = require "core.command"
local common = require "core.common"
local keymap = require "core.keymap"
local projects_file = ".lite_projects.lua"
project_manager.projects = {}
local function load_projects()
local ok, t = pcall(dofile, EXEDIR .. "/" .. projects_file)
if ok then project_manager.projects = t end
end
load_projects()
local function serialize(val)
if type(val) == "string" then
return string.format("%q", val)
elseif type(val) == "table" then
local t = {}
for k, v in pairs(val) do
table.insert(t, "[" .. serialize(k) .. "]=" .. serialize(v))
end
return "{" .. table.concat(t, ",") .. "}"
end
return tostring(val)
end
local function save_projects()
local fp = io.open(EXEDIR .. "/" .. projects_file, "w")
if fp then
fp:write("return ", serialize(project_manager.projects), "\n")
fp:close()
end
end
local function path_base_name(str)
local pattern = "[\\/]?([^\\/]+)[\\/]?$"
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
system.exec(string.format("%q %q", EXEFILE, item.info))
os.exit()
end
end)
end
function project_manager.remove_project()
project_lister(function(text, item)
if item then
project_manager.projects[item.text] = nil
save_projects()
end
end)
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,
})
return project_manager
|