forked from Mythos-404/xmake.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync.lua
67 lines (55 loc) · 1.51 KB
/
async.lua
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
local M = {}
---去除ASCII的特殊字符
---@param str string
---@return string
local function remove_ansii_code(str)
if str == nil then
return ""
end
str = str:gsub("\x1b%[%d+;%d+;%d+;%d+;%d+m", "")
:gsub("\x1b%[%d+;%d+;%d+;%d+m", "")
:gsub("\x1b%[%d+;%d+;%d+m", "")
:gsub("\x1b%[%d+;%d+m", "")
:gsub("\x1b%[%d+m", "")
return str
end
--- 包装执行函数
--- 为其添加其他功能
---@param cmd string[]
---@param cb function
---@param message? string
---@return fun(sc: SystemCompleted)
local function warpper_on_exit(cmd, cb, message)
local log = require("xmake.log")
local config = require("xmake.config").config
return vim.schedule_wrap(function(sc)
local stdout, stderr = remove_ansii_code(sc.stdout), remove_ansii_code(sc.stderr)
if config.debug then
log.error(string.format("Error Out:\n%s", stderr))
log.error(string.format("Out:\n%s", stdout))
end
if sc.code ~= 0 then
log.error(string.format("Exec: %s(%s)", table.concat(cmd, " "), sc.code))
return
end
cb(stdout:gmatch("[^\n]+"))
if message ~= nil then
log.info(message)
end
end)
end
--- 执行命令
---@param cmd string[]
---@param on_exit string|fun(data: string)
---@param message? string
function M.exec_commnd(cmd, on_exit, message)
local config = require("xmake.config").config
local cb = nil
if type(on_exit) == "string" then
cb = warpper_on_exit(cmd, function() end, on_exit)
else
cb = warpper_on_exit(cmd, on_exit, message)
end
vim.system(cmd, { cwd = config.work_dir, text = true }, cb)
end
return M