59 lines
1.5 KiB
Lua
59 lines
1.5 KiB
Lua
local util = require("git.util")
|
|
|
|
local M = {}
|
|
|
|
local BUF_NAME = "githistory://log"
|
|
|
|
---@type integer?
|
|
local cached_buf
|
|
|
|
---@return integer
|
|
local function get_buf()
|
|
if cached_buf and vim.api.nvim_buf_is_valid(cached_buf) then
|
|
return cached_buf
|
|
end
|
|
local buf = vim.api.nvim_create_buf(false, true)
|
|
pcall(vim.api.nvim_buf_set_name, buf, BUF_NAME)
|
|
util.setup_scratch(buf, { bufhidden = "hide" })
|
|
cached_buf = buf
|
|
return buf
|
|
end
|
|
|
|
---@param args string[]
|
|
---@param lines string[]
|
|
function M.append(args, lines)
|
|
if #lines == 0 then
|
|
return
|
|
end
|
|
local buf = get_buf()
|
|
local count = vim.api.nvim_buf_line_count(buf)
|
|
local first = vim.api.nvim_buf_get_lines(buf, 0, 1, false)[1] or ""
|
|
local empty = count == 1 and first == ""
|
|
local payload = { "$ git " .. table.concat(args, " ") }
|
|
vim.list_extend(payload, lines)
|
|
vim.bo[buf].modifiable = true
|
|
if empty then
|
|
vim.api.nvim_buf_set_lines(buf, 0, -1, false, payload)
|
|
else
|
|
table.insert(payload, 1, "")
|
|
vim.api.nvim_buf_set_lines(buf, count, count, false, payload)
|
|
end
|
|
vim.bo[buf].modifiable = false
|
|
vim.bo[buf].modified = false
|
|
end
|
|
|
|
function M.open()
|
|
local buf = get_buf()
|
|
local win = vim.fn.bufwinid(buf)
|
|
if win ~= -1 then
|
|
vim.api.nvim_set_current_win(win)
|
|
else
|
|
util.place_buf(buf, nil)
|
|
win = vim.api.nvim_get_current_win()
|
|
end
|
|
local last = vim.api.nvim_buf_line_count(buf)
|
|
vim.api.nvim_win_set_cursor(win, { last, 0 })
|
|
end
|
|
|
|
return M
|