Skip to content

Commit 7d02da2

Browse files
committed
feat(ui): added multiple options for diff command
1 parent 8ad05fe commit 7d02da2

File tree

6 files changed

+83
-5
lines changed

6 files changed

+83
-5
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,14 @@ return {
327327
task = "",
328328
},
329329
throttle = 20, -- how frequently should the ui process render events
330+
diff = {
331+
-- diff command <d> can be one of:
332+
-- * browser: opens the github compare view. Note that this is always mapped to <K> as well,
333+
-- so you can have a different command for diff <d>
334+
-- * git: will run git diff and open a buffer with filetype git
335+
-- * terminal_git: will open a pseudo terminal with git diff
336+
-- * diffview.nvim: will open Diffview to show the diff
337+
cmd = "git",
330338
},
331339
checker = {
332340
-- automatically check for plugin updates

lua/lazy/core/config.lua

+8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ M.defaults = {
5050
task = "",
5151
},
5252
throttle = 20, -- how frequently should the ui process render events
53+
diff = {
54+
-- diff command <d> can be one of:
55+
-- * browser: opens the github compare view. Note that this is always mapped to <K> as well,
56+
-- so you can have a different command for diff <d>
57+
-- * git: will run git diff and open a buffer with filetype git
58+
-- * terminal_git: will open a pseudo terminal with git diff
59+
-- * diffview.nvim: will open Diffview to show the diff
60+
cmd = "git",
5361
},
5462
checker = {
5563
-- automatically check for plugin updates

lua/lazy/manage/process.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,15 @@ function M.spawn(cmd, opts)
109109
end
110110

111111
---@param cmd string[]
112-
---@param opts? {cwd:string}
112+
---@param opts? {cwd:string, env:table}
113113
function M.exec(cmd, opts)
114114
opts = opts or {}
115115
---@type string[]
116116
local lines
117117
local job = vim.fn.jobstart(cmd, {
118118
cwd = opts.cwd,
119119
pty = false,
120+
env = opts.env,
120121
stdout_buffered = true,
121122
on_stdout = function(_, _lines)
122123
lines = _lines

lua/lazy/view/config.lua

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ end
2626

2727
M.keys = {
2828
hover = "K",
29+
diff = "d",
2930
close = "q",
3031
details = "<cr>",
3132
profile_sort = "<C-s>",

lua/lazy/view/diff.lua

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
local Util = require("lazy.util")
2+
3+
local M = {}
4+
5+
---@alias LazyDiff {commit:string} | {from:string, to:string}
6+
---@alias LazyDiffFun fun(plugin:LazyPlugin, diff:LazyDiff)
7+
8+
M.handlers = {
9+
10+
---@type LazyDiffFun
11+
browser = function(plugin, diff)
12+
if plugin.url then
13+
local url = plugin.url:gsub("%.git$", "")
14+
if diff.commit then
15+
Util.open(url .. "/commit/" .. diff.commit)
16+
else
17+
Util.open(url .. "/compare/" .. diff.from .. ".." .. diff.to)
18+
end
19+
else
20+
Util.error("No url for " .. plugin.name)
21+
end
22+
end,
23+
24+
---@type LazyDiffFun
25+
["diffview.nvim"] = function(plugin, diff)
26+
if diff.commit then
27+
vim.cmd.DiffviewOpen(("-C=%s"):format(plugin.dir) .. " " .. diff.commit)
28+
else
29+
vim.cmd.DiffviewOpen(("-C=%s"):format(plugin.dir) .. " " .. diff.from .. ".." .. diff.to)
30+
end
31+
end,
32+
33+
---@type LazyDiffFun
34+
git = function(plugin, diff)
35+
local cmd = { "git", "diff" }
36+
if diff.commit then
37+
cmd[#cmd + 1] = diff.commit
38+
else
39+
cmd[#cmd + 1] = diff.from
40+
cmd[#cmd + 1] = diff.to
41+
end
42+
Util.open_cmd(cmd, { cwd = plugin.dir, filetype = "git" })
43+
end,
44+
45+
---@type LazyDiffFun
46+
terminal_git = function(plugin, diff)
47+
local cmd = { "git", "diff" }
48+
if diff.commit then
49+
cmd[#cmd + 1] = diff.commit
50+
else
51+
cmd[#cmd + 1] = diff.from
52+
cmd[#cmd + 1] = diff.to
53+
end
54+
Util.open_cmd(cmd, { cwd = plugin.dir, terminal = true, env = { PAGER = "cat" } })
55+
end,
56+
}
57+
58+
return M

lua/lazy/view/render.lua

+6-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function M.new(view)
2525
local self = setmetatable({}, { __index = setmetatable(M, { __index = Text }) })
2626
self.view = view
2727
self.padding = 2
28-
self.wrap = view.win_opts.width
28+
self.wrap = view.opts.win_opts.width
2929
return self
3030
end
3131

@@ -56,8 +56,9 @@ function M:update()
5656
end
5757
end
5858

59-
local mode = self:title()
59+
self:title()
6060

61+
local mode = self.view.state.mode
6162
if mode == "help" then
6263
self:help()
6364
elseif mode == "profile" then
@@ -139,7 +140,6 @@ function M:title()
139140
end
140141
self:nl():nl()
141142
end
142-
return self.view.state.mode
143143
end
144144

145145
function M:help()
@@ -395,12 +395,14 @@ function M:log(task)
395395
if msg:find("^%S+!:") then
396396
self:diagnostic({ message = "Breaking Changes", severity = vim.diagnostic.severity.WARN })
397397
end
398-
self:append(ref .. " ", "LazyCommit", { indent = 6 })
398+
self:append(ref:sub(1, 7) .. " ", "LazyCommit", { indent = 6 })
399399
self:append(vim.trim(msg)):highlight({
400400
["#%d+"] = "Number",
401401
["^%S+:"] = "Title",
402402
["^%S+(%(.*%)):"] = "Italic",
403403
["`.-`"] = "@text.literal.markdown_inline",
404+
["%*.-%*"] = "Italic",
405+
["%*%*.-%*%*"] = "Bold",
404406
})
405407
-- string.gsub
406408
self:append(" " .. time, "Comment")

0 commit comments

Comments
 (0)