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: add Terminal.find function #486

Merged
merged 1 commit into from
Sep 11, 2023
Merged
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
14 changes: 14 additions & 0 deletions lua/toggleterm/terminal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,20 @@ function M.get(id, include_hidden)
return (term and (include_hidden == true or not term.hidden)) and term or nil
end

---Get the first terminal that matches a predicate
---@param predicate fun(term: Terminal): boolean
---@return Terminal?
function M.find(predicate)
if type(predicate) ~= "function" then
utils.notify("terminal.find expects a function, got " .. type(predicate), "error")
return
end
for _, term in pairs(terminals) do
if predicate(term) then return term end
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if only a well defined subset of the terminal's fields be passed ot the predicate so people can't do undesirable things or use private fields or trigger side effects here that they shouldn't 🤔. I'm sure some some people are already doing stuff like this but I worry about users getting used to operating on the whole terminal in a way that a lot of people become dependent on private fields.

The cat might already be out of the bag on that one though since lua doesn't really support privacy for fields

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm definitely doing a lot of janky things in my config taking advantage of Lua's lack of privacy haha (haven't needed to do so with toggleterm, though). I'm not really sure about this specific case - the idea makes sense, but the function will return a full Terminal object that will be exposed either way, so that would really only avoid mutation in the predicate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be better to expose one or two functions for filtering terminals by bufnr or name, etc. and avoid using a higher order function entirely?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 I guess it's always been the case that users can interact with the terminal directly and I have advised in the README not to use the private methods but I'm sure some users will anyway 🤦🏾‍♂️, I guess more specific functions will just lead to future requests for more variants so probably one function that is super generic is better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, that makes sense. Let me know if there are any changes you'd like!

I think with Lua it's almost impossible to make something private, even locals can be retrieved with debug.getupvalue, so I'm not sure it's worth spending much energy on haha

willothy marked this conversation as resolved.
Show resolved Hide resolved
end
return nil
end

---Return the potentially non contiguous map of terminals as a sorted array
---@param include_hidden boolean? whether or nor to filter out hidden
---@return Terminal[]
Expand Down
Loading