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

remote TUI: refactor TUI from thread to separate process #18375

Merged
merged 3 commits into from
Dec 31, 2022

Conversation

bfredl
Copy link
Member

@bfredl bfredl commented May 2, 2022

Rebase of most of #10071

Very rough draft. "builtin TUI" replacement sorta works, but not remote attachment yet (actually input works, but not redrawing..)

Also needs a lot of cleanup, obviously.

@github-actions github-actions bot added build building and installing Neovim using the provided scripts dependencies build dependencies (LuaJIT, LibUV, etc.) tui ui labels May 2, 2022
@bfredl bfredl force-pushed the tui_rework branch 2 times, most recently from d7a2d08 to 8dfe26e Compare May 3, 2022 19:02
justinmk pushed a commit that referenced this pull request Jun 19, 2022
Problem:
Piping NodeJS output into Neovim makes the editor unusable.
This happens because NodeJS changes the tty state on exit after
Nvim calls uv_tty_set_mode(). (May not always happen due to race
condition.)
This should have been fixed by 4ba5b4a #13084. But some
commands and functions (:sleep, system(), …) call ui_flush()
internally, in particular the first tui_mode_change() is called before
the end of startup.

Steps to reproduce:
1. node -e "setTimeout(()=>{console.log('test')}, 1000)" | nvim -u NORC +"sleep 500m" -
2. The cursor key letters just overwrite the editor screen, and CTRL+C exits.

Solution:
Skip pending_mode_update during startup.
Note: Delaying ui_flush() entirely could be a more general solution
(emit a new UI event on VimEnter?). But "remote/coprocess TUI" #18375
could make all of this moot anyway.
Fixes #18470
kraftwerk28 pushed a commit to kraftwerk28/neovim that referenced this pull request Jul 6, 2022
Problem:
Piping NodeJS output into Neovim makes the editor unusable.
This happens because NodeJS changes the tty state on exit after
Nvim calls uv_tty_set_mode(). (May not always happen due to race
condition.)
This should have been fixed by 4ba5b4a neovim#13084. But some
commands and functions (:sleep, system(), …) call ui_flush()
internally, in particular the first tui_mode_change() is called before
the end of startup.

Steps to reproduce:
1. node -e "setTimeout(()=>{console.log('test')}, 1000)" | nvim -u NORC +"sleep 500m" -
2. The cursor key letters just overwrite the editor screen, and CTRL+C exits.

Solution:
Skip pending_mode_update during startup.
Note: Delaying ui_flush() entirely could be a more general solution
(emit a new UI event on VimEnter?). But "remote/coprocess TUI" neovim#18375
could make all of this moot anyway.
Fixes neovim#18470
@@ -55,8 +55,8 @@ with these (optional) keys:
`stdin_fd` Read buffer from `fd` as if it was a stdin pipe
This option can only used by |--embed| ui,
see |ui-startup-stdin|.


`term_ttyin` Tells if `stdin` is a `tty` or not.
Copy link
Member

Choose a reason for hiding this comment

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

for parallel form with stdin_fd this could be named stdin_tty , stdout_tty

@justinmk
Copy link
Member

justinmk commented Jan 9, 2023

followup: #21675

@3N4N
Copy link
Contributor

3N4N commented Feb 10, 2023

@bfredl can I ask for a brief overview of how the embedded nvim process is being created? Don't need you to describe the whole thing. Some high-level overview would be fine. I can do some research on my own then.

@bfredl
Copy link
Member Author

bfredl commented Feb 10, 2023

@3N4N we use the same internal API as jobstart() i e channel_job_start() which in the end leads to uv_spawn() at which point libuv figures the details for respective platform.

vimpostor added a commit to vimpostor/vim-tpipeline that referenced this pull request Feb 16, 2023
Of course GUI detection wasn't fucked up enough in neovim already (see
c2603e4 for the last rant about this
topic). Instead of requiring special handling just for neovim, we now
also need special treatment just for neovim 0.9, because of course they
don't adhere to their own documentation anymore and v:event['chan'] may
now also return 1, as a sideeffect of running the internal TUI in a
separate process [0].

So to this day there is still no sane way to detect TUI in neovim,
instead we have to add a hacky workaround to check nvim_list_uis() for
ext_termcolors, which I am 100% confident will break in the future too.

Vim had sane API for this since forever and it is as simple as checking
for has('gui_running') at any point of time, but of course neovim can't
have this set at startup because we have to make everything convoluted
as fuck by sourcing plugins before the UI has a chance to attach.

Why the UI is not allowed to set a flag as the very first thing in the
startup sequence (and then attach later), is beyond stupid.

This is also not the first time that neovim's weird startup sequence
causes problems [1].

Fixes #46

[0] neovim/neovim#18375
[1] neovim/neovim#19362
@vimpostor
Copy link
Contributor

vimpostor commented Feb 16, 2023

It would be good to update the outdated documentation :h v:event for chan to reflect the change, that after this patch the internal TUI will return 1 instead of 0, because the channel for the TUI is now also a separate process:

			chan		|channel-id| or 0 for "internal".

Note that GUIs as well as the internal TUI may now return 1, so as of now there is no sane way to detect TUI in neovim (but adding official API for this would be a separate issue).

To reproduce this sideeffect of this patch, run the following which will output 1 after this patch and 0 before this patch:

nvim --clean -Nu <(cat<<EOF
au UIEnter * echom v:event['chan']
EOF)

Also see vimpostor/vim-tpipeline#46 (comment)

vimpostor added a commit to vimpostor/vim-tpipeline that referenced this pull request Mar 1, 2023
It looks like finally neovim got sane GUI detection support, like vim
[0].
This means we can remove the temporary workaround from 95a6ccb
completely.

Nightly neovim users who used a version of neovim released after
the broken 2022-12-01 version [1], but before the fix in 2023-02-28,
should update their version of neovim as soon as possible, as the root
of the problem is now properly fixed for neovim 0.9.

Fixes #46.

[0] neovim/neovim#22382
[1] neovim/neovim#18375
@egolep
Copy link

egolep commented Mar 6, 2023

Maybe a stupid question, but how should I kill or detach the TUI without killing the terminal I am in?

@msva
Copy link
Contributor

msva commented Mar 8, 2023

how should I kill or detach the TUI without killing the terminal I am in?

Eeerm...
:qa!
?

@egolep
Copy link

egolep commented Mar 8, 2023

how should I kill or detach the TUI without killing the terminal I am in?

Eeerm... :qa! ?

That would also kill the headless neovim instance. I just want to detach/kill the TUI and leave the headless session running.
So right now my workflow is forced to be:

  • run nvim --headless --listen server:port
  • run nvim --remote-ui --server server:port
  • Edit some stuff
  • Kill the whole terminal I am using in order to kill the remote TUI while leaving the headless instance running
    My question is if there is a way to keep the terminal alive and just kill the remote-ui. I tried with :suspend, but then the TUI process remains alive in the background and a new call to --remote ui would simply spawn a new process.
    Sorry, my previous post wasn't clear.

@bfredl
Copy link
Member Author

bfredl commented Mar 8, 2023

@egolep There is no real user-level functionality yet.. You could use nvim_list_uis() and use the "chan" field to get the channel number ( the largest channel number for latest attached ui, and so on), and then chanclose(chan) to close it, detatching that one UI.

A direct way for the user to say "detach the current ui" (the one last sending input to nvim), would be a useful addition to core, for sure.

@neovim neovim locked as resolved and limited conversation to collaborators Mar 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
build building and installing Neovim using the provided scripts dependencies build dependencies (LuaJIT, LibUV, etc.) tui ui
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants