Skip to content

Commit

Permalink
feat: a gazilion rendering improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Nov 23, 2022
1 parent 00ff59f commit a11fc5a
Show file tree
Hide file tree
Showing 5 changed files with 291 additions and 50 deletions.
7 changes: 5 additions & 2 deletions lua/lazy/view/colors.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
local M = {}

M.colors = {
Error = "Error",
Error = "ErrorMsg",
H1 = "Title",
H2 = "Title",
H2 = "Bold",
Muted = "Comment",
Normal = "NormalFloat",
Commit = "@variable.builtin",
Key = "@comment",
Value = "@string",
ProgressDone = {
bold = true,
default = true,
Expand Down
8 changes: 8 additions & 0 deletions lua/lazy/view/commands.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local View = require("lazy.view")
local Manager = require("lazy.manager")
local Util = require("lazy.core.util")

local M = {}

Expand All @@ -18,6 +19,10 @@ M.commands = {
clean = function()
Manager.clean({ clear = true, show = true })
end,
clear = function()
Manager.clear()
View.show()
end,
install = function()
Manager.install({ clear = true, show = true })
end,
Expand All @@ -27,6 +32,9 @@ M.commands = {
show = function()
View.show()
end,
docs = function()
Manager.docs({ clear = true, show = true })
end,
sync = function()
Manager.update({ clear = true, show = true })
Manager.install({ show = true })
Expand Down
92 changes: 87 additions & 5 deletions lua/lazy/view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function M.show()
require("lazy.view.colors").setup()

if M._buf and vim.api.nvim_buf_is_valid(M._buf) then
vim.api.nvim_win_set_cursor(M._win, { 1, 0 })
return
end

Expand All @@ -28,6 +29,7 @@ function M.show()
opts.row = (vim.o.lines - opts.height) / 2
opts.col = (vim.o.columns - opts.width) / 2
local win = vim.api.nvim_open_win(buf, true, opts)
M._win = win

vim.api.nvim_set_current_win(win)

Expand Down Expand Up @@ -68,24 +70,104 @@ function M.show()
callback = close,
})

local render = Util.throttle(30, function()
local render = Render.new(buf, win, 2)
local update = Util.throttle(30, function()
vim.bo[buf].modifiable = true
Render.render_plugins(buf, win, 2)
render:update()
vim.bo[buf].modifiable = false
vim.cmd.redraw()
end)

local function get_plugin()
local pos = vim.api.nvim_win_get_cursor(win)
return render:get_plugin(pos[1])
end

vim.keymap.set("n", "<cr>", function()
local plugin = get_plugin()
if plugin then
if render._details == plugin.name then
render._details = nil
else
render._details = plugin.name
end
update()
end
end, {
nowait = true,
buffer = buf,
})

local function open(path)
local plugin = get_plugin()
if plugin then
local url = plugin.uri:gsub("%.git$", "")
if Util.file_exists(url) then
url = "https://github.com/" .. plugin[1]
end
Util.open(url .. path)
end
end

M.keys(buf, {
["%s(" .. string.rep("[a-z0-9]", 7) .. ")%s"] = function(hash)
open("/commit/" .. hash)
end,
["%s(" .. string.rep("[a-z0-9]", 7) .. ")$"] = function(hash)
open("/commit/" .. hash)
end,
["^(" .. string.rep("[a-z0-9]", 7) .. ")%s"] = function(hash)
open("/commit/" .. hash)
end,
["#(%d+)"] = function(issue)
open("/issues/" .. issue)
end,
["README.md"] = function()
local plugin = get_plugin()
Util.open(plugin.dir .. "/README.md")
end,
["(https?://%S+)"] = function(url)
Util.open(url)
end,
})

vim.api.nvim_create_autocmd("User", {
pattern = "LazyRender",
callback = function()
if not vim.api.nvim_buf_is_valid(buf) then
return true
end

render()
update()
end,
})
render()
update()
end

---@param handlers table<string, fun(str:string)>
function M.keys(buf, handlers)
local function map(lhs)
vim.keymap.set("n", lhs, function()
local line = vim.api.nvim_get_current_line()
local pos = vim.api.nvim_win_get_cursor(0)
local col = pos[2] + 1

for pattern, handler in pairs(handlers) do
local from = 1
local to, url
while from do
from, to, url = line:find(pattern, from)
if from and col >= from and col <= to then
return handler(url)
end
if from then
from = to + 1
end
end
end
end, { buffer = buf, silent = true })
end

map("K")
end

return M

0 comments on commit a11fc5a

Please sign in to comment.