38 lines
1022 B
Lua
38 lines
1022 B
Lua
-- 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
|