Skip to content

Commit

Permalink
feat: git log
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Nov 22, 2022
1 parent 54d5ff1 commit 3218c2d
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tt.lua
11 changes: 11 additions & 0 deletions lua/lazy/manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ function M.run(operation, opts, filter)
runner:add(Task.new(plugin, "run"))
end
plugin.dirty = false
if operation == "update" then
runner:add(Task.new(plugin, "log"))
end
end
-- wait for post-install to finish
runner:wait(on_done)
Expand Down Expand Up @@ -85,6 +88,14 @@ function M.update(opts)
end)
end

---@param opts? ManagerOpts
function M.log(opts)
---@param plugin LazyPlugin
M.run("log", opts, function(plugin)
return plugin.uri and plugin.installed
end)
end

---@param opts? ManagerOpts
function M.clean(opts)
opts = opts or {}
Expand Down
28 changes: 24 additions & 4 deletions lua/lazy/task.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ local Util = require("lazy.util")
---@field running boolean
local Task = {}

---@alias TaskType "update"|"install"|"run"|"clean"
---@alias TaskType "update"|"install"|"run"|"clean"|"log"

---@param plugin LazyPlugin
---@param type TaskType
Expand All @@ -27,7 +27,6 @@ end

function Task:_done()
self.running = false

vim.cmd("do User LazyRender")
end

Expand All @@ -54,7 +53,7 @@ function Task:clean()
end

self.plugin.installed = false
self.running = false
self:_done()
end

function Task:install()
Expand Down Expand Up @@ -161,14 +160,36 @@ function Task:start()
self:run()
elseif self.type == "clean" then
self:clean()
elseif self.type == "log" then
self:log()
end
end)

if not ok then
self.error = err or "failed"
self:_done()
end
end

function Task:log()
if not Util.file_exists(self.plugin.dir .. "/.git") then
self:_done()
return
end

local args = {
"log",
"--pretty=format:%h %s (%cr)",
"--abbrev-commit",
"--decorate",
"--date=short",
"--color=never",
"--since='7 days ago'",
}
self:spawn("git", {
args = args,
cwd = self.plugin.dir,
})
end

function Task:update()
Expand All @@ -180,7 +201,6 @@ function Task:update()
})
vim.opt.runtimepath:append(self.plugin.uri)
end

self:_done()
else
local args = {
Expand Down
3 changes: 3 additions & 0 deletions lua/lazy/view/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ M.commands = {
install = function()
Manager.install({ clear = true, show = true })
end,
log = function()
Manager.log({ clear = true, show = true })
end,
show = function()
View.show()
end,
Expand Down
19 changes: 19 additions & 0 deletions lua/lazy/view/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,25 @@ function M:plugin(plugin)
self:nl()
end
end
elseif task.type == "log" then
local log = vim.trim(task.output)
if log ~= "" then
local lines = vim.split(log, "\n")
for l, line in ipairs(lines) do
if l == 1 then
self:nl()
end
local ref, msg, time = line:match("^(%w+) (.*) (%(.*%))$")
self:append(" " .. ref .. " ", "@variable.builtin")
local col = self:col()
self:append(msg)
-- string.gsub
self:append(" " .. time, "Comment")
if l ~= #lines then
self:nl()
end
end
end
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions lua/lazy/view/sections.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ return {
end,
title = "Running",
},
{
filter = function(plugin)
return has_task(plugin, function(task)
return task.type == "log" and vim.trim(task.output) ~= ""
end)
end,
title = "Log",
},
{
filter = function(plugin)
return plugin.installed and not plugin.uri
Expand Down
17 changes: 16 additions & 1 deletion lua/lazy/view/text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function Text.new()
end

---@param str string
---@param hl string|table
---@param hl? string|table
function Text:append(str, hl)
if #self._lines == 0 then
self:nl()
Expand Down Expand Up @@ -84,4 +84,19 @@ function Text:trim()
end
end

function Text:row()
return #self._lines == 0 and 1 or #self._lines
end

function Text:col()
if #self._lines == 0 then
return 0
end
local width = 0
for _, segment in ipairs(self._lines[#self._lines]) do
width = width + vim.fn.strlen(segment.str)
end
return width
end

return Text

0 comments on commit 3218c2d

Please sign in to comment.