refactor: extract terminal logic into addons/term
This commit is contained in:
parent
e45a3b991e
commit
131bcb8bf5
|
|
@ -0,0 +1,37 @@
|
||||||
|
-- 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
|
||||||
|
|
@ -38,27 +38,10 @@ G.map({
|
||||||
{ 'n', '<right>', ':vertical resize +5<CR>' },
|
{ 'n', '<right>', ':vertical resize +5<CR>' },
|
||||||
})
|
})
|
||||||
|
|
||||||
-- 终端 / Reasonix:横屏→右侧(45%屏宽),竖屏→下方(40%屏高),持久化 buffer(按 c-t/c-i 切换显隐)
|
-- 终端 / Reasonix(addons/term:持久化 + 自适应分屏)
|
||||||
do
|
local T = require('addons.term')
|
||||||
local terms = {} -- 缓存,复用同一个终端实例
|
G.map({
|
||||||
|
{ 'n', '<c-t>', T.shell, { noremap = true } },
|
||||||
local function split_term(cmd)
|
{ 'i', '<c-t>', T.shell, { noremap = true } },
|
||||||
local ui = vim.api.nvim_list_uis()[1]
|
{ 'n', '<C-i>', function() T.open('reasonix') end, { noremap = true } },
|
||||||
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
|
|
||||||
|
|
||||||
G.map({
|
|
||||||
{ 'n', '<c-t>', function() split_term() end, { noremap = true } },
|
|
||||||
{ 'i', '<c-t>', function() split_term() end, { noremap = true } },
|
|
||||||
{ 'n', '<C-i>', function() split_term('reasonix') end, { noremap = true } },
|
|
||||||
})
|
|
||||||
end
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue