[nvim] Add floating terminal
This commit is contained in:
1 parent
021f1dc500
commit
6293173d01
2 files changed
+56
-1
No files matched your search
@@ -1,7 +1,7 @@
|
||||
require("config.autocmds")
|
||||
require("config.filetypes")
|
||||
require("config.keymaps")
|
||||
require("config.options")
|
||||
require("config.autocmds")
|
||||
|
||||
require("config.lazy")
|
||||
require("config.theme") -- ignored by git
|
||||
|
||||
@@ -38,3 +38,58 @@ vim.api.nvim_create_autocmd("LspAttach", {
|
||||
map("<leader>K", vim.lsp.buf.hover, "Hover documentation")
|
||||
end,
|
||||
})
|
||||
|
||||
-- Floating terminal
|
||||
vim.keymap.set("t", "<esc><esc>", "<c-\\><c-n>")
|
||||
|
||||
local state = {
|
||||
floating = {
|
||||
buf = -1,
|
||||
win = -1,
|
||||
}
|
||||
}
|
||||
|
||||
local function create_floating_window(opts)
|
||||
opts = opts or {}
|
||||
|
||||
local width = opts.width or math.floor(vim.o.columns * 0.8)
|
||||
local height = opts.height or math.floor(vim.o.lines * 0.8)
|
||||
|
||||
local col = math.floor((vim.o.columns - width) / 2)
|
||||
local row = math.floor((vim.o.lines - height) / 2)
|
||||
|
||||
local buf = nil
|
||||
if vim.api.nvim_buf_is_valid(opts.buf) then
|
||||
buf = opts.buf
|
||||
else
|
||||
buf = vim.api.nvim_create_buf(false, true)
|
||||
end
|
||||
|
||||
local win_config = {
|
||||
relative = "editor",
|
||||
width = width,
|
||||
height = height,
|
||||
col = col,
|
||||
row = row,
|
||||
style = "minimal",
|
||||
border = "rounded",
|
||||
}
|
||||
|
||||
local win = vim.api.nvim_open_win(buf, true, win_config)
|
||||
|
||||
return { buf = buf, win = win }
|
||||
end
|
||||
|
||||
local toggle_terminal = function()
|
||||
if not vim.api.nvim_win_is_valid(state.floating.win) then
|
||||
state.floating = create_floating_window { buf = state.floating.buf }
|
||||
if vim.bo[state.floating.buf].buftype ~= "terminal" then
|
||||
vim.cmd.terminal()
|
||||
end
|
||||
else
|
||||
vim.api.nvim_win_hide(state.floating.win)
|
||||
end
|
||||
end
|
||||
|
||||
vim.api.nvim_create_user_command("FloatingTerminal", toggle_terminal, {})
|
||||
vim.keymap.set({ "n", "t" }, "<leader>tt", toggle_terminal, { desc = "Toggle floating terminal" })
|
||||
Reference in new issue
Block a user