-
Notifications
You must be signed in to change notification settings - Fork 65
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
Samples on windows are inefficient #41
Comments
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
Oh, sorry, I didn't get your point. So you suggest some improvement in example. |
But I'm not sure this is a problem specific to this crate. All widgets have the same issue, maybe? It is not possible to know when to render the widget only from key input. Demo example of ratatui has the same issue. It calls I think this is an issue of ratatui's API design rather than handling it within this crate somehow. What we can do is rendering the first screen separately like this: // First rendering
term.draw(|f| {
f.render_widget(textarea.widget(), f.size());
})?;
loop {
match crossterm::event::read()?.into() {
Input { key: Key::Esc, .. } => break,
Input { key: Key::Null, .. } => continue,
input => {
textarea.input(input);
}
}
term.draw(|f| {
f.render_widget(textarea.widget(), f.size());
})?;
} |
If my understanding is correct, ratatui checks diff of content and updates terminal screen differentially. So, even if the |
This is less ratatui design, and more that most of the examples use this common approach (the |
no saying its slow just that its ugly to see the input/render cycle run twice for every key stroke. Rust is supposed to be super fast, hate to see cycles wasted |
I've thought about this for a while. But I think this issue cannot be resolved generally. It's kind of optimization specific to application.
We can ignore the press event safely on It would be nice to have some performance tips in document so that users can consider to apply the technique to their applications. @joshka You're right. But PowerShell's rendering is very slow. So I'm skeptical that it can meets the 10fps requirement on Windows honestly. |
its just the samples. It cannot be fixed in general. I fixed the error in gitui (the project I am using textarea for, they had a much more serious issue, they were processing the release as a key stroke so everything was duplicated !). But the samples are meant to show how to correctly use the API. THis fix works in minimal, can be generalized for all samples.
|
For example, your code doesn't work properly when you resize the terminal window. |
Your wrong assumption is that receiving |
ah yes, so really key into should return Key::Skip or something to indicate that this input can be ignored. |
You mean converting key events whose kind is If you know all other widgets in your application ignore use crossterm::event::{Event, KeyEvent, KeyEventKind};
term.draw(|f| {
let rect = ...; // Get area to render textarea
f.render_widget(textarea.widget(), rect);
// Render other widgets...
})?;
loop {
let event = crossterm::event::read()?;
if let Event::Key(KeyEvent { kind: KeyEventKind::Release, .. }) = &event {
continue;
}
match event.into() {
Input { key: Key::Esc, .. } => break,
input => {
textarea.input(input);
}
}
term.draw(|f| {
let rect = ...; // Get area to render textarea
f.render_widget(textarea.widget(), rect);
// Render other widgets...
})?;
} |
I know how to deal with it in real life - I am a user of textarea in another project , the double keystrokes from crossterm were a huge issue. I am just trying to see if a) it needs to be documented b) we can fix the samples. Maybe do nothing. I just hursts my brain whenever I put traces in the code and see everything triggered twice |
On windows every key stroke is doubled due to the key press and release. TTA maps the release to Null but that null is still sent through the main input and draw loop. No harm is done but the key lookup and , more importantly, the render is repeated, uselessly.
I know these are samples but people will copy them.
There is no simple one liner, this isnt enough
as all the samples do render then input, so the render gets done again
The text was updated successfully, but these errors were encountered: