From e45a3b991e45524f9f11e6ece1b9cb5c2a62d5b4 Mon Sep 17 00:00:00 2001 From: newbieQQ Date: Thu, 11 Jun 2026 16:53:09 +0800 Subject: [PATCH] feat: terminal persistence (toggle show/hide), size 40%-45% like IDE split --- lua/keymap.lua | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/lua/keymap.lua b/lua/keymap.lua index 201c811..a316c09 100644 --- a/lua/keymap.lua +++ b/lua/keymap.lua @@ -38,22 +38,27 @@ G.map({ { 'n', '', ':vertical resize +5' }, }) --- 终端 / Reasonix:横屏→右侧,竖屏→下方 +-- 终端 / Reasonix:横屏→右侧(45%屏宽),竖屏→下方(40%屏高),持久化 buffer(按 c-t/c-i 切换显隐) do - local function split_term(ui, cmd) + local terms = {} -- 缓存,复用同一个终端实例 + + local function split_term(cmd) + local ui = vim.api.nvim_list_uis()[1] local tall = ui.height > ui.width - local term = require('toggleterm.terminal').Terminal:new({ - -- 横屏→右侧(垂直分屏);竖屏→下方(水平分屏) - direction = tall and 'horizontal' or 'vertical', - cmd = cmd, - size = tall and 10 or 40, - }) - term:toggle() + 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', '', function() split_term(vim.api.nvim_list_uis()[1]) end, { noremap = true } }, - { 'i', '', function() split_term(vim.api.nvim_list_uis()[1]) end, { noremap = true } }, - { 'n', '', function() split_term(vim.api.nvim_list_uis()[1], 'reasonix') end, { noremap = true } }, + { 'n', '', function() split_term() end, { noremap = true } }, + { 'i', '', function() split_term() end, { noremap = true } }, + { 'n', '', function() split_term('reasonix') end, { noremap = true } }, }) end