From d25f5918017479480bd6c7a30144f4cd934e48d2 Mon Sep 17 00:00:00 2001 From: Blackmorse Date: Wed, 14 Dec 2022 15:55:46 +0400 Subject: [PATCH 1/2] feat: enable switching to the next and previous terminal in the float mode --- README.md | 4 ++++ lua/toggleterm.lua | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/README.md b/README.md index 650d0e48..ae6e1150 100644 --- a/README.md +++ b/README.md @@ -294,6 +294,10 @@ You can map this to a key and call it with a count, which will then prompt you a Alternatively you can call it with just the name e.g. `:ToggleTermSetName work` this will the prompt you for which terminal it should apply to. Lastly you can call it without any arguments, and it will prompt you for which terminal it should apply to then prompt you for the name to use. +### ToggleTermFloatNext/ToggleTermFloatPrev + +This functions allow you to switch the terminals by cycling through them when you are in a float one. + ### Set terminal shading This plugin automatically shades terminal filetypes to be darker than other window diff --git a/lua/toggleterm.lua b/lua/toggleterm.lua index 50041fb8..e2dd40f4 100644 --- a/lua/toggleterm.lua +++ b/lua/toggleterm.lua @@ -70,6 +70,37 @@ local function smart_toggle(_, size, dir, direction) end end +---@param terminal_chooser fun(terminal_index: number, terminals_size: number) +function M.neighbor_float_term(terminal_chooser) + if not ui.find_open_windows() then return end + + local terminals = terms.get_all() + local focused_term_index + for index, terminal in ipairs(terminals) do + if terminal:is_focused() then focused_term_index = index end + end + + local focused_term = terminals[focused_term_index] + if not focused_term:is_float() then return end + + if not focused_term_index then return end + local next_terminal_index = terminal_chooser(focused_term_index, #terminals) + + local next_terminal = terminals[next_terminal_index] + + focused_term:close() + terms.get_or_create_term(next_terminal.id):open() +end + +function M.prev_float_term() + M.neighbor_float_term(function(focused_index, terminals_number) return focused_index == 1 and terminals_number or focused_index - 1 end) +end + +function M.next_float_term() + M.neighbor_float_term(function(focused_index, terminals_number) return focused_index == terminals_number and 1 or focused_index + 1 end) +end + + --- @param num number --- @param size number? --- @param dir string? @@ -398,6 +429,16 @@ local function setup_commands() { nargs = "?" } ) + cmd( + "ToggleTermFloatNext", + function() M.next_float_term() end, + {}) + + cmd( + "ToggleTermFloatPrev", + function() M.prev_float_term() end, + {}) + cmd("ToggleTermSetName", function(opts) local no_count = not opts.count or opts.count < 1 local no_name = opts.args == "" From 38611adab805e4a70e3df1b07b8f715444277946 Mon Sep 17 00:00:00 2001 From: Blackmorse Date: Wed, 14 Dec 2022 17:14:52 +0400 Subject: [PATCH 2/2] Fix checks for float --- lua/toggleterm.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/toggleterm.lua b/lua/toggleterm.lua index e2dd40f4..ce93fd81 100644 --- a/lua/toggleterm.lua +++ b/lua/toggleterm.lua @@ -80,10 +80,10 @@ function M.neighbor_float_term(terminal_chooser) if terminal:is_focused() then focused_term_index = index end end + if not focused_term_index or not terminals[focused_term_index]:is_float() then return end + local focused_term = terminals[focused_term_index] - if not focused_term:is_float() then return end - if not focused_term_index then return end local next_terminal_index = terminal_chooser(focused_term_index, #terminals) local next_terminal = terminals[next_terminal_index]