Skip to content

Commit

Permalink
feat(ui): added multiple options for diff command
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Dec 24, 2022
1 parent 8ad05fe commit 7d02da2
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 5 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,14 @@ return {
task = "",
},
throttle = 20, -- how frequently should the ui process render events
diff = {
-- diff command <d> can be one of:
-- * browser: opens the github compare view. Note that this is always mapped to <K> as well,
-- so you can have a different command for diff <d>
-- * git: will run git diff and open a buffer with filetype git
-- * terminal_git: will open a pseudo terminal with git diff
-- * diffview.nvim: will open Diffview to show the diff
cmd = "git",
},
checker = {
-- automatically check for plugin updates
Expand Down
8 changes: 8 additions & 0 deletions lua/lazy/core/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ M.defaults = {
task = "",
},
throttle = 20, -- how frequently should the ui process render events
diff = {
-- diff command <d> can be one of:
-- * browser: opens the github compare view. Note that this is always mapped to <K> as well,
-- so you can have a different command for diff <d>
-- * git: will run git diff and open a buffer with filetype git
-- * terminal_git: will open a pseudo terminal with git diff
-- * diffview.nvim: will open Diffview to show the diff
cmd = "git",
},
checker = {
-- automatically check for plugin updates
Expand Down
3 changes: 2 additions & 1 deletion lua/lazy/manage/process.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,15 @@ function M.spawn(cmd, opts)
end

---@param cmd string[]
---@param opts? {cwd:string}
---@param opts? {cwd:string, env:table}
function M.exec(cmd, opts)
opts = opts or {}
---@type string[]
local lines
local job = vim.fn.jobstart(cmd, {
cwd = opts.cwd,
pty = false,
env = opts.env,
stdout_buffered = true,
on_stdout = function(_, _lines)
lines = _lines
Expand Down
1 change: 1 addition & 0 deletions lua/lazy/view/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ end

M.keys = {
hover = "K",
diff = "d",
close = "q",
details = "<cr>",
profile_sort = "<C-s>",
Expand Down
58 changes: 58 additions & 0 deletions lua/lazy/view/diff.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
local Util = require("lazy.util")

local M = {}

---@alias LazyDiff {commit:string} | {from:string, to:string}
---@alias LazyDiffFun fun(plugin:LazyPlugin, diff:LazyDiff)

M.handlers = {

---@type LazyDiffFun
browser = function(plugin, diff)
if plugin.url then
local url = plugin.url:gsub("%.git$", "")
if diff.commit then
Util.open(url .. "/commit/" .. diff.commit)
else
Util.open(url .. "/compare/" .. diff.from .. ".." .. diff.to)
end
else
Util.error("No url for " .. plugin.name)
end
end,

---@type LazyDiffFun
["diffview.nvim"] = function(plugin, diff)
if diff.commit then
vim.cmd.DiffviewOpen(("-C=%s"):format(plugin.dir) .. " " .. diff.commit)
else
vim.cmd.DiffviewOpen(("-C=%s"):format(plugin.dir) .. " " .. diff.from .. ".." .. diff.to)
end
end,

---@type LazyDiffFun
git = function(plugin, diff)
local cmd = { "git", "diff" }
if diff.commit then
cmd[#cmd + 1] = diff.commit
else
cmd[#cmd + 1] = diff.from
cmd[#cmd + 1] = diff.to
end
Util.open_cmd(cmd, { cwd = plugin.dir, filetype = "git" })
end,

---@type LazyDiffFun
terminal_git = function(plugin, diff)
local cmd = { "git", "diff" }
if diff.commit then
cmd[#cmd + 1] = diff.commit
else
cmd[#cmd + 1] = diff.from
cmd[#cmd + 1] = diff.to
end
Util.open_cmd(cmd, { cwd = plugin.dir, terminal = true, env = { PAGER = "cat" } })
end,
}

return M
10 changes: 6 additions & 4 deletions lua/lazy/view/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function M.new(view)
local self = setmetatable({}, { __index = setmetatable(M, { __index = Text }) })
self.view = view
self.padding = 2
self.wrap = view.win_opts.width
self.wrap = view.opts.win_opts.width
return self
end

Expand Down Expand Up @@ -56,8 +56,9 @@ function M:update()
end
end

local mode = self:title()
self:title()

local mode = self.view.state.mode
if mode == "help" then
self:help()
elseif mode == "profile" then
Expand Down Expand Up @@ -139,7 +140,6 @@ function M:title()
end
self:nl():nl()
end
return self.view.state.mode
end

function M:help()
Expand Down Expand Up @@ -395,12 +395,14 @@ function M:log(task)
if msg:find("^%S+!:") then
self:diagnostic({ message = "Breaking Changes", severity = vim.diagnostic.severity.WARN })
end
self:append(ref .. " ", "LazyCommit", { indent = 6 })
self:append(ref:sub(1, 7) .. " ", "LazyCommit", { indent = 6 })
self:append(vim.trim(msg)):highlight({
["#%d+"] = "Number",
["^%S+:"] = "Title",
["^%S+(%(.*%)):"] = "Italic",
["`.-`"] = "@text.literal.markdown_inline",
["%*.-%*"] = "Italic",
["%*%*.-%*%*"] = "Bold",
})
-- string.gsub
self:append(" " .. time, "Comment")
Expand Down

0 comments on commit 7d02da2

Please sign in to comment.