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/config.lua | 1 - lua/functions.lua | 21 +++++++++++---------- lua/maps/terminal.lua | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lua/config.lua b/lua/config.lua index a6de657..919d711 100644 --- a/lua/config.lua +++ b/lua/config.lua @@ -62,7 +62,6 @@ vim.g.nerdtree_tabs_open_on_console_startup = 1 vim.g.NERDTreeWinSize = 40 -- Custom globals -vim.g.is_term_open = 0 vim.g.terminal_height = 12 vim.g.term_buf = 0 vim.g.term_win = 0 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 diff --git a/lua/maps/terminal.lua b/lua/maps/terminal.lua index c3dc804..a1cde60 100644 --- a/lua/maps/terminal.lua +++ b/lua/maps/terminal.lua @@ -1,14 +1,14 @@ vim.api.nvim_set_keymap( "n", "t", - ":lua toggle_terminal(vim.g.terminal_height)", + ":lua toggle_terminal()", {silent = true, noremap = true} ) vim.api.nvim_set_keymap( "t", "t", - ":lua toggle_terminal(vim.g.terminal_height)", + ":lua toggle_terminal()", {silent = true, noremap = true} ) -- cgit v1.2.3-18-g5258