Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable switching to the next and previous float terminal #368

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<CR>` 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
Expand Down
41 changes: 41 additions & 0 deletions lua/toggleterm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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 == ""
Expand Down