From 20e7a895fad4dfb3b7b25fe014112fa64a4e4852 Mon Sep 17 00:00:00 2001 From: Kim Gert Nielsen Date: Sun, 9 Feb 2025 12:01:01 +0100 Subject: [PATCH] refactor(term): make buildin term use a % of screen --- README.md | 2 +- lua/greyjoy/config.lua | 2 +- lua/greyjoy/terminals.lua | 45 ++++++++++++++++++++++++++++----------- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index f894fb2..9672e4e 100644 --- a/README.md +++ b/README.md @@ -106,7 +106,7 @@ So in the above example its possible to run the generic and makefile plugin by r size = nil, }, term = { - height = 5, + width_pct = 0.2, -- 20% of screen }, telescope = { keys = { diff --git a/lua/greyjoy/config.lua b/lua/greyjoy/config.lua index 2a87d4c..fa57b95 100644 --- a/lua/greyjoy/config.lua +++ b/lua/greyjoy/config.lua @@ -16,7 +16,7 @@ local defaults = { size = nil, }, term = { - height = 5, + width_pct = 0.2, }, telescope = { keys = { diff --git a/lua/greyjoy/terminals.lua b/lua/greyjoy/terminals.lua index 979becb..7d2858d 100644 --- a/lua/greyjoy/terminals.lua +++ b/lua/greyjoy/terminals.lua @@ -1,24 +1,45 @@ local M = {} M.term = function(command, config) - local height = config.ui.term.height + local total_lines = vim.o.lines + local total_cols = vim.o.columns + local widthpct = config.ui.term.width_pct or 0.2 -- default to 20% of screen width - local curwin = vim.api.nvim_get_current_win() - local curbuf = vim.api.nvim_win_get_buf(curwin) + local width = math.floor(total_cols * widthpct) + + local current_win = vim.api.nvim_get_current_win() + local win_width = vim.api.nvim_win_get_width(current_win) if vim.g["greyjoytermid"] == nil then - vim.cmd.vnew() - vim.cmd.term() + vim.api.nvim_win_set_width(current_win, win_width - width) + + local buf = vim.api.nvim_create_buf(false, true) + + local win = vim.api.nvim_open_win(buf, true, { + relative = "editor", + width = width, + height = total_lines, + row = 0, + col = total_cols - width, + style = "minimal", + border = "none", + }) - vim.fn.jobpid(vim.o.channel) + -- Move it to the bottom vim.cmd.wincmd("J") + vim.cmd.term() + vim.fn.jobpid(vim.o.channel) - local termwin = vim.api.nvim_get_current_win() - vim.api.nvim_win_set_height(0, height) + -- if someone defined the hight we use it + if config.ui.term.height then + vim.api.nvim_win_set_height(0, config.ui.term.height) + end - vim.g["greyjoytermid"] = termwin + vim.g["greyjoytermid"] = win vim.g["greyjoychanid"] = vim.bo.channel - vim.g["greyjoybufid"] = vim.api.nvim_get_current_buf() + vim.g["greyjoybufid"] = buf + vim.bo[buf].buflisted = false + vim.bo[buf].bufhidden = "hide" vim.api.nvim_create_autocmd({ "WinClosed", "TermClose" }, { callback = function(ev) @@ -44,9 +65,7 @@ M.term = function(command, config) vim.fn.chansend(vim.g["greyjoychanid"], { commandstr }) - -- restore window/buf - vim.api.nvim_set_current_win(curwin) - vim.fn.bufload(curbuf) + vim.api.nvim_set_current_win(current_win) end M.toggleterm = function(command, config)