1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 13:40:05 +08:00

feat(repl): add spinners

This commit is contained in:
wsdjeg 2023-09-05 21:12:17 +08:00
parent 82cabd7c99
commit caa371fae4
2 changed files with 65 additions and 3 deletions

View File

@ -0,0 +1,44 @@
local M = {}
M._data = {
dot1 = {
frames = { '', '', '', '', '', '', '', '', '', '' },
strwidth = 1,
timeout = 80,
},
}
M._id = 0
function M.Onframe(...)
if M.index < #M.spinners then
M.index = M.index + 1
else
M.index = 1
end
M.func(M.spinners[M.index])
end
function M.stop()
vim.fn.timer_stop(M.timer_id)
end
-- if var is a function, then the function will be called with one argv
function M.apply(name, var)
local data = M._data[name] or {}
local time = data.timeout or 80
M.index = 1
M.spinners = M._data[name].frames
if type(var) == 'function' then
M.func = var
M.func(M.spinners[M.index])
end
M.timer_id = vim.fn.timer_start(time, M.Onframe, { ['repeat'] = -1 })
return { M.timer_id, M._data[name].strwidth }
end
function M.get_str()
return M.str
end
return M

View File

@ -10,6 +10,7 @@ local job = require('spacevim.api.job')
local nt = require('spacevim.api.notify')
local vopt = require('spacevim.api.vim.option')
local str = require('spacevim.api.data.string')
local spi = require('spacevim.api.unicode.spinners')
local log = require('spacevim.logger').derive('repl')
@ -21,6 +22,7 @@ local start_time
local end_time
local job_id = 0
local exes = {}
local repl_spinners = ''
local M = {}
@ -119,13 +121,21 @@ local function on_exit(id, code, single)
end_time = vim.fn.reltime(start_time)
status.is_exit = true
status.exit_code = code
local done = {'', '[Done] exited with code=' .. code .. ' in ' .. str.trim(vim.fn.reltimestr(end_time)) .. ' seconds'}
local done = {
'',
'[Done] exited with code='
.. code
.. ' in '
.. str.trim(vim.fn.reltimestr(end_time))
.. ' seconds',
}
if vim.api.nvim_buf_is_valid(bufnr) then
vim.api.nvim_buf_set_option(bufnr, 'modifiable', true)
vim.api.nvim_buf_set_lines(bufnr, lines, lines + 1, false, done)
vim.api.nvim_buf_set_option(bufnr, 'modifiable', false)
end
job_id = 0
spi.stop()
end
local function start(exe)
@ -155,6 +165,14 @@ local function start(exe)
on_stderr = on_stderr,
on_exit = on_exit,
})
if job_id > 0 then
spi.apply('dot1', function(v)
repl_spinners = v
if vim.api.nvim_win_is_valid(winid) then
vim.fn.win_execute(winid, 'redrawstatus')
end
end)
end
end
function M.start(ft)
@ -180,7 +198,7 @@ function M.send(t, ...)
job.send(job_id, data)
elseif t == 'raw' then
local context = select(1, ...)
if type(context) == "string" then
if type(context) == 'string' then
job.send(job_id, context)
end
elseif t == 'selection' then
@ -203,7 +221,7 @@ end
function M.status()
if status.is_running then
return 'running'
return 'running ' .. repl_spinners
elseif status.is_exit then
return 'exit code:' .. status.exit_code .. ' time:' .. str.trim(vim.fn.reltimestr(end_time))
end