Skip to content

Commit 00155b5

Browse files
committed
feat: support viewing the diff via other pagers, e.g. *Delta*
Add the `log_pager` option, which can be set to `delta`.
1 parent 683ee5d commit 00155b5

File tree

6 files changed

+50
-5
lines changed

6 files changed

+50
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ neogit.setup {
9090
-- Show relative date by default. When set, use `strftime` to display dates
9191
commit_date_format = nil,
9292
log_date_format = nil,
93+
-- When set, used to format the diff.
94+
log_pager = nil,
9395
-- Used to generate URL's for branch popup action "pull request".
9496
git_services = {
9597
["github.com"] = "https://github.com/${owner}/${repository}/compare/${branch_name}?expand=1",

doc/neogit.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ TODO: Detail what these do
9191
graph_style = "ascii",
9292
commit_date_format = nil,
9393
log_date_format = nil,
94+
log_pager = nil,
9495
filewatcher = {
9596
enabled = true,
9697
},

lua/neogit/buffers/common.lua

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local Job = require("plenary.job")
2+
13
local Ui = require("neogit.lib.ui")
24
local Component = require("neogit.lib.ui.component")
35
local util = require("neogit.lib.util")
@@ -25,22 +27,45 @@ M.Diff = Component.new(function(diff)
2527
}, { foldable = true, folded = false, context = true })
2628
end)
2729

28-
-- Use vim iter api?
2930
M.DiffHunks = Component.new(function(diff)
3031
local hunk_props = vim
3132
.iter(diff.hunks)
3233
:map(function(hunk)
33-
hunk.content = vim.iter(diff.lines):slice(hunk.diff_from + 1, hunk.diff_to):totable()
34+
local header = diff.lines[hunk.diff_from]
35+
local content = vim.list_slice(diff.lines, hunk.diff_from + 1, hunk.diff_to)
36+
local job = nil
37+
if config.values.log_pager ~= nil then
38+
job = Job:new {
39+
command = config.values.log_pager[1],
40+
args = vim.list_slice(config.values.log_pager, 2),
41+
}
42+
job:start()
43+
for _, part in ipairs { diff.header, { header }, content } do
44+
for _, line in ipairs(part) do
45+
job:send(line .. "\n")
46+
end
47+
end
48+
job.stdin:close()
49+
end
3450

3551
return {
36-
header = diff.lines[hunk.diff_from],
37-
content = hunk.content,
52+
header = header,
53+
content = content,
54+
job = job,
3855
hunk = hunk,
3956
folded = hunk._folded,
4057
}
4158
end)
4259
:totable()
4360

61+
if config.values.log_pager ~= nil then
62+
vim.iter(hunk_props):each(function(hunk)
63+
hunk.job:wait()
64+
hunk.content = hunk.job:result()
65+
hunk.job = nil
66+
end)
67+
end
68+
4469
return col.tag("DiffContent") {
4570
col.tag("DiffInfo")(map(diff.info, text)),
4671
col.tag("HunkList")(map(hunk_props, M.Hunk)),

lua/neogit/config.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ end
302302
---@field graph_style? NeogitGraphStyle Style for graph
303303
---@field commit_date_format? string Commit date format
304304
---@field log_date_format? string Log date format
305+
---@field log_pager? [string] Log pager
305306
---@field disable_hint? boolean Remove the top hint in the Status buffer
306307
---@field disable_context_highlighting? boolean Disable context highlights based on cursor position
307308
---@field disable_signs? boolean Special signs to draw for sections etc. in Neogit
@@ -355,6 +356,7 @@ function M.get_default_values()
355356
graph_style = "ascii",
356357
commit_date_format = nil,
357358
log_date_format = nil,
359+
log_pager = nil,
358360
process_spinner = true,
359361
filewatcher = {
360362
enabled = true,

lua/neogit/lib/buffer.lua

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,21 @@ function Buffer:set_extmarks(extmarks)
139139
end
140140

141141
function Buffer:set_line_highlights(highlights)
142+
local line_ansi_colorized = {}
143+
142144
for _, hl in ipairs(highlights) do
143-
self:add_line_highlight(unpack(hl))
145+
local line_nr, hl_group = unpack(hl)
146+
if hl_group == "NeogitDiffContext" then
147+
if not line_ansi_colorized[line_nr] then
148+
local text = self:get_line(line_nr + 1)
149+
vim.g.baleia.buf_set_lines(self.handle, line_nr, line_nr + 1, false, text)
150+
line_ansi_colorized[line_nr] = true
151+
end
152+
end
153+
154+
if not line_ansi_colorized[line_nr] then
155+
self:add_line_highlight(unpack(hl))
156+
end
144157
end
145158
end
146159

lua/neogit/lib/git/diff.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ local sha256 = vim.fn.sha256
1212
---@field staged_stats fun(): DiffStagedStats
1313
---
1414
---@class Diff
15+
---@field header string[]
1516
---@field kind string
1617
---@field lines string[]
1718
---@field file string
@@ -225,6 +226,7 @@ local function parse_diff(raw_diff, raw_stats)
225226
local stats = parse_diff_stats(raw_stats or {})
226227

227228
return { ---@type Diff
229+
header = header,
228230
kind = kind,
229231
lines = lines,
230232
file = file,

0 commit comments

Comments
 (0)