From 99eada00b8ca79070b265dcc3e9751e14a1d1326 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Thu, 6 Feb 2025 13:19:18 +0100 Subject: [PATCH] nvim: Improve custom toggle terms Added euporia term to split it from other python variations. Fixed python command selection. Improved mapping and which key display. Removed indentline settings since that is responsibility of indentline. --- nvim/.config/nvim/lua/plugins/terminal.lua | 119 +++++++++------------ 1 file changed, 50 insertions(+), 69 deletions(-) diff --git a/nvim/.config/nvim/lua/plugins/terminal.lua b/nvim/.config/nvim/lua/plugins/terminal.lua index ac97546..54f5726 100644 --- a/nvim/.config/nvim/lua/plugins/terminal.lua +++ b/nvim/.config/nvim/lua/plugins/terminal.lua @@ -1,68 +1,54 @@ -return { - -- simpler, programmable and multiple terminal toggling for nvim +return { -- simple programmable terminal toggling for nvim { "akinsho/nvim-toggleterm.lua", + init = function(_) + if require("core.util").is_available("which-key") then + require("which-key").add({ "t", group = "terminal" }) + end + end, config = function() require("toggleterm").setup({ - open_mapping = [[=]], + open_mapping = [[tt]], insert_mappings = false, -- don't map the key in insert mode terminal_mappings = false, }) local Terminal = require("toggleterm.terminal").Terminal - -- need to disable indentlines since they obscure first line of terminal - if require("core.util").is_available("mini.nvim") then - vim.api.nvim_create_autocmd({ "TermOpen" }, { - pattern = "*", - callback = function() - vim.b.miniindentscope_disable = true - end, - }) - end - local function custom_term_set_toggle_key(term) vim.keymap.set("t", "", function() term:toggle() - end, { silent = true, buffer = true }) + end, { silent = true, buffer = true, desc = "terminal" }) end - -- create python window - local function get_python_cmd() - if vim.fn.executable("py") then - return "py" - end - if vim.fn.executable("ptipython") then - return "ptipython" - end - if vim.fn.executable("ipython") then - return "ipython" - end - if vim.fn.executable("ptpython") then - return "ptpython" - end - if vim.fn.executable("python") then - return "python" - end + local custom_term_default_style = function(cmd) + return { + cmd = cmd, + hidden = true, + direction = "float", + float_opts = { border = "curved" }, + on_open = custom_term_set_toggle_key, + } end local terms = { - lazygit = Terminal:new({ - cmd = "lazygit", - hidden = true, - direction = "float", - float_opts = { border = "curved" }, - on_open = custom_term_set_toggle_key, - }), - python = Terminal:new({ - cmd = get_python_cmd(), - hidden = true, - direction = "float", - float_opts = { border = "curved" }, - on_open = custom_term_set_toggle_key, - }), + lazygit = Terminal:new(custom_term_default_style("lazygit")), + python = Terminal:new(custom_term_default_style(function() + for _, exec in pairs({ "py", "ptipython", "ipython", "ptpython", "python" }) do + if vim.fn.executable(exec) > 0 then + return exec + end + end + end)), + euporie = Terminal:new(custom_term_default_style(function() + local kernel = vim.b.sessionfile + if kernel then + return "euporie-console --connection-file '" .. kernel .. "'" + end + return "euporie-console" + end)), } - -- create a lazygit window with the lazygit command - local function toggle_custom_term(term, bang, vertsize) + -- have user decide between floating or split + local function toggle_split_or_float(term, bang, vertsize) vertsize = vertsize or vim.o.columns * 0.4 if not bang then term.direction = "float" @@ -74,30 +60,25 @@ return { end end - local function _Pythonterm_toggle(opts) - toggle_custom_term(terms.python, opts.bang) - end - local function _Lazygit_toggle(opts) - toggle_custom_term(terms.lazygit, opts.bang, vim.o.columns * 0.6) - end - - vim.api.nvim_create_user_command( - "Lazygit", - _Lazygit_toggle, - { desc = "Toggle floating Lazygit terminal", bang = true } - ) - vim.api.nvim_create_user_command( - "Pythonterm", - _Pythonterm_toggle, - { desc = "Toggle floating Python terminal", bang = true } - ) + vim.api.nvim_create_user_command("Lazygit", function(opts) + toggle_split_or_float(terms.lazygit, opts.bang, vim.o.columns * 0.6) + end, { desc = "Toggle floating Lazygit terminal", bang = true }) + vim.api.nvim_create_user_command("Pythonterm", function(opts) + toggle_split_or_float(terms.python, opts.bang) + end, { desc = "Toggle floating Python terminal", bang = true }) + vim.api.nvim_create_user_command("Euporieterm", function(opts) + toggle_split_or_float(terms.euporie, opts.bang) + end, { desc = "Toggle floating Euporie terminal", bang = true }) end, - cmd = { "ToggleTerm", "TermExec", "Lazygit", "Pythonterm" }, + cmd = { "ToggleTerm", "TermExec", "Lazygit", "Pythonterm", "Euporieterm" }, keys = { - { "sg", ":Lazygit", desc = "git floating" }, - { "sG", ":Lazygit!", desc = "git buffer" }, - { "sp", ":Pythonterm", desc = "python floating" }, - { "sP", ":Pythonterm!", desc = "python buffer" }, + { "tt", ":ToggleTerm", desc = "terminal" }, + { "tg", ":Lazygit", desc = "git floating" }, + { "tG", ":Lazygit!", desc = "git buffer" }, + { "tp", ":Pythonterm", desc = "python floating" }, + { "tP", ":Pythonterm!", desc = "python buffer" }, + { "te", ":Euporieterm", desc = "euporie floating" }, + { "tE", ":Euporieterm!", desc = "euporie buffer" }, }, }, }