feat(toggleterm.lua): add support for toggleterm plugin to open a terminal and lazygit

This commit adds support for the toggleterm plugin in Neovim. It introduces a new Lua file `toggleterm.lua` in the `absolute/after` directory, which configures the toggleterm plugin. It sets up key mappings to open a terminal and toggle the lazygit terminal.

The `toggleterm.lua` file imports the `toggleterm` and `Terminal` modules from the toggleterm plugin. It then sets up the toggleterm plugin using the `toggle_term.setup()` function.

The `lazygit` terminal is defined using the `Terminal:new()` function. It specifies the command to run (`lazygit`), the directory to run the command in (`git_dir`), and the floating window options. It also defines an `on_open` function to start insert mode and set a key mapping to close the terminal, and an `on_close` function to start insert mode.

The `_LAZYGIT_TOGGLE()` function is defined to toggle the `lazygit` terminal.

Key mappings are set using the `vim.api.nvim_set_keymap()` function. The `<leader>gl` mapping is set to call the `_LAZYGIT_TOGGLE()` function, and the `<C-`` mapping is set to open a terminal using the `ToggleTerm` command.

Additionally, a new Lua file `toggleterm.lua` is added in the `absolute/plugins` directory, which exports the toggleterm plugin configuration. It requires the `absolute.after.toggleterm` module to configure the toggleterm plugin.
This commit is contained in:
David Ibia
2024-02-16 14:13:23 +01:00
parent bbc43f98ee
commit 15246a74ab
3 changed files with 55 additions and 13 deletions

View File

@@ -0,0 +1,34 @@
local toggle_term = require("toggleterm")
local Terminal = require("toggleterm.terminal").Terminal
toggle_term.setup()
local opts = { noremap = true, silent = true }
local lazygit = Terminal:new({
cmd = "lazygit",
dir = "git_dir",
direction = "float",
float_opts = {
border = "double",
},
-- function to run on opening the terminal
on_open = function(term)
vim.cmd("startinsert!")
vim.api.nvim_buf_set_keymap(term.bufnr, "n", "q", "<cmd>close<CR>", opts)
end,
-- function to run on closing the terminal
on_close = function(term)
vim.cmd("startinsert!")
end,
})
function _LAZYGIT_TOGGLE()
lazygit:toggle()
end
opts.desc = "Open lazygit"
vim.api.nvim_set_keymap("n", "<leader>gl", "<cmd>lua _LAZYGIT_TOGGLE()<CR>", opts)
opts.desc = "Open a terminal"
vim.api.nvim_set_keymap("n", "<C-`>", "<cmd>ToggleTerm<CR>", opts)

View File

@@ -0,0 +1,7 @@
return {
"akinsho/toggleterm.nvim",
version = "*",
config = function()
require("absolute.after.toggleterm")
end,
}