mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-03-26 12:10:32 +08:00
feat(repl): add spinners
This commit is contained in:
parent
82cabd7c99
commit
caa371fae4
44
lua/spacevim/api/unicode/spinners.lua
Normal file
44
lua/spacevim/api/unicode/spinners.lua
Normal 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
|
@ -10,6 +10,7 @@ local job = require('spacevim.api.job')
|
|||||||
local nt = require('spacevim.api.notify')
|
local nt = require('spacevim.api.notify')
|
||||||
local vopt = require('spacevim.api.vim.option')
|
local vopt = require('spacevim.api.vim.option')
|
||||||
local str = require('spacevim.api.data.string')
|
local str = require('spacevim.api.data.string')
|
||||||
|
local spi = require('spacevim.api.unicode.spinners')
|
||||||
|
|
||||||
local log = require('spacevim.logger').derive('repl')
|
local log = require('spacevim.logger').derive('repl')
|
||||||
|
|
||||||
@ -21,6 +22,7 @@ local start_time
|
|||||||
local end_time
|
local end_time
|
||||||
local job_id = 0
|
local job_id = 0
|
||||||
local exes = {}
|
local exes = {}
|
||||||
|
local repl_spinners = ''
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
@ -119,13 +121,21 @@ local function on_exit(id, code, single)
|
|||||||
end_time = vim.fn.reltime(start_time)
|
end_time = vim.fn.reltime(start_time)
|
||||||
status.is_exit = true
|
status.is_exit = true
|
||||||
status.exit_code = code
|
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
|
if vim.api.nvim_buf_is_valid(bufnr) then
|
||||||
vim.api.nvim_buf_set_option(bufnr, 'modifiable', true)
|
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_lines(bufnr, lines, lines + 1, false, done)
|
||||||
vim.api.nvim_buf_set_option(bufnr, 'modifiable', false)
|
vim.api.nvim_buf_set_option(bufnr, 'modifiable', false)
|
||||||
end
|
end
|
||||||
job_id = 0
|
job_id = 0
|
||||||
|
spi.stop()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function start(exe)
|
local function start(exe)
|
||||||
@ -155,6 +165,14 @@ local function start(exe)
|
|||||||
on_stderr = on_stderr,
|
on_stderr = on_stderr,
|
||||||
on_exit = on_exit,
|
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
|
end
|
||||||
|
|
||||||
function M.start(ft)
|
function M.start(ft)
|
||||||
@ -180,7 +198,7 @@ function M.send(t, ...)
|
|||||||
job.send(job_id, data)
|
job.send(job_id, data)
|
||||||
elseif t == 'raw' then
|
elseif t == 'raw' then
|
||||||
local context = select(1, ...)
|
local context = select(1, ...)
|
||||||
if type(context) == "string" then
|
if type(context) == 'string' then
|
||||||
job.send(job_id, context)
|
job.send(job_id, context)
|
||||||
end
|
end
|
||||||
elseif t == 'selection' then
|
elseif t == 'selection' then
|
||||||
@ -203,7 +221,7 @@ end
|
|||||||
|
|
||||||
function M.status()
|
function M.status()
|
||||||
if status.is_running then
|
if status.is_running then
|
||||||
return 'running'
|
return 'running ' .. repl_spinners
|
||||||
elseif status.is_exit then
|
elseif status.is_exit then
|
||||||
return 'exit code:' .. status.exit_code .. ' time:' .. str.trim(vim.fn.reltimestr(end_time))
|
return 'exit code:' .. status.exit_code .. ' time:' .. str.trim(vim.fn.reltimestr(end_time))
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user