1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 07:10:06 +08:00

feat(git): rewrite :Git branch with lua

This commit is contained in:
Eric Wong 2024-02-27 23:02:43 +08:00
parent 206c56db80
commit 9947755e66

View File

@ -69,5 +69,71 @@ end
function M.detect()
update_branch_name(vim.fn.getcwd(), true)
end
local branch_jobid = -1
local stderr_data = {}
local function on_stdout(id, data)
if id ~= branch_jobid then
return
end
for _, line in pairs(data) do
nt.notify_max_width = vim.fn.max({ vim.fn.strwidth(line) + 5, nt.notify_max_width })
nt.notify(line)
end
end
local function on_stderr(id, data)
if id ~= branch_jobid then
return
end
for _, line in pairs(data) do
table.insert(stderr_data, line)
end
end
local function on_exit(id, code, single)
log.debug('branch exit code:' .. code .. ' single:' .. single)
if id ~= branch_jobid then
return
end
if code == 0 and single == 0 then
for _, line in ipairs(stderr_data) do
nt.notify(line)
end
else
for _, line in ipairs(stderr_data) do
nt.notify(line, 'WarningMsg')
end
end
branch_jobid = -1
end
function M.run(argv)
if branch_jobid ~= -1 then
nt.notify('previous branch command is not finished')
end
nt.notify_max_width = vim.fn.float2nr(vim.o.columns * 0.3)
stderr_data = {}
local cmd = { 'git', 'branch' }
if argv then
for _, v in ipairs(argv) do
table.insert(cmd, v)
end
end
log.debug(vim.inspect(cmd))
branch_jobid = job.start(cmd, {
on_stdout = on_stdout,
on_stderr = on_stderr,
on_exit = on_exit,
})
if branch_jobid == -1 then
nt.notify('`git` is not executable', 'WarningMsg')
end
end
return M