forked from CopilotC-Nvim/CopilotChat.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspinner.lua
More file actions
87 lines (73 loc) · 1.91 KB
/
Copy pathspinner.lua
File metadata and controls
87 lines (73 loc) · 1.91 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
---@class CopilotChat.Spinner
---@field bufnr number
---@field set fun(self: CopilotChat.Spinner, text: string, offset: number)
---@field start fun(self: CopilotChat.Spinner)
---@field finish fun(self: CopilotChat.Spinner)
local utils = require('CopilotChat.utils')
local class = utils.class
local is_stable = utils.is_stable
local spinner_frames = {
'⠋',
'⠙',
'⠹',
'⠸',
'⠼',
'⠴',
'⠦',
'⠧',
'⠇',
'⠏',
}
local Spinner = class(function(self, bufnr, title)
self.ns = vim.api.nvim_create_namespace('copilot-spinner')
self.bufnr = bufnr
self.title = title
self.timer = nil
self.index = 1
end)
function Spinner:set(text, offset)
offset = offset or 0
vim.schedule(function()
if not vim.api.nvim_buf_is_valid(self.bufnr) then
self:finish()
return
end
local line = vim.api.nvim_buf_line_count(self.bufnr) - 1 + offset
line = math.max(0, line)
local opts = {
id = self.ns,
hl_mode = 'combine',
priority = 100,
virt_text = vim.tbl_map(function(t)
return { t, 'CursorColumn' }
end, vim.split(text, '\n')),
}
-- stable do not supports virt_text_pos
if not is_stable() then
opts.virt_text_pos = offset ~= 0 and 'inline' or 'eol'
end
vim.api.nvim_buf_set_extmark(self.bufnr, self.ns, line, 0, opts)
end)
end
function Spinner:start()
self.timer = vim.loop.new_timer()
self.timer:start(0, 100, function()
self:set(spinner_frames[self.index])
self.index = self.index % #spinner_frames + 1
end)
end
function Spinner:finish()
if self.timer then
self.timer:stop()
self.timer:close()
self.timer = nil
vim.schedule(function()
if not vim.api.nvim_buf_is_valid(self.bufnr) then
return
end
vim.api.nvim_buf_del_extmark(self.bufnr, self.ns, self.ns)
vim.notify('Done!', vim.log.levels.INFO, { title = self.title })
end)
end
end
return Spinner