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..ce93fd81 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 + + if not focused_term_index or not terminals[focused_term_index]:is_float() then return end + + local focused_term = terminals[focused_term_index] + + 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 == ""