Skip to content

Commit

Permalink
neovim: Rewrite lazygit integration using toggleterm
Browse files Browse the repository at this point in the history
  • Loading branch information
juanibiapina committed Dec 22, 2024
1 parent bfa65d2 commit f1c7d21
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 36 deletions.
56 changes: 21 additions & 35 deletions dotfiles/nvim/.config/nvim/conf/auto/lazygit.lua
Original file line number Diff line number Diff line change
@@ -1,44 +1,30 @@
-- Do not show 'Process exited' message when closing lazygit buffer
local nvim_terminal_augroup = vim.api.nvim_create_augroup('nvim_terminal', { clear = false })
require("toggleterm").setup{}

vim.api.nvim_create_autocmd({ 'TermClose' }, {
group = nvim_terminal_augroup,
desc = 'Automatically close buffers after running lazygit',
pattern = 'term://*:lazygit',
callback = function(args)
if vim.v.event.status == 0 then
vim.cmd({ cmd = 'bdelete', args = { args.buf }, bang = true })
end
end,
})

-- Open Lazygit in a floating window
function Lazygit()
local width = vim.api.nvim_get_option("columns")
local height = vim.api.nvim_get_option("lines")
local Terminal = require('toggleterm.terminal').Terminal

local buffer = vim.api.nvim_create_buf(false, true)
local win = vim.api.nvim_open_win(buffer, false, {
relative = 'editor',
local lazygit = Terminal:new({
cmd = "lazygit",
display_name = "lazygit",
close_on_exit = true,
direction = "float",
float_opts = {
border = "none",
row = 0,
col = 0,
width = width,
height = height - 1,
style = 'minimal',
zindex = 1000
})

-- override float background color to be the same as the normal background
vim.api.nvim_set_option_value('winhl', 'NormalFloat:Normal', {win = win})
width = function()
return vim.api.nvim_get_option("columns")
end,
height = function()
return vim.api.nvim_get_option("lines") - 1
end,
zindex = 1001,
}
})

vim.api.nvim_set_current_win(win)
vim.fn.termopen("lazygit")
vim.cmd("startinsert")
function LazygitOpen()
lazygit:open()
end

function LazygitClose()
local win = vim.api.nvim_get_current_win()
local buf = vim.api.nvim_win_get_buf(win)
vim.api.nvim_win_close(win, true)
vim.api.nvim_buf_delete(buf, { force = true })
lazygit:shutdown()
end
1 change: 1 addition & 0 deletions dotfiles/nvim/.config/nvim/lua/plugins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Plug("juanibiapina/vim-runner")

-- Terminal
Plug("kassio/neoterm")
Plug("akinsho/toggleterm.nvim")

-- Not organized yet
Plug("AndrewRadev/sideways.vim")
Expand Down
2 changes: 1 addition & 1 deletion dotfiles/nvim/.config/nvim/lua/shortcuts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ map('<leader>gp', ':<C-U>Gitsigns prev_hunk', 'Jump to previous hunk')
map('<Leader>gq', ':GLoadChanged', 'Git: load modified files into quickfix')
map('<Leader>gr', ':silent ! hub browse', 'Browse current branch in git repository')
map('<leader>gu', ':<C-U>Gitsigns reset_hunk', 'Reset hunk')
map('<leader>gg', ':lua Lazygit()', 'Open Lazygit')
map('<leader>gg', ':lua LazygitOpen()', 'Open Lazygit')

-- lsp
map('<Leader>ld', ':lua vim.lsp.buf.definition()', 'LSP: Goto definition')
Expand Down

0 comments on commit f1c7d21

Please sign in to comment.