-
-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathspinner.lua
More file actions
84 lines (71 loc) · 1.67 KB
/
spinner.lua
File metadata and controls
84 lines (71 loc) · 1.67 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
local notify = require('CopilotChat.utils.notify')
local utils = require('CopilotChat.utils')
local class = require('CopilotChat.utils.class')
local spinner_frames = {
'⠋',
'⠙',
'⠹',
'⠸',
'⠼',
'⠴',
'⠦',
'⠧',
'⠇',
'⠏',
}
---@class CopilotChat.ui.spinner.Spinner : Class
---@field bufnr number
---@field status string?
---@field private index number
---@field private timer table
---@field private ns number
local Spinner = class(function(self, bufnr)
self.bufnr = bufnr
self.status = nil
self.index = 1
self.timer = nil
self.ns = vim.api.nvim_create_namespace('copilot-chat-spinner')
notify.listen(notify.STATUS, function(status)
self.status = tostring(status)
end)
end)
function Spinner:start()
if self.timer then
return
end
self.timer = vim.uv.new_timer()
self.timer:start(
0,
100,
vim.schedule_wrap(function()
if not utils.buf_valid(self.bufnr) or not self.timer then
self:finish()
return
end
local frame = spinner_frames[self.index]
if self.status then
frame = self.status .. ' ' .. frame
end
vim.api.nvim_buf_set_extmark(self.bufnr, self.ns, math.max(0, vim.api.nvim_buf_line_count(self.bufnr) - 1), 0, {
id = 1,
hl_mode = 'combine',
priority = 100,
virt_text = {
{ frame, 'CopilotChatStatus' },
},
})
self.index = self.index % #spinner_frames + 1
end)
)
end
function Spinner:finish()
if not self.timer then
return
end
local timer = self.timer
self.timer = nil
timer:stop()
timer:close()
vim.api.nvim_buf_del_extmark(self.bufnr, self.ns, 1)
end
return Spinner