lazy.nvim/lua/lazy/view/text.lua

103 lines
2.0 KiB
Lua
Raw Normal View History

local Config = require("lazy.core.config")
2022-11-21 06:34:35 +08:00
---@alias TextSegment {str: string, hl?:string, extmark?:table}
2022-11-21 06:25:21 +08:00
---@class Text
2022-11-21 06:34:35 +08:00
---@field _lines TextSegment[][]
2022-11-21 06:25:21 +08:00
local Text = {}
function Text.new()
local self = setmetatable({}, {
__index = Text,
})
self._lines = {}
return self
end
---@param str string
2022-11-23 04:12:50 +08:00
---@param hl? string|table
2022-11-21 06:25:21 +08:00
function Text:append(str, hl)
if #self._lines == 0 then
self:nl()
end
table.insert(self._lines[#self._lines], {
str = str,
hl = type(hl) == "string" and hl or nil,
extmark = type(hl) == "table" and hl or nil,
})
return self
end
function Text:nl()
table.insert(self._lines, {})
return self
end
function Text:render(buf, padding)
padding = padding or 0
local lines = {}
for _, line in ipairs(self._lines) do
local str = (" "):rep(padding)
for _, segment in ipairs(line) do
str = str .. segment.str
end
table.insert(lines, str)
end
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
for l, line in ipairs(self._lines) do
local col = padding
for _, segment in ipairs(line) do
local width = vim.fn.strlen(segment.str)
if segment.hl then
vim.api.nvim_buf_set_extmark(buf, Config.ns, l - 1, col, {
hl_group = segment.hl,
end_col = col + width,
})
end
if segment.extmark then
vim.api.nvim_buf_set_extmark(buf, Config.ns, l - 1, col, segment.extmark)
end
col = col + width
end
end
end
function Text:trim()
while #self._lines > 0 and #self._lines[1] == 0 do
table.remove(self._lines, 1)
end
while #self._lines > 0 and #self._lines[#self._lines] == 0 do
table.remove(self._lines)
end
end
2022-11-23 04:12:50 +08:00
function Text:row()
return #self._lines == 0 and 1 or #self._lines
end
function Text:col()
if #self._lines == 0 then
return 0
end
local width = 0
for _, segment in ipairs(self._lines[#self._lines]) do
width = width + vim.fn.strlen(segment.str)
end
return width
end
2022-11-21 06:25:21 +08:00
return Text