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

tui: Only query keyboard enhancement support once #6160

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

9 changes: 5 additions & 4 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,10 @@ fn restore_term() -> Result<(), Error> {
let mut stdout = stdout();
// reset cursor shape
write!(stdout, "\x1B[0 q")?;
if matches!(terminal::supports_keyboard_enhancement(), Ok(true)) {
execute!(stdout, PopKeyboardEnhancementFlags)?;
}
// Ignore errors on disabling, this might trigger on windows if we call
// disable without calling enable previously
let _ = execute!(stdout, DisableMouseCapture);
let _ = execute!(stdout, PopKeyboardEnhancementFlags);
Comment on lines -115 to +118
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we would check terminal.supports_keyboard_enhancement_protocol() here but it's complicated to pass in values from Application because this function is set as a panic handler. We might be able to pass in just the boolean for whether keyboard enhancement is supported. I will check on some different terminals to see if it works to just send this PopKeyboardEnhancementFlags always though

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gave this a test with Kitty, WezTerm (with enhanced keyboard protocol disabled), Alacritty and the default login terminal for my linux machine and it seems to not make a difference - the terminal ignores the escape sequence when it doesn't recognize it

execute!(
stdout,
DisableBracketedPaste,
Expand Down Expand Up @@ -1071,7 +1069,10 @@ impl Application {
if self.config.load().editor.mouse {
execute!(stdout, EnableMouseCapture)?;
}
if matches!(terminal::supports_keyboard_enhancement(), Ok(true)) {
if matches!(
self.terminal.supports_keyboard_enhancement_protocol(),
Ok(true)
) {
log::debug!("The enhanced keyboard protocol is supported on this terminal");
execute!(
stdout,
Expand Down
1 change: 1 addition & 0 deletions helix-tui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ unicode-segmentation = "1.10"
crossterm = { version = "0.26", optional = true }
termini = "0.1"
serde = { version = "1", "optional" = true, features = ["derive"]}
once_cell = "1.17"
helix-view = { version = "0.6", path = "../helix-view", features = ["term"] }
helix-core = { version = "0.6", path = "../helix-core" }
9 changes: 9 additions & 0 deletions helix-tui/src/backend/crossterm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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 @@ -52,6 +53,7 @@ impl Capabilities {
pub struct CrosstermBackend<W: Write> {
buffer: W,
capabilities: Capabilities,
supports_keyboard_enhancement_protocol: OnceCell<bool>,
}

impl<W> CrosstermBackend<W>
Expand All @@ -62,6 +64,7 @@ where
CrosstermBackend {
buffer,
capabilities: Capabilities::from_env_or_default(),
supports_keyboard_enhancement_protocol: OnceCell::new(),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This value is not included in Capabilities because the tty needs to be in raw mode to perform the query and the backend is created before raw mode is enabled

}
}
}
Expand Down Expand Up @@ -187,6 +190,12 @@ where
fn flush(&mut self) -> io::Result<()> {
self.buffer.flush()
}

fn supports_keyboard_enhancement_protocol(&self) -> Result<bool, io::Error> {
self.supports_keyboard_enhancement_protocol
.get_or_try_init(terminal::supports_keyboard_enhancement)
.copied()
}
}

fn map_error(error: crossterm::Result<()>) -> io::Result<()> {
Expand Down
1 change: 1 addition & 0 deletions helix-tui/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ pub trait Backend {
fn clear(&mut self) -> Result<(), io::Error>;
fn size(&self) -> Result<Rect, io::Error>;
fn flush(&mut self) -> Result<(), io::Error>;
fn supports_keyboard_enhancement_protocol(&self) -> Result<bool, io::Error>;
}
4 changes: 4 additions & 0 deletions helix-tui/src/backend/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,8 @@ impl Backend for TestBackend {
fn flush(&mut self) -> Result<(), io::Error> {
Ok(())
}

fn supports_keyboard_enhancement_protocol(&self) -> Result<bool, io::Error> {
Ok(false)
}
}
6 changes: 6 additions & 0 deletions helix-tui/src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,10 @@ where
pub fn size(&self) -> io::Result<Rect> {
self.backend.size()
}

/// Checks whether the host terminal emulator supports the keyboard
/// enhancement protocol for disambiguating keycodes.
pub fn supports_keyboard_enhancement_protocol(&self) -> io::Result<bool> {
self.backend.supports_keyboard_enhancement_protocol()
}
}