feat: dynamic or fixed toggle terminal size (#3110)

This commit is contained in:
Gabriele Musco 2022-11-02 11:31:28 +01:00 committed by GitHub
parent 8d3f9b8bf7
commit 1e0ab6a3a3
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: 4AEE18F83AFDEB23

View file

@ -41,26 +41,59 @@ M.config = function()
-- lvim.builtin.terminal.execs[#lvim.builtin.terminal.execs+1] = {"gdb", "tg", "GNU Debugger"} -- lvim.builtin.terminal.execs[#lvim.builtin.terminal.execs+1] = {"gdb", "tg", "GNU Debugger"}
-- TODO: pls add mappings in which key and refactor this -- TODO: pls add mappings in which key and refactor this
execs = { execs = {
{ vim.o.shell, "<M-1>", "Horizontal Terminal", "horizontal", 10 }, { vim.o.shell, "<M-1>", "Horizontal Terminal", "horizontal", 0.3 },
{ vim.o.shell, "<M-2>", "Vertical Terminal", "vertical", 60 }, { vim.o.shell, "<M-2>", "Vertical Terminal", "vertical", 0.3 },
{ vim.o.shell, "<M-3>", "Float Terminal", "float", nil }, { vim.o.shell, "<M-3>", "Float Terminal", "float", nil },
}, },
} }
end end
--- Get current buffer size
---@return {width: number, height: number}
local function get_buf_size()
local cbuf = vim.api.nvim_get_current_buf()
local bufinfo = vim.tbl_filter(function(buf)
return buf.bufnr == cbuf
end, vim.fn.getwininfo(vim.api.nvim_get_current_win()))[1]
if bufinfo == nil then
return { width = -1, height = -1 }
end
return { width = bufinfo.width, height = bufinfo.height }
end
--- Get the dynamic terminal size in cells
---@param direction number
---@param size integer
---@return integer
local function get_dynamic_terminal_size(direction, size)
size = size or lvim.builtin.terminal.size
if direction ~= "float" and tostring(size):find(".", 1, true) then
size = math.min(size, 1.0)
local buf_sizes = get_buf_size()
local buf_size = direction == "horizontal" and buf_sizes.height or buf_sizes.width
return buf_size * size
else
return size
end
end
M.setup = function() M.setup = function()
local terminal = require "toggleterm" local terminal = require "toggleterm"
terminal.setup(lvim.builtin.terminal) terminal.setup(lvim.builtin.terminal)
for i, exec in pairs(lvim.builtin.terminal.execs) do for i, exec in pairs(lvim.builtin.terminal.execs) do
local direction = exec[4] or lvim.builtin.terminal.direction
local opts = { local opts = {
cmd = exec[1], cmd = exec[1],
keymap = exec[2], keymap = exec[2],
label = exec[3], label = exec[3],
-- NOTE: unable to consistently bind id/count <= 9, see #2146 -- NOTE: unable to consistently bind id/count <= 9, see #2146
count = i + 100, count = i + 100,
direction = exec[4] or lvim.builtin.terminal.direction, direction = direction,
size = exec[5] or lvim.builtin.terminal.size, size = function()
return get_dynamic_terminal_size(direction, exec[5])
end,
} }
M.add_exec(opts) M.add_exec(opts)
@ -79,7 +112,7 @@ M.add_exec = function(opts)
end end
vim.keymap.set({ "n", "t" }, opts.keymap, function() vim.keymap.set({ "n", "t" }, opts.keymap, function()
M._exec_toggle { cmd = opts.cmd, count = opts.count, direction = opts.direction, size = opts.size } M._exec_toggle { cmd = opts.cmd, count = opts.count, direction = opts.direction, size = opts.size() }
end, { desc = opts.label, noremap = true, silent = true }) end, { desc = opts.label, noremap = true, silent = true })
end end