feat(view): modes and help

This commit is contained in:
Folke Lemaitre 2022-11-29 10:30:14 +01:00
parent 88869e67d2
commit 0db98bf053
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
5 changed files with 95 additions and 24 deletions

View File

@ -8,6 +8,7 @@ local M = {}
---@field wait? boolean ---@field wait? boolean
---@field clear? boolean ---@field clear? boolean
---@field interactive? boolean ---@field interactive? boolean
---@field mode? string
---@param ropts RunnerOpts ---@param ropts RunnerOpts
---@param opts? ManagerOpts ---@param opts? ManagerOpts
@ -23,7 +24,7 @@ function M.run(ropts, opts)
if opts.interactive then if opts.interactive then
vim.schedule(function() vim.schedule(function()
require("lazy.view").show() require("lazy.view").show(opts.mode)
end) end)
end end

View File

@ -2,7 +2,7 @@ local M = {}
M.colors = { M.colors = {
Error = "ErrorMsg", Error = "ErrorMsg",
H1 = "Title", H1 = "IncSearch",
H2 = "Bold", H2 = "Bold",
Muted = "Comment", Muted = "Comment",
Normal = "NormalFloat", Normal = "NormalFloat",
@ -21,7 +21,9 @@ M.colors = {
LoaderKeys = "Statement", LoaderKeys = "Statement",
LoaderStart = "@field", LoaderStart = "@field",
LoaderSource = "Character", LoaderSource = "Character",
LoaderCmd = "Operator" LoaderCmd = "Operator",
Button = "CursorLine",
ButtonActive = "Visual",
} }
M.did_setup = false M.did_setup = false

View File

@ -17,34 +17,37 @@ end
M.commands = { M.commands = {
clean = function() clean = function()
Manage.clean({ clear = true, interactive = true }) Manage.clean({ clear = true, interactive = true, mode = "clean" })
end, end,
clear = function() clear = function()
Manage.clear() Manage.clear()
View.show() View.show()
end, end,
install = function() install = function()
Manage.install({ clear = true, interactive = true }) Manage.install({ clear = true, interactive = true, mode = "install" })
end, end,
log = function() log = function()
Manage.log({ clear = true, interactive = true }) Manage.log({ clear = true, interactive = true, mode = "log" })
end, end,
show = function() show = function()
View.show() View.show()
end, end,
help = function()
View.show("help")
end,
sync = function() sync = function()
Manage.clean({ interactive = true, clear = true, wait = true }) Manage.clean({ interactive = true, clear = true, wait = true, mode = "sync" })
Manage.update({ interactive = true }) Manage.update({ interactive = true })
Manage.install({ interactive = true }) Manage.install({ interactive = true })
end, end,
update = function() update = function()
Manage.update({ clear = true, interactive = true }) Manage.update({ clear = true, interactive = true, mode = "update" })
end, end,
check = function() check = function()
Manage.check({ clear = true, interactive = true }) Manage.check({ clear = true, interactive = true, mode = "check" })
end, end,
restore = function() restore = function()
Manage.update({ clear = true, interactive = true, lockfile = true }) Manage.update({ clear = true, interactive = true, lockfile = true, mode = "restore" })
end, end,
} }

View File

@ -4,16 +4,36 @@ local Config = require("lazy.core.config")
local M = {} local M = {}
M.modes = {
{ name = "install", key = "I", desc = "Install missing plugins" },
{ name = "update", key = "U", desc = "Update all plugins. This will also update the lockfile" },
{ name = "sync", key = "S", desc = "Run install, clean and update" },
{ name = "clean", key = "X", desc = "Clean plugins that are no longer needed" },
{ name = "check", key = "C", desc = "Check for updates and show the log (git fetch)" },
{ name = "log", key = "L", desc = "Show recent updates for all plugins" },
{ name = "restore", key = "R", desc = "Updates all plugins to the state in the lockfile" },
{ name = "help", key = "g?", hide = true, desc = "Toggle this help page" },
}
---@type string?
M.mode = nil
function M.setup() function M.setup()
require("lazy.view.commands").setup() require("lazy.view.commands").setup()
require("lazy.view.colors").setup() require("lazy.view.colors").setup()
end end
function M.show() function M.show(mode)
if mode == "help" and M.mode == "help" then
M.mode = nil
else
M.mode = mode or M.mode
end
require("lazy.view.colors").setup() require("lazy.view.colors").setup()
if M._buf and vim.api.nvim_buf_is_valid(M._buf) then if M._buf and vim.api.nvim_buf_is_valid(M._buf) then
vim.api.nvim_win_set_cursor(M._win, { 1, 0 }) vim.api.nvim_win_set_cursor(M._win, { 1, 0 })
vim.cmd([[do User LazyRender]])
return return
end end
@ -24,8 +44,8 @@ function M.show()
local opts = { local opts = {
relative = "editor", relative = "editor",
style = "minimal", style = "minimal",
width = math.min(vim.o.columns - hpad * 2, 150), width = math.min(vim.o.columns - hpad * 2, 200),
height = math.min(vim.o.lines - vpad * 2, 50), height = math.min(vim.o.lines - vpad * 2, 70),
} }
opts.row = (vim.o.lines - opts.height) / 2 opts.row = (vim.o.lines - opts.height) / 2
opts.col = (vim.o.columns - opts.width) / 2 opts.col = (vim.o.columns - opts.width) / 2
@ -130,6 +150,13 @@ function M.show()
end, end,
}) })
for _, m in ipairs(M.modes) do
vim.keymap.set("n", m.key, function()
local Commands = require("lazy.view.commands")
Commands.cmd(m.name)
end, { buffer = buf })
end
vim.api.nvim_create_autocmd("User", { vim.api.nvim_create_autocmd("User", {
pattern = "LazyRender", pattern = "LazyRender",
callback = function() callback = function()

View File

@ -34,8 +34,6 @@ function M:update()
self._diagnostics = {} self._diagnostics = {}
self.plugin_range = {} self.plugin_range = {}
Plugin.update_state(true)
self.plugins = vim.tbl_values(Config.plugins) self.plugins = vim.tbl_values(Config.plugins)
vim.list_extend(self.plugins, vim.tbl_values(Config.to_clean)) vim.list_extend(self.plugins, vim.tbl_values(Config.to_clean))
table.sort(self.plugins, function(a, b) table.sort(self.plugins, function(a, b)
@ -58,11 +56,15 @@ function M:update()
end end
end end
self:title() local mode = self:title()
if mode == "help" then
self:help()
else
for _, section in ipairs(Sections) do for _, section in ipairs(Sections) do
self:section(section) self:section(section)
end end
end
self:trim() self:trim()
self:render(self.buf) self:render(self.buf)
@ -90,19 +92,55 @@ function M:get_plugin(row)
end end
function M:title() function M:title()
self:append("Lazy", "LazyH1") self:append(" lazy.nvim ", "LazyH1"):center():nl()
if self.progress.done < self.progress.total then self:append("press "):append("g?", "LazySpecial"):append(" for help"):center():nl()
self:append(" (" .. self.progress.done .. "/" .. self.progress.total .. ")", "LazyMuted"):nl() self:append("https://github.com/folke/lazy.nvim", "LazyMuted"):center():nl()
self:progressbar()
else local View = require("lazy.view")
self:append(" (" .. #self.plugins .. ")", "LazyMuted"):nl() for _, mode in ipairs(View.modes) do
if not mode.hide then
local title = " " .. mode.name:sub(1, 1):upper() .. mode.name:sub(2) .. " (" .. mode.key .. ") "
self:append(title, View.mode == mode.name and "LazyButtonActive" or "LazyButton"):append(" ")
end
end end
self:nl() self:nl()
if self.progress.done < self.progress.total then
self:progressbar()
end
self:nl()
if View.mode ~= "help" then
if self.progress.done < self.progress.total then
self:append("Tasks: ", "LazyH2")
self:append(self.progress.done .. "/" .. self.progress.total, "LazyMuted")
else
self:append("Total: ", "LazyH2")
self:append(#self.plugins .. " plugins", "LazyMuted")
end
self:nl():nl()
end
return View.mode
end
function M:help()
local View = require("lazy.view")
self:append("Help", "LazyH2"):nl():nl()
self:append("Keyboard Shortcuts", "LazyH2"):nl()
for _, mode in ipairs(View.modes) do
local title = mode.name:sub(1, 1):upper() .. mode.name:sub(2)
self:append("- ", "LazySpecial", { indent = 2 })
self:append(title, "Title"):append(" <" .. mode.key .. "> ", "LazyKey")
self:append(mode.desc or ""):nl()
end
end end
function M:progressbar() function M:progressbar()
local width = vim.api.nvim_win_get_width(self.win) - 2 * self.padding 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) local done = math.floor((self.progress.done / self.progress.total) * width + 0.5)
if self.progress.done == self.progress.total then
done = 0
end
self:append("", { self:append("", {
virt_text_win_col = self.padding, virt_text_win_col = self.padding,
virt_text = { { string.rep("", done), "LazyProgressDone" } }, virt_text = { { string.rep("", done), "LazyProgressDone" } },