From 38a172434027c9ac2a71cd658803ec3f7a39ab09 Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Mon, 6 Dec 2021 07:30:29 +0100 Subject: feat(terminal): lazygit can now be toggled (#2039) * feat(terminal): lazygit can now be toggled - remove `hidden` parameter so it can be persistent - map keybinding for both normal and term modes - a default shell is now always reachable with `` in a split - add an addition keybinding `` for `lazygit` since it's hard to hit `gg` within the `timeoutlen` < 300ms * revert(terminal): use float direction by default --- lua/lvim/core/terminal.lua | 80 ++++++++++++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 28 deletions(-) (limited to 'lua/lvim/core/terminal.lua') diff --git a/lua/lvim/core/terminal.lua b/lua/lvim/core/terminal.lua index aa6989ec..61c0acd0 100644 --- a/lua/lvim/core/terminal.lua +++ b/lua/lvim/core/terminal.lua @@ -40,53 +40,77 @@ M.config = function() -- lvim.builtin.terminal.execs = {{}} to overwrite -- lvim.builtin.terminal.execs[#lvim.builtin.terminal.execs+1] = {"gdb", "tg", "GNU Debugger"} execs = { - { "lazygit", "gg", "LazyGit" }, + -- TODO: this should probably be removed since it's hard to hit gg within the timeoutlen + { "lazygit", "gg", "LazyGit", "float" }, + { "lazygit", "", "LazyGit", "float" }, }, } end M.setup = function() local terminal = require "toggleterm" - for _, exec in pairs(lvim.builtin.terminal.execs) do - require("lvim.core.terminal").add_exec(exec[1], exec[2], exec[3]) - end terminal.setup(lvim.builtin.terminal) - if lvim.builtin.terminal.on_config_done then - lvim.builtin.terminal.on_config_done(terminal) - end -end + -- setup the default terminal so it's always reachable + local default_term_opts = { + cmd = lvim.builtin.terminal.shell, + keymap = lvim.builtin.terminal.open_mapping, + label = "Toggle terminal", + count = 1, + direction = lvim.builtin.terminal.direction, + size = lvim.builtin.terminal.size, + } + M.add_exec(default_term_opts) -M.add_exec = function(exec, keymap, name) - vim.api.nvim_set_keymap( - "n", - "" .. keymap, - "lua require('lvim.core.terminal')._exec_toggle('" .. exec .. "')", - { noremap = true, silent = true } - ) - lvim.builtin.which_key.mappings[keymap] = name -end + for i, exec in pairs(lvim.builtin.terminal.execs) do + local opts = { + cmd = exec[1], + keymap = exec[2], + label = exec[3], + count = i + 1, + direction = exec[4] or lvim.builtin.terminal.direction, + size = lvim.builtin.terminal.size, + } -M._split = function(inputstr, sep) - if sep == nil then - sep = "%s" + M.add_exec(opts) end - local t = {} - for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do - table.insert(t, str) + + if lvim.builtin.terminal.on_config_done then + lvim.builtin.terminal.on_config_done(terminal) end - return t end -M._exec_toggle = function(exec) - local binary = M._split(exec)[1] +M.add_exec = function(opts) + local binary = opts.cmd:match "(%S+)" if vim.fn.executable(binary) ~= 1 then Log:error("Unable to run executable " .. binary .. ". Please make sure it is installed properly.") return end + + local exec_func = string.format( + "lua require('lvim.core.terminal')._exec_toggle({ cmd = '%s', count = %d, direction = '%s'})", + opts.cmd, + opts.count, + opts.direction + ) + + require("lvim.keymappings").load { + normal_mode = { [opts.keymap] = exec_func }, + term_mode = { [opts.keymap] = exec_func }, + } + + local wk_status_ok, wk = pcall(require, "whichkey") + if not wk_status_ok then + return + end + wk.register({ [opts.keymap] = { opts.label } }, { mode = "n" }) + wk.register({ [opts.keymap] = { opts.label } }, { mode = "t" }) +end + +M._exec_toggle = function(opts) local Terminal = require("toggleterm.terminal").Terminal - local exec_term = Terminal:new { cmd = exec, hidden = true } - exec_term:toggle() + local term = Terminal:new { cmd = opts.cmd, count = opts.count, direction = opts.direction } + term:toggle(lvim.builtin.terminal.size, opts.direction) end ---Toggles a log viewer according to log.viewer.layout_config -- cgit v1.2.3 From c43ee9aa3a6353e74d577cb79e60c5c885bc02f5 Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Wed, 8 Dec 2021 09:01:33 +0100 Subject: fix: no longer treat lazygit missing as an error (#2051) --- lua/lvim/core/terminal.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lua/lvim/core/terminal.lua') diff --git a/lua/lvim/core/terminal.lua b/lua/lvim/core/terminal.lua index 61c0acd0..20485cda 100644 --- a/lua/lvim/core/terminal.lua +++ b/lua/lvim/core/terminal.lua @@ -83,7 +83,7 @@ end M.add_exec = function(opts) local binary = opts.cmd:match "(%S+)" if vim.fn.executable(binary) ~= 1 then - Log:error("Unable to run executable " .. binary .. ". Please make sure it is installed properly.") + Log:debug("Skipping configuring executable " .. binary .. ". Please make sure it is installed properly.") return end -- cgit v1.2.3 From abf127db83794569af204c017c0e003a5e9e63c9 Mon Sep 17 00:00:00 2001 From: Chase Colman Date: Sat, 11 Dec 2021 16:57:52 +0800 Subject: fix(terminal): allow disabling the open binding for toggleterm --- lua/lvim/core/terminal.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'lua/lvim/core/terminal.lua') diff --git a/lua/lvim/core/terminal.lua b/lua/lvim/core/terminal.lua index 20485cda..7eb343ce 100644 --- a/lua/lvim/core/terminal.lua +++ b/lua/lvim/core/terminal.lua @@ -60,7 +60,9 @@ M.setup = function() direction = lvim.builtin.terminal.direction, size = lvim.builtin.terminal.size, } - M.add_exec(default_term_opts) + if lvim.builtin.terminal.open_mapping then + M.add_exec(default_term_opts) + end for i, exec in pairs(lvim.builtin.terminal.execs) do local opts = { -- cgit v1.2.3 From 53bb4effbc645ed75fc13f38b4e4f58db8d4ebe1 Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Sat, 11 Dec 2021 21:19:20 +0100 Subject: chore(terminal): avoid problematic layzgit mapping (#2068) --- lua/lvim/core/terminal.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lua/lvim/core/terminal.lua') diff --git a/lua/lvim/core/terminal.lua b/lua/lvim/core/terminal.lua index 7eb343ce..c5d1ea04 100644 --- a/lua/lvim/core/terminal.lua +++ b/lua/lvim/core/terminal.lua @@ -40,9 +40,8 @@ M.config = function() -- lvim.builtin.terminal.execs = {{}} to overwrite -- lvim.builtin.terminal.execs[#lvim.builtin.terminal.execs+1] = {"gdb", "tg", "GNU Debugger"} execs = { - -- TODO: this should probably be removed since it's hard to hit gg within the timeoutlen { "lazygit", "gg", "LazyGit", "float" }, - { "lazygit", "", "LazyGit", "float" }, + { "lazygit", "", "LazyGit", "float" }, }, } end -- cgit v1.2.3