summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriele Musco <[email protected]>2022-11-02 11:31:28 +0100
committerGitHub <[email protected]>2022-11-02 11:31:28 +0100
commit1e0ab6a3a3db1b630f05c640057ad1d6fa744641 (patch)
tree71d1822be1840fc8f6157a1678c7ebf1d421e362
parent8d3f9b8bf7f5fddca2e8046a84ecbd7c3945dcba (diff)
feat: dynamic or fixed toggle terminal size (#3110)
-rw-r--r--lua/lvim/core/terminal.lua43
1 files changed, 38 insertions, 5 deletions
diff --git a/lua/lvim/core/terminal.lua b/lua/lvim/core/terminal.lua
index 006452e1..7e1ccb6a 100644
--- a/lua/lvim/core/terminal.lua
+++ b/lua/lvim/core/terminal.lua
@@ -41,26 +41,59 @@ M.config = function()
-- lvim.builtin.terminal.execs[#lvim.builtin.terminal.execs+1] = {"gdb", "tg", "GNU Debugger"}
-- TODO: pls add mappings in which key and refactor this
execs = {
- { vim.o.shell, "<M-1>", "Horizontal Terminal", "horizontal", 10 },
- { vim.o.shell, "<M-2>", "Vertical Terminal", "vertical", 60 },
+ { vim.o.shell, "<M-1>", "Horizontal Terminal", "horizontal", 0.3 },
+ { vim.o.shell, "<M-2>", "Vertical Terminal", "vertical", 0.3 },
{ vim.o.shell, "<M-3>", "Float Terminal", "float", nil },
},
}
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()
local terminal = require "toggleterm"
terminal.setup(lvim.builtin.terminal)
for i, exec in pairs(lvim.builtin.terminal.execs) do
+ local direction = exec[4] or lvim.builtin.terminal.direction
+
local opts = {
cmd = exec[1],
keymap = exec[2],
label = exec[3],
-- NOTE: unable to consistently bind id/count <= 9, see #2146
count = i + 100,
- direction = exec[4] or lvim.builtin.terminal.direction,
- size = exec[5] or lvim.builtin.terminal.size,
+ direction = direction,
+ size = function()
+ return get_dynamic_terminal_size(direction, exec[5])
+ end,
}
M.add_exec(opts)
@@ -79,7 +112,7 @@ M.add_exec = function(opts)
end
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