numToStr / FTerm.nvim

:fire: No-nonsense floating terminal plugin for neovim :fire:
MIT License
721 stars 24 forks source link

Get buffer number of a terminal #71

Closed phha closed 1 year ago

phha commented 1 year ago

Is it possible to get the buffer number of a custom terminal? I would like to be able to set a custom keymap for a specific terminal. This would also be useful for other buffer-local settings. Something like the (not working) example below:


local uvicorn = fterm:new {
    cmd = 'poetry run uvicorn api.main:app --host 0.0.0.0 --reload',
    ft = 'fterm__uvicorn'
}
vim.keymap.set(
    't',
    'q',
    function() uvicorn:close() end,
    { buffer = uvicorn:get_buf(), desc = 'Close uvicorn window' }
)
numToStr commented 1 year ago

Better to use TermOpen autocmd for binding terminal keybindings. See https://github.com/numToStr/FTerm.nvim/pull/68#issuecomment-1228532830

phha commented 1 year ago

I thought about using autocommands and indeed it would work for the specific case here. But being able to access the buffer id programmatically would open up a lot of other possibilities as well. Also notice how the autocommand solution requires a lot of boilerplate code and care to make sure everything is cleaned up again. Is there a specific reason why you are hesitant to add this functionality? I haven’t looked at the code but I figured that FTerm would need to hold a reference to the buffer internally anyway and it’s simply a matter of exposing it.

numToStr commented 1 year ago

But being able to access the buffer id programmatically would open up a lot of other possibilities as well.

Huh, Is autocmd not a programmatic access? What other possibilities you are looking for?

Also notice how the autocommand solution requires a lot of boilerplate code and care to make sure everything is cleaned up again.

Boilerplate? Using TermOpen and TermClose is literally the right way to do it.

Your example code will only run on startup not every time when terminal opens, which is totally useless. And afterward you need to clean your buffer keybinds when buffer is destroyed or when terminal exits unless you decide not to do it.

Is there a specific reason why you are hesitant to add this functionality? I haven’t looked at the code but I figured that FTerm would need to hold a reference to the buffer internally anyway and it’s simply a matter of exposing it.

I won't add needless features when there is a vim-ish and right way to do it. And yes, you could access the buffer id via uvicorn.buf but it'll be nil on startup.