From 3bdf68fb70e92acbb9fcd53d92b5ec33b91565ff Mon Sep 17 00:00:00 2001 From: calcu1on Date: Tue, 18 Feb 2025 10:19:11 -0500 Subject: [PATCH] Adding floating terminal. --- plugin/danterm.lua | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 plugin/danterm.lua diff --git a/plugin/danterm.lua b/plugin/danterm.lua new file mode 100644 index 0000000..25103e2 --- /dev/null +++ b/plugin/danterm.lua @@ -0,0 +1,54 @@ +local t_opts = {silent = true} +vim.keymap.set('t', '', '', t_opts) +vim.keymap.set('t', '', 'Danterm') +vim.keymap.set('n', 'dt', 'Danterm') + +local state = { + floating = { + buf = -1, + win = -1, + } +} + +local function dan_term(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 screen_width = vim.o.columns + local screen_height = vim.o.lines + local col = math.floor((screen_width - width) / 2) + local row = math.floor((screen_height - 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', -- optional, choose 'none', 'single', 'double', etc. + } + + local win = vim.api.nvim_open_win(buf, true, win_config) + + return { buf = buf, win = win } +end + +vim.api.nvim_create_user_command("Danterm", function() + if not vim.api.nvim_win_is_valid(state.floating.win) then + state.floating = dan_term { buf = state.floating.buf } + if vim.bo[state.floating.buf].buftype ~= "terminal" then + vim.cmd.term() + vim.api.nvim_input("i") + end + else + vim.api.nvim_win_hide(state.floating.win) + end +end, {})