Skip to content

Commit

Permalink
tui: Cache the keyboard enhancement check
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
the-mikedavis authored and archseer committed Mar 8, 2023
1 parent 3d85024 commit 611701c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions helix-tui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ unicode-segmentation = "1.10"
crossterm = { version = "0.26", optional = true }
termini = "0.1"
serde = { version = "1", "optional" = true, features = ["derive"]}
once_cell = "1.17"
log = "~0.4"
helix-view = { version = "0.6", path = "../helix-view", features = ["term"] }
helix-core = { version = "0.6", path = "../helix-core" }
14 changes: 12 additions & 2 deletions helix-tui/src/backend/crossterm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crossterm::{
Command,
};
use helix_view::graphics::{Color, CursorKind, Modifier, Rect, UnderlineStyle};
use once_cell::sync::OnceCell;
use std::{
fmt,
io::{self, Write},
Expand Down Expand Up @@ -57,6 +58,7 @@ impl Capabilities {
pub struct CrosstermBackend<W: Write> {
buffer: W,
capabilities: Capabilities,
supports_keyboard_enhancement_protocol: OnceCell<bool>,
}

impl<W> CrosstermBackend<W>
Expand All @@ -67,8 +69,16 @@ where
CrosstermBackend {
buffer,
capabilities: Capabilities::from_env_or_default(),
supports_keyboard_enhancement_protocol: OnceCell::new(),
}
}

#[inline]
fn supports_keyboard_enhancement_protocol(&self) -> io::Result<bool> {
self.supports_keyboard_enhancement_protocol
.get_or_try_init(terminal::supports_keyboard_enhancement)
.copied()
}
}

impl<W> Write for CrosstermBackend<W>
Expand Down Expand Up @@ -100,7 +110,7 @@ where
if config.enable_mouse_capture {
execute!(self.buffer, EnableMouseCapture)?;
}
if matches!(terminal::supports_keyboard_enhancement(), Ok(true)) {
if self.supports_keyboard_enhancement_protocol()? {
log::debug!("The enhanced keyboard protocol is supported on this terminal");
execute!(
self.buffer,
Expand All @@ -121,7 +131,7 @@ where
if config.enable_mouse_capture {
execute!(self.buffer, DisableMouseCapture)?;
}
if matches!(terminal::supports_keyboard_enhancement(), Ok(true)) {
if self.supports_keyboard_enhancement_protocol()? {
execute!(self.buffer, PopKeyboardEnhancementFlags)?;
}
execute!(
Expand Down

0 comments on commit 611701c

Please sign in to comment.