Skip to content

Commit

Permalink
refactor(term): make buildin term use a % of screen
Browse files Browse the repository at this point in the history
  • Loading branch information
desdic committed Feb 9, 2025
1 parent acc0d4c commit 20e7a89
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion lua/greyjoy/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ local defaults = {
size = nil,
},
term = {
height = 5,
width_pct = 0.2,
},
telescope = {
keys = {
Expand Down
45 changes: 32 additions & 13 deletions lua/greyjoy/terminals.lua
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 20e7a89

Please sign in to comment.