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
100 lines (89 loc) · 2.15 KB
/
spinner.lua
File metadata and controls
100 lines (89 loc) · 2.15 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
88
89
90
91
92
93
94
95
96
97
98
99
100
-- spinner.lua
--
-- This library is free software; you can redistribute it and/or modify it
-- under the terms of the MIT license. See LICENSE for details.
local M = {}
-- User configuration section
local config = {
-- Show notification when done.
-- Set to false to disable.
show_notification = true,
-- Name of the plugin.
plugin = 'CopilotChat.nvim',
-- Spinner frames.
spinner_frames = {
'⠋',
'⠙',
'⠹',
'⠸',
'⠼',
'⠴',
'⠦',
'⠧',
'⠇',
'⠏',
},
}
-- {{{ NO NEED TO CHANGE
local spinner_index = 1
local spinner_timer = nil
local spinner_buf = nil
local spinner_win = nil
--- Show a spinner at the specified position.
---@param position? table
function M.show(position)
-- Default position: the top right corner
local default_position = {
relative = 'editor',
width = 1,
height = 1,
col = vim.o.columns - 1,
row = 0,
}
local options = position or default_position
options.style = 'minimal'
-- Create buffer and window for the spinner
spinner_buf = vim.api.nvim_create_buf(false, true)
spinner_win = vim.api.nvim_open_win(spinner_buf, false, options)
-- Set up timer and update spinner
spinner_timer = vim.loop.new_timer()
spinner_timer:start(
0,
100,
vim.schedule_wrap(function()
if vim.fn.bufexists(spinner_buf) == 0 then
-- Hide the spinner if the buffer does not exist
M.hide()
return
end
vim.api.nvim_buf_set_lines(
spinner_buf,
0,
-1,
false,
{ config.spinner_frames[spinner_index] }
)
spinner_index = spinner_index % #config.spinner_frames + 1
end)
)
end
--- Hide the spinner.
---@param show_msg? boolean
function M.hide(show_msg)
if spinner_timer then
spinner_timer:stop()
spinner_timer:close()
spinner_timer = nil
if spinner_win then
vim.api.nvim_win_close(spinner_win, true)
end
if spinner_buf then
vim.api.nvim_buf_delete(spinner_buf, { force = true })
end
if config.show_notification or show_msg then
vim.notify('Done!', vim.log.levels.INFO, { title = config.plugin })
end
end
end
-- }}}
return M