-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateview.lua
113 lines (97 loc) · 2.7 KB
/
updateview.lua
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
local uv = vim.loop
local api = vim.api
local tinsert = table.insert
local UPDATE_INTERVAL = 50 -- minimum time in ms to flush changes
local COLUMN_WIDTH = 50 -- plugin uri column width
---@type NeopmState
local state = require('neopm.state')
---@type number
local bufnr
---@class NeopmUpdateView
---@field bufnr number
---@field lines string[]
---@field last_update number
---@field pending_redraw boolean
local UpdateView = {}
UpdateView.__index = UpdateView
--- Create a new update view
---@return NeopmUpdateView
function UpdateView.new()
if bufnr and api.nvim_buf_is_loaded(bufnr) then
api.nvim_set_current_buf(bufnr)
else
vim.cmd([[
enew
setl buftype=nofile
setl nowrap
]])
bufnr = api.nvim_get_current_buf()
local ok, err = pcall(api.nvim_buf_set_name, bufnr, 'neopm://update')
if not ok then
if err:match('^Vim:E95:') then
local created = false
for i = 2, 999 do -- to not go forever if something goes wrong I guess
ok, err = pcall(api.nvim_buf_set_name, bufnr, 'neopm://update('..i..')')
if ok then
created = true
break
elseif not err:match('^Vim:E95:') then
error(err)
end
end
if not created then
error('failed to create a new buffer')
end
else
error(err)
end
end
end
api.nvim_buf_set_option(bufnr, 'modifiable', false)
local lines = {}
-- populate buffer with plugin uris
for i, plug in ipairs(state.by_order) do
lines[i] = plug.uri
end
-- add line for global status
tinsert(lines, '')
local this = setmetatable({
bufnr = bufnr,
lines = lines,
last_update = uv.now(),
pending_redraw = false,
}, UpdateView)
return this
end
--- Set plugin status string
---@param plug NeopmPlug
---@param status string
function UpdateView:set(plug, status)
self.lines[plug.order] = plug.uri..string.rep(' ', COLUMN_WIDTH - #plug.uri)..status
self.pending_redraw = true
end
--- Set global status string
---@param status string
function UpdateView:global(status)
self.lines[#self.lines] = status
self.pending_redraw = true
self:flush(true)
end
--- Flush changes to the buffer
---@param force? boolean Update the buffer immediately
function UpdateView:flush(force)
if not self.pending_redraw then
return
end
local now = uv.now()
if not force and now - self.last_update < UPDATE_INTERVAL then
return
end
self.last_update = now
self.pending_redraw = false
api.nvim_buf_set_option(self.bufnr, 'modifiable', true)
api.nvim_buf_set_lines(self.bufnr, 0, -1, false, self.lines)
api.nvim_buf_set_option(self.bufnr, 'modifiable', false)
vim.cmd('redraw')
end
return UpdateView