lazy.nvim/lua/lazy/view/render.lua

137 lines
3.4 KiB
Lua
Raw Normal View History

2022-11-20 21:33:47 +00:00
local Config = require("lazy.config")
local Util = require("lazy.util")
local Manager = require("lazy.manager")
local Sections = require("lazy.view.sections")
local Text = require("lazy.view.text")
---@class Render:Text
---@field buf buffer
---@field win window
---@field padding number
---@field plugins LazyPlugin[]
---@field progress {total:number, done:number}
local M = setmetatable({}, { __index = Text })
function M.render_plugins(buf, win, padding)
2022-11-20 21:34:55 +00:00
local self = setmetatable({}, { __index = M })
self._lines = {}
self.buf = buf
self.win = win
self.padding = padding
Manager.check_clean()
self.plugins = vim.tbl_values(Config.plugins)
table.sort(self.plugins, function(a, b)
return a.name < b.name
end)
self.progress = {
total = 0,
done = 0,
}
for _, plugin in ipairs(self.plugins) do
if plugin.tasks then
for _, task in ipairs(plugin.tasks) do
self.progress.total = self.progress.total + 1
if not task.running then
self.progress.done = self.progress.done + 1
end
end
end
end
self:title()
for _, section in ipairs(Sections) do
self:section(section)
end
self:trim()
self:render(buf, padding)
return self
2022-11-20 21:33:47 +00:00
end
function M:title()
2022-11-20 21:34:55 +00:00
self:append("Lazy", "LazyH1")
if self.progress.done < self.progress.total then
self:append(" (" .. self.progress.done .. "/" .. self.progress.total .. ")", "LazyMuted"):nl()
self:progressbar()
else
self:append(" (" .. #self.plugins .. ")", "LazyMuted"):nl()
end
self:nl()
2022-11-20 21:33:47 +00:00
end
function M:progressbar()
2022-11-20 21:34:55 +00:00
local width = vim.api.nvim_win_get_width(self.win) - 2 * self.padding
local done = math.floor((self.progress.done / self.progress.total) * width + 0.5)
self:append("", {
virt_text_win_col = self.padding,
virt_text = { { string.rep("", done), "LazyProgressDone" } },
})
self:append("", {
virt_text_win_col = done + self.padding,
virt_text = { { string.rep("", width - done), "LazyProgressTodo" } },
})
2022-11-20 21:33:47 +00:00
end
---@param section LazySection
function M:section(section)
2022-11-20 21:34:55 +00:00
---@type LazyPlugin[]
local section_plugins = {}
---@param plugin LazyPlugin
self.plugins = vim.tbl_filter(function(plugin)
if section.filter(plugin) then
table.insert(section_plugins, plugin)
return false
end
return true
end, self.plugins)
local count = #section_plugins
if count > 0 then
self:append(section.title, "LazyH2"):append(" (" .. count .. ")", "LazyMuted"):nl()
for _, plugin in ipairs(section_plugins) do
self:plugin(plugin)
end
self:nl()
end
2022-11-20 21:33:47 +00:00
end
---@param plugin LazyPlugin
function M:plugin(plugin)
2022-11-20 21:34:55 +00:00
self:append(" - ", "LazySpecial"):append(plugin.name)
if plugin.tasks then
for _, task in ipairs(plugin.tasks) do
if task.running then
self:append(" [" .. task.type .. "] ", "Identifier")
self:append(task.status, "LazyMuted")
elseif task.error then
local lines = vim.split(vim.trim(task.error), "\n")
self:append(" [" .. task.type .. "] ", "Identifier")
for l, line in ipairs(lines) do
self:append(line, "LazyError")
if l ~= #lines then
self:nl()
end
end
end
end
end
self:nl()
-- self:details(plugin)
2022-11-20 21:33:47 +00:00
end
---@param plugin LazyPlugin
function M:details(plugin)
2022-11-20 21:34:55 +00:00
local git = Util.git_info(plugin.dir)
if git then
self:append(git.branch)
end
self:nl()
2022-11-20 21:33:47 +00:00
end
return M