|
1 | 1 | //! UNIX related logic for terminal manipulation.
|
2 | 2 |
|
| 3 | +#[cfg(feature = "events")] |
| 4 | +use crate::event::KeyboardEnhancementFlags; |
3 | 5 | use crate::terminal::{
|
4 | 6 | sys::file_descriptor::{tty_fd, FileDesc},
|
5 | 7 | WindowSize,
|
@@ -184,23 +186,32 @@ fn set_terminal_attr(fd: impl AsFd, termios: &Termios) -> io::Result<()> {
|
184 | 186 | /// [`crossterm::event::read`](crate::event::read) or [`crossterm::event::poll`](crate::event::poll) are being called.
|
185 | 187 | #[cfg(feature = "events")]
|
186 | 188 | pub fn supports_keyboard_enhancement() -> io::Result<bool> {
|
| 189 | + query_keyboard_enhancement_flags().map(|flags| flags.is_some()) |
| 190 | +} |
| 191 | + |
| 192 | +/// Queries the terminal's currently active keyboard enhancement flags. |
| 193 | +/// |
| 194 | +/// On unix systems, this function will block and possibly time out while |
| 195 | +/// [`crossterm::event::read`](crate::event::read) or [`crossterm::event::poll`](crate::event::poll) are being called. |
| 196 | +#[cfg(feature = "events")] |
| 197 | +pub fn query_keyboard_enhancement_flags() -> io::Result<Option<KeyboardEnhancementFlags>> { |
187 | 198 | if is_raw_mode_enabled() {
|
188 |
| - read_supports_keyboard_enhancement_raw() |
| 199 | + query_keyboard_enhancement_flags_raw() |
189 | 200 | } else {
|
190 |
| - read_supports_keyboard_enhancement_flags() |
| 201 | + query_keyboard_enhancement_flags_nonraw() |
191 | 202 | }
|
192 | 203 | }
|
193 | 204 |
|
194 | 205 | #[cfg(feature = "events")]
|
195 |
| -fn read_supports_keyboard_enhancement_flags() -> io::Result<bool> { |
| 206 | +fn query_keyboard_enhancement_flags_nonraw() -> io::Result<Option<KeyboardEnhancementFlags>> { |
196 | 207 | enable_raw_mode()?;
|
197 |
| - let flags = read_supports_keyboard_enhancement_raw(); |
| 208 | + let flags = query_keyboard_enhancement_flags_raw(); |
198 | 209 | disable_raw_mode()?;
|
199 | 210 | flags
|
200 | 211 | }
|
201 | 212 |
|
202 | 213 | #[cfg(feature = "events")]
|
203 |
| -fn read_supports_keyboard_enhancement_raw() -> io::Result<bool> { |
| 214 | +fn query_keyboard_enhancement_flags_raw() -> io::Result<Option<KeyboardEnhancementFlags>> { |
204 | 215 | use crate::event::{
|
205 | 216 | filter::{KeyboardEnhancementFlagsFilter, PrimaryDeviceAttributesFilter},
|
206 | 217 | poll_internal, read_internal, InternalEvent,
|
@@ -236,12 +247,12 @@ fn read_supports_keyboard_enhancement_raw() -> io::Result<bool> {
|
236 | 247 | ) {
|
237 | 248 | Ok(true) => {
|
238 | 249 | match read_internal(&KeyboardEnhancementFlagsFilter) {
|
239 |
| - Ok(InternalEvent::KeyboardEnhancementFlags(_current_flags)) => { |
| 250 | + Ok(InternalEvent::KeyboardEnhancementFlags(current_flags)) => { |
240 | 251 | // Flush the PrimaryDeviceAttributes out of the event queue.
|
241 | 252 | read_internal(&PrimaryDeviceAttributesFilter).ok();
|
242 |
| - return Ok(true); |
| 253 | + return Ok(Some(current_flags)); |
243 | 254 | }
|
244 |
| - _ => return Ok(false), |
| 255 | + _ => return Ok(None), |
245 | 256 | }
|
246 | 257 | }
|
247 | 258 | Ok(false) => {
|
|
0 commit comments