feat(text): center text

This commit is contained in:
Folke Lemaitre 2022-11-29 10:29:56 +01:00
parent 7303017b6f
commit 88869e67d2
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
1 changed files with 16 additions and 0 deletions

View File

@ -120,6 +120,22 @@ function Text:highlight(patterns)
end end
end end
function Text:center()
local last = self._lines[#self._lines]
if not last then
return
end
local width = 0
for _, segment in ipairs(last) do
width = width + vim.fn.strwidth(segment.str)
end
width = vim.api.nvim_win_get_width(self.win) - 2 * self.padding - width
table.insert(last, 1, {
str = string.rep(" ", math.floor(width / 2 + 0.5)),
})
return self
end
function Text:trim() function Text:trim()
while #self._lines > 0 and #self._lines[1] == 0 do while #self._lines > 0 and #self._lines[1] == 0 do
table.remove(self._lines, 1) table.remove(self._lines, 1)