Repository navigation
Expand file tree
/
Copy pathloading_animation.lua
More file actions
239 lines (196 loc) · 6.13 KB
/
Copy pathloading_animation.lua
File metadata and controls
239 lines (196 loc) · 6.13 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
local state = require('opencode.state')
local config = require('opencode.config')
local Timer = require('opencode.ui.timer')
local M = {}
M._animation = {
frames = nil,
text = 'Thinking... ',
status_data = nil,
current_frame = 1,
timer = nil,
fps = 10,
extmark_id = nil,
ns_id = vim.api.nvim_create_namespace('opencode_loading_animation'),
status_event_manager = nil,
}
---@param status table|nil
---@return string|nil
function M._format_status_text(status)
if type(status) ~= 'table' then
return nil
end
local status_type = status.type
if status_type == 'busy' then
return M._animation.text
end
if status_type == 'idle' then
return nil
end
if status_type == 'retry' then
local message = status.message or 'Retrying request'
local details = {}
if type(status.attempt) == 'number' then
table.insert(details, 'retry ' .. status.attempt)
end
if type(status.next) == 'number' then
local now_ms = os.time() * 1000
local seconds = math.max(0, math.ceil((status.next - now_ms) / 1000))
table.insert(details, 'in ' .. seconds .. 's')
end
if #details > 0 then
return string.format('%s (%s)... ', message, table.concat(details, ', '))
end
return message .. '... '
end
if type(status.message) == 'string' and status.message ~= '' then
return status.message .. '... '
end
return M._animation.text
end
local function unsubscribe_session_status_event(manager)
if manager and M._animation.status_event_manager == manager then
manager:unsubscribe('session.status', M.on_session_status)
M._animation.status_event_manager = nil
end
end
local function subscribe_session_status_event(manager)
if not manager then
return
end
if M._animation.status_event_manager and M._animation.status_event_manager ~= manager then
unsubscribe_session_status_event(M._animation.status_event_manager)
end
if M._animation.status_event_manager == manager then
return
end
manager:subscribe('session.status', M.on_session_status)
M._animation.status_event_manager = manager
end
function M.on_session_status(properties)
if not properties or type(properties) ~= 'table' then
return
end
local active_session = state.active_session
if active_session and active_session.id and properties.sessionID ~= active_session.id then
return
end
M._animation.status_data = properties.status
M.render(state.windows)
end
local function on_active_session_change(_, new_session, old_session)
local new_id = new_session and new_session.id
local old_id = old_session and old_session.id
if new_id ~= old_id then
M._animation.status_data = nil
end
end
local function on_event_manager_change(_, new_manager, old_manager)
unsubscribe_session_status_event(old_manager)
subscribe_session_status_event(new_manager)
end
function M._get_display_text()
return M._format_status_text(M._animation.status_data) or M._animation.text
end
function M._get_frames()
if M._animation.frames then
return M._animation.frames
end
local ui_config = config.ui
if ui_config and ui_config.loading_animation and ui_config.loading_animation.frames then
return ui_config.loading_animation.frames
end
-- return { '·', '․', '•', '∙', '●', '⬤', '●', '∙', '•', '․' }
return { '⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏' }
end
M.render = vim.schedule_wrap(function(windows)
windows = windows or state.windows
if not windows or not windows.output_buf or not windows.footer_buf then
return false
end
if not vim.api.nvim_buf_is_valid(windows.output_buf) or not vim.api.nvim_buf_is_valid(windows.footer_buf) then
return false
end
if not state.jobs.is_running() then
M.stop()
return false
end
local loading_text = M._get_display_text() .. M._get_frames()[M._animation.current_frame]
M._animation.extmark_id = vim.api.nvim_buf_set_extmark(windows.footer_buf, M._animation.ns_id, 0, 0, {
id = M._animation.extmark_id or nil,
virt_text = { { loading_text, 'OpencodeHint' } },
virt_text_pos = 'overlay',
hl_mode = 'replace',
})
return true
end)
function M._next_frame()
return (M._animation.current_frame % #M._get_frames()) + 1
end
function M._start_animation_timer(windows)
M._clear_animation_timer()
local interval = math.floor(1000 / M._animation.fps)
M._animation.timer = Timer.new({
interval = interval,
on_tick = function()
M._animation.current_frame = M._next_frame()
M.render(state.windows)
if state.jobs.is_running() then
return true
else
M.stop()
return false
end
end,
repeat_timer = true,
})
M._animation.timer:start()
end
function M._clear_animation_timer()
if M._animation.timer then
M._animation.timer:stop()
M._animation.timer = nil
end
end
function M.start(windows)
windows = windows or state.windows
if not windows then
return
end
M._start_animation_timer(windows)
M.render(windows)
end
function M.stop()
M._clear_animation_timer()
M._animation.current_frame = 1
M._animation.status_data = nil
if state.windows and state.windows.footer_buf and vim.api.nvim_buf_is_valid(state.windows.footer_buf) then
pcall(vim.api.nvim_buf_clear_namespace, state.windows.footer_buf, M._animation.ns_id, 0, -1)
end
end
function M.is_running()
return M._animation.timer ~= nil
end
local function on_running_change(_, new_value)
if not state.windows then
return
end
if not M.is_running() and new_value and new_value > 0 then
M.start(state.windows)
else
M.stop()
end
end
function M.setup()
state.store.subscribe('job_count', on_running_change)
state.store.subscribe('active_session', on_active_session_change)
state.store.subscribe('event_manager', on_event_manager_change)
subscribe_session_status_event(state.event_manager)
end
function M.teardown()
state.store.unsubscribe('job_count', on_running_change)
state.store.unsubscribe('active_session', on_active_session_change)
state.store.unsubscribe('event_manager', on_event_manager_change)
unsubscribe_session_status_event(M._animation.status_event_manager)
M._animation.status_data = nil
end
return M