From a6123a6dd5ee3f7e63077cf72a40a1085cebada1 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 30 May 2024 19:15:41 +0200 Subject: make terminal resizing more robust --- lua/functions.lua | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'lua/functions.lua') diff --git a/lua/functions.lua b/lua/functions.lua index 12eca3a..1f088a8 100644 --- a/lua/functions.lua +++ b/lua/functions.lua @@ -12,6 +12,14 @@ function table.filter(list, predicate) return filtered_list end +local function set_terminal_height(height) + if not vim.api.nvim_win_is_valid(vim.g.term_win) then + return + end + + vim.api.nvim_win_set_height(vim.g.term_win, height) +end + function _G.check_back_space() local col = vim.api.nvim_win_get_cursor(0)[2] @@ -32,14 +40,13 @@ function _G.show_documentation() end end -function _G.toggle_terminal(height) +function _G.toggle_terminal() if vim.fn.win_gotoid(vim.g.term_win) == 1 then - vim.g.is_term_open = 0 vim.cmd("quit!") else vim.cmd("botright new") - vim.cmd("resize " .. height) + vim.api.nvim_win_set_height(vim.fn.win_getid(), vim.g.terminal_height) if not pcall(function() vim.cmd("buffer " .. vim.g.term_buf) end) then vim.fn.termopen(vim.env.SHELL, {detach=0}) @@ -54,7 +61,6 @@ function _G.toggle_terminal(height) vim.cmd("startinsert!") vim.g.term_win = vim.fn.win_getid() - vim.g.is_term_open = 1 if vim.fn.exists("g:NERDTree") and vim.api.nvim_eval("g:NERDTree.IsOpen()") then vim.cmd("NERDTreeFocus") @@ -114,11 +120,6 @@ function _G.close_current_buffer() vim.api.nvim_buf_delete(current_buf_nr, { force = true }) - -- Resize terminal - if vim.g.is_term_open == 1 then - vim.cmd("wincmd j") - vim.cmd("resize " .. vim.g.terminal_height) - vim.cmd("wincmd k") - end + set_terminal_height(vim.g.terminal_height) end -- cgit v1.2.3-18-g5258