Skip to content

Commit 4ed7575

Browse files
committed
update
1 parent 51d6c81 commit 4ed7575

File tree

1 file changed

+66
-32
lines changed

1 file changed

+66
-32
lines changed

lua/minpm/init.lua

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
local api, stdpath, uv = vim.api, vim.fn.stdpath, vim.uv
2-
local repos, INSTALL, UPDATE = {}, 0, 1
2+
local repos, INSTALL = {}, 0
33
local buf_set_lines, create_autocmd = api.nvim_buf_set_lines, api.nvim_create_autocmd
44
local packadd = vim.cmd.packadd
55
local exec_autocmds = api.nvim_exec_autocmds
@@ -18,6 +18,7 @@ end
1818

1919
local use_meta = {}
2020
use_meta.__index = use_meta
21+
2122
function use_meta:when(e)
2223
self.event = as_table(e)
2324
self.islazy = true
@@ -32,7 +33,7 @@ function use_meta:when(e)
3233
data = args.data,
3334
})
3435
if self.setup_config then
35-
module = self.tail:gsub('%.nvim', '') or self.tail:gsub('-nvim', '')
36+
local module = self.tail:gsub('%.nvim', ''):gsub('-nvim', '')
3637
require(module).setup(self.setup_config)
3738
end
3839
end
@@ -68,9 +69,9 @@ function window:new()
6869
return o
6970
end
7071

71-
function window:creaet()
72+
function window:create_window()
7273
self.bufnr = api.nvim_create_buf(false, false)
73-
self.win = api.nvim_open_win(self.bufnr, true, {
74+
self.winid = api.nvim_open_win(self.bufnr, true, {
7475
relative = 'editor',
7576
height = math.floor(vim.o.lines * 0.5),
7677
width = math.floor(vim.o.columns * 0.8),
@@ -79,10 +80,16 @@ function window:creaet()
7980
border = 'rounded',
8081
noautocmd = true,
8182
style = 'minimal',
82-
hide = true,
8383
})
84-
vim.wo[self.win].wrap = false
84+
vim.wo[self.winid].wrap = false
8585
vim.bo[self.bufnr].buftype = 'nofile'
86+
vim.bo[self.bufnr].bufhidden = 'wipe'
87+
vim.keymap.set('n', 'q', function()
88+
if self.winid and api.nvim_win_is_valid(self.winid) then
89+
api.nvim_win_close(self.winid, true)
90+
self.bufnr, self.winid = nil, nil
91+
end
92+
end, { buffer = self.bufnr, desc = 'quit window' })
8693
end
8794

8895
function window:get_row(repo_name)
@@ -103,38 +110,65 @@ function window:write_output(name, data)
103110
if not self.bufnr then
104111
self:create_window()
105112
end
113+
vim.bo[self.bufnr].modifiable = true
106114
buf_set_lines(self.bufnr, row, row + 1, false, { ('%s: %s'):format(name, data) })
115+
vim.bo[self.bufnr].modifiable = false
107116
end)
108117
end
109118

119+
local MAX_CONCURRENT_TASKS = if_nil(vim.g.minpm_max_concurrent_tasks, 2)
120+
local active_tasks = 0
121+
local task_queue = {}
122+
123+
local function process_queue()
124+
while active_tasks < MAX_CONCURRENT_TASKS and #task_queue > 0 do
125+
local task = table.remove(task_queue, 1)
126+
active_tasks = active_tasks + 1
127+
task(function()
128+
active_tasks = active_tasks - 1
129+
process_queue()
130+
end)
131+
end
132+
end
133+
134+
local function queue_task(fn)
135+
table.insert(task_queue, fn)
136+
process_queue()
137+
end
138+
110139
function use_meta:do_action(action, winobj)
111-
local path = vim.fs.joinpath(self.islazy and OPTDIR or STARTDIR, self.tail)
112-
local url = ('https://github.com/%s'):format(self.name)
113-
local cmd = action == INSTALL and { 'git', 'clone', '--progress', url, path }
114-
or { 'git', '-C', '--progress', path, 'pull' }
115-
uv.fs_stat(path, function(_, stat)
116-
if stat and stat.type == 'directory' then
117-
return
118-
end
119-
coroutine.resume(coroutine.create(function()
120-
local co = assert(coroutine.running())
121-
vim.system(cmd, {
122-
timeout = 5000,
123-
stderr = function(err, data)
124-
coroutine.resume(co, err, data)
125-
end,
126-
})
127-
while true do
128-
local err, data = coroutine.yield()
129-
if not data then
130-
break
131-
end
132-
local lines = err and err or data
133-
lines = lines:gsub('\r', '\n'):gsub('\n+', '\n')
134-
lines = vim.split(lines, '\n', { trimempty = true })
135-
winobj:write_output(self.name, lines[#lines])
140+
queue_task(function(task_done)
141+
local path = vim.fs.joinpath(self.islazy and OPTDIR or STARTDIR, self.tail)
142+
local url = ('https://github.com/%s'):format(self.name)
143+
local cmd = action == INSTALL and { 'git', 'clone', '--progress', url, path }
144+
or { 'git', '-C', path, 'pull', '--progress' }
145+
uv.fs_stat(path, function(_, stat)
146+
if stat and stat.type == 'directory' then
147+
winobj:write_output(self.name, 'Directory already exists skipped')
148+
task_done()
149+
return
136150
end
137-
end))
151+
coroutine.resume(coroutine.create(function()
152+
local co = assert(coroutine.running())
153+
vim.system(cmd, {
154+
timeout = 5000,
155+
stderr = function(err, data)
156+
coroutine.resume(co, err, data)
157+
end,
158+
})
159+
while true do
160+
local err, data = coroutine.yield()
161+
local lines = err and err or data
162+
if not lines then
163+
task_done()
164+
break
165+
end
166+
lines = lines:gsub('\r', '\n'):gsub('\n+', '\n')
167+
lines = vim.split(lines, '\n', { trimempty = true })
168+
winobj:write_output(self.name, lines[#lines])
169+
end
170+
end))
171+
end)
138172
end)
139173
end
140174

0 commit comments

Comments
 (0)