Skip to content

Commit

Permalink
Add query_keyboard_enhancement_flags to read enabled flags
Browse files Browse the repository at this point in the history
This is mostly a refactor of `supports_keyboard_enhancement` and its
helper functions. We previously discarded the response to the `CSI ? u`
query but this returns the currently active flags. This response value
can be useful for a client to know which flags to emit to disable the
keyboard enhancement protocol or to know which flags a terminal supports
(after the client has pushed flags).
  • Loading branch information
the-mikedavis committed Dec 16, 2024
1 parent fc8f977 commit 1f9a5c3
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/terminal/sys/unix.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! UNIX related logic for terminal manipulation.
#[cfg(feature = "events")]
use crate::event::KeyboardEnhancementFlags;
use crate::terminal::{
sys::file_descriptor::{tty_fd, FileDesc},
WindowSize,
Expand Down Expand Up @@ -184,23 +186,28 @@ fn set_terminal_attr(fd: impl AsFd, termios: &Termios) -> io::Result<()> {
/// [`crossterm::event::read`](crate::event::read) or [`crossterm::event::poll`](crate::event::poll) are being called.
#[cfg(feature = "events")]
pub fn supports_keyboard_enhancement() -> io::Result<bool> {
query_keyboard_enhancement_flags().map(|flags| flags.is_some())
}

#[cfg(feature = "events")]
pub fn query_keyboard_enhancement_flags() -> io::Result<Option<KeyboardEnhancementFlags>> {
if is_raw_mode_enabled() {
read_supports_keyboard_enhancement_raw()
query_keyboard_enhancement_flags_raw()
} else {
read_supports_keyboard_enhancement_flags()
query_keyboard_enhancement_flags_nonraw()
}
}

#[cfg(feature = "events")]
fn read_supports_keyboard_enhancement_flags() -> io::Result<bool> {
fn query_keyboard_enhancement_flags_nonraw() -> io::Result<Option<KeyboardEnhancementFlags>> {
enable_raw_mode()?;
let flags = read_supports_keyboard_enhancement_raw();
let flags = query_keyboard_enhancement_flags_raw();
disable_raw_mode()?;
flags
}

#[cfg(feature = "events")]
fn read_supports_keyboard_enhancement_raw() -> io::Result<bool> {
fn query_keyboard_enhancement_flags_raw() -> io::Result<Option<KeyboardEnhancementFlags>> {
use crate::event::{
filter::{KeyboardEnhancementFlagsFilter, PrimaryDeviceAttributesFilter},
poll_internal, read_internal, InternalEvent,
Expand Down Expand Up @@ -236,12 +243,12 @@ fn read_supports_keyboard_enhancement_raw() -> io::Result<bool> {
) {
Ok(true) => {
match read_internal(&KeyboardEnhancementFlagsFilter) {
Ok(InternalEvent::KeyboardEnhancementFlags(_current_flags)) => {
Ok(InternalEvent::KeyboardEnhancementFlags(current_flags)) => {
// Flush the PrimaryDeviceAttributes out of the event queue.
read_internal(&PrimaryDeviceAttributesFilter).ok();
return Ok(true);
return Ok(Some(current_flags));
}
_ => return Ok(false),
_ => return Ok(None),
}
}
Ok(false) => {
Expand Down

0 comments on commit 1f9a5c3

Please sign in to comment.