|
| 1 | +local M = {} |
| 2 | +local config = { enabled = true } |
| 3 | +local vim = vim |
| 4 | + |
| 5 | +M.term_buf = nil |
| 6 | +M.term_win = nil |
| 7 | + |
| 8 | +local function create_buffer() |
| 9 | + return vim.api.nvim_create_buf(false, true) |
| 10 | +end |
| 11 | + |
| 12 | +local function open_window(buf, mode) |
| 13 | + local win |
| 14 | + |
| 15 | + if mode == "vertical" then |
| 16 | + vim.cmd("vsplit") |
| 17 | + win = vim.api.nvim_get_current_win() |
| 18 | + vim.api.nvim_win_set_buf(win, buf) |
| 19 | + elseif mode == "floating" then |
| 20 | + local width = math.floor(vim.o.columns * 0.8) |
| 21 | + local height = math.floor(vim.o.lines * 0.8) |
| 22 | + local row = math.floor((vim.o.lines - height) / 2) |
| 23 | + local col = math.floor((vim.o.columns - width) / 2) |
| 24 | + |
| 25 | + win = vim.api.nvim_open_win(buf, true, { |
| 26 | + relative = "editor", |
| 27 | + width = width, |
| 28 | + height = height, |
| 29 | + row = row, |
| 30 | + col = col, |
| 31 | + style = "minimal", |
| 32 | + border = "rounded", |
| 33 | + }) |
| 34 | + else |
| 35 | + vim.cmd("split") |
| 36 | + win = vim.api.nvim_get_current_win() |
| 37 | + vim.api.nvim_win_set_buf(win, buf) |
| 38 | + vim.cmd("resize 10") |
| 39 | + end |
| 40 | + |
| 41 | + return win |
| 42 | +end |
| 43 | + |
| 44 | +local function setup_terminal(buf, win) |
| 45 | + vim.fn.termopen(vim.o.shell) |
| 46 | + vim.bo[buf].buflisted = false |
| 47 | + vim.api.nvim_set_current_win(win) |
| 48 | + vim.cmd("startinsert") |
| 49 | +end |
| 50 | + |
| 51 | +function M.toggle_term(mode) |
| 52 | + mode = mode or "floating" |
| 53 | + |
| 54 | + if M.term_win and vim.api.nvim_win_is_valid(M.term_win) then |
| 55 | + vim.api.nvim_win_close(M.term_win, true) |
| 56 | + M.term_win = nil |
| 57 | + return |
| 58 | + end |
| 59 | + |
| 60 | + if not (M.term_buf and vim.api.nvim_buf_is_valid(M.term_buf)) then |
| 61 | + M.term_buf = create_buffer() |
| 62 | + end |
| 63 | + |
| 64 | + M.term_win = open_window(M.term_buf, mode) |
| 65 | + setup_terminal(M.term_buf, M.term_win) |
| 66 | +end |
| 67 | + |
| 68 | +function M.new_term(mode) |
| 69 | + local buf = create_buffer() |
| 70 | + local win = open_window(buf, mode) |
| 71 | + setup_terminal(buf, win) |
| 72 | +end |
| 73 | + |
| 74 | +local function create_terminal_command(name, func) |
| 75 | + vim.api.nvim_create_user_command(name, function(opts) |
| 76 | + func(opts.args) |
| 77 | + end, { |
| 78 | + nargs = "?", |
| 79 | + complete = function() |
| 80 | + return { "floating", "horizontal", "vertical", "plain" } |
| 81 | + end, |
| 82 | + }) |
| 83 | +end |
| 84 | + |
| 85 | +function M.setup(opts) |
| 86 | + if opts then |
| 87 | + config = vim.tbl_deep_extend("force", config, opts) |
| 88 | + end |
| 89 | + |
| 90 | + if config.enabled then |
| 91 | + create_terminal_command("Terminal", M.toggle_term) |
| 92 | + create_terminal_command("NewTerminal", M.new_term) |
| 93 | + create_terminal_command("Term", M.toggle_term) |
| 94 | + create_terminal_command("NewTerm", M.new_term) |
| 95 | + end |
| 96 | +end |
| 97 | + |
| 98 | +return M |
0 commit comments