Skip to content

Commit bfb7a72

Browse files
authored
feat(terminal): add mechanism to fetch last focused terminal (#411)
This is useful for plugins like flatten.nvim where you (read: I) want to be able to check if the last toggled terminal was a float and if not not bother re-opening it
1 parent fd63194 commit bfb7a72

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lua/toggleterm/terminal.lua

+8-2
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ function M.get_focused_id()
121121
return nil
122122
end
123123

124+
function M.get_last_focused()
125+
local last_focus = ui.get_terminal_view().focus_term_id
126+
return M.get(last_focus, true)
127+
end
128+
124129
--- @param bufnr number
125130
local function setup_buffer_mappings(bufnr)
126131
local mapping = config.open_mapping
@@ -526,10 +531,11 @@ end
526531

527532
---Get a single terminal by id, unless it is hidden
528533
---@param id number?
534+
---@param include_hidden boolean? whether or nor to filter out hidden
529535
---@return Terminal?
530-
function M.get(id)
536+
function M.get(id, include_hidden)
531537
local term = terminals[id]
532-
return (term and not term.hidden) and term or nil
538+
return (term and (include_hidden == true or not term.hidden)) and term or nil
533539
end
534540

535541
---Return the potentially non contiguous map of terminals as a sorted array

lua/toggleterm/ui.lua

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ local api = vim.api
1919
local origin_window
2020
local persistent = {}
2121

22+
---@alias TerminalView {terminals: number[], focus_term_id: number}
23+
24+
---@type TerminalView
2225
local terminal_view = {
2326
---@type number[]
2427
-- A list of terminal IDs that are saved from the view on smart toggle.
@@ -454,4 +457,7 @@ function M.save_terminal_view(terminals, focus_term_id)
454457
terminal_view = { terminals = terminals, focus_term_id = focus_term_id }
455458
end
456459

460+
---@return TerminalView
461+
function M.get_terminal_view() return terminal_view end
462+
457463
return M

0 commit comments

Comments
 (0)