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

Move terminal claim/restore to helix-tui, cache keyboard enhancement support query #6194

Conversation

the-mikedavis
Copy link
Member

@the-mikedavis the-mikedavis commented Mar 5, 2023

This is #6160 but in a way that is more extendable in the long-term. This will make it easier to add checks for other host terminal information like initial cursor shape, title line, whether the terminal supports synchronized output, etc. without adding a whole bunch of fields to Application or functions to the Terminal trait.

The first commit in this change moves Application::claim_term and application::restore_term to helix-tui into the crossterm backend. I believe this is the more proper place for this code and it makes the second change much easier: we cache whether the terminal supports the enhanced keyboard protocol, resolving #6149 (though it would stil be nice to improve this upstream).

This moves the `Application::claim_term` and
`helix-term::application::restore_term` functions into the helix-tui
crate. How the terminal should be claimed and restored is a TUI concern
and is implemented differently through different TUI backends.

This cleans out a lot of crossterm and TUI code in Application and
makes it easier to modify claim/restore based on information we query
from the terminal host. The child commit will take advantage of this
to cache the check for whether the host terminal supports the keyboard
enhancement protocol. Without this change, caching that information
takes much more code which is not easily reusable for anything else.

The code to restore the terminal is somewhat duplicated by this patch:
we want to restore the terminal in cases of panics. Panic handler hooks
must live for `'static` and the Application's terminal does not.
Wether the host terminal supports keyboard enhancement can be cached
for the lifetime of a Helix session.

Caching this lookup prevents a potential lockup within crossterm's
event reading system where the query for the keyboard enhancement
support waits on the next keyboard event, which can happen if the
crossterm event stream is checked by `tokio::select!` in another
thread.
@the-mikedavis the-mikedavis added A-helix-term Area: Helix term improvements S-waiting-on-review Status: Awaiting review from a maintainer. labels Mar 5, 2023
@the-mikedavis the-mikedavis linked an issue Mar 5, 2023 that may be closed by this pull request
@rcorre
Copy link
Contributor

rcorre commented Mar 6, 2023

This resolves #6149 for me with st 0.8.5.

I don't know if there's a good way to measure it, but it feels like there's still a small but noticeable lag when I first open a file, compared to versions before a066815, where opening a file feels instantaneous.

Would it make sense to have an option similar to true-color, where you can just set it once in your config and bypass the live check every time?

@the-mikedavis
Copy link
Member Author

I'll give st a try. The query itself has been unnoticeably fast in the editors that I have tried so if there's a delay, I would guess it may be an issue with crossterm's event polling system. This PR executes the same query but just caches the result so the initial query shouldn't be any slower. I'll test a bunch of different terminals to see what I can find though.

Would it make sense to have an option similar to true-color, where you can just set it once in your config and bypass the live check every time?

The mechanisms for detecting true-color and undercurl support (see also #6196) are not very robust (they depend on an environment variable and your installed terminfo database respectively) and the query for keyboard enhancement support here is much more reliable (a direct request/response between the application and host terminal) so I don't think a new option to force it should be necessary.

@rcorre
Copy link
Contributor

rcorre commented Mar 7, 2023

so the initial query shouldn't be any slower

Sorry, to clarify, this PR isn't any slower than a066815, and is a significant improvement in general.
I meant that helix appears slightly slower in a066815 than in prior versions.

In my testing this takes around 3-4ms in terminals that support the
enhanced keyboard protocol (Kitty, WezTerm) and a few hundred
microseconds in terminals that don't (st, Alacritty).
@the-mikedavis
Copy link
Member Author

I added some logging to the debug line that says whether the protocol is supported or not. I checked around and the check seems pretty fast on terminals that do support the protocol (3-4ms on Kitty, WezTerm) and very fast on terminals that don't (~100μs on st, Alacritty). Let me know if you find longer measurements!

I think it might be a nice improvement to run this query and enable keyboard enhancement asynchronously after startup. It isn't safe to do it with the current way queries work in crossterm though - that would need to wait on a change in crossterm that would allow multiple concurrent readers.

@rcorre
Copy link
Contributor

rcorre commented Mar 7, 2023

Ugh, I was comparing a debug build to a release build. That might have been it, or I was imagining. Either way, I'm not noticing it now. Sorry for the wild goose chase, and thanks for the fix!

@archseer archseer merged commit 563ac1a into helix-editor:master Mar 8, 2023
@the-mikedavis the-mikedavis deleted the md-move-claim/restore-term-code-to-tui branch March 8, 2023 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-helix-term Area: Helix term improvements S-waiting-on-review Status: Awaiting review from a maintainer.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Weird behavior when forking helix to background
3 participants