85 lines
2.0 KiB
Lua
85 lines
2.0 KiB
Lua
---@class chunk: table
|
|
---@field [1] string text to be displayed
|
|
---@field [2] string neovim highlight group to use
|
|
|
|
---@class page
|
|
---@field name string name of the ui page
|
|
---@field kb string keybind of the page
|
|
---@field content chunk[]|chunk[][] all the chunks
|
|
---@field hlns number highlight namespace
|
|
---@field pre_draw function things to do prior to drawing to the buffer
|
|
---@field post_draw function things to do post drawing to the buffer
|
|
local page = {}
|
|
|
|
--- create a new page
|
|
---@param name string the name of the page
|
|
---@param kb string keybind to change to the page
|
|
---@return page page
|
|
function page:new(name, kb)
|
|
local o = {}
|
|
self.__index = self
|
|
setmetatable(o, self)
|
|
|
|
o.hlns = vim.api.nvim_create_namespace("DepUi")
|
|
o.name = name
|
|
o.kb = kb
|
|
o.content = {}
|
|
|
|
return o
|
|
end
|
|
|
|
--- add a new line to the page
|
|
---@param line chunk|chunk[] new line
|
|
function page:new_line(line)
|
|
table.insert(self.content, line)
|
|
end
|
|
|
|
--- draw the page to the given buffer
|
|
---@param bufnr number buffer number
|
|
function page:draw(bufnr)
|
|
-- try to run pre_draw steps
|
|
if self.pre_draw then
|
|
self.pre_draw()
|
|
end
|
|
|
|
-- ready all information for rendering
|
|
for i, chunk in ipairs(self.content) do
|
|
local linenr = i - 1
|
|
local text = ""
|
|
local hls = {}
|
|
|
|
if type(chunk[1]) == "table" then
|
|
local j = 0
|
|
for _, ch in ipairs(chunk) do
|
|
text = text..ch[1]
|
|
table.insert(hls, { ch[2], j, j + #ch[1] })
|
|
j = j + #ch[1]
|
|
end
|
|
elseif type(chunk[1]) == "string" then
|
|
text = chunk[1]
|
|
table.insert(hls, { chunk[2], 0, #text })
|
|
end
|
|
|
|
-- draw the text to the buffer
|
|
vim.api.nvim_buf_set_lines(bufnr, linenr, -1, false, { text })
|
|
|
|
-- highlight the buffer
|
|
for _, hl in ipairs(hls) do
|
|
vim.api.nvim_buf_set_extmark(bufnr, self.hlns, linenr, hl[2], {
|
|
hl_mode = "replace",
|
|
hl_group = hl[1],
|
|
end_col = hl[3],
|
|
end_row = linenr
|
|
})
|
|
end
|
|
|
|
end
|
|
|
|
-- try to run post_draw steps
|
|
if self.post_draw then
|
|
self.post_draw()
|
|
end
|
|
end
|
|
|
|
return page
|