From 88869e67d2f06c7778b9bdbf57681615d3d41f11 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Tue, 29 Nov 2022 10:29:56 +0100 Subject: [PATCH] feat(text): center text --- lua/lazy/view/text.lua | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lua/lazy/view/text.lua b/lua/lazy/view/text.lua index 15fd56a7..ac009f48 100644 --- a/lua/lazy/view/text.lua +++ b/lua/lazy/view/text.lua @@ -120,6 +120,22 @@ function Text:highlight(patterns) 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() while #self._lines > 0 and #self._lines[1] == 0 do table.remove(self._lines, 1)