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

Allow configuring enhanced keyboard protocol detection #7020

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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 book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Its settings will be merged with the configuration directory `config.toml` and t
| `color-modes` | Whether to color the mode indicator with different colors depending on the mode itself | `false` |
| `text-width` | Maximum line length. Used for the `:reflow` command and soft-wrapping if `soft-wrap.wrap-at-text-width` is set | `80` |
| `workspace-lsp-roots` | Directories relative to the workspace root that are treated as LSP roots. Should only be set in `.helix/config.toml` | `[]` |
| `enhanced-keyboard-protocol` | Set to `true` to force enabling the enhanced keyboard protocol or `false` to disable. When omitted, the host terminal is queried to detect support | none |

### `[editor.statusline]` Section

Expand Down
63 changes: 46 additions & 17 deletions helix-tui/src/backend/crossterm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{backend::Backend, buffer::Cell, terminal::Config};
use crate::{
backend::Backend,
buffer::Cell,
terminal::{Config, FeatureToggle},
};
use crossterm::{
cursor::{Hide, MoveTo, SetCursorStyle, Show},
event::{
Expand Down Expand Up @@ -64,6 +68,7 @@ pub struct CrosstermBackend<W: Write> {
capabilities: Capabilities,
supports_keyboard_enhancement_protocol: OnceCell<bool>,
mouse_capture_enabled: bool,
enhanced_keyboard_protocol_enabled: bool,
}

impl<W> CrosstermBackend<W>
Expand All @@ -76,24 +81,31 @@ where
capabilities: Capabilities::from_env_or_default(config),
supports_keyboard_enhancement_protocol: OnceCell::new(),
mouse_capture_enabled: false,
enhanced_keyboard_protocol_enabled: false,
}
}

#[inline]
fn supports_keyboard_enhancement_protocol(&self) -> bool {
*self.supports_keyboard_enhancement_protocol
.get_or_init(|| {
use std::time::Instant;

let now = Instant::now();
let supported = matches!(terminal::supports_keyboard_enhancement(), Ok(true));
log::debug!(
"The keyboard enhancement protocol is {}supported in this terminal (checked in {:?})",
if supported { "" } else { "not " },
Instant::now().duration_since(now)
);
supported
})
fn enable_keyboard_enhancement_protocol(&self, config: Config) -> bool {
match config.enable_enhanced_keyboard_protocol {
FeatureToggle::Enable => true,
FeatureToggle::Disable => false,
FeatureToggle::Detect => {
*self.supports_keyboard_enhancement_protocol
.get_or_init(|| {
use std::time::Instant;

let now = Instant::now();
let supported = matches!(terminal::supports_keyboard_enhancement(), Ok(true));
log::debug!(
"The keyboard enhancement protocol is {}supported in this terminal (checked in {:?})",
if supported { "" } else { "not " },
Instant::now().duration_since(now)
);
supported
})
}
}
}
}

Expand Down Expand Up @@ -127,14 +139,15 @@ where
execute!(self.buffer, EnableMouseCapture)?;
self.mouse_capture_enabled = true;
}
if self.supports_keyboard_enhancement_protocol() {
if self.enable_keyboard_enhancement_protocol(config) {
execute!(
self.buffer,
PushKeyboardEnhancementFlags(
KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES
| KeyboardEnhancementFlags::REPORT_ALTERNATE_KEYS
)
)?;
self.enhanced_keyboard_protocol_enabled = true;
}
Ok(())
}
Expand All @@ -149,6 +162,22 @@ where
self.mouse_capture_enabled = config.enable_mouse_capture;
}

let enable_enhanced_keyboard_protocol = self.enable_keyboard_enhancement_protocol(config);
if self.enhanced_keyboard_protocol_enabled != enable_enhanced_keyboard_protocol {
if enable_enhanced_keyboard_protocol {
execute!(
self.buffer,
PushKeyboardEnhancementFlags(
KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES
| KeyboardEnhancementFlags::REPORT_ALTERNATE_KEYS
)
)?;
} else {
execute!(self.buffer, PopKeyboardEnhancementFlags)?;
}
self.enhanced_keyboard_protocol_enabled = enable_enhanced_keyboard_protocol;
}

Ok(())
}

Expand All @@ -158,7 +187,7 @@ where
if config.enable_mouse_capture {
execute!(self.buffer, DisableMouseCapture)?;
}
if self.supports_keyboard_enhancement_protocol() {
if self.enable_keyboard_enhancement_protocol(config) {
execute!(self.buffer, PopKeyboardEnhancementFlags)?;
}
execute!(
Expand Down
15 changes: 15 additions & 0 deletions helix-tui/src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,30 @@ pub struct Viewport {
resize_behavior: ResizeBehavior,
}

#[derive(Debug)]
pub enum FeatureToggle {
Enable,
Disable,
Detect,
}

#[derive(Debug)]
pub struct Config {
pub enable_mouse_capture: bool,
pub enable_enhanced_keyboard_protocol: FeatureToggle,
}

impl From<EditorConfig> for Config {
fn from(config: EditorConfig) -> Self {
let enable_enhanced_keyboard_protocol = match config.enhanced_keyboard_protocol {
Some(true) => FeatureToggle::Enable,
Some(false) => FeatureToggle::Disable,
None => FeatureToggle::Detect,
};

Self {
enable_mouse_capture: config.mouse,
enable_enhanced_keyboard_protocol,
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ pub struct Config {
pub soft_wrap: SoftWrap,
/// Workspace specific lsp ceiling dirs
pub workspace_lsp_roots: Vec<PathBuf>,
/// Whether to force enabling or disabling support for the enhanced keyboard protocol.
/// Set to `None` to automatically detect support by querying the host terminal.
pub enhanced_keyboard_protocol: Option<bool>,
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
Expand Down Expand Up @@ -759,6 +762,7 @@ impl Default for Config {
text_width: 80,
completion_replace: false,
workspace_lsp_roots: Vec::new(),
enhanced_keyboard_protocol: None,
}
}
}
Expand Down
Loading