toggleterm.nvim icon indicating copy to clipboard operation
toggleterm.nvim copied to clipboard

Buffer of TUI apps are scrolled when hiding terminal or entering normal mode

Open x3rAx opened this issue 2 years ago • 3 comments

Hi,

first of all Thank You for this plugin!

I encountered some strange behavior with "fullscreen" / TUI terminal apps like lazy-git, bacon (Rust test runner) or ranger:

I'll split this into two parts but I guess they are linked somehow.

Scrolling sideways in normal mode

Whenever I toggle the terminal to be hidden or when a terminal loses focus, the apps mentioned above are scrolled to the left by the amount of characters set in sidescrolloff.

This also happens (though not always) when I just enter normal mode in a terminal with a TUI app running.

I think the issue is as follows:

sidescrolloff scrolls the buffer before the cursor reaches the edge of the buffer. When a TUI app is drawn, the cursor is at the bottom right of the buffer. When the terminal loses focus, the terminal enters normal mode. Since the cursor is at the right side of the screen, the buffer is scrolled left to fulfill the sidescrolloff.

When I set sidescrolloff to 0, this does not happen.

Is it possible (either for the plugin or by using some sort of on_focus / on_blur hook to disable and restore the sidescrolloff?

Scrolling down when hiding terminal

Even with sidescrolloff and scrolloff set to 0, when I hide a terminal that has a "fullscreen" / TUI app running, the buffer is scrolled down by one line when I bring the terminal back up. Even stranger: when parts of the TUI get redrawn, they are now drawn one line below where they seem to belong (in fact they are drawn exactly where they should be but since everything is scrolled up by one line, it looks like they are drawn one line off)

When I enter normal mode after hiding and showing the terminal, the cursor is in the bottom left corner. I assume that for some reason the cursor moves down one line when the terminal is hidden and that this causes the buffer to be scrolled up.

x3rAx avatar Feb 10 '23 11:02 x3rAx

@x3rAx regarding the sidescrolloff behaviour this can be set as a window local value so you as a user can use the on_open function for toggleterm and set vim.wo.sidescrolloff = 0 as you please to solve this issue. It might warrant adding for everyone if I can confirm this behaviour is general enough but that should solve that aspect of your issue.

Regarding the one line being off, have you tried opening a full screen TUI app in a buffer outside of toggleterm to see if this plugin is causing this issue or if this is nvim upstream. I often get issues that are really core terminal issues since this plugins is less involved in how the terminal behaves than most people imagine

akinsho avatar Mar 05 '23 10:03 akinsho

I believe this is more of an nvim upstream issue than a toggleterm issue, I have weird scroll behavior no matter whether I use a plugin or :term. A good workaround for now is saving and restoring the window view on BufEnter/BufLeave/TermLeave using winsaveview / winrestview, which occasionally will flicker but is generally fast enough to make the scroll "disappear". Also setting splitkeep can help with certain setups.

willothy avatar Sep 06 '23 09:09 willothy

It's not enough for me to reset the sidescrolloff. So I also reset the cursor position with

vim.api.nvim_win_set_cursor(0, { 1, 0 })

linsui avatar May 02 '24 13:05 linsui