aboutsummaryrefslogtreecommitdiff
path: root/lua/config/git.lua
blob: 41b4f5a24b029098fc5c71ecba9fb6a93f2e37de (plain)
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
vim.pack.add({
	"https://github.com/tpope/vim-fugitive",
})

local lazygit_buf = nil
local lazygit_win = nil

local function get_geometry()
	local width = math.floor(vim.o.columns * 0.8)
	local height = math.floor(vim.o.lines * 0.8)
	return {
		relative = "editor",
		row = math.floor((vim.o.lines - height) / 2),
		col = math.floor((vim.o.columns - width) / 2),
		width = width,
		height = height,
		border = "rounded",
		style = "minimal",
	}
end

local function toggle_lazygit()
	if lazygit_win and vim.api.nvim_win_is_valid(lazygit_win) then
		vim.api.nvim_win_close(lazygit_win, true)
		lazygit_win = nil
		return
	end

	if not lazygit_buf or not vim.api.nvim_buf_is_valid(lazygit_buf) then
		lazygit_buf = vim.api.nvim_create_buf(false, true)
	end

	lazygit_win = vim.api.nvim_open_win(lazygit_buf, true, get_geometry())

	local autocmd_id
	autocmd_id = vim.api.nvim_create_autocmd("VimResized", {
		callback = function()
			if lazygit_win and vim.api.nvim_win_is_valid(lazygit_win) then
				vim.api.nvim_win_set_config(lazygit_win, get_geometry())
			else
				vim.api.nvim_del_autocmd(autocmd_id)
			end
		end,
	})

	if vim.bo[lazygit_buf].buftype ~= "terminal" then
		vim.fn.termopen("lazygit", {
			on_exit = function()
				if lazygit_win and vim.api.nvim_win_is_valid(lazygit_win) then
					vim.api.nvim_win_close(lazygit_win, true)
					lazygit_win = nil
				end
				lazygit_buf = nil
			end,
		})
	end

	vim.cmd("startinsert")
end

vim.keymap.set("n", "<leader>gg", toggle_lazygit)