From 6c1cec82e477b70f8dd860840ad105566620e9a1 Mon Sep 17 00:00:00 2001 From: zbirenbaum Date: Fri, 29 Apr 2022 18:03:13 -0400 Subject: fix NvChad/Nvchad#978 --- lua/nvterm/terminal.lua | 45 ++++++++++++++++++--------------------------- lua/nvterm/termutil.lua | 15 ++++----------- 2 files changed, 22 insertions(+), 38 deletions(-) diff --git a/lua/nvterm/terminal.lua b/lua/nvterm/terminal.lua index ff73119..3c77e43 100644 --- a/lua/nvterm/terminal.lua +++ b/lua/nvterm/terminal.lua @@ -10,10 +10,11 @@ local function get_last(list) return terminals[#terminals] or nil end -local function get_type(type) +local function get_type(type, list) + list = list or terminals.list return vim.tbl_filter(function(t) return t.type == type - end, terminals.list) + end, list) end local function get_still_open() @@ -31,31 +32,13 @@ local function get_type_last(type) end local create_term_window = function(type) - util.execute_type_cmd(type, terminals) - + local existing = #get_type(type, get_still_open()) > 0 + util.execute_type_cmd(type, terminals, existing) vim.wo.relativenumber = false vim.wo.number = false - return a.nvim_get_current_win() end -local create_term = function(type) - local win = create_term_window(type) - local buf = a.nvim_create_buf(false, true) - - a.nvim_buf_set_option(buf, "filetype", "terminal") - a.nvim_buf_set_option(buf, "buflisted", false) - a.nvim_win_set_buf(win, buf) - - local job_id = vim.fn.termopen(vim.o.shell) - local term = { win = win, buf = buf, open = true, type = type, job_id = job_id } - - table.insert(terminals.list, term) - vim.cmd("startinsert") - - return term -end - local ensure_and_send = function(cmd, type) terminals = util.verify_terminals(terminals) local term = type and get_type_last(type) or get_last_still_open() or nvterm.new("vertical") @@ -81,20 +64,18 @@ nvterm.send = function(cmd, type) if not cmd then return end - call_and_restore(ensure_and_send, { cmd, type }) end nvterm.hide_term = function(term) - term.open = false + terminals.list[term.id].open = false a.nvim_win_close(term.win, false) end nvterm.show_term = function(term) - term.open = true term.win = create_term_window(term.type) a.nvim_win_set_buf(term.win, term.buf) - + terminals.list[term.id].open = true vim.cmd("startinsert") end @@ -109,7 +90,17 @@ nvterm.show = function(type) end nvterm.new = function(type) - local term = create_term(type) + local win = create_term_window(type) + local buf = a.nvim_create_buf(false, true) + a.nvim_buf_set_option(buf, "filetype", "terminal") + a.nvim_buf_set_option(buf, "buflisted", false) + a.nvim_win_set_buf(win, buf) + + local job_id = vim.fn.termopen(vim.o.shell) + local id = #terminals + 1 + local term = { id = id, win = win, buf = buf, open = true, type = type, job_id = job_id } + terminals.list[id] = term + vim.cmd("startinsert") return term end diff --git a/lua/nvterm/termutil.lua b/lua/nvterm/termutil.lua index b104075..94bad8e 100644 --- a/lua/nvterm/termutil.lua +++ b/lua/nvterm/termutil.lua @@ -4,13 +4,10 @@ local a = vim.api util.calc_float_opts = function(opts) return { relative = "editor", - width = math.ceil(opts.width * vim.o.columns), height = math.ceil(opts.height * vim.o.lines), - row = math.floor(opts.row * vim.o.lines), col = math.floor(opts.col * vim.o.columns), - border = opts.border, } end @@ -18,26 +15,22 @@ end util.get_split_dims = function(type, ratio) local type_switch = type == "horizontal" local type_func = type_switch and a.nvim_win_get_height or a.nvim_win_get_width - return math.floor(type_func(0) * ratio) end -util.execute_type_cmd = function(type, terminals) +util.execute_type_cmd = function(type, terminals, override) local opts = terminals.type_opts[type] - + local dims = type ~= 'float' and util.get_split_dims(type, opts.split_ratio) or util.calc_float_opts(opts) + dims = override and "" or dims local type_cmds = { horizontal = function() - local dims = util.get_split_dims(type, opts.split_ratio) vim.cmd(opts.location .. dims .. " split") end, - vertical = function() - local dims = util.get_split_dims(type, opts.split_ratio) vim.cmd(opts.location .. dims .. " vsplit") end, - float = function() - a.nvim_open_win(0, true, util.calc_float_opts(opts)) + a.nvim_open_win(0, true, dims) end, } -- cgit v1.2.3-70-g09d2