From 611701c36290b81c3c51ed30c49245f341a580e8 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Sun, 5 Mar 2023 12:13:11 -0600 Subject: tui: Cache the keyboard enhancement check 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. --- helix-tui/src/backend/crossterm.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'helix-tui/src/backend') diff --git a/helix-tui/src/backend/crossterm.rs b/helix-tui/src/backend/crossterm.rs index e81c1e00..7c512ce3 100644 --- a/helix-tui/src/backend/crossterm.rs +++ b/helix-tui/src/backend/crossterm.rs @@ -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}, @@ -57,6 +58,7 @@ impl Capabilities { pub struct CrosstermBackend { buffer: W, capabilities: Capabilities, + supports_keyboard_enhancement_protocol: OnceCell, } impl CrosstermBackend @@ -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 { + self.supports_keyboard_enhancement_protocol + .get_or_try_init(terminal::supports_keyboard_enhancement) + .copied() + } } impl Write for CrosstermBackend @@ -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, @@ -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!( -- cgit v1.2.3-70-g09d2