nvim/lua/addons/term.lua

38 lines
1022 B
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- addons/term.lua — 持久化自适应终端
--
-- 用法:
-- local T = require('addons.term')
-- T.shell() -- 打开/关闭普通终端(<c-t>
-- T.open('reasonix') -- 打开/关闭 Reasonix<C-i>
--
-- 特性:
-- - 同一个终端实例持久化(关掉再开回来,对话不丢)
-- - 窗口方向自适应横屏→右侧45%竖屏→下方40%
-- - 依赖 toggleterm.nvim
local M = {}
local terms = {} -- 缓存终端实例key 是命令名nil = 普通 shell
function M.open(cmd)
local ui = vim.api.nvim_list_uis()[1]
local tall = ui.height > ui.width
local name = cmd or '__shell__'
if not terms[name] or not terms[name]:is_open() then
terms[name] = require('toggleterm.terminal').Terminal:new({
direction = tall and 'horizontal' or 'vertical',
cmd = cmd,
size = tall and math.floor(ui.height * 0.4) or math.floor(ui.width * 0.45),
})
end
terms[name]:toggle()
end
function M.shell()
M.open(nil)
end
return M