diff options
Diffstat (limited to 'lua/config/terminal.lua')
| -rw-r--r-- | lua/config/terminal.lua | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/lua/config/terminal.lua b/lua/config/terminal.lua new file mode 100644 index 0000000..2df596e --- /dev/null +++ b/lua/config/terminal.lua @@ -0,0 +1,72 @@ +-- Source: https://github.com/tjdevries/advent-of-nvim/blob/master/nvim/plugin/floaterminal.lua +vim.keymap.set("t", "<ESC><ESC>", "<C-\\><C-n>") + +vim.api.nvim_create_autocmd("TermOpen", { + group = vim.api.nvim_create_augroup("config-term-open", { clear = true }), + callback = function() + vim.opt.number = false + vim.opt.relativenumber = false + vim.cmd("startinsert") + end, +}) + +local function create_terminal() + vim.cmd.vnew() + vim.cmd.term() + vim.cmd.wincmd("J") + vim.api.nvim_win_set_height(0, 15) +end + +vim.keymap.set("n", "<Leader>T", create_terminal, { desc = "Open a terminal" }) + +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 = { + border = "double", + style = "minimal", + relative = "editor", + width = width, + height = height, + col = col, + row = row, + } + + local win = vim.api.nvim_open_win(buf, true, win_config) + + return { buf = buf, win = win } +end + +vim.api.nvim_create_user_command("Floaterminal", 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.term() + else + vim.cmd("startinsert") + end + else + vim.api.nvim_win_hide(state.floating.win) + end +end, {}) + +vim.keymap.set("n", "<Leader>t", "<CMD>Floaterminal<CR>", { desc = "Opens a floating terminal" }) |
