-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathcommit_log_panel.lua
136 lines (108 loc) · 3.21 KB
/
commit_log_panel.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
local Job = require("diffview.job").Job
local Panel = require("diffview.ui.panel").Panel
local async = require("diffview.async")
local get_user_config = require("diffview.config").get_config
local oop = require("diffview.oop")
local utils = require("diffview.utils")
local await = async.await
local M = {}
---@class CommitLogPanel : Panel
---@field adapter VCSAdapter
---@field args string[]
---@field job_out string[]
local CommitLogPanel = oop.create_class("CommitLogPanel", Panel)
CommitLogPanel.winopts = vim.tbl_extend("force", Panel.winopts, {
wrap = true,
breakindent = true,
})
CommitLogPanel.bufopts = vim.tbl_extend("force", Panel.bufopts, {
buftype = "nowrite",
filetype = "git",
})
CommitLogPanel.default_type = "float"
CommitLogPanel.default_config_split = vim.tbl_extend("force", Panel.default_config_split, {
position = "bottom",
height = 14,
})
CommitLogPanel.default_config_float = function()
local c = vim.deepcopy(Panel.default_config_float)
local viewport_width = vim.o.columns
local viewport_height = vim.o.lines
c.width = math.min(100, viewport_width)
c.height = math.min(24, viewport_height)
c.col = math.floor(viewport_width * 0.5 - c.width * 0.5)
c.row = math.floor(viewport_height * 0.5 - c.height * 0.5)
return c
end
---@class CommitLogPanelSpec
---@field config PanelConfig
---@field args string[]
---@field name string
---@param adapter VCSAdapter
---@param opt CommitLogPanelSpec
function CommitLogPanel:init(parent, adapter, opt)
self:super({
bufname = opt.name,
config = opt.config or get_user_config().commit_log_panel.win_config,
})
self.adapter = adapter
self.args = opt.args or { "-n256" }
self:on_autocmd("BufWinEnter" , {
callback = function()
vim.bo[self.bufid].bufhidden = "wipe"
end,
})
local conf = get_user_config().keymaps
local default_opt = { silent = true, nowait = true, buffer = self.bufid }
for _, mapping in ipairs(conf.commit_log_panel) do
local map_opt = vim.tbl_extend("force", default_opt, mapping[4] or {}, { buffer = self.bufid })
vim.keymap.set(mapping[1], mapping[2], mapping[3], map_opt)
end
parent.emitter:on("close", function(e)
if self:is_focused() then
self:close()
e:stop_propagation()
end
end)
end
---@param self CommitLogPanel
---@param args string|string[]
CommitLogPanel.update = async.void(function(self, args)
if type(args) ~= "table" then
args = { args }
end
local job = Job({
command = self.adapter:bin(),
args = self.adapter:get_log_args(args or self.args),
cwd = self.adapter.ctx.toplevel,
})
local ok = await(job)
await(async.scheduler())
if not ok then
utils.err("Failed to open log!")
return
end
self.job_out = utils.vec_slice(job.stdout)
if not next(self.job_out) then
utils.info("No log content available for these changes.")
return
end
if not self:is_open() then
self:init_buffer()
else
self:render()
self:redraw()
end
self:focus()
vim.cmd("norm! gg")
end)
function CommitLogPanel:update_components()
end
function CommitLogPanel:render()
self.render_data:clear()
if self.job_out then
self.render_data.lines = utils.vec_slice(self.job_out)
end
end
M.CommitLogPanel = CommitLogPanel
return M