Merge branch 'experimental'

This commit is contained in:
JuanZoran
2023-02-03 15:38:05 +08:00
15 changed files with 1118 additions and 981 deletions

View File

@@ -1,55 +0,0 @@
local display = function(self)
local callback = self.callback or function()
end
local target = self.times
if self.sync then
if target then
for i = 1, target do
if self.run then
self:frame(i)
end
end
else
while self.run do
self:frame()
end
end
callback()
else
local frame
if target then
local times = 0
frame = function()
if self.run and times < target then
times = times + 1
self:frame(times)
vim.defer_fn(frame, self.interval)
else
callback()
end
end
else
frame = function()
if self.run then
self:frame()
vim.defer_fn(frame, self.interval)
else
callback()
end
end
end
frame()
end
end
return function(opts)
opts.run = true
opts.display = display
return opts
end

View File

@@ -12,9 +12,32 @@ local curl = {}
curl.GET = function(uri, opts)
--- TODO :
vim.validate {
uri = { uri, 's' },
opts = { opts, 't' }
}
local cmd = {'curl', '-s', ('"%s"'):format(uri)}
local callback = opts.callback
local output = ''
local option = {
stdin = 'null',
on_stdout = function(_, stdout)
local str = table.concat(stdout)
if str ~= '' then
output = output .. str
end
end,
on_exit = function()
callback(output)
end,
}
vim.fn.jobstart(table.concat(cmd, ' '), option)
end
curl.POST = function(uri, opts)
vim.validate {
uri = { uri, 's' },
@@ -23,7 +46,7 @@ curl.POST = function(uri, opts)
local callback = opts.callback
local cmd = { 'curl', '-s', uri }
local cmd = { 'curl', '-s', ('"%s"'):format(uri) }
local size = 3
local function insert(...)
@@ -63,5 +86,4 @@ curl.POST = function(uri, opts)
vim.fn.jobstart(table.concat(cmd, ' '), option)
end
return curl

View File

@@ -0,0 +1,48 @@
return function(opts)
local target = opts.times
opts.run = target ~= 0
---@type function[]
local tasks = {}
local function do_task()
for _, task in ipairs(tasks) do
task()
end
end
local frame
if target then
local times = 0
frame = function()
if opts.run and times < target then
times = times + 1
opts:frame(times)
vim.defer_fn(frame, opts.interval)
else
do_task()
end
end
else
frame = function()
if opts.run then
opts:frame()
vim.defer_fn(frame, opts.interval)
else
do_task()
end
end
end
frame()
---任务句柄, 如果任务结束了则立即执行, 否则立即执行
---@param task function
return function(task)
if opts.run then
tasks[#tasks + 1] = task
else
task()
end
end
end