@@ -15,6 +15,7 @@ use crossterm::{
1515 Command,
1616};
1717use helix_view::graphics::{Color, CursorKind, Modifier, Rect, UnderlineStyle};
18+use once_cell::sync::OnceCell;
1819use std::{
1920 fmt,
2021 io::{self, Write},
@@ -57,6 +58,7 @@ impl Capabilities {
5758pub struct CrosstermBackend<W: Write> {
5859 buffer: W,
5960 capabilities: Capabilities,
61+ supports_keyboard_enhancement_protocol: OnceCell<bool>,
6062}
6163
6264impl<W> CrosstermBackend<W>
@@ -67,8 +69,16 @@ where
6769 CrosstermBackend {
6870 buffer,
6971 capabilities: Capabilities::from_env_or_default(),
72+ supports_keyboard_enhancement_protocol: OnceCell::new(),
7073 }
7174 }
75+
76+ #[inline]
77+ fn supports_keyboard_enhancement_protocol(&self) -> io::Result<bool> {
78+ self.supports_keyboard_enhancement_protocol
79+ .get_or_try_init(terminal::supports_keyboard_enhancement)
80+ .copied()
81+ }
7282}
7383
7484impl<W> Write for CrosstermBackend<W>
@@ -100,7 +110,7 @@ where
100110 if config.enable_mouse_capture {
101111 execute!(self.buffer, EnableMouseCapture)?;
102112 }
103− if matches!(terminal::supports_keyboard_enhancement(), Ok(true)) {
113+ if self.supports_keyboard_enhancement_protocol()? {
104114 log::debug!("The enhanced keyboard protocol is supported on this terminal");
105115 execute!(
106116 self.buffer,
@@ -121,7 +131,7 @@ where
121131 if config.enable_mouse_capture {
122132 execute!(self.buffer, DisableMouseCapture)?;
123133 }
124− if matches!(terminal::supports_keyboard_enhancement(), Ok(true)) {
134+ if self.supports_keyboard_enhancement_protocol()? {
125135 execute!(self.buffer, PopKeyboardEnhancementFlags)?;
126136 }
127137 execute!(